From 589c617e4e1ea46f0d7c9d5b871e3496671c21a0 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Fri, 31 Mar 2023 19:23:38 +0200 Subject: [PATCH] Update plugin framework and move to using default values Signed-off-by: Philip Laine --- go.mod | 4 +- go.sum | 8 +- internal/framework/planmodifiers/default.go | 109 -------------------- internal/provider/resource_bootstrap_git.go | 46 ++++----- 4 files changed, 25 insertions(+), 142 deletions(-) delete mode 100644 internal/framework/planmodifiers/default.go diff --git a/go.mod b/go.mod index 472216a2..28cb10ab 100644 --- a/go.mod +++ b/go.mod @@ -30,10 +30,10 @@ require ( github.com/fluxcd/source-controller/api v0.36.1 github.com/google/go-containerregistry v0.13.0 github.com/hashicorp/terraform-plugin-docs v0.13.0 - github.com/hashicorp/terraform-plugin-framework v1.1.1 + github.com/hashicorp/terraform-plugin-framework v1.2.0 github.com/hashicorp/terraform-plugin-framework-validators v0.9.0 github.com/hashicorp/terraform-plugin-go v0.14.3 - github.com/hashicorp/terraform-plugin-log v0.7.0 + github.com/hashicorp/terraform-plugin-log v0.8.0 github.com/hashicorp/terraform-plugin-testing v1.1.0 github.com/mitchellh/go-homedir v1.1.0 github.com/stretchr/testify v1.8.2 diff --git a/go.sum b/go.sum index da6ce1b4..5357a118 100644 --- a/go.sum +++ b/go.sum @@ -289,14 +289,14 @@ github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM= github.com/hashicorp/terraform-plugin-docs v0.13.0 h1:6e+VIWsVGb6jYJewfzq2ok2smPzZrt1Wlm9koLeKazY= github.com/hashicorp/terraform-plugin-docs v0.13.0/go.mod h1:W0oCmHAjIlTHBbvtppWHe8fLfZ2BznQbuv8+UD8OucQ= -github.com/hashicorp/terraform-plugin-framework v1.1.1 h1:PbnEKHsIU8KTTzoztHQGgjZUWx7Kk8uGtpGMMc1p+oI= -github.com/hashicorp/terraform-plugin-framework v1.1.1/go.mod h1:DyZPxQA+4OKK5ELxFIIcqggcszqdWWUpTLPHAhS/tkY= +github.com/hashicorp/terraform-plugin-framework v1.2.0 h1:MZjFFfULnFq8fh04FqrKPcJ/nGpHOvX4buIygT3MSNY= +github.com/hashicorp/terraform-plugin-framework v1.2.0/go.mod h1:nToI62JylqXDq84weLJ/U3umUsBhZAaTmU0HXIVUOcw= github.com/hashicorp/terraform-plugin-framework-validators v0.9.0 h1:LYz4bXh3t7bTEydXOmPDPupRRnA480B/9+jV8yZvxBA= github.com/hashicorp/terraform-plugin-framework-validators v0.9.0/go.mod h1:+BVERsnfdlhYR2YkXMBtPnmn9UsL19U3qUtSZ+Y/5MY= github.com/hashicorp/terraform-plugin-go v0.14.3 h1:nlnJ1GXKdMwsC8g1Nh05tK2wsC3+3BL/DBBxFEki+j0= github.com/hashicorp/terraform-plugin-go v0.14.3/go.mod h1:7ees7DMZ263q8wQ6E4RdIdR6nHHJtrdt4ogX5lPkX1A= -github.com/hashicorp/terraform-plugin-log v0.7.0 h1:SDxJUyT8TwN4l5b5/VkiTIaQgY6R+Y2BQ0sRZftGKQs= -github.com/hashicorp/terraform-plugin-log v0.7.0/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4= +github.com/hashicorp/terraform-plugin-log v0.8.0 h1:pX2VQ/TGKu+UU1rCay0OlzosNKe4Nz1pepLXj95oyy0= +github.com/hashicorp/terraform-plugin-log v0.8.0/go.mod h1:1myFrhVsBLeylQzYYEV17VVjtG8oYPRFdaZs7xdW2xs= github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1 h1:zHcMbxY0+rFO9gY99elV/XC/UnQVg7FhRCbj1i5b7vM= github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1/go.mod h1:+tNlb0wkfdsDJ7JEiERLz4HzM19HyiuIoGzTsM7rPpw= github.com/hashicorp/terraform-plugin-testing v1.1.0 h1:l5UuTAt7yQcThGe0dFGSCOHR4M1k0VVTqW60K2+q6AE= diff --git a/internal/framework/planmodifiers/default.go b/internal/framework/planmodifiers/default.go deleted file mode 100644 index 8aaf536d..00000000 --- a/internal/framework/planmodifiers/default.go +++ /dev/null @@ -1,109 +0,0 @@ -/* -Copyright 2023 The Flux authors - -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 planmodifiers - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/attr" - "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" - "github.com/hashicorp/terraform-plugin-framework/types" -) - -type stringDefaultModifier struct { - value string -} - -func (m stringDefaultModifier) Description(ctx context.Context) string { - return fmt.Sprintf("If value is not configured, defaults to %v", m.value) -} - -func (m stringDefaultModifier) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("If value is not configured, defaults to `%v`", m.value) -} - -func (m stringDefaultModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { - if !req.ConfigValue.IsNull() { - return - } - if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() { - return - } - resp.PlanValue = types.StringValue(m.value) -} - -func DefaultStringValue(value string) planmodifier.String { - return &stringDefaultModifier{value: value} -} - -type boolDefaultModifier struct { - value bool -} - -func (m boolDefaultModifier) Description(ctx context.Context) string { - return fmt.Sprintf("If value is not configured, defaults to %v", m.value) -} - -func (m boolDefaultModifier) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("If value is not configured, defaults to `%v`", m.value) -} - -func (m boolDefaultModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) { - if !req.ConfigValue.IsNull() { - return - } - if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() { - return - } - resp.PlanValue = types.BoolValue(m.value) -} - -func DefaultBoolValue(value bool) planmodifier.Bool { - return boolDefaultModifier{value: value} -} - -type stringSetDefaultModifier struct { - value []string -} - -func (m stringSetDefaultModifier) Description(ctx context.Context) string { - return fmt.Sprintf("If value is not configured, defaults to %v", m.value) -} - -func (m stringSetDefaultModifier) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("If value is not configured, defaults to `%v`", m.value) -} - -func (m stringSetDefaultModifier) PlanModifySet(ctx context.Context, req planmodifier.SetRequest, resp *planmodifier.SetResponse) { - if !req.ConfigValue.IsNull() { - return - } - if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() { - return - } - attrValues := []attr.Value{} - for _, v := range m.value { - attrValues = append(attrValues, types.StringValue(v)) - } - set := types.SetValueMust(types.StringType, attrValues) - resp.PlanValue = set -} - -func DefaultStringSetValue(value []string) planmodifier.Set { - return stringSetDefaultModifier{value: value} -} diff --git a/internal/provider/resource_bootstrap_git.go b/internal/provider/resource_bootstrap_git.go index 30b3ffb1..7b457985 100644 --- a/internal/provider/resource_bootstrap_git.go +++ b/internal/provider/resource_bootstrap_git.go @@ -37,7 +37,10 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" @@ -59,7 +62,6 @@ import ( "github.com/fluxcd/pkg/git/repository" sourcev1 "github.com/fluxcd/source-controller/api/v1beta2" - "github.com/fluxcd/terraform-provider-flux/internal/framework/planmodifiers" customtypes "github.com/fluxcd/terraform-provider-flux/internal/framework/types" "github.com/fluxcd/terraform-provider-flux/internal/framework/validators" "github.com/fluxcd/terraform-provider-flux/internal/utils" @@ -130,6 +132,12 @@ func (r *bootstrapGitResource) Metadata(ctx context.Context, req resource.Metada func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { defaultOpts := install.MakeDefaultOptions() + componentsSet, diags := types.SetValueFrom(ctx, types.StringType, defaultOpts.Components) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + resp.Schema = schema.Schema{ MarkdownDescription: "Commits Flux components to a Git repository and configures a Kubernetes cluster to synchronize with the same Git repository.", Attributes: map[string]schema.Attribute{ @@ -140,9 +148,7 @@ func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRe Description: fmt.Sprintf("Flux version. Defaults to `%s`.", utils.DefaultFluxVersion), Optional: true, Computed: true, - PlanModifiers: []planmodifier.String{ - planmodifiers.DefaultStringValue(utils.DefaultFluxVersion), - }, + Default: stringdefault.StaticString(utils.DefaultFluxVersion), Validators: []validator.String{ stringvalidator.RegexMatches(regexp.MustCompile("(latest|^v.*)"), "must either be latest or start with 'v'"), }, @@ -151,18 +157,14 @@ func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRe Description: fmt.Sprintf("The internal cluster domain. Defaults to `%s`", defaultOpts.ClusterDomain), Optional: true, Computed: true, - PlanModifiers: []planmodifier.String{ - planmodifiers.DefaultStringValue(defaultOpts.ClusterDomain), - }, + Default: stringdefault.StaticString(defaultOpts.ClusterDomain), }, "components": schema.SetAttribute{ ElementType: types.StringType, Description: fmt.Sprintf("Toolkit components to include in the install manifests. Defaults to `%s`", defaultOpts.Components), Optional: true, Computed: true, - PlanModifiers: []planmodifier.Set{ - planmodifiers.DefaultStringSetValue(defaultOpts.Components), - }, + Default: setdefault.StaticValue(componentsSet), Validators: []validator.Set{ setvalidator.SizeAtLeast(2), setvalidator.ValueStringsAre(stringvalidator.OneOf("source-controller", "kustomize-controller", "helm-controller", "notification-controller")), @@ -190,9 +192,7 @@ func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRe Description: fmt.Sprintf("Log level for toolkit components. Defaults to `%s`.", defaultOpts.LogLevel), Optional: true, Computed: true, - PlanModifiers: []planmodifier.String{ - planmodifiers.DefaultStringValue(defaultOpts.LogLevel), - }, + Default: stringdefault.StaticString(defaultOpts.LogLevel), Validators: []validator.String{ stringvalidator.OneOf("info", "debug", "error"), }, @@ -201,9 +201,9 @@ func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRe Description: fmt.Sprintf("The namespace scope for install manifests. Defaults to `%s`.", defaultOpts.Namespace), Optional: true, Computed: true, + Default: stringdefault.StaticString(defaultOpts.Namespace), PlanModifiers: []planmodifier.String{ stringplanmodifier.RequiresReplace(), - planmodifiers.DefaultStringValue(defaultOpts.Namespace), }, Validators: []validator.String{ stringvalidator.RegexMatches(regexp.MustCompile(rfc1123LabelRegex), rfc1123LabelError), @@ -214,18 +214,14 @@ func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRe Description: fmt.Sprintf("Deny ingress access to the toolkit controllers from other namespaces using network policies. Defaults to `%v`.", defaultOpts.NetworkPolicy), Optional: true, Computed: true, - PlanModifiers: []planmodifier.Bool{ - planmodifiers.DefaultBoolValue(defaultOpts.NetworkPolicy), - }, + Default: booldefault.StaticBool(defaultOpts.NetworkPolicy), }, "registry": schema.StringAttribute{ CustomType: customtypes.URLType{}, Description: fmt.Sprintf("Container registry where the toolkit images are published. Defaults to `%s`.", defaultOpts.Registry), Optional: true, Computed: true, - PlanModifiers: []planmodifier.String{ - planmodifiers.DefaultStringValue(defaultOpts.Registry), - }, + Default: stringdefault.StaticString(defaultOpts.Registry), }, "toleration_keys": schema.SetAttribute{ ElementType: types.StringType, @@ -242,18 +238,14 @@ func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRe Description: fmt.Sprintf("If true watch for custom resources in all namespaces. Defaults to `%v`.", defaultOpts.WatchAllNamespaces), Optional: true, Computed: true, - PlanModifiers: []planmodifier.Bool{ - planmodifiers.DefaultBoolValue(defaultOpts.WatchAllNamespaces), - }, + Default: booldefault.StaticBool(defaultOpts.WatchAllNamespaces), }, "interval": schema.StringAttribute{ CustomType: customtypes.DurationType{}, Description: fmt.Sprintf("Interval at which to reconcile from bootstrap repository. Defaults to `%s`.", time.Minute.String()), Optional: true, Computed: true, - PlanModifiers: []planmodifier.String{ - planmodifiers.DefaultStringValue(time.Minute.String()), - }, + Default: stringdefault.StaticString(time.Minute.String()), }, "path": schema.StringAttribute{ Description: "Path relative to the repository root, when specified the cluster sync will be scoped to this path.", @@ -267,9 +259,9 @@ func (r *bootstrapGitResource) Schema(ctx context.Context, req resource.SchemaRe Description: fmt.Sprintf("Name of the secret the sync credentials can be found in or stored to. Defaults to `%s`.", defaultOpts.Namespace), Optional: true, Computed: true, + Default: stringdefault.StaticString(defaultOpts.Namespace), PlanModifiers: []planmodifier.String{ stringplanmodifier.RequiresReplace(), - planmodifiers.DefaultStringValue(defaultOpts.Namespace), }, Validators: []validator.String{ stringvalidator.RegexMatches(regexp.MustCompile(rfc1123DomainRegex), rfc1123DomainError),