Skip to content

Commit

Permalink
Merge pull request #441 from fluxcd/update/framework
Browse files Browse the repository at this point in the history
Update plugin framework and move to using default values
  • Loading branch information
phillebaba authored Apr 2, 2023
2 parents 932e9d2 + 589c617 commit 9433a8a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 142 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
109 changes: 0 additions & 109 deletions internal/framework/planmodifiers/default.go

This file was deleted.

46 changes: 19 additions & 27 deletions internal/provider/resource_bootstrap_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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'"),
},
Expand All @@ -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")),
Expand Down Expand Up @@ -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"),
},
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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.",
Expand All @@ -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),
Expand Down

0 comments on commit 9433a8a

Please sign in to comment.