diff --git a/.changes/unreleased/Feature-20240801-124310.yaml b/.changes/unreleased/Feature-20240801-124310.yaml new file mode 100644 index 00000000..02238645 --- /dev/null +++ b/.changes/unreleased/Feature-20240801-124310.yaml @@ -0,0 +1,4 @@ +kind: Feature +body: Add ability for 'opslevel_check_service_property' to target custom properties + of a service +time: 2024-08-01T12:43:10.20199-05:00 diff --git a/examples/resources/opslevel_check_service_property/resource.tf b/examples/resources/opslevel_check_service_property/resource.tf index f61407b7..a264ef3a 100644 --- a/examples/resources/opslevel_check_service_property/resource.tf +++ b/examples/resources/opslevel_check_service_property/resource.tf @@ -39,3 +39,18 @@ resource "opslevel_check_service_property" "example" { } notes = "Optional additional info on why this check is run or how to fix it" } + +resource "opslevel_check_service_property" "example2" { + name = "foo2" + enabled = true + # To set a future enable date remove field 'enabled' and use 'enable_on' + # enable_on = "2022-05-23T14:14:18.782000Z" + category = data.opslevel_rubric_category.security.id + level = data.opslevel_rubric_level.bronze.id + property = "custom_property" + property_definition = "my_custom_property" + predicate = { + type = "exists" + } + notes = "Optional additional info on why this check is run or how to fix it" +} diff --git a/opslevel/resource_opslevel_check_service_property.go b/opslevel/resource_opslevel_check_service_property.go index 2777d3c1..64710b10 100644 --- a/opslevel/resource_opslevel_check_service_property.go +++ b/opslevel/resource_opslevel_check_service_property.go @@ -45,8 +45,9 @@ type CheckServicePropertyResourceModel struct { Notes types.String `tfsdk:"notes"` Owner types.String `tfsdk:"owner"` - Property types.String `tfsdk:"property"` - Predicate types.Object `tfsdk:"predicate"` + Property types.String `tfsdk:"property"` + PropertyDefinition types.String `tfsdk:"property_definition"` + Predicate types.Object `tfsdk:"predicate"` } func NewCheckServicePropertyResourceModel(ctx context.Context, check opslevel.Check, planModel CheckServicePropertyResourceModel) CheckServicePropertyResourceModel { @@ -74,6 +75,10 @@ func NewCheckServicePropertyResourceModel(ctx context.Context, check opslevel.Ch stateModel.Property = RequiredStringValue(string(check.ServicePropertyCheckFragment.Property)) + if check.ServicePropertyCheckFragment.PropertyDefinition != nil { + stateModel.PropertyDefinition = planModel.PropertyDefinition + } + if check.ServicePropertyCheckFragment.Predicate == nil { stateModel.Predicate = types.ObjectNull(predicateType) } else { @@ -109,6 +114,10 @@ func (r *CheckServicePropertyResource) Schema(ctx context.Context, req resource. stringvalidator.OneOf(opslevel.AllServicePropertyTypeEnum...), }, }, + "property_definition": schema.StringAttribute{ + Description: "The alias of the property that the check will verify (e.g. the specific custom property).", + Optional: true, + }, "predicate": PredicateSchema(), }), } @@ -210,6 +219,9 @@ func (r *CheckServicePropertyResource) Create(ctx context.Context, req resource. } input.ServiceProperty = opslevel.ServicePropertyTypeEnum(planModel.Property.ValueString()) + if !planModel.PropertyDefinition.IsNull() { + input.PropertyDefinition = opslevel.NewIdentifier(planModel.PropertyDefinition.ValueString()) + } // convert environment_predicate object to model from plan predicateModel, diags := PredicateObjectToModel(ctx, planModel.Predicate) @@ -260,9 +272,11 @@ func (r *CheckServicePropertyResource) Read(ctx context.Context, req resource.Re func (r *CheckServicePropertyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { var planModel CheckServicePropertyResourceModel + var stateModel CheckServicePropertyResourceModel // Read Terraform plan data into the planModel resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + resp.Diagnostics.Append(req.State.Get(ctx, &stateModel)...) if resp.Diagnostics.HasError() { return @@ -287,6 +301,11 @@ func (r *CheckServicePropertyResource) Update(ctx context.Context, req resource. } input.ServiceProperty = opslevel.RefOf(opslevel.ServicePropertyTypeEnum(planModel.Property.ValueString())) + if !planModel.PropertyDefinition.IsNull() { + input.PropertyDefinition = opslevel.NewIdentifier(planModel.PropertyDefinition.ValueString()) + } else if !stateModel.PropertyDefinition.IsNull() { + input.PropertyDefinition = &opslevel.IdentifierInput{} + } // convert environment_predicate object to model from plan predicateModel, diags := PredicateObjectToModel(ctx, planModel.Predicate) @@ -308,10 +327,10 @@ func (r *CheckServicePropertyResource) Update(ctx context.Context, req resource. return } - stateModel := NewCheckServicePropertyResourceModel(ctx, *data, planModel) + verifiedStateModel := NewCheckServicePropertyResourceModel(ctx, *data, planModel) tflog.Trace(ctx, "updated a check service property resource") - resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) + resp.Diagnostics.Append(resp.State.Set(ctx, &verifiedStateModel)...) } func (r *CheckServicePropertyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { diff --git a/submodules/opslevel-go b/submodules/opslevel-go index 2a389626..40886719 160000 --- a/submodules/opslevel-go +++ b/submodules/opslevel-go @@ -1 +1 @@ -Subproject commit 2a389626af97bd93cddeef7f0455536e894c4f9a +Subproject commit 408867194d2bb24daf1627d8cb759fdfc24f9089