Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions internal/testprovider/schema_required_input_with_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testprovider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ProviderRequiredInputWithDefaultFunc() *schema.Provider {
resourceRequiredInputWithDefaultFunc := func() *schema.Resource {
return &schema.Resource{
Schema: resourceRequiredInputWithDefaultSchema(),
}
}

return &schema.Provider{
Schema: map[string]*schema.Schema{},
ResourcesMap: map[string]*schema.Resource{
"testprovider_res": resourceRequiredInputWithDefaultFunc(),
},
}
}

func resourceRequiredInputWithDefaultSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: func() (interface{}, error) {
return "default", nil
},
},
"other_name": {
Type: schema.TypeString,
Required: true,
Default: "default",
},
"req": {
Type: schema.TypeString,
Required: true,
},
}
}
10 changes: 9 additions & 1 deletion pkg/pf/internal/pfutils/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ import (
// attrLike exposes these methods.
//
// GetAttributes method is special since it returns a NestedAttributes interface that is also internal and cannot be
// linked to. Instead, NestedAttriutes information is recorded in a dedicated new field.
// linked to. Instead, NestedAttributes information is recorded in a dedicated new field.
type Attr interface {
AttrLike
IsNested() bool
Nested() map[string]Attr
NestingMode() NestingMode
HasNestedObject() bool
HasDefault() bool
}

type AttrLike interface {
Expand Down Expand Up @@ -66,16 +67,19 @@ func FromResourceAttribute(x rschema.Attribute) Attr {

func FromAttrLike(attrLike AttrLike) Attr {
nested, nestingMode := extractNestedAttributes(attrLike)
hasDefault := hasDefault(attrLike)
return &attrAdapter{
nested: nested,
nestingMode: nestingMode,
hasDefault: hasDefault,
AttrLike: attrLike,
}
}

type attrAdapter struct {
nested map[string]Attr
nestingMode NestingMode
hasDefault bool
AttrLike
}

Expand All @@ -102,6 +106,10 @@ func (a *attrAdapter) NestingMode() NestingMode {
return a.nestingMode
}

func (a *attrAdapter) HasDefault() bool {
return a.hasDefault
}

type NestingMode uint8

const (
Expand Down
99 changes: 99 additions & 0 deletions pkg/pf/internal/pfutils/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package pfutils

import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
)

// These interfaces are re-implemented here from "github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
// as we can not link to them directly.

type attributeLikeWithBoolDefaultValue interface {
AttrLike
BoolDefaultValue() defaults.Bool
}

type attributeLikeWithFloat32DefaultValue interface {
AttrLike
Float32DefaultValue() defaults.Float32
}

type attributeLikeWithFloat64DefaultValue interface {
AttrLike
Float64DefaultValue() defaults.Float64
}

type attributeLikeWithInt32DefaultValue interface {
AttrLike
Int32DefaultValue() defaults.Int32
}

type attributeLikeWithInt64DefaultValue interface {
AttrLike
Int64DefaultValue() defaults.Int64
}

type attributeLikeWithListDefaultValue interface {
AttrLike
ListDefaultValue() defaults.List
}

type attributeLikeWithMapDefaultValue interface {
AttrLike
MapDefaultValue() defaults.Map
}

type attributeLikeWithNumberDefaultValue interface {
AttrLike
NumberDefaultValue() defaults.Number
}

type attributeLikeWithObjectDefaultValue interface {
AttrLike
ObjectDefaultValue() defaults.Object
}

type attributeLikeWithSetDefaultValue interface {
AttrLike
SetDefaultValue() defaults.Set
}

type attributeLikeWithStringDefaultValue interface {
AttrLike
StringDefaultValue() defaults.String
}

type attributeLikeWithDynamicDefaultValue interface {
AttrLike
DynamicDefaultValue() defaults.Dynamic
}

func hasDefault(attr AttrLike) bool {
switch a := attr.(type) {
case attributeLikeWithBoolDefaultValue:
return a.BoolDefaultValue() != nil
case attributeLikeWithFloat32DefaultValue:
return a.Float32DefaultValue() != nil
case attributeLikeWithFloat64DefaultValue:
return a.Float64DefaultValue() != nil
case attributeLikeWithInt32DefaultValue:
return a.Int32DefaultValue() != nil
case attributeLikeWithInt64DefaultValue:
return a.Int64DefaultValue() != nil
case attributeLikeWithListDefaultValue:
return a.ListDefaultValue() != nil
case attributeLikeWithMapDefaultValue:
return a.MapDefaultValue() != nil
case attributeLikeWithNumberDefaultValue:
return a.NumberDefaultValue() != nil
case attributeLikeWithObjectDefaultValue:
return a.ObjectDefaultValue() != nil
case attributeLikeWithSetDefaultValue:
return a.SetDefaultValue() != nil
case attributeLikeWithStringDefaultValue:
return a.StringDefaultValue() != nil
case attributeLikeWithDynamicDefaultValue:
return a.DynamicDefaultValue() != nil
default:
return false
}
}
6 changes: 6 additions & 0 deletions pkg/pf/internal/schemashim/attr_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var _ shim.Schema = (*attrSchema)(nil)

var _ shim.SchemaWithWriteOnly = (*attrSchema)(nil)

var _ shim.SchemaWithHasDefault = (*attrSchema)(nil)

func (s *attrSchema) Type() shim.ValueType {
ty := s.attr.GetType()
vt, err := convertType(ty)
Expand Down Expand Up @@ -63,6 +65,10 @@ func (*attrSchema) DefaultValue() (interface{}, error) {
return nil, bridge.ErrSchemaDefaultValue
}

func (s *attrSchema) HasDefault() bool {
return s.attr.HasDefault()
}

func (s *attrSchema) Description() string {
if desc := s.attr.GetMarkdownDescription(); desc != "" {
return desc
Expand Down
112 changes: 112 additions & 0 deletions pkg/pf/tfgen/testdata/TestPFNestedFullyComputed.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"name": "testprovider",
"attribution": "This Pulumi package is based on the [`testprovider` Terraform Provider](https://github.com/terraform-providers/terraform-provider-testprovider).",
"meta": {
"moduleFormat": "(.*)(?:/[^/]*)"
},
"language": {
"nodejs": {
"readme": "\u003e This provider is a derived work of the [Terraform Provider](https://github.com/terraform-providers/terraform-provider-testprovider)\n\u003e distributed under [MPL 2.0](https://www.mozilla.org/en-US/MPL/2.0/). If you encounter a bug or missing feature,\n\u003e please consult the source [`terraform-provider-testprovider` repo](https://github.com/terraform-providers/terraform-provider-testprovider/issues).",
"compatibility": "tfbridge20",
"disableUnionOutputTypes": true
},
"python": {
"readme": "\u003e This provider is a derived work of the [Terraform Provider](https://github.com/terraform-providers/terraform-provider-testprovider)\n\u003e distributed under [MPL 2.0](https://www.mozilla.org/en-US/MPL/2.0/). If you encounter a bug or missing feature,\n\u003e please consult the source [`terraform-provider-testprovider` repo](https://github.com/terraform-providers/terraform-provider-testprovider/issues).",
"compatibility": "tfbridge20",
"pyproject": {}
}
},
"config": {},
"types": {
"testprovider:index/ResB1:ResB1": {
"properties": {
"a1": {
"type": "string"
},
"a2": {
"type": "string"
}
},
"type": "object"
},
"testprovider:index/ResB1Output:ResB1Output": {
"properties": {
"a1": {
"type": "string"
},
"a2": {
"type": "string"
}
},
"type": "object",
"required": [
"a1"
],
"language": {
"nodejs": {
"requiredInputs": []
}
}
}
},
"provider": {
"description": "The provider type for the testprovider package. By default, resources use package-wide configuration\nsettings, however an explicit `Provider` instance may be created and passed during resource\nconstruction to achieve fine-grained programmatic control over provider settings. See the\n[documentation](https://www.pulumi.com/docs/reference/programming-model/#providers) for more information.\n",
"methods": {
"terraformConfig": "pulumi:providers:testprovider/terraformConfig"
}
},
"resources": {
"testprovider:index:Res": {
"properties": {
"b1": {
"$ref": "#/types/testprovider:index/ResB1Output:ResB1OutputOutput"
}
},
"inputProperties": {
"b1": {
"$ref": "#/types/testprovider:index/ResB1:ResB1"
}
},
"stateInputs": {
"description": "Input properties used for looking up and filtering Res resources.\n",
"properties": {
"b1": {
"$ref": "#/types/testprovider:index/ResB1:ResB1"
}
},
"type": "object"
}
}
},
"functions": {
"pulumi:providers:testprovider/terraformConfig": {
"description": "This function returns a Terraform config object with terraform-namecased keys,to be used with the Terraform Module Provider.",
"inputs": {
"properties": {
"__self__": {
"type": "ref",
"$ref": "#/resources/pulumi:providers:testprovider"
}
},
"type": "pulumi:providers:testprovider/terraformConfig",
"required": [
"__self__"
]
},
"outputs": {
"properties": {
"result": {
"additionalProperties": {
"$ref": "pulumi.json#/Any"
},
"type": "object"
}
},
"required": [
"result"
],
"type": "object"
}
}
}
}
Loading
Loading