-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #608 from imjaroiswebdev/a2actionAssociationWithSe…
…rvice Add support for Automation Actions' Action association to a Service
- Loading branch information
Showing
5 changed files
with
407 additions
and
35 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
pagerduty/import_pagerduty_automation_actions_action_service_association_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
pagerduty/resource_pagerduty_automation_actions_action_service_association.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
134 changes: 134 additions & 0 deletions
134
pagerduty/resource_pagerduty_automation_actions_action_service_association_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.