Skip to content

Commit

Permalink
Add custom property check (#422)
Browse files Browse the repository at this point in the history
* Add custom property check

* fixes after testing

* linting is the bane
  • Loading branch information
rocktavious authored Aug 1, 2024
1 parent caa5f51 commit 071266f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .changes/unreleased/Feature-20240801-124310.yaml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions examples/resources/opslevel_check_service_property/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
27 changes: 23 additions & 4 deletions opslevel/resource_opslevel_check_service_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
}),
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 071266f

Please sign in to comment.