Skip to content

Commit

Permalink
nuke tf:... annotations (except object for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchy committed Jan 9, 2025
1 parent 10d702a commit ae1fc5f
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 83 deletions.
46 changes: 23 additions & 23 deletions internal/providers/pluginfw/converters/converters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ import (
)

type DummyTfSdk struct {
Enabled types.Bool `tfsdk:"enabled" tf:"optional"`
Workers types.Int64 `tfsdk:"workers" tf:""`
Floats types.Float64 `tfsdk:"floats" tf:""`
Description types.String `tfsdk:"description" tf:""`
Tasks types.String `tfsdk:"task" tf:"optional"`
NoPointerNested types.List `tfsdk:"no_pointer_nested" tf:"optional"`
NestedList types.List `tfsdk:"nested_list" tf:"optional"`
NestedPointerList types.List `tfsdk:"nested_pointer_list" tf:"optional"`
Map types.Map `tfsdk:"map" tf:"optional"`
NestedMap types.Map `tfsdk:"nested_map" tf:"optional"`
Repeated types.List `tfsdk:"repeated" tf:"optional"`
Attributes types.Map `tfsdk:"attributes" tf:"optional"`
EnumField types.String `tfsdk:"enum_field" tf:"optional"`
AdditionalField types.String `tfsdk:"additional_field" tf:"optional"`
DistinctField types.String `tfsdk:"distinct_field" tf:"optional"`
SliceStructPtr types.List `tfsdk:"slice_struct_ptr" tf:"optional"`
Enabled types.Bool `tfsdk:"enabled"`
Workers types.Int64 `tfsdk:"workers"`
Floats types.Float64 `tfsdk:"floats"`
Description types.String `tfsdk:"description"`
Tasks types.String `tfsdk:"task"`
NoPointerNested types.List `tfsdk:"no_pointer_nested"`
NestedList types.List `tfsdk:"nested_list"`
NestedPointerList types.List `tfsdk:"nested_pointer_list"`
Map types.Map `tfsdk:"map"`
NestedMap types.Map `tfsdk:"nested_map"`
Repeated types.List `tfsdk:"repeated"`
Attributes types.Map `tfsdk:"attributes"`
EnumField types.String `tfsdk:"enum_field"`
AdditionalField types.String `tfsdk:"additional_field"`
DistinctField types.String `tfsdk:"distinct_field"`
SliceStructPtr types.List `tfsdk:"slice_struct_ptr"`
Irrelevant types.String `tfsdk:"-"`
Object types.Object `tfsdk:"object" tf:"optional"`
ObjectPtr types.Object `tfsdk:"object_ptr" tf:"optional"`
Type_ types.String `tfsdk:"type" tf:""` // Test Type_ renaming
EmptyStructList types.List `tfsdk:"empty_struct_list" tf:"optional"`
EmptyStructObject types.Object `tfsdk:"empty_struct_object" tf:"optional"`
Object types.Object `tfsdk:"object"`
ObjectPtr types.Object `tfsdk:"object_ptr"`
Type_ types.String `tfsdk:"type"` // Test Type_ renaming
EmptyStructList types.List `tfsdk:"empty_struct_list"`
EmptyStructObject types.Object `tfsdk:"empty_struct_object"`
}

func (DummyTfSdk) GetComplexFieldTypes(ctx context.Context) map[string]reflect.Type {
Expand Down Expand Up @@ -84,8 +84,8 @@ func (f *TestEnum) Type() string {
}

type DummyNestedTfSdk struct {
Name types.String `tfsdk:"name" tf:"optional"`
Enabled types.Bool `tfsdk:"enabled" tf:"optional"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
}

type DummyNestedTfSdkEmpty struct{}
Expand Down
8 changes: 7 additions & 1 deletion internal/providers/pluginfw/products/app/data_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ type dataSourceApps struct {
}

type dataApps struct {
Apps types.List `tfsdk:"app" tf:"computed"`
Apps types.List `tfsdk:"app"`
}

func (dataApps) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder {
attrs["app"] = attrs["app"].SetComputed()

return attrs
}

func (dataApps) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
Expand Down
14 changes: 11 additions & 3 deletions internal/providers/pluginfw/products/cluster/data_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ type ClusterDataSource struct {
}

type ClusterInfo struct {
ClusterId types.String `tfsdk:"cluster_id" tf:"optional,computed"`
Name types.String `tfsdk:"cluster_name" tf:"optional,computed"`
ClusterInfo types.List `tfsdk:"cluster_info" tf:"optional,computed"`
ClusterId types.String `tfsdk:"cluster_id"`
Name types.String `tfsdk:"cluster_name"`
ClusterInfo types.List `tfsdk:"cluster_info"`
}

func (ClusterInfo) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder {
attrs["cluster_id"] = attrs["cluster_id"].SetOptional().SetComputed()
attrs["cluster_name"] = attrs["cluster_name"].SetOptional().SetComputed()
attrs["cluster_info"] = attrs["cluster_info"].SetOptional().SetComputed()

return attrs
}

func (ClusterInfo) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ type NotificationDestinationsDataSource struct {
}

type NotificationDestinationsInfo struct {
DisplayNameContains types.String `tfsdk:"display_name_contains" tf:"optional"`
Type types.String `tfsdk:"type" tf:"optional"`
NotificationDestinations types.List `tfsdk:"notification_destinations" tf:"computed"`
DisplayNameContains types.String `tfsdk:"display_name_contains"`
Type types.String `tfsdk:"type"`
NotificationDestinations types.List `tfsdk:"notification_destinations"`
}

func (NotificationDestinationsInfo) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder {
attrs["display_name_contains"] = attrs["display_name_contains"].SetOptional()
attrs["type"] = attrs["type"].SetOptional()
attrs["notification_destinations"] = attrs["notification_destinations"].SetComputed()

return attrs
}

func (NotificationDestinationsInfo) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ type ServingEndpointsDataSource struct {
}

type ServingEndpointsData struct {
Endpoints types.List `tfsdk:"endpoints" tf:"optional,computed"`
Endpoints types.List `tfsdk:"endpoints"`
}

func (ServingEndpointsData) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder {
attrs["endpoints"] = attrs["endpoints"].SetOptional().SetComputed()

return attrs
}

func (ServingEndpointsData) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
Expand Down
8 changes: 7 additions & 1 deletion internal/providers/pluginfw/products/sharing/data_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import (
const dataSourceNameShares = "shares"

type SharesList struct {
Shares types.List `tfsdk:"shares" tf:"computed,optional,slice_set"`
Shares types.List `tfsdk:"shares"`
}

func (SharesList) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder {
attrs["shares"] = attrs["shares"].SetComputed().SetOptional()

return attrs
}

func (SharesList) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
Expand Down
12 changes: 6 additions & 6 deletions internal/providers/pluginfw/tfschema/customizable_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
)

type TestTfSdk struct {
Description types.String `tfsdk:"description" tf:""`
Nested types.List `tfsdk:"nested" tf:"optional"`
NestedSliceObject types.List `tfsdk:"nested_slice_object" tf:"optional,object"`
Map types.Map `tfsdk:"map" tf:"optional"`
Description types.String `tfsdk:"description"`
Nested types.List `tfsdk:"nested"`
NestedSliceObject types.List `tfsdk:"nested_slice_object" tf:"object"`
Map types.Map `tfsdk:"map"`
}

func (TestTfSdk) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
Expand All @@ -30,8 +30,8 @@ func (TestTfSdk) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
}

type NestedTfSdk struct {
Name types.String `tfsdk:"name" tf:"optional"`
Enabled types.Bool `tfsdk:"enabled" tf:"optional"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
}

type stringLengthBetweenValidator struct {
Expand Down
35 changes: 7 additions & 28 deletions internal/providers/pluginfw/tfschema/struct_to_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ type CustomizableSchemaProvider interface {
}

type structTag struct {
optional bool
computed bool
singleObject bool
}

Expand All @@ -46,7 +44,7 @@ func typeToSchema(ctx context.Context, v reflect.Value) NestedBlockObject {
if fieldName == "-" {
continue
}
structTag, hasStructTag := getStructTag(typeField)
structTag := getStructTag(typeField)
value := field.Value.Interface()
if _, ok := value.(attr.Value); !ok {
panic(fmt.Errorf("unexpected type %T in tfsdk structs, expected a plugin framework value type. %s", value, common.TerraformBugErrorMessage))
Expand Down Expand Up @@ -145,27 +143,6 @@ func typeToSchema(ctx context.Context, v reflect.Value) NestedBlockObject {
}
panic(fmt.Errorf("unexpected type %T in tfsdk structs, expected a plugin framework value type. %s", value, common.TerraformBugErrorMessage))
}

attr := scmAttr[fieldName]

if hasStructTag && !implementsSchemaProvider {
if structTag.computed {
// Computed attributes are always computed and may be optional.
attr = attr.SetComputed()
if structTag.optional {
attr = attr.SetOptional()
}
} else {
// Non-computed attributes must be either optional or required.
if structTag.optional {
attr = attr.SetOptional()
} else {
attr = attr.SetRequired()
}
}
}

scmAttr[fieldName] = attr
}

if implementsSchemaProvider {
Expand All @@ -174,13 +151,15 @@ func typeToSchema(ctx context.Context, v reflect.Value) NestedBlockObject {
return NestedBlockObject{Attributes: scmAttr}
}

func getStructTag(field reflect.StructField) (structTag, bool) {
func getStructTag(field reflect.StructField) structTag {
tagValue := field.Tag.Get("tf")
if tagValue != "" && tagValue != "object" {
panic(`"tf:..." annotations are no longer supported. You should implement the CustomizableSchemaProvider interface on the struct and apply the appropriate schema customizations there.`)
}

return structTag{
optional: strings.Contains(tagValue, "optional"),
computed: strings.Contains(tagValue, "computed"),
singleObject: strings.Contains(tagValue, "object"),
}, tagValue != ""
}
}

// ResourceStructToSchema builds a resource schema from a tfsdk struct, with custoimzations applied.
Expand Down
Loading

0 comments on commit ae1fc5f

Please sign in to comment.