Skip to content

Commit

Permalink
Added azure-devops to tf (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsinai authored Apr 2, 2023
1 parent 8e4635f commit 4e8d5d3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 16 deletions.
1 change: 1 addition & 0 deletions port/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type (
Agent bool `json:"agent,omitempty"`
Org string `json:"org,omitempty"`
Repo string `json:"repo,omitempty"`
Webhook string `json:"webhook,omitempty"`
Workflow string `json:"workflow,omitempty"`
OmitPayload bool `json:"omitPayload,omitempty"`
OmitUserInputs bool `json:"omitUserInputs,omitempty"`
Expand Down
61 changes: 45 additions & 16 deletions port/resource_port_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var requiredGithubArguments = []string{"invocation_method.0.org", "invocation_method.0.repo", "invocation_method.0.workflow"}
var requiredWebhookArguments = []string{"invocation_method.0.url"}
var requiredAzureDevopsArguments = []string{"invocation_method.0.azure_org", "invocation_method.0.webhook"}

func newActionResource() *schema.Resource {
return &schema.Resource{
Expand Down Expand Up @@ -122,14 +123,14 @@ func newActionResource() *schema.Resource {
Type: schema.TypeList,
MinItems: 1,
MaxItems: 1,
Description: "The methods the action is dispatched in. Supports WEBHOOK, KAFKA and GITHUB",
Description: "The methods the action is dispatched in. Supports WEBHOOK, KAFKA, GITHUB and AZURE-DEVOPS",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
Description: "One of WEBHOOK, KAFKA and GITHUB",
ValidateFunc: validation.StringInSlice([]string{"WEBHOOK", "KAFKA", "GITHUB"}, false),
ValidateFunc: validation.StringInSlice([]string{"WEBHOOK", "KAFKA", "GITHUB", "AZURE-DEVOPS"}, false),
},
"url": {
Type: schema.TypeString,
Expand All @@ -150,6 +151,19 @@ func newActionResource() *schema.Resource {
RequiredWith: requiredGithubArguments,
ConflictsWith: []string{"invocation_method.0.url"},
},
"azure_org": {
Type: schema.TypeString,
Optional: true,
Description: "Required when selecting type AZURE-DEVOPS. The Azure Devops org that the webhook belongs to",
RequiredWith: requiredAzureDevopsArguments,
},
"webhook": {
Type: schema.TypeString,
Optional: true,
Description: "Required when selecting type AZURE-DEVOPS. The Azure Devops webhook id",
RequiredWith: requiredAzureDevopsArguments,
ConflictsWith: []string{"invocation_method.0.url"},
},
"repo": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -216,13 +230,21 @@ func writeActionFieldsToResource(d *schema.ResourceData, action *cli.Action) {
if action.InvocationMethod.ReportWorkflowStatus != nil {
reportWorkflowStatus = *action.InvocationMethod.ReportWorkflowStatus
}

orgKey := "org"

if action.InvocationMethod.Type == "AZURE-DEVOPS" {
orgKey = "azure_org"
}

d.Set("invocation_method", []any{map[string]any{
"type": action.InvocationMethod.Type,
"url": action.InvocationMethod.Url,
"agent": action.InvocationMethod.Agent,
"org": action.InvocationMethod.Org,
orgKey: action.InvocationMethod.Org,
"repo": action.InvocationMethod.Repo,
"workflow": action.InvocationMethod.Workflow,
"webhook": action.InvocationMethod.Webhook,
"omit_payload": action.InvocationMethod.OmitPayload,
"omit_user_inputs": action.InvocationMethod.OmitUserInputs,
"report_workflow_status": reportWorkflowStatus,
Expand Down Expand Up @@ -289,21 +311,28 @@ func actionResourceToBody(d *schema.ResourceData) (*cli.Action, error) {
}

action.InvocationMethod.Type = invocationMethod.([]any)[0].(map[string]interface{})["type"].(string)
action.InvocationMethod.Url = invocationMethod.([]any)[0].(map[string]interface{})["url"].(string)
action.InvocationMethod.Agent = invocationMethod.([]any)[0].(map[string]interface{})["agent"].(bool)
action.InvocationMethod.Org = invocationMethod.([]any)[0].(map[string]interface{})["org"].(string)
action.InvocationMethod.Repo = invocationMethod.([]any)[0].(map[string]interface{})["repo"].(string)
action.InvocationMethod.Workflow = invocationMethod.([]any)[0].(map[string]interface{})["workflow"].(string)
action.InvocationMethod.OmitPayload = invocationMethod.([]any)[0].(map[string]interface{})["omit_payload"].(bool)
action.InvocationMethod.OmitUserInputs = invocationMethod.([]any)[0].(map[string]interface{})["omit_user_inputs"].(bool)

reportWorkflowStatus := invocationMethod.([]any)[0].(map[string]interface{})["report_workflow_status"].(bool)
if reportWorkflowStatus {
action.InvocationMethod.ReportWorkflowStatus = nil
} else {
action.InvocationMethod.ReportWorkflowStatus = new(bool)
*action.InvocationMethod.ReportWorkflowStatus = reportWorkflowStatus
if action.InvocationMethod.Type == "GITHUB" {
action.InvocationMethod.Org = invocationMethod.([]any)[0].(map[string]interface{})["org"].(string)
action.InvocationMethod.Repo = invocationMethod.([]any)[0].(map[string]interface{})["repo"].(string)
action.InvocationMethod.Workflow = invocationMethod.([]any)[0].(map[string]interface{})["workflow"].(string)
action.InvocationMethod.OmitPayload = invocationMethod.([]any)[0].(map[string]interface{})["omit_payload"].(bool)
action.InvocationMethod.OmitUserInputs = invocationMethod.([]any)[0].(map[string]interface{})["omit_user_inputs"].(bool)
reportWorkflowStatus := invocationMethod.([]any)[0].(map[string]interface{})["report_workflow_status"].(bool)
if reportWorkflowStatus {
action.InvocationMethod.ReportWorkflowStatus = nil
} else {
action.InvocationMethod.ReportWorkflowStatus = new(bool)
*action.InvocationMethod.ReportWorkflowStatus = reportWorkflowStatus
}
} else if action.InvocationMethod.Type == "AZURE-DEVOPS" {
action.InvocationMethod.Org = invocationMethod.([]any)[0].(map[string]interface{})["azure_org"].(string)
action.InvocationMethod.Webhook = invocationMethod.([]any)[0].(map[string]interface{})["webhook"].(string)
} else if action.InvocationMethod.Type == "WEBHOOK" {
action.InvocationMethod.Url = invocationMethod.([]any)[0].(map[string]interface{})["url"].(string)
}
action.InvocationMethod.Agent = invocationMethod.([]any)[0].(map[string]interface{})["agent"].(bool)

}
action.Trigger = d.Get("trigger").(string)

Expand Down
50 changes: 50 additions & 0 deletions port/resource_port_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,53 @@ func TestAccPortActionGithubInvocation(t *testing.T) {
},
})
}

func TestAccPortActionAzureInvocation(t *testing.T) {
identifier := genID()
actionIdentifier := genID()
var testAccActionConfigCreate = fmt.Sprintf(`
resource "port-labs_blueprint" "microservice" {
title = "TF test microservice"
icon = "Terraform"
identifier = "%s"
properties {
identifier = "text"
type = "string"
title = "text"
}
}
resource "port-labs_action" "restart_microservice" {
title = "Restart service"
icon = "Terraform"
identifier = "%s"
blueprint_identifier = port-labs_blueprint.microservice.identifier
trigger = "DAY-2"
invocation_method {
type = "AZURE-DEVOPS"
azure_org = "port-labs"
webhook = "test"
}
}
`, identifier, actionIdentifier)
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"port-labs": Provider(),
},
Steps: []resource.TestStep{
{
Config: testAccActionConfigCreate,
Destroy: false,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "title", "Restart service"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "icon", "Terraform"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "identifier", actionIdentifier),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "blueprint_identifier", identifier),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "trigger", "DAY-2"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "invocation_method.0.type", "AZURE-DEVOPS"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "invocation_method.0.azure_org", "port-labs"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "invocation_method.0.webhook", "test"),
),
},
},
})
}

0 comments on commit 4e8d5d3

Please sign in to comment.