Skip to content

Commit

Permalink
add categories field to scorecard resource (#398)
Browse files Browse the repository at this point in the history
* add categories field to scorecard resource

* fix error message in local test
  • Loading branch information
davidbloss authored Jul 12, 2024
1 parent 1039cc0 commit 83b7a11
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20240710-121237.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: add categories field to scorecard resource, returns category ids
time: 2024-07-10T12:12:37.146024-05:00
48 changes: 43 additions & 5 deletions opslevel/resource_opslevel_scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ScorecardResource struct {
type ScorecardResourceModel struct {
AffectsOverallServiceLevels types.Bool `tfsdk:"affects_overall_service_levels"`
Aliases types.List `tfsdk:"aliases"`
CategoryIds types.List `tfsdk:"categories"`
Description types.String `tfsdk:"description"`
FilterId types.String `tfsdk:"filter_id"`
Id types.String `tfsdk:"id"`
Expand All @@ -43,7 +44,8 @@ type ScorecardResourceModel struct {
TotalChecks types.Int64 `tfsdk:"total_checks"`
}

func NewScorecardResourceModel(ctx context.Context, scorecard opslevel.Scorecard, givenModel ScorecardResourceModel) (ScorecardResourceModel, diag.Diagnostics) {
func NewScorecardResourceModel(ctx context.Context, scorecard opslevel.Scorecard, categoryIds []string, givenModel ScorecardResourceModel) (ScorecardResourceModel, diag.Diagnostics) {
var diags diag.Diagnostics
scorecardDataSourceModel := ScorecardResourceModel{
AffectsOverallServiceLevels: types.BoolValue(scorecard.AffectsOverallServiceLevels),
Description: StringValueFromResourceAndModelField(scorecard.Description, givenModel.Description),
Expand All @@ -56,7 +58,12 @@ func NewScorecardResourceModel(ctx context.Context, scorecard opslevel.Scorecard
TotalChecks: types.Int64Value(int64(scorecard.ChecksCount)),
}

scorecardAliases, diags := types.ListValueFrom(ctx, types.StringType, scorecard.Aliases)
scorecardCategoryIds, idsDiags := types.ListValueFrom(ctx, types.StringType, categoryIds)
diags.Append(idsDiags...)
scorecardDataSourceModel.CategoryIds = scorecardCategoryIds

scorecardAliases, aliasesDiags := types.ListValueFrom(ctx, types.StringType, scorecard.Aliases)
diags.Append(aliasesDiags...)
scorecardDataSourceModel.Aliases = scorecardAliases

return scorecardDataSourceModel, diags
Expand All @@ -81,6 +88,11 @@ func (r *ScorecardResource) Schema(ctx context.Context, req resource.SchemaReque
Computed: true,
ElementType: types.StringType,
},
"categories": schema.ListAttribute{
Description: "The ids of the categories on this scorecard.",
Computed: true,
ElementType: types.StringType,
},
"description": schema.StringAttribute{
Description: "The scorecard's description.",
Optional: true,
Expand Down Expand Up @@ -143,7 +155,11 @@ func (r *ScorecardResource) Create(ctx context.Context, req resource.CreateReque
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to create scorecard, got error: %s", err))
return
}
createdScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *scorecard, planModel)
categoryIds, err := getScorecardCategoyIds(r.client, *scorecard)
if err != nil {
resp.Diagnostics.AddWarning("opslevel client error", fmt.Sprintf("Unable to retrieve category ids from scorecard, got error: %s", err))
}
createdScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *scorecard, categoryIds, planModel)
if diags != nil && diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -153,6 +169,20 @@ func (r *ScorecardResource) Create(ctx context.Context, req resource.CreateReque
resp.Diagnostics.Append(resp.State.Set(ctx, &createdScorecardResourceModel)...)
}

func getScorecardCategoyIds(client *opslevel.Client, scorecard opslevel.Scorecard) ([]string, error) {
var categoryIds []string

result, err := scorecard.ListCategories(client, nil)
if err != nil {
return categoryIds, err
}
for _, category := range result.Nodes {
categoryIds = append(categoryIds, string(category.Id))
}

return categoryIds, nil
}

func (r *ScorecardResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var stateModel ScorecardResourceModel

Expand All @@ -168,7 +198,11 @@ func (r *ScorecardResource) Read(ctx context.Context, req resource.ReadRequest,
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read scorecard, got error: %s", err))
return
}
readScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *readScorecard, stateModel)
categoryIds, err := getScorecardCategoyIds(r.client, *readScorecard)
if err != nil {
resp.Diagnostics.AddWarning("opslevel client error", fmt.Sprintf("Unable to retrieve category ids from scorecard, got error: %s", err))
}
readScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *readScorecard, categoryIds, stateModel)
if diags != nil && diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -198,7 +232,11 @@ func (r *ScorecardResource) Update(ctx context.Context, req resource.UpdateReque
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to update scorecard, got error: %s", err))
return
}
updatedScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *scorecard, planModel)
categoryIds, err := getScorecardCategoyIds(r.client, *scorecard)
if err != nil {
resp.Diagnostics.AddWarning("opslevel client error", fmt.Sprintf("Unable to retrieve category ids from scorecard, got error: %s", err))
}
updatedScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *scorecard, categoryIds, planModel)
if diags != nil && diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down
1 change: 1 addition & 0 deletions tests/local/mock_resource/scorecard.tfmock.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mock_resource "opslevel_scorecard" {
defaults = {
# id intentionally omitted - will be assigned a random string
aliases = ["scorecard-one", "scorecard-two"]
categories = ["Z2lkOi8vlaksjdfsd"]
passing_checks = 7
service_count = 3
total_checks = 10
Expand Down
5 changes: 5 additions & 0 deletions tests/local/resource_scorecard.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ run "resource_scorecard_big" {
error_message = "wrong affects_overall_service_levels in opslevel_scorecard.big"
}

assert {
condition = startswith(element(opslevel_scorecard.big.categories, 0), "Z2lkOi8v")
error_message = "expected category id that starts with 'Z2lkOi8v'"
}

assert {
condition = opslevel_scorecard.big.description == "This is a big scorecard"
error_message = "wrong description in opslevel_scorecard.big"
Expand Down
1 change: 1 addition & 0 deletions tests/remote/scorecard.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ run "resource_scorecard_create_with_all_fields" {
condition = alltrue([
can(opslevel_scorecard.test.affects_overall_service_levels),
can(opslevel_scorecard.test.aliases),
can(opslevel_scorecard.test.categories),
can(opslevel_scorecard.test.description),
can(opslevel_scorecard.test.filter_id),
can(opslevel_scorecard.test.id),
Expand Down

0 comments on commit 83b7a11

Please sign in to comment.