Skip to content

Commit

Permalink
Merge pull request #172 from port-labs/PORT-9649-bug-terraform-does-n…
Browse files Browse the repository at this point in the history
…ot-support-multiple-approvals

Port 9649 bug terraform does not support multiple approvals
  • Loading branch information
talsabagport authored Aug 5, 2024
2 parents 0bc13ee + cb418c0 commit f9ce8c6
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 17 deletions.
2 changes: 2 additions & 0 deletions docs/data-sources/port_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,5 @@ Read-Only:
- `identifier` (String)
- `level` (String)
- `status` (String)


9 changes: 4 additions & 5 deletions docs/resources/port_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ resource "port_action" "create_microservice" {
- `icon` (String) Icon
- `kafka_method` (Attributes) Kafka invocation method (see [below for nested schema](#nestedatt--kafka_method))
- `publish` (Boolean) Publish action
- `required_approval` (Boolean) Require approval before invoking the action
- `required_approval` (String) Require approval before invoking the action. Can be one of "true", "false", "ANY" or "ALL"
- `self_service_trigger` (Attributes) Self service trigger for the action (see [below for nested schema](#nestedatt--self_service_trigger))
- `title` (String) Title
- `upsert_entity_method` (Attributes) Upsert Entity invocation method (see [below for nested schema](#nestedatt--upsert_entity_method))
Expand Down Expand Up @@ -661,13 +661,10 @@ Optional:
<a id="nestedatt--upsert_entity_method--mapping"></a>
### Nested Schema for `upsert_entity_method.mapping`

Required:

- `identifier` (String) Required when selecting type Upsert Entity. The entity identifier for the upsert

Optional:

- `icon` (String) The icon of the entity
- `identifier` (String) Required when selecting type Upsert Entity. The entity identifier for the upsert
- `properties` (String) The properties of the entity (key-value object encoded to a string)
- `relations` (String) The relations of the entity (key-value object encoded to a string)
- `teams` (List of String) The teams the entity belongs to
Expand All @@ -688,3 +685,5 @@ Optional:
- `headers` (Map of String) The HTTP headers for invoking the action. They should be encoded as a key-value object to a string using [jsonencode](https://developer.hashicorp.com/terraform/language/functions/jsonencode). Learn about how to [define the action payload](https://docs.getport.io/create-self-service-experiences/setup-backend/#define-the-actions-payload).
- `method` (String) The HTTP method to invoke the action
- `synchronized` (String) Synchronize the action


2 changes: 2 additions & 0 deletions docs/resources/port_action_permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,5 @@ Optional:
- `roles` (List of String) The roles with execution permission
- `teams` (List of String) The teams with execution permission
- `users` (List of String) The users with execution permission


2 changes: 2 additions & 0 deletions docs/resources/port_aggregation_properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,5 @@ Optional:

- `average_of` (String) The time periods to calculate the average of, e.g. hour, day, week, month
- `measure_time_by` (String) The property name on which to calculate the the time periods, e.g. $createdAt, $updated_at or any other date property


2 changes: 2 additions & 0 deletions docs/resources/port_blueprint.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,5 @@ Required:
Optional:

- `agent` (Boolean) The agent of the webhook changelog destination


2 changes: 2 additions & 0 deletions docs/resources/port_blueprint_permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,5 @@ Optional:
- `roles` (List of String) Roles with update specific relation permissions
- `teams` (List of String) Teams with update specific relation permissions
- `users` (List of String) Users with update specific relation permissions


2 changes: 2 additions & 0 deletions docs/resources/port_entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ Optional:

- `many_relations` (Map of List of String) The many relation of the entity
- `single_relations` (Map of String) The single relation of the entity


2 changes: 2 additions & 0 deletions docs/resources/port_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ Required:
Optional:

- `agent` (Boolean) The agent of the webhook changelog destination


2 changes: 2 additions & 0 deletions docs/resources/port_page.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,5 @@ terraform import port_page.home_page "\$home"
- `id` (String) The ID of this resource.
- `updated_at` (String) The last update date of the page
- `updated_by` (String) The last updater of the page


2 changes: 2 additions & 0 deletions docs/resources/port_page_permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ Optional:
- `roles` (List of String) The roles with read permission
- `teams` (List of String) The teams with read permission
- `users` (List of String) The users with read permission


2 changes: 2 additions & 0 deletions docs/resources/port_scorecard.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,5 @@ Required:

- `color` (String) The color of the level
- `title` (String) The title of the level


2 changes: 2 additions & 0 deletions docs/resources/port_team.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ Team resource
- `id` (String) The ID of this resource.
- `provider_name` (String) The provider of the team
- `updated_at` (String) The last update date of the team


2 changes: 2 additions & 0 deletions docs/resources/port_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ Optional:
- `signature_algorithm` (String) The signature algorithm of the webhook
- `signature_header_name` (String) The signature header name of the webhook
- `signature_prefix` (String) The signature prefix of the webhook


2 changes: 1 addition & 1 deletion internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ type (
Description *string `json:"description,omitempty"`
Trigger *Trigger `json:"trigger"`
InvocationMethod *InvocationMethod `json:"invocationMethod,omitempty"`
RequiredApproval *bool `json:"requiredApproval,omitempty"`
RequiredApproval any `json:"requiredApproval,omitempty"`
ApprovalNotification *ApprovalNotification `json:"approvalNotification,omitempty"`
Publish *bool `json:"publish,omitempty"`
}
Expand Down
17 changes: 17 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,20 @@ func TFStringListToStringArray(list []types.String) []string {

return res
}

func TerraformStringToBooleanOrString(s types.String) interface{} {
var obj interface{}

if s.IsNull() {
return obj
}

value := s.ValueString()
if value == "true" {
return true
}
if value == "false" {
return false
}
return value
}
17 changes: 11 additions & 6 deletions port/action/actionStateToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package action

import (
"context"
"reflect"

"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
"github.com/port-labs/terraform-provider-port-labs/v2/internal/consts"
Expand Down Expand Up @@ -39,12 +40,16 @@ func actionDataSetToPortBody(dataSet *DatasetModel) *cli.Dataset {
func actionStateToPortBody(ctx context.Context, data *ActionModel) (*cli.Action, error) {
var err error
action := &cli.Action{
Identifier: data.Identifier.ValueString(),
Title: data.Title.ValueStringPointer(),
Icon: data.Icon.ValueStringPointer(),
Description: data.Description.ValueStringPointer(),
RequiredApproval: data.RequiredApproval.ValueBoolPointer(),
Publish: data.Publish.ValueBoolPointer(),
Identifier: data.Identifier.ValueString(),
Title: data.Title.ValueStringPointer(),
Icon: data.Icon.ValueStringPointer(),
Description: data.Description.ValueStringPointer(),
Publish: data.Publish.ValueBoolPointer(),
}

action.RequiredApproval = utils.TerraformStringToBooleanOrString(data.RequiredApproval)
if action.RequiredApproval != nil && reflect.TypeOf(action.RequiredApproval).Kind() == reflect.String {
action.RequiredApproval = map[string]interface{}{"type": action.RequiredApproval}
}

action.Trigger, err = triggerToBody(ctx, data)
Expand Down
4 changes: 2 additions & 2 deletions port/action/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ type ActionModel struct {
GitlabMethod *GitlabMethodModel `tfsdk:"gitlab_method"`
AzureMethod *AzureMethodModel `tfsdk:"azure_method"`
UpsertEntityMethod *UpsertEntityMethodModel `tfsdk:"upsert_entity_method"`
RequiredApproval types.Bool `tfsdk:"required_approval"`
RequiredApproval types.String `tfsdk:"required_approval"`
ApprovalWebhookNotification *ApprovalWebhookNotificationModel `tfsdk:"approval_webhook_notification"`
ApprovalEmailNotification types.Object `tfsdk:"approval_email_notification"`
Publish types.Bool `tfsdk:"publish"`
Expand All @@ -403,7 +403,7 @@ type ActionValidationModel struct {
GitlabMethod types.Object `tfsdk:"gitlab_method"`
AzureMethod types.Object `tfsdk:"azure_method"`
UpsertEntityMethod types.Object `tfsdk:"upsert_entity_method"`
RequiredApproval types.Bool `tfsdk:"required_approval"`
RequiredApproval types.String `tfsdk:"required_approval"`
ApprovalWebhookNotification types.Object `tfsdk:"approval_webhook_notification"`
ApprovalEmailNotification types.Object `tfsdk:"approval_email_notification"`
Publish types.Bool `tfsdk:"publish"`
Expand Down
10 changes: 9 additions & 1 deletion port/action/refreshActionState.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strconv"

"github.com/samber/lo"

Expand Down Expand Up @@ -408,7 +409,14 @@ func refreshActionState(ctx context.Context, state *ActionModel, a *cli.Action)
return err
}

state.RequiredApproval = flex.GoBoolToFramework(a.RequiredApproval)
if a.RequiredApproval == nil {
state.RequiredApproval = types.StringNull()
} else if reflect.TypeOf(a.RequiredApproval).Kind() == reflect.Map {
state.RequiredApproval = types.StringValue(a.RequiredApproval.(map[string]interface{})["type"].(string))
} else {
state.RequiredApproval = types.StringValue(strconv.FormatBool(a.RequiredApproval.(bool)))
}

if a.ApprovalNotification != nil {
if a.ApprovalNotification.Type == "email" {
state.ApprovalEmailNotification, _ = types.ObjectValue(nil, nil)
Expand Down
80 changes: 80 additions & 0 deletions port/action/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2067,3 +2067,83 @@ func TestAccPortActionUpsertEntityWithoutMappingIdentifier(t *testing.T) {
},
})
}

func TestRequiredApprovalAny(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
}
kafka_method = {}
required_approval = "ANY"
approval_webhook_notification = {
url = "https://example.com"
format = "json"
}
}`, 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", "required_approval", "ANY"),
resource.TestCheckResourceAttr("port_action.create_microservice", "approval_webhook_notification.url", "https://example.com"),
resource.TestCheckResourceAttr("port_action.create_microservice", "approval_webhook_notification.format", "json"),
),
},
},
})
}

func TestRequiredApprovalAll(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
}
kafka_method = {}
required_approval = "ALL"
approval_webhook_notification = {
url = "https://example.com"
format = "json"
}
}`, 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", "required_approval", "ALL"),
resource.TestCheckResourceAttr("port_action.create_microservice", "approval_webhook_notification.url", "https://example.com"),
resource.TestCheckResourceAttr("port_action.create_microservice", "approval_webhook_notification.format", "json"),
),
},
},
})
}
7 changes: 5 additions & 2 deletions port/action/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,12 @@ func ActionSchema() map[string]schema.Attribute {
},
},
},
"required_approval": schema.BoolAttribute{
MarkdownDescription: "Require approval before invoking the action",
"required_approval": schema.StringAttribute{
MarkdownDescription: "Require approval before invoking the action. Can be one of \"true\", \"false\", \"ANY\" or \"ALL\"",
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("true", "false", "ANY", "ALL"),
},
},
"approval_webhook_notification": schema.SingleNestedAttribute{
MarkdownDescription: "The webhook notification of the approval",
Expand Down

0 comments on commit f9ce8c6

Please sign in to comment.