Skip to content

Commit

Permalink
provider: make framework usable
Browse files Browse the repository at this point in the history
Copies over the sdkv2 client configuration to make the provider operate 1:1
regardless of the mechanism used.
  • Loading branch information
jacobbednarz committed Jan 17, 2023
1 parent 8ebcddf commit a094993
Show file tree
Hide file tree
Showing 19 changed files with 687 additions and 272 deletions.
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/hashicorp/go-plugin v1.4.8 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hcl/v2 v2.15.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/hashicorp/yamux v0.0.0-20210826001029-26ff87cf9493 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
Expand All @@ -35,7 +35,10 @@ require (
require (
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-framework v1.1.1
github.com/hashicorp/terraform-plugin-framework-validators v0.9.0
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-mux v0.8.0
github.com/stretchr/testify v1.8.1
)

Expand All @@ -52,8 +55,6 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.17.3 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-framework v1.1.1 // indirect
github.com/hashicorp/terraform-plugin-mux v0.8.0 // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/imdario/mergo v0.3.13 // indirect
Expand All @@ -63,7 +64,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.51.0 // indirect
Expand Down
82 changes: 4 additions & 78 deletions go.sum

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions internal/framework/modifiers/defaults/bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package defaults

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ planmodifier.Bool = boolDefaultModifier{}

func DefaultBool(s bool) boolDefaultModifier {
return boolDefaultModifier{Default: s}
}

type boolDefaultModifier struct {
Default bool
}

// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m boolDefaultModifier) Description(ctx context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to %v", m.Default)
}

// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m boolDefaultModifier) MarkdownDescription(ctx context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to `%v`", m.Default)
}

// PlanModifyBool updates the planned value with the default if its not null
func (m boolDefaultModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) {
// If the attribute configuration is not null, we are done here
if !req.ConfigValue.IsNull() {
return
}

// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
return
}

resp.PlanValue = types.BoolValue(m.Default)
}
45 changes: 45 additions & 0 deletions internal/framework/modifiers/defaults/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package defaults

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"
)

var _ planmodifier.List = listDefaultModifier{}

func DefaultList(elements []attr.Value) listDefaultModifier {
return listDefaultModifier{Elements: elements}
}

type listDefaultModifier struct {
Elements []attr.Value
}

// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m listDefaultModifier) Description(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to %s", m.Elements)
}

// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m listDefaultModifier) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to `%s`", m.Elements)
}

// PlanModifyList updates the planned value with the default if its not null
func (m listDefaultModifier) PlanModifyList(ctx context.Context, req planmodifier.ListRequest, resp *planmodifier.ListResponse) {
// If the attribute configuration is not null, we are done here
if !req.ConfigValue.IsNull() {
return
}

// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
return
}
resp.PlanValue, resp.Diagnostics = types.ListValue(req.PlanValue.ElementType(ctx), m.Elements)
}
45 changes: 45 additions & 0 deletions internal/framework/modifiers/defaults/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package defaults

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"
)

var _ planmodifier.Map = mapDefaultModifier{}

func DefaultMap(elements map[string]attr.Value) mapDefaultModifier {
return mapDefaultModifier{Elements: elements}
}

type mapDefaultModifier struct {
Elements map[string]attr.Value
}

// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m mapDefaultModifier) Description(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to %s", m.Elements)
}

// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m mapDefaultModifier) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to `%s`", m.Elements)
}

// PlanModifyMap updates the planned value with the default if its not null
func (m mapDefaultModifier) PlanModifyMap(ctx context.Context, req planmodifier.MapRequest, resp *planmodifier.MapResponse) {
// If the attribute configuration is not null, we are done here
if !req.ConfigValue.IsNull() {
return
}

// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
return
}
resp.PlanValue, resp.Diagnostics = types.MapValue(req.PlanValue.ElementType(ctx), m.Elements)
}
45 changes: 45 additions & 0 deletions internal/framework/modifiers/defaults/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package defaults

import (
"context"
"fmt"
"math/big"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ planmodifier.Number = numberDefaultModifier{}

func DefaultNumber(s *big.Float) numberDefaultModifier {
return numberDefaultModifier{Default: s}
}

type numberDefaultModifier struct {
Default *big.Float
}

// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m numberDefaultModifier) Description(ctx context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to %s", m.Default)
}

// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m numberDefaultModifier) MarkdownDescription(ctx context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to `%s`", m.Default)
}

// PlanModifyNumber updates the planned value with the default if its not null
func (m numberDefaultModifier) PlanModifyNumber(ctx context.Context, req planmodifier.NumberRequest, resp *planmodifier.NumberResponse) {
// If the attribute configuration is not null, we are done here
if !req.ConfigValue.IsNull() {
return
}

// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
return
}
resp.PlanValue = types.NumberValue(m.Default)
}
45 changes: 45 additions & 0 deletions internal/framework/modifiers/defaults/object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package defaults

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"
)

var _ planmodifier.Object = objectDefaultModifier{}

func DefaultObject(elements map[string]attr.Value) objectDefaultModifier {
return objectDefaultModifier{Elements: elements}
}

type objectDefaultModifier struct {
Elements map[string]attr.Value
}

// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m objectDefaultModifier) Description(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to %s", m.Elements)
}

// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m objectDefaultModifier) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to `%s`", m.Elements)
}

// PlanModifyObject updates the planned value with the default if its not null
func (m objectDefaultModifier) PlanModifyObject(ctx context.Context, req planmodifier.ObjectRequest, resp *planmodifier.ObjectResponse) {
// If the attribute configuration is not null, we are done here
if !req.ConfigValue.IsNull() {
return
}

// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
return
}
resp.PlanValue, resp.Diagnostics = types.ObjectValue(req.PlanValue.AttributeTypes(ctx), m.Elements)
}
45 changes: 45 additions & 0 deletions internal/framework/modifiers/defaults/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package defaults

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"
)

var _ planmodifier.Set = setDefaultModifier{}

func DefaultSet(elements []attr.Value) setDefaultModifier {
return setDefaultModifier{Elements: elements}
}

type setDefaultModifier struct {
Elements []attr.Value
}

// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m setDefaultModifier) Description(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to %s", m.Elements)
}

// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m setDefaultModifier) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to `%s`", m.Elements)
}

// PlanModifySet updates the planned value with the default if its not null
func (m setDefaultModifier) PlanModifySet(ctx context.Context, req planmodifier.SetRequest, resp *planmodifier.SetResponse) {
// If the attribute configuration is not null, we are done here
if !req.ConfigValue.IsNull() {
return
}

// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
return
}
resp.PlanValue, resp.Diagnostics = types.SetValue(req.PlanValue.ElementType(ctx), m.Elements)
}
45 changes: 45 additions & 0 deletions internal/framework/modifiers/defaults/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package defaults

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ planmodifier.String = stringDefaultModifier{}

func DefaultString(s string) stringDefaultModifier {
return stringDefaultModifier{Default: s}
}

type stringDefaultModifier struct {
Computed bool
Default string
}

// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m stringDefaultModifier) Description(ctx context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to %s", m.Default)
}

// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
func (m stringDefaultModifier) MarkdownDescription(ctx context.Context) string {
return fmt.Sprintf("If value is not configured, defaults to `%s`, computed? %t", m.Default, m.Computed)
}

// PlanModifyString updates the planned value with the default if its not null
func (m stringDefaultModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
// If the attribute configuration is not null, we are done here
if !req.ConfigValue.IsNull() {
return
}

// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
return
}
resp.PlanValue = types.StringValue(m.Default)
}
Loading

0 comments on commit a094993

Please sign in to comment.