From c699e7826e20f07c3344e18213ba56e0bb2c73ee Mon Sep 17 00:00:00 2001 From: Tomer Buhadana Date: Sun, 22 Dec 2024 17:36:05 +0200 Subject: [PATCH] add teams_jq support --- port/action/actionStateToPortBody.go | 3 ++ port/action/model.go | 1 + port/action/refreshActionState.go | 5 ++- port/action/resource_test.go | 50 ++++++++++++++++++++++++++++ port/action/schema.go | 7 ++++ 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/port/action/actionStateToPortBody.go b/port/action/actionStateToPortBody.go index 567ef02b..a17b7e70 100644 --- a/port/action/actionStateToPortBody.go +++ b/port/action/actionStateToPortBody.go @@ -360,6 +360,9 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca if data.UpsertEntityMethod.Mapping.Teams != nil { team = flex.TerraformStringListToGoArray(data.UpsertEntityMethod.Mapping.Teams) } + if !data.UpsertEntityMethod.Mapping.TeamsJQ.IsNull() { + team = data.UpsertEntityMethod.Mapping.TeamsJQ.ValueString() + } properties, err := utils.TerraformJsonStringToGoObject(data.UpsertEntityMethod.Mapping.Properties.ValueStringPointer()) if err != nil { return nil, err diff --git a/port/action/model.go b/port/action/model.go index fcc0f7c9..da7473df 100644 --- a/port/action/model.go +++ b/port/action/model.go @@ -380,6 +380,7 @@ type MappingModel struct { Relations types.String `tfsdk:"relations"` Identifier types.String `tfsdk:"identifier"` Teams []types.String `tfsdk:"teams"` + TeamsJQ types.String `tfsdk:"teams_jq"` Icon types.String `tfsdk:"icon"` } diff --git a/port/action/refreshActionState.go b/port/action/refreshActionState.go index 1ff7d88d..cfb66d2c 100644 --- a/port/action/refreshActionState.go +++ b/port/action/refreshActionState.go @@ -102,14 +102,16 @@ func writeInvocationMethodToResource(ctx context.Context, a *cli.Action, state * if a.InvocationMethod.Type == consts.UpsertEntity { var teams []types.String + var teamsJQ types.String switch team := a.InvocationMethod.Mapping.Team.(type) { case string: - teams = append(teams, types.StringValue(team)) + teamsJQ = types.StringValue(team) case []interface{}: teams = make([]types.String, 0) for _, t := range team { teams = append(teams, types.StringValue(t.(string))) } + teamsJQ = types.StringNull() } properties, err := utils.GoObjectToTerraformString(a.InvocationMethod.Mapping.Properties) if err != nil { @@ -128,6 +130,7 @@ func writeInvocationMethodToResource(ctx context.Context, a *cli.Action, state * Relations: relations, Icon: flex.GoStringToFramework(a.InvocationMethod.Mapping.Icon), Teams: teams, + TeamsJQ: teamsJQ, Identifier: types.StringPointerValue(a.InvocationMethod.Mapping.Identifier), }, } diff --git a/port/action/resource_test.go b/port/action/resource_test.go index 7fd8b049..c7d15c6f 100644 --- a/port/action/resource_test.go +++ b/port/action/resource_test.go @@ -448,6 +448,56 @@ func TestAccPortActionUpsertEntityInvocation(t *testing.T) { }) } +func TestAccPortActionUpsertEntityInvocationWithJqTeams(t *testing.T) { + identifier := utils.GenID() + actionIdentifier := utils.GenID() + var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(` + resource "port_action" "create_microservice" { + title = "TF Provider Test" + identifier = "%s" + icon = "Terraform" + self_service_trigger = { + operation = "DAY-2" + blueprint_identifier = port_blueprint.microservice.identifier + } + upsert_entity_method = { + title = "Test Entity" + blueprint_identifier = port_blueprint.microservice.identifier + mapping = { + identifier = "test-entity" + teams_jq = "{{.entity.teams}}" + icon = "Terraform" + properties = jsonencode({"text": "test"}) + relations = jsonencode({"test-rel": "target-bp"}) + } + } + }`, actionIdentifier) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.TestAccPreCheck(t) }, + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: acctest.ProviderConfig + testAccActionConfigCreate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("port_action.create_microservice", "title", "TF Provider Test"), + resource.TestCheckResourceAttr("port_action.create_microservice", "identifier", actionIdentifier), + resource.TestCheckResourceAttr("port_action.create_microservice", "icon", "Terraform"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.blueprint_identifier", identifier), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.operation", "DAY-2"), + resource.TestCheckResourceAttr("port_action.create_microservice", "upsert_entity_method.title", "Test Entity"), + resource.TestCheckResourceAttr("port_action.create_microservice", "upsert_entity_method.blueprint_identifier", identifier), + resource.TestCheckResourceAttr("port_action.create_microservice", "upsert_entity_method.mapping.identifier", "test-entity"), + resource.TestCheckResourceAttr("port_action.create_microservice", "upsert_entity_method.mapping.teams_jq", "{{.entity.teams}}"), + resource.TestCheckResourceAttr("port_action.create_microservice", "upsert_entity_method.mapping.icon", "Terraform"), + resource.TestCheckResourceAttr("port_action.create_microservice", "upsert_entity_method.mapping.properties", "{\"text\":\"test\"}"), + resource.TestCheckResourceAttr("port_action.create_microservice", "upsert_entity_method.mapping.relations", "{\"test-rel\":\"target-bp\"}"), + ), + }, + }, + }) +} + func TestAccPortActionImport(t *testing.T) { blueprintIdentifier := utils.GenID() actionIdentifier := utils.GenID() diff --git a/port/action/schema.go b/port/action/schema.go index 6e541b81..af87d1e3 100644 --- a/port/action/schema.go +++ b/port/action/schema.go @@ -427,6 +427,13 @@ func ActionSchema() map[string]schema.Attribute { ElementType: types.StringType, Optional: true, }, + "teams_jq": schema.StringAttribute{ + MarkdownDescription: "Jq that returns the teams the entity belongs to", + Optional: true, + Validators: []validator.String{ + stringvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("teams")), + }, + }, "icon": schema.StringAttribute{ MarkdownDescription: "The icon of the entity", Optional: true,