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

Add support for Automation Actions' Action association to a Service #608

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccPagerDutyAutomationActionsActionServiceAssociation_import(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
actionName := fmt.Sprintf("tf-%s", acctest.RandString(5))
serviceName := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyAutomationActionsActionServiceAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyAutomationActionsActionServiceAssociationConfig(username, email, escalationPolicy, serviceName, actionName),
},
{
ResourceName: "pagerduty_automation_actions_action_service_association.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
71 changes: 36 additions & 35 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,42 @@ func Provider() *schema.Provider {
},

ResourcesMap: map[string]*schema.Resource{
"pagerduty_addon": resourcePagerDutyAddon(),
"pagerduty_escalation_policy": resourcePagerDutyEscalationPolicy(),
"pagerduty_maintenance_window": resourcePagerDutyMaintenanceWindow(),
"pagerduty_schedule": resourcePagerDutySchedule(),
"pagerduty_service": resourcePagerDutyService(),
"pagerduty_service_integration": resourcePagerDutyServiceIntegration(),
"pagerduty_team": resourcePagerDutyTeam(),
"pagerduty_team_membership": resourcePagerDutyTeamMembership(),
"pagerduty_user": resourcePagerDutyUser(),
"pagerduty_user_contact_method": resourcePagerDutyUserContactMethod(),
"pagerduty_user_notification_rule": resourcePagerDutyUserNotificationRule(),
"pagerduty_extension": resourcePagerDutyExtension(),
"pagerduty_extension_servicenow": resourcePagerDutyExtensionServiceNow(),
"pagerduty_event_rule": resourcePagerDutyEventRule(),
"pagerduty_ruleset": resourcePagerDutyRuleset(),
"pagerduty_ruleset_rule": resourcePagerDutyRulesetRule(),
"pagerduty_business_service": resourcePagerDutyBusinessService(),
"pagerduty_service_dependency": resourcePagerDutyServiceDependency(),
"pagerduty_response_play": resourcePagerDutyResponsePlay(),
"pagerduty_tag": resourcePagerDutyTag(),
"pagerduty_tag_assignment": resourcePagerDutyTagAssignment(),
"pagerduty_service_event_rule": resourcePagerDutyServiceEventRule(),
"pagerduty_slack_connection": resourcePagerDutySlackConnection(),
"pagerduty_business_service_subscriber": resourcePagerDutyBusinessServiceSubscriber(),
"pagerduty_webhook_subscription": resourcePagerDutyWebhookSubscription(),
"pagerduty_event_orchestration": resourcePagerDutyEventOrchestration(),
"pagerduty_event_orchestration_router": resourcePagerDutyEventOrchestrationPathRouter(),
"pagerduty_event_orchestration_unrouted": resourcePagerDutyEventOrchestrationPathUnrouted(),
"pagerduty_event_orchestration_service": resourcePagerDutyEventOrchestrationPathService(),
"pagerduty_automation_actions_runner": resourcePagerDutyAutomationActionsRunner(),
"pagerduty_automation_actions_action": resourcePagerDutyAutomationActionsAction(),
"pagerduty_automation_actions_action_team_association": resourcePagerDutyAutomationActionsActionTeamAssociation(),
"pagerduty_automation_actions_runner_team_association": resourcePagerDutyAutomationActionsRunnerTeamAssociation(),
"pagerduty_incident_workflow": resourcePagerDutyIncidentWorkflow(),
"pagerduty_incident_workflow_trigger": resourcePagerDutyIncidentWorkflowTrigger(),
"pagerduty_addon": resourcePagerDutyAddon(),
"pagerduty_escalation_policy": resourcePagerDutyEscalationPolicy(),
"pagerduty_maintenance_window": resourcePagerDutyMaintenanceWindow(),
"pagerduty_schedule": resourcePagerDutySchedule(),
"pagerduty_service": resourcePagerDutyService(),
"pagerduty_service_integration": resourcePagerDutyServiceIntegration(),
"pagerduty_team": resourcePagerDutyTeam(),
"pagerduty_team_membership": resourcePagerDutyTeamMembership(),
"pagerduty_user": resourcePagerDutyUser(),
"pagerduty_user_contact_method": resourcePagerDutyUserContactMethod(),
"pagerduty_user_notification_rule": resourcePagerDutyUserNotificationRule(),
"pagerduty_extension": resourcePagerDutyExtension(),
"pagerduty_extension_servicenow": resourcePagerDutyExtensionServiceNow(),
"pagerduty_event_rule": resourcePagerDutyEventRule(),
"pagerduty_ruleset": resourcePagerDutyRuleset(),
"pagerduty_ruleset_rule": resourcePagerDutyRulesetRule(),
"pagerduty_business_service": resourcePagerDutyBusinessService(),
"pagerduty_service_dependency": resourcePagerDutyServiceDependency(),
"pagerduty_response_play": resourcePagerDutyResponsePlay(),
"pagerduty_tag": resourcePagerDutyTag(),
"pagerduty_tag_assignment": resourcePagerDutyTagAssignment(),
"pagerduty_service_event_rule": resourcePagerDutyServiceEventRule(),
"pagerduty_slack_connection": resourcePagerDutySlackConnection(),
"pagerduty_business_service_subscriber": resourcePagerDutyBusinessServiceSubscriber(),
"pagerduty_webhook_subscription": resourcePagerDutyWebhookSubscription(),
"pagerduty_event_orchestration": resourcePagerDutyEventOrchestration(),
"pagerduty_event_orchestration_router": resourcePagerDutyEventOrchestrationPathRouter(),
"pagerduty_event_orchestration_unrouted": resourcePagerDutyEventOrchestrationPathUnrouted(),
"pagerduty_event_orchestration_service": resourcePagerDutyEventOrchestrationPathService(),
"pagerduty_automation_actions_runner": resourcePagerDutyAutomationActionsRunner(),
"pagerduty_automation_actions_action": resourcePagerDutyAutomationActionsAction(),
"pagerduty_automation_actions_action_team_association": resourcePagerDutyAutomationActionsActionTeamAssociation(),
"pagerduty_automation_actions_runner_team_association": resourcePagerDutyAutomationActionsRunnerTeamAssociation(),
"pagerduty_incident_workflow": resourcePagerDutyIncidentWorkflow(),
"pagerduty_incident_workflow_trigger": resourcePagerDutyIncidentWorkflowTrigger(),
"pagerduty_automation_actions_action_service_association": resourcePagerDutyAutomationActionsActionServiceAssociation(),
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package pagerduty

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourcePagerDutyAutomationActionsActionServiceAssociation() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyAutomationActionsActionServiceAssociationCreate,
Read: resourcePagerDutyAutomationActionsActionServiceAssociationRead,
Delete: resourcePagerDutyAutomationActionsActionServiceAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"action_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"service_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourcePagerDutyAutomationActionsActionServiceAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).Client()
if err != nil {
return err
}

actionID := d.Get("action_id").(string)
serviceID := d.Get("service_id").(string)

log.Printf("[INFO] Creating PagerDuty AutomationActionsActionServiceAssociation %s:%s", d.Get("action_id").(string), d.Get("service_id").(string))

retryErr := resource.Retry(10*time.Second, func() *resource.RetryError {
if serviceRef, _, err := client.AutomationActionsAction.AssociateToService(actionID, serviceID); err != nil {
if isErrCode(err, 429) {
time.Sleep(2 * time.Second)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
} else if serviceRef != nil {
d.SetId(fmt.Sprintf("%s:%s", actionID, serviceID))
}
return nil
})

if retryErr != nil {
return retryErr
}

return fetchPagerDutyAutomationActionsActionServiceAssociation(d, meta, handleNotFoundError)
}

func fetchPagerDutyAutomationActionsActionServiceAssociation(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, err := meta.(*Config).Client()
if err != nil {
return err
}

actionID, serviceID := resourcePagerDutyParseColonCompoundID(d.Id())
return resource.Retry(30*time.Second, func() *resource.RetryError {
resp, _, err := client.AutomationActionsAction.GetAssociationToService(actionID, serviceID)
if err != nil {
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}

if resp.Service.ID != serviceID {
log.Printf("[WARN] Removing %s since the service: %s is not associated to the action: %s", d.Id(), actionID, serviceID)
d.SetId("")
return nil
}

d.Set("action_id", actionID)
d.Set("service_id", resp.Service.ID)

return nil
})
}

func resourcePagerDutyAutomationActionsActionServiceAssociationRead(d *schema.ResourceData, meta interface{}) error {
return fetchPagerDutyAutomationActionsActionServiceAssociation(d, meta, handleNotFoundError)
}

func resourcePagerDutyAutomationActionsActionServiceAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).Client()
if err != nil {
return err
}

actionID, serviceID := resourcePagerDutyParseColonCompoundID(d.Id())
log.Printf("[INFO] Deleting PagerDuty AutomationActionsActionServiceAssociation %s", d.Id())

retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if _, err := client.AutomationActionsAction.DissociateFromService(actionID, serviceID); err != nil {
return resource.RetryableError(err)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}
d.SetId("")

// giving the API time to catchup
time.Sleep(time.Second)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func init() {
resource.AddTestSweepers("pagerduty_automation_actions_action_service_association", &resource.Sweeper{
Name: "pagerduty_automation_actions_action_service_association",
F: testSweepAutomationActionsActionServiceAssociation,
})
}

func testSweepAutomationActionsActionServiceAssociation(region string) error {
return nil
}

func TestAccPagerDutyAutomationActionsActionServiceAssociation_Basic(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
actionName := fmt.Sprintf("tf-%s", acctest.RandString(5))
serviceName := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyAutomationActionsActionServiceAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyAutomationActionsActionServiceAssociationConfig(username, email, escalationPolicy, serviceName, actionName),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyAutomationActionsActionServiceAssociationExists("pagerduty_automation_actions_action_service_association.foo"),
resource.TestCheckResourceAttrSet("pagerduty_automation_actions_action_service_association.foo", "action_id"),
resource.TestCheckResourceAttrSet("pagerduty_automation_actions_action_service_association.foo", "service_id"),
),
},
},
})
}

func testAccCheckPagerDutyAutomationActionsActionServiceAssociationDestroy(s *terraform.State) error {
client, _ := testAccProvider.Meta().(*Config).Client()
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_automation_actions_action_service_association" {
continue
}
actionID, serviceID := resourcePagerDutyParseColonCompoundID(r.Primary.ID)
if _, _, err := client.AutomationActionsAction.GetAssociationToService(actionID, serviceID); err == nil {
return fmt.Errorf("Automation Actions Runner still exists")
}
}
return nil
}

func testAccCheckPagerDutyAutomationActionsActionServiceAssociationExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Automation Actions Runner ID is set")
}

client, _ := testAccProvider.Meta().(*Config).Client()
actionID, serviceID := resourcePagerDutyParseColonCompoundID(rs.Primary.ID)
found, _, err := client.AutomationActionsAction.GetAssociationToService(actionID, serviceID)
if err != nil {
return err
}
if fmt.Sprintf("%s:%s", actionID, found.Service.ID) != rs.Primary.ID {
return fmt.Errorf("Automation Actions Action association to service not found: %v - %v", rs.Primary.ID, found)
}

return nil
}
}

func testAccCheckPagerDutyAutomationActionsActionServiceAssociationConfig(username, email, escalationPolicy, serviceName, actionName string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
name = "%s"
email = "%s"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}

resource "pagerduty_escalation_policy" "foo" {
name = "%s"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = pagerduty_user.foo.id
}
}
}

resource "pagerduty_service" "foo" {
name = "%s"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = pagerduty_escalation_policy.foo.id
alert_creation = "create_incidents"
}

resource "pagerduty_automation_actions_action" "foo" {
name = "%s"
description = "PA Action created by TF"
action_type = "script"
action_data_reference {
script = "java --version"
invocation_command = "/bin/bash"
}
}

resource "pagerduty_automation_actions_action_service_association" "foo" {
action_id = pagerduty_automation_actions_action.foo.id
service_id = pagerduty_service.foo.id
}

`, username, email, escalationPolicy, serviceName, actionName)
}
Loading