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

[PORT-8779] Added custom scorecards #160

Closed
wants to merge 4 commits into from
Closed
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
30 changes: 21 additions & 9 deletions internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ type (
}

ScorecardRulesModel struct {
Identifier string `tfsdk:"identifier"`
Status string `tfsdk:"status"`
Level string `tfsdk:"level"`
Identifier string `json:"identifier"`
Status string `json:"status"`
Level string `json:"level"`
}

ScorecardLevelModel struct {
Title string `json:"title"`
Color string `json:"color"`
}

ScorecardModel struct {
Rules []ScorecardRulesModel `tfsdk:"rules"`
Level string `tfsdk:"level"`
Rules []ScorecardRulesModel `json:"rules"`
Levels []ScorecardLevelModel `json:"levels"`
Level string `json:"level"`
}

Entity struct {
Expand Down Expand Up @@ -326,10 +332,11 @@ type (

Scorecard struct {
Meta
Identifier string `json:"identifier,omitempty"`
Title string `json:"title,omitempty"`
Blueprint string `json:"blueprint,omitempty"`
Rules []Rule `json:"rules,omitempty"`
Identifier string `json:"identifier,omitempty"`
Title string `json:"title,omitempty"`
Blueprint string `json:"blueprint,omitempty"`
Levels []Level `json:"levels,omitempty"`
Rules []Rule `json:"rules,omitempty"`
}

Rule struct {
Expand All @@ -339,6 +346,11 @@ type (
Query Query `json:"query,omitempty"`
}

Level struct {
Title string `json:"title,omitempty"`
Color string `json:"color,omitempty"`
}

Query struct {
Combinator string `json:"combinator,omitempty"`
Conditions []any `json:"conditions,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions port/scorecard/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ type Rule struct {
Query *Query `tfsdk:"query"`
}

type Level struct {
Title types.String `tfsdk:"title"`
Color types.String `tfsdk:"color"`
}

type ScorecardModel struct {
ID types.String `tfsdk:"id"`
Identifier types.String `tfsdk:"identifier"`
Blueprint types.String `tfsdk:"blueprint"`
Title types.String `tfsdk:"title"`
Levels []Level `tfsdk:"levels"`
Rules []Rule `tfsdk:"rules"`
CreatedAt types.String `tfsdk:"created_at"`
CreatedBy types.String `tfsdk:"created_by"`
Expand Down
23 changes: 22 additions & 1 deletion port/scorecard/refreshScorecardState.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
)

func refreshScorecardState(ctx context.Context, state *ScorecardModel, s *cli.Scorecard, blueprintIdentifier string) {
func refreshScorecardState(ctx context.Context, state *ScorecardModel, s *cli.Scorecard, blueprintIdentifier string, expectLevels *bool) {
state.ID = types.StringValue(fmt.Sprintf("%s:%s", blueprintIdentifier, s.Identifier))
state.Identifier = types.StringValue(s.Identifier)
state.Blueprint = types.StringValue(blueprintIdentifier)
Expand Down Expand Up @@ -43,4 +43,25 @@

state.Rules = stateRules

if expectLevels != nil && !*expectLevels {
return
}

var cliLevels []cli.Level
if len(s.Levels) == 0 {
cliLevels = DefaultLevels()
} else {
cliLevels = s.Levels
}

stateLevels := []Level{}
for _, cliLevel := range cliLevels {
level := &Level{
Color: types.StringValue(cliLevel.Color),
Title: types.StringValue(cliLevel.Title),
}
stateLevels = append(stateLevels, *level)
}
fmt.Sprintf("============================================================================================================================ %s", stateLevels)

Check failure on line 65 in port/scorecard/refreshScorecardState.go

View workflow job for this annotation

GitHub Actions / lint

unusedresult: result of fmt.Sprintf call not used (govet)
state.Levels = stateLevels
}
21 changes: 17 additions & 4 deletions port/scorecard/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@
return
}

refreshScorecardState(ctx, state, s, blueprintIdentifier)

refreshScorecardState(ctx, state, s, blueprintIdentifier, nil)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}

Expand All @@ -81,18 +80,32 @@
return
}

writeScorecardComputedFieldsToState(state, sp)
expectLevels := false
if len(s.Levels) > 0 {
expectLevels = true
}
refreshScorecardState(ctx, state, sp, state.Blueprint.ValueString(), &expectLevels)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}

func writeScorecardComputedFieldsToState(state *ScorecardModel, wp *cli.Scorecard) {

Check failure on line 92 in port/scorecard/resource.go

View workflow job for this annotation

GitHub Actions / lint

`writeScorecardComputedFieldsToState` is unused (deadcode)
state.ID = types.StringValue(fmt.Sprintf("%s:%s", wp.Blueprint, wp.Identifier))
state.Identifier = types.StringValue(wp.Identifier)
state.CreatedAt = types.StringValue(wp.CreatedAt.String())
state.CreatedBy = types.StringValue(wp.CreatedBy)
state.UpdatedAt = types.StringValue(wp.UpdatedAt.String())
state.UpdatedBy = types.StringValue(wp.UpdatedBy)
stateLevels := []Level{}
cliLevels := wp.Levels
for _, cliLevel := range cliLevels {
level := &Level{
Color: types.StringValue(cliLevel.Color),
Title: types.StringValue(cliLevel.Title),
}
stateLevels = append(stateLevels, *level)
}
state.Levels = stateLevels
}

func (r *ScorecardResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down Expand Up @@ -125,7 +138,7 @@
return
}

writeScorecardComputedFieldsToState(state, sp)
refreshScorecardState(ctx, state, sp, state.Blueprint.ValueString(), nil)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
Expand Down
21 changes: 21 additions & 0 deletions port/scorecard/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/port-labs/terraform-provider-port-labs/v2/internal/acctest"
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
)

func printResourceAttributes(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}

for key, value := range rs.Primary.Attributes {
fmt.Printf("%s: %s\n", key, value)
}
return nil
}
}

func testAccCreateBlueprintConfig(identifier string) string {
return fmt.Sprintf(`
resource "port_blueprint" "microservice" {
Expand Down Expand Up @@ -71,6 +86,7 @@ func TestAccPortScorecardBasic(t *testing.T) {
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
printResourceAttributes("port_scorecard.test"),
resource.TestCheckResourceAttr("port_scorecard.test", "title", "Scorecard 1"),
resource.TestCheckResourceAttr("port_scorecard.test", "blueprint", blueprintIdentifier),
resource.TestCheckResourceAttr("port_scorecard.test", "rules.#", "1"),
Expand All @@ -80,6 +96,11 @@ func TestAccPortScorecardBasic(t *testing.T) {
resource.TestCheckResourceAttr("port_scorecard.test", "rules.0.query.combinator", "and"),
resource.TestCheckResourceAttr("port_scorecard.test", "rules.0.query.conditions.#", "1"),
resource.TestCheckResourceAttr("port_scorecard.test", "rules.0.query.conditions.0", "{\"operator\":\"isNotEmpty\",\"property\":\"$team\"}"),
resource.TestCheckResourceAttr("port_scorecard.test", "levels.#", "4"),
resource.TestCheckResourceAttr("port_scorecard.test", "levels.0", "{\"color\":\"paleBlue\",\"title\":\"Basic\"}"),
resource.TestCheckResourceAttr("port_scorecard.test", "levels.1", "{\"color\":\"bronze\",\"title\":\"Bronze\"}"),
resource.TestCheckResourceAttr("port_scorecard.test", "levels.2", "{\"color\":\"silver\",\"title\":\"Silver\"}"),
resource.TestCheckResourceAttr("port_scorecard.test", "levels.3", "{\"color\":\"gold\",\"title\":\"Gold\"}"),
),
},
},
Expand Down
23 changes: 20 additions & 3 deletions port/scorecard/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func LevelSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"color": schema.StringAttribute{
MarkdownDescription: "The color of the level",
Required: true,
},
"title": schema.StringAttribute{
MarkdownDescription: "The title of the level",
Required: true,
},
}
}

func RuleSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"identifier": schema.StringAttribute{
Expand All @@ -26,9 +39,6 @@ func RuleSchema() map[string]schema.Attribute {
"level": schema.StringAttribute{
MarkdownDescription: "The level of the rule",
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("Bronze", "Silver", "Gold"),
},
},
"query": schema.SingleNestedAttribute{
MarkdownDescription: "The query of the rule",
Expand Down Expand Up @@ -70,6 +80,13 @@ func ScorecardSchema() map[string]schema.Attribute {
MarkdownDescription: "The title of the scorecard",
Required: true,
},
"levels": schema.ListNestedAttribute{
MarkdownDescription: "The Levels of the scorecard",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: LevelSchema(),
},
},
"rules": schema.ListNestedAttribute{
MarkdownDescription: "The rules of the scorecard",
Required: true,
Expand Down
32 changes: 32 additions & 0 deletions port/scorecard/scorecardResourceToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ import (
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
)

func DefaultLevels() []cli.Level {
return []cli.Level{
{
Color: "paleBlue",
Title: "Basic",
},
{
Color: "bronze",
Title: "Bronze",
},
{
Color: "silver",
Title: "Silver",
},
{
Color: "gold",
Title: "Gold",
},
}
}

func scorecardResourceToPortBody(ctx context.Context, state *ScorecardModel) (*cli.Scorecard, error) {
s := &cli.Scorecard{
Identifier: state.Identifier.ValueString(),
Expand Down Expand Up @@ -43,5 +64,16 @@ func scorecardResourceToPortBody(ctx context.Context, state *ScorecardModel) (*c

s.Rules = rules

if len(state.Levels) > 0 {
var levels []cli.Level
for _, stateLevel := range state.Levels {
level := &cli.Level{
Color: stateLevel.Color.ValueString(),
Title: stateLevel.Title.ValueString(),
}
levels = append(levels, *level)
}
s.Levels = levels
}
return s, nil
}
Loading