From f3dc7d8aecdbaba8889f61a5c1598e643a91ed9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Thu, 5 Jan 2023 16:45:41 -0300 Subject: [PATCH 1/4] update to replace heimweh/go-pagerduty by imjaroiswebdev/go-pagerduty@a2actionAssociationWithService --- go.mod | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.mod b/go.mod index 353891d12..acc9924ef 100644 --- a/go.mod +++ b/go.mod @@ -16,3 +16,5 @@ require ( google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb // indirect google.golang.org/grpc v1.33.2 // indirect ) + +replace github.com/heimweh/go-pagerduty => github.com/imjaroiswebdev/go-pagerduty v0.0.0-20230105193220-4341f1ea9ed9 From 7d8d1759b49cbc8edf8f9654000ec5aa83e17af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Thu, 5 Jan 2023 17:13:40 -0300 Subject: [PATCH 2/4] add support for resource `pagerduty_automation_actions_action_service_association` --- ...actions_action_service_association_test.go | 33 +++++ pagerduty/provider.go | 71 +++++----- ...tion_actions_action_service_association.go | 127 +++++++++++++++++ ...actions_action_service_association_test.go | 134 ++++++++++++++++++ ...s_action_service_association.html.markdown | 78 ++++++++++ 5 files changed, 408 insertions(+), 35 deletions(-) create mode 100644 pagerduty/import_pagerduty_automation_actions_action_service_association_test.go create mode 100644 pagerduty/resource_pagerduty_automation_actions_action_service_association.go create mode 100644 pagerduty/resource_pagerduty_automation_actions_action_service_association_test.go create mode 100644 website/docs/r/automation_actions_action_service_association.html.markdown diff --git a/pagerduty/import_pagerduty_automation_actions_action_service_association_test.go b/pagerduty/import_pagerduty_automation_actions_action_service_association_test.go new file mode 100644 index 000000000..7d3a03c76 --- /dev/null +++ b/pagerduty/import_pagerduty_automation_actions_action_service_association_test.go @@ -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, + }, + }, + }) +} diff --git a/pagerduty/provider.go b/pagerduty/provider.go index 32fc1ec14..5818226c1 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -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(), }, } diff --git a/pagerduty/resource_pagerduty_automation_actions_action_service_association.go b/pagerduty/resource_pagerduty_automation_actions_action_service_association.go new file mode 100644 index 000000000..e670d7f12 --- /dev/null +++ b/pagerduty/resource_pagerduty_automation_actions_action_service_association.go @@ -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.DissociateToService(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 +} diff --git a/pagerduty/resource_pagerduty_automation_actions_action_service_association_test.go b/pagerduty/resource_pagerduty_automation_actions_action_service_association_test.go new file mode 100644 index 000000000..37e1d1e3d --- /dev/null +++ b/pagerduty/resource_pagerduty_automation_actions_action_service_association_test.go @@ -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) +} diff --git a/website/docs/r/automation_actions_action_service_association.html.markdown b/website/docs/r/automation_actions_action_service_association.html.markdown new file mode 100644 index 000000000..56c29ce51 --- /dev/null +++ b/website/docs/r/automation_actions_action_service_association.html.markdown @@ -0,0 +1,78 @@ +--- +layout: "pagerduty" +page_title: "PagerDuty: pagerduty_automation_actions_action_service_association" +sidebar_current: "docs-pagerduty-resource-automation-actions-action" +description: |- + Creates and manages an Automation Actions action assciation with a Service in PagerDuty. +--- + +# pagerduty\_automation\_actions\_action_service_association + +An Automation Actions [action association with a service](https://developer.pagerduty.com/api-reference/5d2f051f3fb43-associate-an-automation-action-with-a-service) configures the relation of a specific Action with a Service. + +## Example Usage + +```hcl +resource "pagerduty_user" "example" { + name = "Earline Greenholt" + email = "125.greenholt.earline@graham.name" +} + +resource "pagerduty_escalation_policy" "foo" { + name = "Engineering Escalation Policy" + num_loops = 2 + + rule { + escalation_delay_in_minutes = 10 + + target { + type = "user_reference" + id = pagerduty_user.example.id + } + } +} + +resource "pagerduty_service" "example" { + name = "My Web App" + auto_resolve_timeout = 14400 + acknowledgement_timeout = 600 + escalation_policy = pagerduty_escalation_policy.foo.id + alert_creation = "create_alerts_and_incidents" + + auto_pause_notifications_parameters { + enabled = true + timeout = 300 + } +} + +resource "pagerduty_automation_actions_action" "pa_action_example" { + name = "PA Action created via TF" + description = "Description of the PA Action created via TF" + action_type = "process_automation" + action_data_reference { + process_automation_job_id = "P123456" + } +} + +resource "pagerduty_automation_actions_action_service_association" "foo" { + action_id = pagerduty_automation_actions_action.pa_action_example.id + service_id = pagerduty_service.example.id +} + +``` + +## Argument Reference + +The following arguments are supported: + + * `action_id` - (Required) Id of the action. + * `service_id` - (Required) Id of the service associated to the action. + +## Import + +Actions can be imported using the `id`, e.g. +Action service association can be imported using the `action_id` and `service_id` separated by a colon, e.g. + +``` +$ terraform import pagerduty_automation_actions_action_service_association.example 01DER7CUUBF7TH4116K0M4WKPU:PLB09Z +``` From 3c052c15a1e38f364587110699c49353d53671cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Fri, 6 Jan 2023 13:43:31 -0300 Subject: [PATCH 3/4] fix typos --- ...automation_actions_action_service_association.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/website/docs/r/automation_actions_action_service_association.html.markdown b/website/docs/r/automation_actions_action_service_association.html.markdown index 56c29ce51..014228dcd 100644 --- a/website/docs/r/automation_actions_action_service_association.html.markdown +++ b/website/docs/r/automation_actions_action_service_association.html.markdown @@ -3,7 +3,7 @@ layout: "pagerduty" page_title: "PagerDuty: pagerduty_automation_actions_action_service_association" sidebar_current: "docs-pagerduty-resource-automation-actions-action" description: |- - Creates and manages an Automation Actions action assciation with a Service in PagerDuty. + Creates and manages an Automation Actions action association with a Service in PagerDuty. --- # pagerduty\_automation\_actions\_action_service_association @@ -70,7 +70,6 @@ The following arguments are supported: ## Import -Actions can be imported using the `id`, e.g. Action service association can be imported using the `action_id` and `service_id` separated by a colon, e.g. ``` From f7ad1ee507876aec3557219bf3e5446f40a63e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Fri, 6 Jan 2023 13:47:30 -0300 Subject: [PATCH 4/4] update to use heimweh/go-pagerduty@1866a66 --- go.mod | 2 -- ...e_pagerduty_automation_actions_action_service_association.go | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/go.mod b/go.mod index acc9924ef..353891d12 100644 --- a/go.mod +++ b/go.mod @@ -16,5 +16,3 @@ require ( google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb // indirect google.golang.org/grpc v1.33.2 // indirect ) - -replace github.com/heimweh/go-pagerduty => github.com/imjaroiswebdev/go-pagerduty v0.0.0-20230105193220-4341f1ea9ed9 diff --git a/pagerduty/resource_pagerduty_automation_actions_action_service_association.go b/pagerduty/resource_pagerduty_automation_actions_action_service_association.go index e670d7f12..db2e00326 100644 --- a/pagerduty/resource_pagerduty_automation_actions_action_service_association.go +++ b/pagerduty/resource_pagerduty_automation_actions_action_service_association.go @@ -110,7 +110,7 @@ func resourcePagerDutyAutomationActionsActionServiceAssociationDelete(d *schema. log.Printf("[INFO] Deleting PagerDuty AutomationActionsActionServiceAssociation %s", d.Id()) retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { - if _, err := client.AutomationActionsAction.DissociateToService(actionID, serviceID); err != nil { + if _, err := client.AutomationActionsAction.DissociateFromService(actionID, serviceID); err != nil { return resource.RetryableError(err) } return nil