Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Default values as supported by newer terraform-plugin-framework #183

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 0 additions & 105 deletions jupiterone/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

func BoolDefaultValuePlanModifier(b bool) planmodifier.Bool {
return &boolDefaultValuePlanModifier{
DefaultValue: types.BoolValue(b),
}
}

type boolDefaultValuePlanModifier struct {
DefaultValue types.Bool
}

var _ planmodifier.Bool = (*boolDefaultValuePlanModifier)(nil)

func (pm *boolDefaultValuePlanModifier) Description(ctx context.Context) string {
return "sets a default value for a bool value"
}

func (pm *boolDefaultValuePlanModifier) MarkdownDescription(ctx context.Context) string {
return pm.Description(ctx)
}

func (pm *boolDefaultValuePlanModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, res *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
}

res.PlanValue = pm.DefaultValue
}

var _ planmodifier.String = (*jsonIgnoreDiff)(nil)
var _ planmodifier.List = (*jsonIgnoreDiff)(nil)
var _ planmodifier.Map = (*jsonIgnoreDiff)(nil)
Expand Down Expand Up @@ -240,73 +205,3 @@ func (jsonIgnoreDiff) PlanModifyMap(ctx context.Context, req planmodifier.MapReq

resp.PlanValue = req.StateValue
}

func Int64DefaultValue(v types.Int64) planmodifier.Int64 {
return &int64DefaultValuePlanModifier{v}
}

// int64DefaultValuePlanModifier is based on the example at:
// https://developer.hashicorp.com/terraform/plugin/framework/migrating/attributes-blocks/default-values
type int64DefaultValuePlanModifier struct {
DefaultValue types.Int64
}

var _ planmodifier.Int64 = (*int64DefaultValuePlanModifier)(nil)

func (apm *int64DefaultValuePlanModifier) Description(ctx context.Context) string {
return "sets a default value for an int64 value"
}

func (apm *int64DefaultValuePlanModifier) MarkdownDescription(ctx context.Context) string {
return apm.Description(ctx)
}

func (apm *int64DefaultValuePlanModifier) PlanModifyInt64(ctx context.Context, req planmodifier.Int64Request, res *planmodifier.Int64Response) {
// 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
}

res.PlanValue = apm.DefaultValue
}

func StringDefaultValue(v string) planmodifier.String {
return &stringDefaultValuePlanModifier{
DefaultValue: types.StringValue(v),
}
}

type stringDefaultValuePlanModifier struct {
DefaultValue types.String
}

var _ planmodifier.String = (*stringDefaultValuePlanModifier)(nil)

func (apm *stringDefaultValuePlanModifier) Description(ctx context.Context) string {
return "sets a default value for an string value"
}

func (apm *stringDefaultValuePlanModifier) MarkdownDescription(ctx context.Context) string {
return apm.Description(ctx)
}

func (apm *stringDefaultValuePlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, res *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
}

res.PlanValue = apm.DefaultValue
}
18 changes: 6 additions & 12 deletions jupiterone/resource_question.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ 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/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 Down Expand Up @@ -102,17 +104,13 @@ func (*QuestionResource) Schema(ctx context.Context, req resource.SchemaRequest,
Description: "Whether to enable daily trend data collection. Defaults to false.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
BoolDefaultValuePlanModifier(false),
},
Default: booldefault.StaticBool(false),
},
"polling_interval": schema.StringAttribute{
Description: "Frequency of automated question evaluation. Defaults to ONE_DAY.",
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
StringDefaultValue(string(client.SchedulerPollingIntervalOneDay)),
},
Default: stringdefault.StaticString(string(client.SchedulerPollingIntervalOneDay)),
Validators: []validator.String{
stringvalidator.OneOf(PollingIntervals...),
},
Expand Down Expand Up @@ -149,17 +147,13 @@ func (*QuestionResource) Schema(ctx context.Context, req resource.SchemaRequest,
"include_deleted": schema.BoolAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
BoolDefaultValuePlanModifier(false),
},
Default: booldefault.StaticBool(false),
},
"results_are": schema.StringAttribute{
Description: "Defaults to INFORMATIVE.",
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
StringDefaultValue(string(client.QueryResultsAreInformative)),
},
Default: stringdefault.StaticString(string(client.QueryResultsAreInformative)),
Validators: []validator.String{
stringvalidator.OneOf(QueryResultsAre...),
},
Expand Down
21 changes: 8 additions & 13 deletions jupiterone/resource_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ 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/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"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 Down Expand Up @@ -185,18 +188,16 @@ func (*QuestionRuleResource) Schema(ctx context.Context, req resource.SchemaRequ
Description: "Rule evaluation specification version in the case of breaking changes.",
Computed: true,
Optional: true,
Default: int64default.StaticInt64(1),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
Int64DefaultValue(types.Int64Value(1)),
},
},
"polling_interval": schema.StringAttribute{
Description: "Frequency of automated rule evaluation. Defaults to ONE_DAY.",
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
StringDefaultValue(string(client.SchedulerPollingIntervalOneDay)),
},
Default: stringdefault.StaticString(string(client.SchedulerPollingIntervalOneDay)),
Validators: []validator.String{
stringvalidator.OneOf(PollingIntervals...),
},
Expand Down Expand Up @@ -248,16 +249,12 @@ func (*QuestionRuleResource) Schema(ctx context.Context, req resource.SchemaRequ
"notify_on_failure": schema.BoolAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
BoolDefaultValuePlanModifier(false),
},
Default: booldefault.StaticBool(false),
},
"trigger_on_new_only": schema.BoolAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
BoolDefaultValuePlanModifier(false),
},
Default: booldefault.StaticBool(false),
},
},
// TODO: Deprecate the use of blocks following new framework guidance:
Expand Down Expand Up @@ -292,9 +289,7 @@ func (*QuestionRuleResource) Schema(ctx context.Context, req resource.SchemaRequ
"include_deleted": schema.BoolAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
BoolDefaultValuePlanModifier(false),
},
Default: booldefault.StaticBool(false),
},
},
},
Expand Down
Loading