|
| 1 | +package tfschema |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 13 | +) |
| 14 | + |
| 15 | +// Namespace is used to store the namespace for unified terraform provider |
| 16 | +// across resources and data sources onboarded to plugin framework. |
| 17 | +// Resources and data sources will use the underlying ProviderConfig and ProviderConfigData |
| 18 | +// type respectively to store the provider configurations. |
| 19 | +type Namespace struct { |
| 20 | + ProviderConfig types.Object `tfsdk:"provider_config"` |
| 21 | +} |
| 22 | + |
| 23 | +// ProviderConfig is used to store the provider configurations for unified terraform provider |
| 24 | +// across resources onboarded to plugin framework. |
| 25 | +type ProviderConfig struct { |
| 26 | + WorkspaceID types.String `tfsdk:"workspace_id"` |
| 27 | +} |
| 28 | + |
| 29 | +// ApplySchemaCustomizations applies the schema customizations to the ProviderConfig type. |
| 30 | +func (r ProviderConfig) ApplySchemaCustomizations(attrs map[string]AttributeBuilder) map[string]AttributeBuilder { |
| 31 | + attrs["workspace_id"] = attrs["workspace_id"].SetRequired() |
| 32 | + attrs["workspace_id"] = attrs["workspace_id"].(StringAttributeBuilder).AddPlanModifier( |
| 33 | + stringplanmodifier.RequiresReplaceIf(workspaceIDPlanModifier, "", "")) |
| 34 | + attrs["workspace_id"] = attrs["workspace_id"].(StringAttributeBuilder).AddValidator(stringvalidator.LengthAtLeast(1)) |
| 35 | + return attrs |
| 36 | +} |
| 37 | + |
| 38 | +// workspaceIDPlanModifier is a plan modifier that requires replacement if the |
| 39 | +// workspace_id changes from one non-empty value to another |
| 40 | +func workspaceIDPlanModifier(ctx context.Context, req planmodifier.StringRequest, resp *stringplanmodifier.RequiresReplaceIfFuncResponse) { |
| 41 | + // Require replacement if workspace_id changes from one non-empty value to another |
| 42 | + oldValue := req.StateValue.ValueString() |
| 43 | + newValue := req.PlanValue.ValueString() |
| 44 | + |
| 45 | + if oldValue != "" && newValue != "" && oldValue != newValue { |
| 46 | + resp.RequiresReplace = true |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// GetComplexFieldTypes returns a map of the types of elements in complex fields in ProviderConfig. |
| 51 | +func (r ProviderConfig) GetComplexFieldTypes(ctx context.Context) map[string]reflect.Type { |
| 52 | + return map[string]reflect.Type{} |
| 53 | +} |
| 54 | + |
| 55 | +// ToObjectValue returns the object value for the resource |
| 56 | +func (r ProviderConfig) ToObjectValue(ctx context.Context) basetypes.ObjectValue { |
| 57 | + return types.ObjectValueMust( |
| 58 | + r.Type(ctx).(basetypes.ObjectType).AttrTypes, |
| 59 | + map[string]attr.Value{ |
| 60 | + "workspace_id": r.WorkspaceID, |
| 61 | + }, |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +// Type returns the object type for the ProviderConfig type. |
| 66 | +func (r ProviderConfig) Type(ctx context.Context) attr.Type { |
| 67 | + return types.ObjectType{ |
| 68 | + AttrTypes: map[string]attr.Type{ |
| 69 | + "workspace_id": types.StringType, |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// ProviderConfigData is used to store the provider configurations for unified terraform provider |
| 75 | +// across data sources onboarded to plugin framework. |
| 76 | +type ProviderConfigData struct { |
| 77 | + WorkspaceID types.String `tfsdk:"workspace_id"` |
| 78 | +} |
| 79 | + |
| 80 | +// ApplySchemaCustomizations applies the schema customizations to the ProviderConfigData type. |
| 81 | +func (r ProviderConfigData) ApplySchemaCustomizations(attrs map[string]AttributeBuilder) map[string]AttributeBuilder { |
| 82 | + attrs["workspace_id"] = attrs["workspace_id"].SetRequired() |
| 83 | + attrs["workspace_id"] = attrs["workspace_id"].(StringAttributeBuilder).AddValidator(stringvalidator.LengthAtLeast(1)) |
| 84 | + return attrs |
| 85 | +} |
| 86 | + |
| 87 | +// GetComplexFieldTypes returns a map of the types of elements in complex fields in ProviderConfigData. |
| 88 | +func (r ProviderConfigData) GetComplexFieldTypes(ctx context.Context) map[string]reflect.Type { |
| 89 | + return map[string]reflect.Type{} |
| 90 | +} |
| 91 | + |
| 92 | +// ToObjectValue returns the object value for the data source |
| 93 | +func (r ProviderConfigData) ToObjectValue(ctx context.Context) basetypes.ObjectValue { |
| 94 | + return types.ObjectValueMust( |
| 95 | + r.Type(ctx).(basetypes.ObjectType).AttrTypes, |
| 96 | + map[string]attr.Value{ |
| 97 | + "workspace_id": r.WorkspaceID, |
| 98 | + }, |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +// Type returns the object type for the ProviderConfigData type. |
| 103 | +func (r ProviderConfigData) Type(ctx context.Context) attr.Type { |
| 104 | + return types.ObjectType{ |
| 105 | + AttrTypes: map[string]attr.Type{ |
| 106 | + "workspace_id": types.StringType, |
| 107 | + }, |
| 108 | + } |
| 109 | +} |
0 commit comments