From b896298a9c0b9c749d38398d73edac158a5c6ae5 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 22 Nov 2022 14:09:32 -0500 Subject: [PATCH 01/21] wip on resource --- ...rce_pagerduty_automation_actions_runner.go | 11 +- pagerduty/provider.go | 3 +- ...rce_pagerduty_automation_actions_runner.go | 136 ++++++++++++++++++ ...agerduty_automation_actions_runner_test.go | 112 +++++++++++++++ 4 files changed, 258 insertions(+), 4 deletions(-) create mode 100644 pagerduty/resource_pagerduty_automation_actions_runner.go create mode 100644 pagerduty/resource_pagerduty_automation_actions_runner_test.go diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner.go b/pagerduty/data_source_pagerduty_automation_actions_runner.go index 02bb6dd4c..720f8fab7 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func dataSourcePagerdutyAutomationActionsRunner() *schema.Resource { +func dataSourcePagerDutyAutomationActionsRunner() *schema.Resource { return &schema.Resource{ Read: dataSourcePagerDutyAutomationActionsRunnerRead, @@ -21,7 +21,11 @@ func dataSourcePagerdutyAutomationActionsRunner() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "type": { + "runner_type": { + Type: schema.TypeString, + Computed: true, + }, + "description": { Type: schema.TypeString, Computed: true, }, @@ -48,7 +52,8 @@ func dataSourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta d.SetId(runner.ID) d.Set("name", runner.Name) - d.Set("type", runner.Type) + d.Set("runner_type", runner.RunnerType) + d.Set("description", runner.Description) return nil }) diff --git a/pagerduty/provider.go b/pagerduty/provider.go index a5b6419d5..5eddcbc3a 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -61,7 +61,7 @@ func Provider() *schema.Provider { "pagerduty_ruleset": dataSourcePagerDutyRuleset(), "pagerduty_tag": dataSourcePagerDutyTag(), "pagerduty_event_orchestration": dataSourcePagerDutyEventOrchestration(), - "pagerduty_automation_actions_runner": dataSourcePagerdutyAutomationActionsRunner(), + "pagerduty_automation_actions_runner": dataSourcePagerDutyAutomationActionsRunner(), }, ResourcesMap: map[string]*schema.Resource{ @@ -94,6 +94,7 @@ func Provider() *schema.Provider { "pagerduty_event_orchestration_router": resourcePagerDutyEventOrchestrationPathRouter(), "pagerduty_event_orchestration_unrouted": resourcePagerDutyEventOrchestrationPathUnrouted(), "pagerduty_event_orchestration_service": resourcePagerDutyEventOrchestrationPathService(), + "pagerduty_automation_actions_runner": resourcePagerDutyAutomationActionsRunner(), }, } diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go new file mode 100644 index 000000000..9ac782809 --- /dev/null +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -0,0 +1,136 @@ +package pagerduty + +import ( + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/heimweh/go-pagerduty/pagerduty" +) + +func resourcePagerDutyAutomationActionsRunner() *schema.Resource { + return &schema.Resource{ + Create: resourcePagerDutyAutomationActionsRunnerCreate, + Read: resourcePagerDutyAutomationActionsRunnerRead, + Delete: resourcePagerDutyAutomationActionsRunnerDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, // Requires creation of new resource while support for update is not implemented + }, + "runner_type": { + Type: schema.TypeString, + Required: true, + Default: "sidecar", + ValidateFunc: validateValueFunc([]string{ + "sidecar", + }), + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + Default: "Managed by Terraform", + }, + }, + } +} + +func buildAutomationActionsRunnerStruct(d *schema.ResourceData) *pagerduty.AutomationActionsRunner { + automationActionsRunner := &pagerduty.AutomationActionsRunner{ + ID: d.Get("id").(string), + Name: d.Get("name").(string), + RunnerType: d.Get("runner_type").(string), + Description: d.Get("description").(string), + } + + return automationActionsRunner +} + +func resourcePagerDutyAutomationActionsRunnerCreate(d *schema.ResourceData, meta interface{}) error { + client, err := meta.(*Config).Client() + if err != nil { + return err + } + + automationActionsRunner := buildAutomationActionsRunnerStruct(d) + + log.Printf("[INFO] Creating PagerDuty AutomationActionsRunner %s", automationActionsRunner.Name) + + retryErr := resource.Retry(10*time.Second, func() *resource.RetryError { + if automationActionsRunner, _, err := client.AutomationActionsRunner.Create(automationActionsRunner); err != nil { + if isErrCode(err, 400) || isErrCode(err, 429) { + return resource.RetryableError(err) + } + + return resource.NonRetryableError(err) + } else if automationActionsRunner != nil { + d.SetId(automationActionsRunner.ID) + } + return nil + }) + + if retryErr != nil { + return retryErr + } + + return resourcePagerDutyAutomationActionsRunnerRead(d, meta) + +} + +func resourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta interface{}) error { + client, err := meta.(*Config).Client() + if err != nil { + return err + } + + log.Printf("[INFO] Reading PagerDuty AutomationActionsRunner %s", d.Id()) + + return resource.Retry(30*time.Second, func() *resource.RetryError { + if automationActionsRunner, _, err := client.AutomationActionsRunner.Get(d.Id()); err != nil { + time.Sleep(2 * time.Second) + return resource.RetryableError(err) + } else if automationActionsRunner != nil { + log.Printf("AutomationActionsRunner Name: %v", automationActionsRunner.Name) + log.Printf("AutomationActionsRunner RunnerType: %v", automationActionsRunner.RunnerType) + d.Set("id", automationActionsRunner.ID) + d.Set("name", automationActionsRunner.Name) + d.Set("runner_type", automationActionsRunner.RunnerType) + d.Set("description", automationActionsRunner.Description) + } + return nil + }) +} + +func resourcePagerDutyAutomationActionsRunnerDelete(d *schema.ResourceData, meta interface{}) error { + client, err := meta.(*Config).Client() + if err != nil { + return err + } + + log.Printf("[INFO] Deleting PagerDuty AutomationActionsRunner %s", d.Id()) + + retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { + if _, err := client.AutomationActionsRunner.Delete(d.Id()); 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_runner_test.go b/pagerduty/resource_pagerduty_automation_actions_runner_test.go new file mode 100644 index 000000000..23e396a46 --- /dev/null +++ b/pagerduty/resource_pagerduty_automation_actions_runner_test.go @@ -0,0 +1,112 @@ +package pagerduty + +import ( + "fmt" + "log" + "strings" + "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" + "github.com/heimweh/go-pagerduty/pagerduty" +) + +func init() { + resource.AddTestSweepers("pagerduty_tag", &resource.Sweeper{ + Name: "pagerduty_tag", + F: testSweepTag, + }) +} + +func testSweepTag(region string) error { + config, err := sharedConfigForRegion(region) + if err != nil { + return err + } + + client, err := config.Client() + if err != nil { + return err + } + + resp, _, err := client.Tags.List(&pagerduty.ListTagsOptions{}) + if err != nil { + return err + } + + for _, tag := range resp.Tags { + if strings.HasPrefix(tag.Label, "test") || strings.HasPrefix(tag.Label, "tf-") { + log.Printf("Destroying tag %s (%s)", tag.Label, tag.ID) + if _, err := client.Tags.Delete(tag.ID); err != nil { + return err + } + } + } + + return nil +} + +func TestAccPagerDutyTag_Basic(t *testing.T) { + tagLabel := fmt.Sprintf("tf-%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPagerDutyTagDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckPagerDutyTagConfig(tagLabel), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyTagExists("pagerduty_tag.foo"), + resource.TestCheckResourceAttr( + "pagerduty_tag.foo", "label", tagLabel), + ), + }, + }, + }) +} + +func testAccCheckPagerDutyTagDestroy(s *terraform.State) error { + client, _ := testAccProvider.Meta().(*Config).Client() + for _, r := range s.RootModule().Resources { + if r.Type != "pagerduty_tag" { + continue + } + if _, _, err := client.Tags.Get(r.Primary.ID); err == nil { + return fmt.Errorf("Tag still exists") + } + } + return nil +} + +func testAccCheckPagerDutyTagExists(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 Tag ID is set") + } + + client, _ := testAccProvider.Meta().(*Config).Client() + found, _, err := client.Tags.Get(rs.Primary.ID) + if err != nil { + return err + } + if found.ID != rs.Primary.ID { + return fmt.Errorf("Tag not found: %v - %v", rs.Primary.ID, found) + } + + return nil + } +} + +func testAccCheckPagerDutyTagConfig(tagLabel string) string { + return fmt.Sprintf(` +resource "pagerduty_tag" "foo" { + label = "%s" +} +`, tagLabel) +} From 7e42504eaf7e98f7ebbe77a4e34cac375d5e6ffb Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 22 Nov 2022 16:41:10 -0500 Subject: [PATCH 02/21] working data tests --- ...e_pagerduty_automation_actions_runner_test.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index 9b7ba9fb9..7cb80cd92 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -4,17 +4,20 @@ 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 TestAccDataSourcePagerDutyAutomationActionsRunner_Basic(t *testing.T) { + name := fmt.Sprintf("tf-%s", acctest.RandString(5)) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig("01DCTHG8L7X4BDEQG3OQO2HZCN"), + Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(name), Check: resource.ComposeTestCheckFunc( testAccCheckPagerdutyAutomationActionsRunnerExists("data.pagerduty_automation_actions_runner.foo"), ), @@ -39,10 +42,15 @@ func testAccCheckPagerdutyAutomationActionsRunnerExists(n string) resource.TestC } } -func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(id string) string { +func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(name string) string { return fmt.Sprintf(` +resource "pagerduty_automation_actions_runner" "test" { + name = "%s" + runner_type = "sidecar" +} + data "pagerduty_automation_actions_runner" "foo" { - id = "%s" + id = pagerduty_automation_actions_runner.test.id } -`, id) +`, name) } From d07da4ae6f4c239d3d7ed929cf1b99bddef347f0 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 22 Nov 2022 16:42:56 -0500 Subject: [PATCH 03/21] resource tests passing --- ...rce_pagerduty_automation_actions_runner.go | 3 +- ...agerduty_automation_actions_runner_test.go | 73 ++++++------------- 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go index 9ac782809..73f5005ff 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -26,10 +26,10 @@ func resourcePagerDutyAutomationActionsRunner() *schema.Resource { "runner_type": { Type: schema.TypeString, Required: true, - Default: "sidecar", ValidateFunc: validateValueFunc([]string{ "sidecar", }), + ForceNew: true, // Requires creation of new resource while support for update is not implemented }, "id": { Type: schema.TypeString, @@ -39,6 +39,7 @@ func resourcePagerDutyAutomationActionsRunner() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "Managed by Terraform", + ForceNew: true, // Requires creation of new resource while support for update is not implemented }, }, } diff --git a/pagerduty/resource_pagerduty_automation_actions_runner_test.go b/pagerduty/resource_pagerduty_automation_actions_runner_test.go index 23e396a46..9f8c5a49e 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner_test.go @@ -2,111 +2,86 @@ package pagerduty import ( "fmt" - "log" - "strings" "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" - "github.com/heimweh/go-pagerduty/pagerduty" ) func init() { - resource.AddTestSweepers("pagerduty_tag", &resource.Sweeper{ - Name: "pagerduty_tag", - F: testSweepTag, + resource.AddTestSweepers("automation_actions_runner", &resource.Sweeper{ + Name: "automation_actions_runner", + F: testSweepAutomationActionsRunner, }) } -func testSweepTag(region string) error { - config, err := sharedConfigForRegion(region) - if err != nil { - return err - } - - client, err := config.Client() - if err != nil { - return err - } - - resp, _, err := client.Tags.List(&pagerduty.ListTagsOptions{}) - if err != nil { - return err - } - - for _, tag := range resp.Tags { - if strings.HasPrefix(tag.Label, "test") || strings.HasPrefix(tag.Label, "tf-") { - log.Printf("Destroying tag %s (%s)", tag.Label, tag.ID) - if _, err := client.Tags.Delete(tag.ID); err != nil { - return err - } - } - } - +func testSweepAutomationActionsRunner(region string) error { + // TODO-AVG: Create a Jira ticket to implement sweepers return nil } -func TestAccPagerDutyTag_Basic(t *testing.T) { - tagLabel := fmt.Sprintf("tf-%s", acctest.RandString(5)) +func TestAccPagerDutyAutomationActionsRunner_Basic(t *testing.T) { + runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccCheckPagerDutyTagDestroy, + CheckDestroy: testAccCheckPagerDutyAutomationActionsRunnerDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckPagerDutyTagConfig(tagLabel), + Config: testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName), Check: resource.ComposeTestCheckFunc( - testAccCheckPagerDutyTagExists("pagerduty_tag.foo"), + testAccCheckPagerDutyAutomationActionsRunnerExists("pagerduty_automation_actions_runner.foo"), resource.TestCheckResourceAttr( - "pagerduty_tag.foo", "label", tagLabel), + "pagerduty_automation_actions_runner.foo", "name", runnerName), ), }, }, }) } -func testAccCheckPagerDutyTagDestroy(s *terraform.State) error { +func testAccCheckPagerDutyAutomationActionsRunnerDestroy(s *terraform.State) error { client, _ := testAccProvider.Meta().(*Config).Client() for _, r := range s.RootModule().Resources { - if r.Type != "pagerduty_tag" { + if r.Type != "pagerduty_automation_actions_runner" { continue } - if _, _, err := client.Tags.Get(r.Primary.ID); err == nil { - return fmt.Errorf("Tag still exists") + if _, _, err := client.AutomationActionsRunner.Get(r.Primary.ID); err == nil { + return fmt.Errorf("Automation Actions Runner still exists") } } return nil } -func testAccCheckPagerDutyTagExists(n string) resource.TestCheckFunc { +func testAccCheckPagerDutyAutomationActionsRunnerExists(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 Tag ID is set") + return fmt.Errorf("No Automation Actions Runner ID is set") } client, _ := testAccProvider.Meta().(*Config).Client() - found, _, err := client.Tags.Get(rs.Primary.ID) + found, _, err := client.AutomationActionsRunner.Get(rs.Primary.ID) if err != nil { return err } if found.ID != rs.Primary.ID { - return fmt.Errorf("Tag not found: %v - %v", rs.Primary.ID, found) + return fmt.Errorf("Automation Actions Runner not found: %v - %v", rs.Primary.ID, found) } return nil } } -func testAccCheckPagerDutyTagConfig(tagLabel string) string { +func testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName string) string { return fmt.Sprintf(` -resource "pagerduty_tag" "foo" { - label = "%s" +resource "pagerduty_automation_actions_runner" "foo" { + name = "%s" + runner_type = "sidecar" } -`, tagLabel) +`, runnerName) } From fa6b827fc6ab56f91ee404845b4ce75875aa9781 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 22 Nov 2022 16:55:47 -0500 Subject: [PATCH 04/21] todo fix --- pagerduty/resource_pagerduty_automation_actions_runner_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pagerduty/resource_pagerduty_automation_actions_runner_test.go b/pagerduty/resource_pagerduty_automation_actions_runner_test.go index 9f8c5a49e..0bc26744f 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner_test.go @@ -17,7 +17,7 @@ func init() { } func testSweepAutomationActionsRunner(region string) error { - // TODO-AVG: Create a Jira ticket to implement sweepers + // TODO: Implement sweepers return nil } From 2346ddce06f9c4385da4f253eb49ceee76c917a4 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Mon, 5 Dec 2022 15:32:12 -0500 Subject: [PATCH 05/21] removed the A2A runner resource --- ...agerduty_automation_actions_runner_test.go | 19 +-- pagerduty/provider.go | 1 - ...rce_pagerduty_automation_actions_runner.go | 137 ------------------ ...agerduty_automation_actions_runner_test.go | 87 ----------- 4 files changed, 5 insertions(+), 239 deletions(-) delete mode 100644 pagerduty/resource_pagerduty_automation_actions_runner.go delete mode 100644 pagerduty/resource_pagerduty_automation_actions_runner_test.go diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index 7cb80cd92..480c3119b 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -4,20 +4,19 @@ 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 TestAccDataSourcePagerDutyAutomationActionsRunner_Basic(t *testing.T) { - name := fmt.Sprintf("tf-%s", acctest.RandString(5)) + // name := fmt.Sprintf("tf-%s", acctest.RandString(5)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(name), + Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(), Check: resource.ComposeTestCheckFunc( testAccCheckPagerdutyAutomationActionsRunnerExists("data.pagerduty_automation_actions_runner.foo"), ), @@ -29,6 +28,7 @@ func TestAccDataSourcePagerDutyAutomationActionsRunner_Basic(t *testing.T) { func testAccCheckPagerdutyAutomationActionsRunnerExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { // client, _ := testAccProvider.Meta().(*Config).Client() + // client.AutomationActionsRunner.Create() rs, ok := s.RootModule().Resources[n] if !ok { @@ -42,15 +42,6 @@ func testAccCheckPagerdutyAutomationActionsRunnerExists(n string) resource.TestC } } -func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(name string) string { - return fmt.Sprintf(` -resource "pagerduty_automation_actions_runner" "test" { - name = "%s" - runner_type = "sidecar" -} - -data "pagerduty_automation_actions_runner" "foo" { - id = pagerduty_automation_actions_runner.test.id -} -`, name) +func testAccDataSourcePagerDutyAutomationActionsRunnerConfig() string { + return fmt.Sprintf(``) } diff --git a/pagerduty/provider.go b/pagerduty/provider.go index 5eddcbc3a..d07e09f8c 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -94,7 +94,6 @@ func Provider() *schema.Provider { "pagerduty_event_orchestration_router": resourcePagerDutyEventOrchestrationPathRouter(), "pagerduty_event_orchestration_unrouted": resourcePagerDutyEventOrchestrationPathUnrouted(), "pagerduty_event_orchestration_service": resourcePagerDutyEventOrchestrationPathService(), - "pagerduty_automation_actions_runner": resourcePagerDutyAutomationActionsRunner(), }, } diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go deleted file mode 100644 index 73f5005ff..000000000 --- a/pagerduty/resource_pagerduty_automation_actions_runner.go +++ /dev/null @@ -1,137 +0,0 @@ -package pagerduty - -import ( - "log" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/heimweh/go-pagerduty/pagerduty" -) - -func resourcePagerDutyAutomationActionsRunner() *schema.Resource { - return &schema.Resource{ - Create: resourcePagerDutyAutomationActionsRunnerCreate, - Read: resourcePagerDutyAutomationActionsRunnerRead, - Delete: resourcePagerDutyAutomationActionsRunnerDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, // Requires creation of new resource while support for update is not implemented - }, - "runner_type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateValueFunc([]string{ - "sidecar", - }), - ForceNew: true, // Requires creation of new resource while support for update is not implemented - }, - "id": { - Type: schema.TypeString, - Computed: true, - }, - "description": { - Type: schema.TypeString, - Optional: true, - Default: "Managed by Terraform", - ForceNew: true, // Requires creation of new resource while support for update is not implemented - }, - }, - } -} - -func buildAutomationActionsRunnerStruct(d *schema.ResourceData) *pagerduty.AutomationActionsRunner { - automationActionsRunner := &pagerduty.AutomationActionsRunner{ - ID: d.Get("id").(string), - Name: d.Get("name").(string), - RunnerType: d.Get("runner_type").(string), - Description: d.Get("description").(string), - } - - return automationActionsRunner -} - -func resourcePagerDutyAutomationActionsRunnerCreate(d *schema.ResourceData, meta interface{}) error { - client, err := meta.(*Config).Client() - if err != nil { - return err - } - - automationActionsRunner := buildAutomationActionsRunnerStruct(d) - - log.Printf("[INFO] Creating PagerDuty AutomationActionsRunner %s", automationActionsRunner.Name) - - retryErr := resource.Retry(10*time.Second, func() *resource.RetryError { - if automationActionsRunner, _, err := client.AutomationActionsRunner.Create(automationActionsRunner); err != nil { - if isErrCode(err, 400) || isErrCode(err, 429) { - return resource.RetryableError(err) - } - - return resource.NonRetryableError(err) - } else if automationActionsRunner != nil { - d.SetId(automationActionsRunner.ID) - } - return nil - }) - - if retryErr != nil { - return retryErr - } - - return resourcePagerDutyAutomationActionsRunnerRead(d, meta) - -} - -func resourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta interface{}) error { - client, err := meta.(*Config).Client() - if err != nil { - return err - } - - log.Printf("[INFO] Reading PagerDuty AutomationActionsRunner %s", d.Id()) - - return resource.Retry(30*time.Second, func() *resource.RetryError { - if automationActionsRunner, _, err := client.AutomationActionsRunner.Get(d.Id()); err != nil { - time.Sleep(2 * time.Second) - return resource.RetryableError(err) - } else if automationActionsRunner != nil { - log.Printf("AutomationActionsRunner Name: %v", automationActionsRunner.Name) - log.Printf("AutomationActionsRunner RunnerType: %v", automationActionsRunner.RunnerType) - d.Set("id", automationActionsRunner.ID) - d.Set("name", automationActionsRunner.Name) - d.Set("runner_type", automationActionsRunner.RunnerType) - d.Set("description", automationActionsRunner.Description) - } - return nil - }) -} - -func resourcePagerDutyAutomationActionsRunnerDelete(d *schema.ResourceData, meta interface{}) error { - client, err := meta.(*Config).Client() - if err != nil { - return err - } - - log.Printf("[INFO] Deleting PagerDuty AutomationActionsRunner %s", d.Id()) - - retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { - if _, err := client.AutomationActionsRunner.Delete(d.Id()); 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_runner_test.go b/pagerduty/resource_pagerduty_automation_actions_runner_test.go deleted file mode 100644 index 0bc26744f..000000000 --- a/pagerduty/resource_pagerduty_automation_actions_runner_test.go +++ /dev/null @@ -1,87 +0,0 @@ -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("automation_actions_runner", &resource.Sweeper{ - Name: "automation_actions_runner", - F: testSweepAutomationActionsRunner, - }) -} - -func testSweepAutomationActionsRunner(region string) error { - // TODO: Implement sweepers - return nil -} - -func TestAccPagerDutyAutomationActionsRunner_Basic(t *testing.T) { - runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckPagerDutyAutomationActionsRunnerDestroy, - Steps: []resource.TestStep{ - { - Config: testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName), - Check: resource.ComposeTestCheckFunc( - testAccCheckPagerDutyAutomationActionsRunnerExists("pagerduty_automation_actions_runner.foo"), - resource.TestCheckResourceAttr( - "pagerduty_automation_actions_runner.foo", "name", runnerName), - ), - }, - }, - }) -} - -func testAccCheckPagerDutyAutomationActionsRunnerDestroy(s *terraform.State) error { - client, _ := testAccProvider.Meta().(*Config).Client() - for _, r := range s.RootModule().Resources { - if r.Type != "pagerduty_automation_actions_runner" { - continue - } - if _, _, err := client.AutomationActionsRunner.Get(r.Primary.ID); err == nil { - return fmt.Errorf("Automation Actions Runner still exists") - } - } - return nil -} - -func testAccCheckPagerDutyAutomationActionsRunnerExists(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() - found, _, err := client.AutomationActionsRunner.Get(rs.Primary.ID) - if err != nil { - return err - } - if found.ID != rs.Primary.ID { - return fmt.Errorf("Automation Actions Runner not found: %v - %v", rs.Primary.ID, found) - } - - return nil - } -} - -func testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName string) string { - return fmt.Sprintf(` -resource "pagerduty_automation_actions_runner" "foo" { - name = "%s" - runner_type = "sidecar" -} -`, runnerName) -} From 2d3e0c47d0cb0e1410cb2cbe789cdbcf0961619f Mon Sep 17 00:00:00 2001 From: mrdubr Date: Mon, 5 Dec 2022 20:12:35 -0500 Subject: [PATCH 06/21] Data source props --- ...rce_pagerduty_automation_actions_runner.go | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner.go b/pagerduty/data_source_pagerduty_automation_actions_runner.go index 720f8fab7..a704c7af7 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner.go @@ -1,6 +1,7 @@ package pagerduty import ( + "fmt" "log" "time" @@ -21,13 +22,39 @@ func dataSourcePagerDutyAutomationActionsRunner() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "type": { + Type: schema.TypeString, + Computed: true, + }, "runner_type": { Type: schema.TypeString, Computed: true, }, + "creation_time": { + Type: schema.TypeString, + Computed: true, + }, + "last_seen": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, "description": { Type: schema.TypeString, Computed: true, + Optional: true, + }, + "runbook_base_uri": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, + "teams": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, }, }, } @@ -52,9 +79,25 @@ func dataSourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta d.SetId(runner.ID) d.Set("name", runner.Name) + d.Set("type", runner.Type) d.Set("runner_type", runner.RunnerType) + d.Set("creation_time", runner.CreationTime) d.Set("description", runner.Description) + if runner.RunbookBaseUri != nil { + d.Set("runbook_base_uri", &runner.RunbookBaseUri) + } + + if runner.LastSeenTime != nil { + d.Set("last_seen", &runner.LastSeenTime) + } + + if runner.Teams != nil { + if err := d.Set("teams", flattenShedTeams(runner.Teams)); err != nil { + return resource.NonRetryableError(fmt.Errorf("error setting teams: %s", err)) + } + } + return nil }) } From 44f3ca118e08d0e406ce69593217a0acbc3eff79 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 6 Dec 2022 12:09:41 -0500 Subject: [PATCH 07/21] docs --- .../d/automation_actions_runner.html.markdown | 41 +++++++++++++++++++ website/docs/d/business_service.html.markdown | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 website/docs/d/automation_actions_runner.html.markdown diff --git a/website/docs/d/automation_actions_runner.html.markdown b/website/docs/d/automation_actions_runner.html.markdown new file mode 100644 index 000000000..3f23a9d90 --- /dev/null +++ b/website/docs/d/automation_actions_runner.html.markdown @@ -0,0 +1,41 @@ +--- +layout: "pagerduty" +page_title: "PagerDuty: pagerduty_automation_actions_runner" +sidebar_current: "docs-pagerduty-datasource-automation-actions-runner" +description: |- + Get information about an Automation Actions runner that you have created. +--- + +# pagerduty\_automation\_actions\_runner + +Use this data source to get information about a specific [automation actions runner][1]. + +## Example Usage + +```hcl +data "pagerduty_automation_actions_runner" "example" { + id = "01DBJLIGED17S1DQKQC2AV8XYZ" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `id` - (Required) The id of the automation actions runner in the PagerDuty API. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the found runner. +* `name` - The name of the found runner. +* `type` - The type of object. The value returned will be `runner`. +* `runner_type` - The type of runner. Allowed values are `sidecar` and `runbook`. +* `creation_time` - The time runner was created. Represented as an ISO 8601 timestamp. +* `description` - (Optional) The description of the runner. +* `last_seen` - (Optional) The last time runner has been seen. Represented as an ISO 8601 timestamp. +* `runbook_base_uri` - (Optional) The base URI of the Runbook server to connect to. Applicable to `runbook` type runners only. +* `teams` - (Optional) Teams associated with the runner. Account must have the `teams` ability enabled. + +[1]: https://developer.pagerduty.com/api-reference/aace61f84cbd0-get-an-automation-action-runner diff --git a/website/docs/d/business_service.html.markdown b/website/docs/d/business_service.html.markdown index 975a234f8..cffc8ee64 100644 --- a/website/docs/d/business_service.html.markdown +++ b/website/docs/d/business_service.html.markdown @@ -29,4 +29,4 @@ The following arguments are supported: * `name` - The short name of the found business service. * `type` - The type of object. The value returned will be `business_service`. Can be used for passing to a service dependency. -* [1]: https://api-reference.pagerduty.com/#!/Business_Services/get_business_services +[1]: https://api-reference.pagerduty.com/#!/Business_Services/get_business_services From 0eeb60b6c3ed195f355d302170c74f922dbca45e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Tue, 6 Dec 2022 17:01:55 -0300 Subject: [PATCH 08/21] wip implementing test acc for data source --- ...agerduty_automation_actions_runner_test.go | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index 480c3119b..868a77007 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -4,31 +4,45 @@ 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" + "github.com/heimweh/go-pagerduty/pagerduty" ) func TestAccDataSourcePagerDutyAutomationActionsRunner_Basic(t *testing.T) { - // name := fmt.Sprintf("tf-%s", acctest.RandString(5)) + runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) + username := fmt.Sprintf("tf-%s", acctest.RandString(5)) + email := fmt.Sprintf("%s@foo.test", username) + escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) + service := fmt.Sprintf("tf-%s", acctest.RandString(5)) + runnerId := "default" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(), + Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, service), + // Config: ` + // provider "pagerduty" {} + // `, Check: resource.ComposeTestCheckFunc( - testAccCheckPagerdutyAutomationActionsRunnerExists("data.pagerduty_automation_actions_runner.foo"), + testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation(runnerName, &runnerId), + ), + }, + { + Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName, runnerId), + Check: resource.ComposeTestCheckFunc( + testAccDataSourcePagerdutyAutomationActionsRunner("data.pagerduty_automation_actions_runner.foo"), ), }, }, }) } -func testAccCheckPagerdutyAutomationActionsRunnerExists(n string) resource.TestCheckFunc { +func testAccDataSourcePagerdutyAutomationActionsRunner(n string) resource.TestCheckFunc { return func(s *terraform.State) error { - // client, _ := testAccProvider.Meta().(*Config).Client() - // client.AutomationActionsRunner.Create() rs, ok := s.RootModule().Resources[n] if !ok { @@ -42,6 +56,31 @@ func testAccCheckPagerdutyAutomationActionsRunnerExists(n string) resource.TestC } } -func testAccDataSourcePagerDutyAutomationActionsRunnerConfig() string { - return fmt.Sprintf(``) +func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName, runnerId string) string { + return fmt.Sprintf(` +data "pagerduty_automation_actions_runner" "tf_test_runner" { + id = %q +} +`, runnerId) +} + +func testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation(runnerName string, runnerId *string) resource.TestCheckFunc { + return func(s *terraform.State) error { + + fmt.Println("runnerId in runner creation:", *runnerId) + client, _ := testAccProvider.Meta().(*Config).Client() + input := &pagerduty.AutomationActionsRunner{ + Name: runnerName, + Description: "sidecar runner provisioned for tf acceptance test", + RunnerType: "sidecar", + } + runner, _, err := client.AutomationActionsRunner.Create(input) + if err != nil { + return err + } + *runnerId = runner.ID + fmt.Println("runnerId", runnerId) + fmt.Println("runnerId in runner creation2:", *runnerId) + return nil + } } From 805f1e821a22477b226fef97a27204707e25ab42 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 6 Dec 2022 16:10:35 -0500 Subject: [PATCH 09/21] pointer fun --- ...agerduty_automation_actions_runner_test.go | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index 868a77007..cf7ac5b8e 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -14,8 +14,6 @@ func TestAccDataSourcePagerDutyAutomationActionsRunner_Basic(t *testing.T) { runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) username := fmt.Sprintf("tf-%s", acctest.RandString(5)) email := fmt.Sprintf("%s@foo.test", username) - escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) - service := fmt.Sprintf("tf-%s", acctest.RandString(5)) runnerId := "default" resource.Test(t, resource.TestCase{ @@ -23,16 +21,13 @@ func TestAccDataSourcePagerDutyAutomationActionsRunner_Basic(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, service), - // Config: ` - // provider "pagerduty" {} - // `, + Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig_1(username, email), Check: resource.ComposeTestCheckFunc( - testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation(runnerName, &runnerId), + testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1(runnerName, &runnerId), ), }, { - Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName, runnerId), + Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName, &runnerId), Check: resource.ComposeTestCheckFunc( testAccDataSourcePagerdutyAutomationActionsRunner("data.pagerduty_automation_actions_runner.foo"), ), @@ -56,18 +51,38 @@ func testAccDataSourcePagerdutyAutomationActionsRunner(n string) resource.TestCh } } -func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName, runnerId string) string { +func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName string, runnerId *string) string { + + fmt.Println("testAccDataSourcePagerDutyAutomationActionsRunnerConfig runnerId:", *runnerId) + return fmt.Sprintf(` data "pagerduty_automation_actions_runner" "tf_test_runner" { id = %q } -`, runnerId) +`, *runnerId) } -func testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation(runnerName string, runnerId *string) resource.TestCheckFunc { +func testAccDataSourcePagerDutyAutomationActionsRunnerConfig_1(username, email string) string { + + fmt.Println("Called: testAccDataSourcePagerDutyAutomationActionsRunnerConfig_1") + + return fmt.Sprintf(` +resource "pagerduty_user" "foo" { + name = "%s" + email = "%s" + color = "green" + role = "user" + job_title = "foo" + description = "foo" +} +`, username, email) +} + +func testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1(runnerName string, runnerId *string) resource.TestCheckFunc { return func(s *terraform.State) error { - fmt.Println("runnerId in runner creation:", *runnerId) + fmt.Println("testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1 runnerId:", *runnerId) + client, _ := testAccProvider.Meta().(*Config).Client() input := &pagerduty.AutomationActionsRunner{ Name: runnerName, @@ -79,8 +94,9 @@ func testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation(runnerName return err } *runnerId = runner.ID - fmt.Println("runnerId", runnerId) - fmt.Println("runnerId in runner creation2:", *runnerId) + + fmt.Println("POST testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1 runnerId:", *runnerId) + return nil } } From 1783fbe18e883d38249a13b01ba1a5fcb7ca3d2e Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 6 Dec 2022 19:33:02 -0500 Subject: [PATCH 10/21] datasource test --- ...agerduty_automation_actions_runner_test.go | 88 ++++++------------- 1 file changed, 28 insertions(+), 60 deletions(-) diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index cf7ac5b8e..28130a02f 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -7,96 +7,64 @@ import ( "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" - "github.com/heimweh/go-pagerduty/pagerduty" ) func TestAccDataSourcePagerDutyAutomationActionsRunner_Basic(t *testing.T) { - runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) - username := fmt.Sprintf("tf-%s", acctest.RandString(5)) - email := fmt.Sprintf("%s@foo.test", username) - runnerId := "default" + name := fmt.Sprintf("tf-%s", acctest.RandString(5)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig_1(username, email), + Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(name), Check: resource.ComposeTestCheckFunc( - testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1(runnerName, &runnerId), - ), - }, - { - Config: testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName, &runnerId), - Check: resource.ComposeTestCheckFunc( - testAccDataSourcePagerdutyAutomationActionsRunner("data.pagerduty_automation_actions_runner.foo"), + testAccDataSourcePagerdutyAutomationActionsRunner("pagerduty_automation_actions_runner.test", "data.pagerduty_automation_actions_runner.foo"), ), }, }, }) } -func testAccDataSourcePagerdutyAutomationActionsRunner(n string) resource.TestCheckFunc { +func testAccDataSourcePagerdutyAutomationActionsRunner(src, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] + srcR := s.RootModule().Resources[src] + srcA := srcR.Primary.Attributes + + r, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { + a := r.Primary.Attributes + + if a["id"] == "" { return fmt.Errorf("No Runner ID is set") } + testAtts := []string{"id", "name", "type", "runner_type", "creation_time", "last_seen", "description", "runbook_base_uri", "teams"} + + for _, att := range testAtts { + if a[att] != srcA[att] { + return fmt.Errorf("Expected the runner %s to be: %s, but got: %s", att, srcA[att], a[att]) + } + } + return nil } } -func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(runnerName string, runnerId *string) string { - - fmt.Println("testAccDataSourcePagerDutyAutomationActionsRunnerConfig runnerId:", *runnerId) - +func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(name string) string { return fmt.Sprintf(` -data "pagerduty_automation_actions_runner" "tf_test_runner" { - id = %q -} -`, *runnerId) +resource "pagerduty_automation_actions_runner" "test" { + name = "%s" + runner_type = "runbook" + runbook_base_uri = "cat-cat" + runbook_api_key = "secret" } -func testAccDataSourcePagerDutyAutomationActionsRunnerConfig_1(username, email string) string { - - fmt.Println("Called: testAccDataSourcePagerDutyAutomationActionsRunnerConfig_1") - - return fmt.Sprintf(` -resource "pagerduty_user" "foo" { - name = "%s" - email = "%s" - color = "green" - role = "user" - job_title = "foo" - description = "foo" +data "pagerduty_automation_actions_runner" "foo" { + id = pagerduty_automation_actions_runner.test.id } -`, username, email) -} - -func testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1(runnerName string, runnerId *string) resource.TestCheckFunc { - return func(s *terraform.State) error { - - fmt.Println("testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1 runnerId:", *runnerId) - - client, _ := testAccProvider.Meta().(*Config).Client() - input := &pagerduty.AutomationActionsRunner{ - Name: runnerName, - Description: "sidecar runner provisioned for tf acceptance test", - RunnerType: "sidecar", - } - runner, _, err := client.AutomationActionsRunner.Create(input) - if err != nil { - return err - } - *runnerId = runner.ID - - fmt.Println("POST testAccDataSourcePagerdutyAutomationActionsRunnerRunnerCreation_1 runnerId:", *runnerId) - - return nil - } +`, name) } From a1cf374a1a332d940c81425e2b0f24817a86907a Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 6 Dec 2022 19:46:22 -0500 Subject: [PATCH 11/21] working tests --- ...rce_pagerduty_automation_actions_runner.go | 16 +- ...agerduty_automation_actions_runner_test.go | 2 +- pagerduty/provider.go | 1 + ...rce_pagerduty_automation_actions_runner.go | 189 ++++++++++++++++++ ...agerduty_automation_actions_runner_test.go | 88 ++++++++ 5 files changed, 280 insertions(+), 16 deletions(-) create mode 100644 pagerduty/resource_pagerduty_automation_actions_runner.go create mode 100644 pagerduty/resource_pagerduty_automation_actions_runner_test.go diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner.go b/pagerduty/data_source_pagerduty_automation_actions_runner.go index a704c7af7..734aa83cb 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner.go @@ -1,7 +1,6 @@ package pagerduty import ( - "fmt" "log" "time" @@ -49,13 +48,6 @@ func dataSourcePagerDutyAutomationActionsRunner() *schema.Resource { Computed: true, Optional: true, }, - "teams": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, - }, }, } } @@ -81,8 +73,8 @@ func dataSourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta d.Set("name", runner.Name) d.Set("type", runner.Type) d.Set("runner_type", runner.RunnerType) - d.Set("creation_time", runner.CreationTime) d.Set("description", runner.Description) + d.Set("creation_time", runner.CreationTime) if runner.RunbookBaseUri != nil { d.Set("runbook_base_uri", &runner.RunbookBaseUri) @@ -92,12 +84,6 @@ func dataSourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta d.Set("last_seen", &runner.LastSeenTime) } - if runner.Teams != nil { - if err := d.Set("teams", flattenShedTeams(runner.Teams)); err != nil { - return resource.NonRetryableError(fmt.Errorf("error setting teams: %s", err)) - } - } - return nil }) } diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index 28130a02f..7006387e9 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -42,7 +42,7 @@ func testAccDataSourcePagerdutyAutomationActionsRunner(src, n string) resource.T return fmt.Errorf("No Runner ID is set") } - testAtts := []string{"id", "name", "type", "runner_type", "creation_time", "last_seen", "description", "runbook_base_uri", "teams"} + testAtts := []string{"id", "name", "type", "runner_type", "creation_time", "last_seen", "description", "runbook_base_uri"} for _, att := range testAtts { if a[att] != srcA[att] { diff --git a/pagerduty/provider.go b/pagerduty/provider.go index d07e09f8c..5eddcbc3a 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -94,6 +94,7 @@ func Provider() *schema.Provider { "pagerduty_event_orchestration_router": resourcePagerDutyEventOrchestrationPathRouter(), "pagerduty_event_orchestration_unrouted": resourcePagerDutyEventOrchestrationPathUnrouted(), "pagerduty_event_orchestration_service": resourcePagerDutyEventOrchestrationPathService(), + "pagerduty_automation_actions_runner": resourcePagerDutyAutomationActionsRunner(), }, } diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go new file mode 100644 index 000000000..6523044dd --- /dev/null +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -0,0 +1,189 @@ +package pagerduty + +import ( + "errors" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/heimweh/go-pagerduty/pagerduty" +) + +func resourcePagerDutyAutomationActionsRunner() *schema.Resource { + return &schema.Resource{ + Create: resourcePagerDutyAutomationActionsRunnerCreate, + Read: resourcePagerDutyAutomationActionsRunnerRead, + Delete: resourcePagerDutyAutomationActionsRunnerDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, // Requires creation of new resource while support for update is not implemented + }, + "runner_type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateValueFunc([]string{ + "sidecar", + "runbook", + }), + ForceNew: true, // Requires creation of new resource while support for update is not implemented + }, + "description": { + Type: schema.TypeString, + Optional: true, + Default: "Managed by Terraform", + ForceNew: true, // Requires creation of new resource while support for update is not implemented + }, + "runbook_base_uri": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, // Requires creation of new resource while support for update is not implemented + }, + "runbook_api_key": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, // Requires creation of new resource while support for update is not implemented + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + "type": { + Type: schema.TypeString, + Computed: true, + }, + "creation_time": { + Type: schema.TypeString, + Computed: true, + }, + "last_seen": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, + }, + } +} + +func buildAutomationActionsRunnerStruct(d *schema.ResourceData) (*pagerduty.AutomationActionsRunner, error) { + automationActionsRunner := pagerduty.AutomationActionsRunner{ + Name: d.Get("name").(string), + RunnerType: d.Get("runner_type").(string), + Description: d.Get("description").(string), + } + + if automationActionsRunner.RunnerType != "runbook" { + return nil, errors.New("only runners of runner_type runbook can be created") + } + + if attr, ok := d.GetOk("runbook_base_uri"); ok { + val := attr.(string) + automationActionsRunner.RunbookBaseUri = &val + } else { + return nil, errors.New("runbook_base_uri must be specified when creating a runbook runner") + } + + if attr, ok := d.GetOk("runbook_api_key"); ok { + val := attr.(string) + automationActionsRunner.RunbookApiKey = &val + } else { + return nil, errors.New("runbook_api_key must be specified when creating a runbook runner") + } + + return &automationActionsRunner, nil +} + +func resourcePagerDutyAutomationActionsRunnerCreate(d *schema.ResourceData, meta interface{}) error { + client, err := meta.(*Config).Client() + if err != nil { + return err + } + + automationActionsRunner, err := buildAutomationActionsRunnerStruct(d) + if err != nil { + return err + } + + log.Printf("[INFO] Creating PagerDuty AutomationActionsRunner %s", automationActionsRunner.Name) + + retryErr := resource.Retry(10*time.Second, func() *resource.RetryError { + if automationActionsRunner, _, err := client.AutomationActionsRunner.Create(automationActionsRunner); err != nil { + if isErrCode(err, 400) || isErrCode(err, 429) { + return resource.RetryableError(err) + } + + return resource.NonRetryableError(err) + } else if automationActionsRunner != nil { + d.SetId(automationActionsRunner.ID) + } + return nil + }) + + if retryErr != nil { + return retryErr + } + + return resourcePagerDutyAutomationActionsRunnerRead(d, meta) +} + +func resourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta interface{}) error { + client, err := meta.(*Config).Client() + if err != nil { + return err + } + + log.Printf("[INFO] Reading PagerDuty AutomationActionsRunner %s", d.Id()) + + return resource.Retry(30*time.Second, func() *resource.RetryError { + if automationActionsRunner, _, err := client.AutomationActionsRunner.Get(d.Id()); err != nil { + time.Sleep(2 * time.Second) + return resource.RetryableError(err) + } else if automationActionsRunner != nil { + d.Set("id", automationActionsRunner.ID) + d.Set("name", automationActionsRunner.Name) + d.Set("type", automationActionsRunner.Type) + d.Set("runner_type", automationActionsRunner.RunnerType) + d.Set("description", automationActionsRunner.Description) + d.Set("creation_time", automationActionsRunner.CreationTime) + + if automationActionsRunner.RunbookBaseUri != nil { + d.Set("runbook_base_uri", &automationActionsRunner.RunbookBaseUri) + } + + if automationActionsRunner.LastSeenTime != nil { + d.Set("last_seen", &automationActionsRunner.LastSeenTime) + } + } + return nil + }) +} + +func resourcePagerDutyAutomationActionsRunnerDelete(d *schema.ResourceData, meta interface{}) error { + client, err := meta.(*Config).Client() + if err != nil { + return err + } + + log.Printf("[INFO] Deleting PagerDuty AutomationActionsRunner %s", d.Id()) + + retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { + if _, err := client.AutomationActionsRunner.Delete(d.Id()); 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_runner_test.go b/pagerduty/resource_pagerduty_automation_actions_runner_test.go new file mode 100644 index 000000000..c8564476a --- /dev/null +++ b/pagerduty/resource_pagerduty_automation_actions_runner_test.go @@ -0,0 +1,88 @@ +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("automation_actions_runner", &resource.Sweeper{ + Name: "automation_actions_runner", + F: testSweepAutomationActionsRunner, + }) +} + +func testSweepAutomationActionsRunner(region string) error { + return nil +} + +func TestAccPagerDutyAutomationActionsRunner_Basic(t *testing.T) { + runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPagerDutyAutomationActionsRunnerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyAutomationActionsRunnerExists("pagerduty_automation_actions_runner.foo"), + resource.TestCheckResourceAttr( + "pagerduty_automation_actions_runner.foo", "name", runnerName), + ), + }, + }, + }) +} + +func testAccCheckPagerDutyAutomationActionsRunnerDestroy(s *terraform.State) error { + client, _ := testAccProvider.Meta().(*Config).Client() + for _, r := range s.RootModule().Resources { + if r.Type != "pagerduty_automation_actions_runner" { + continue + } + if _, _, err := client.AutomationActionsRunner.Get(r.Primary.ID); err == nil { + return fmt.Errorf("Automation Actions Runner still exists") + } + } + return nil +} + +func testAccCheckPagerDutyAutomationActionsRunnerExists(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() + found, _, err := client.AutomationActionsRunner.Get(rs.Primary.ID) + if err != nil { + return err + } + if found.ID != rs.Primary.ID { + return fmt.Errorf("Automation Actions Runner not found: %v - %v", rs.Primary.ID, found) + } + + return nil + } +} + +func testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName string) string { + return fmt.Sprintf(` +resource "pagerduty_automation_actions_runner" "foo" { + name = "%s" + runner_type = "runbook" + runbook_base_uri = "cat-cat" + runbook_api_key = "cat-secret" +} +`, runnerName) +} From e9e64b327e6a300cc85c7726e19dce3b0c64bdc8 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Tue, 6 Dec 2022 20:20:52 -0500 Subject: [PATCH 12/21] fixed description --- ...agerduty_automation_actions_runner_test.go | 1 + ...rce_pagerduty_automation_actions_runner.go | 27 +++++++++++++++---- ...agerduty_automation_actions_runner_test.go | 10 +++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index 7006387e9..3185e8872 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -58,6 +58,7 @@ func testAccDataSourcePagerDutyAutomationActionsRunnerConfig(name string) string return fmt.Sprintf(` resource "pagerduty_automation_actions_runner" "test" { name = "%s" + description = "Runner created by TF" runner_type = "runbook" runbook_base_uri = "cat-cat" runbook_api_key = "secret" diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go index 6523044dd..561309864 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -36,7 +36,6 @@ func resourcePagerDutyAutomationActionsRunner() *schema.Resource { "description": { Type: schema.TypeString, Optional: true, - Default: "Managed by Terraform", ForceNew: true, // Requires creation of new resource while support for update is not implemented }, "runbook_base_uri": { @@ -71,16 +70,31 @@ func resourcePagerDutyAutomationActionsRunner() *schema.Resource { } func buildAutomationActionsRunnerStruct(d *schema.ResourceData) (*pagerduty.AutomationActionsRunner, error) { + automationActionsRunner := pagerduty.AutomationActionsRunner{ - Name: d.Get("name").(string), - RunnerType: d.Get("runner_type").(string), - Description: d.Get("description").(string), + Name: d.Get("name").(string), + RunnerType: d.Get("runner_type").(string), } if automationActionsRunner.RunnerType != "runbook" { return nil, errors.New("only runners of runner_type runbook can be created") } + // The API does not allow new runners without a description, but legacy runners without a description exist + if attr, ok := d.GetOk("description"); ok { + val := attr.(string) + automationActionsRunner.Description = &val + } else { + return nil, errors.New("runner description must be specified when creating a runbook runner") + } + + if attr, ok := d.GetOk("runbook_base_uri"); ok { + val := attr.(string) + automationActionsRunner.RunbookBaseUri = &val + } else { + return nil, errors.New("runbook_base_uri must be specified when creating a runbook runner") + } + if attr, ok := d.GetOk("runbook_base_uri"); ok { val := attr.(string) automationActionsRunner.RunbookBaseUri = &val @@ -148,9 +162,12 @@ func resourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta i d.Set("name", automationActionsRunner.Name) d.Set("type", automationActionsRunner.Type) d.Set("runner_type", automationActionsRunner.RunnerType) - d.Set("description", automationActionsRunner.Description) d.Set("creation_time", automationActionsRunner.CreationTime) + if automationActionsRunner.Description != nil { + d.Set("description", &automationActionsRunner.Description) + } + if automationActionsRunner.RunbookBaseUri != nil { d.Set("runbook_base_uri", &automationActionsRunner.RunbookBaseUri) } diff --git a/pagerduty/resource_pagerduty_automation_actions_runner_test.go b/pagerduty/resource_pagerduty_automation_actions_runner_test.go index c8564476a..b7cd3e020 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner_test.go @@ -32,8 +32,13 @@ func TestAccPagerDutyAutomationActionsRunner_Basic(t *testing.T) { Config: testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName), Check: resource.ComposeTestCheckFunc( testAccCheckPagerDutyAutomationActionsRunnerExists("pagerduty_automation_actions_runner.foo"), - resource.TestCheckResourceAttr( - "pagerduty_automation_actions_runner.foo", "name", runnerName), + resource.TestCheckResourceAttr("pagerduty_automation_actions_runner.foo", "name", runnerName), + resource.TestCheckResourceAttr("pagerduty_automation_actions_runner.foo", "runner_type", "runbook"), + resource.TestCheckResourceAttr("pagerduty_automation_actions_runner.foo", "description", "Runner created by TF"), + resource.TestCheckResourceAttr("pagerduty_automation_actions_runner.foo", "runbook_base_uri", "cat-cat"), + resource.TestCheckResourceAttr("pagerduty_automation_actions_runner.foo", "type", "runner"), + resource.TestCheckResourceAttrSet("pagerduty_automation_actions_runner.foo", "id"), + resource.TestCheckResourceAttrSet("pagerduty_automation_actions_runner.foo", "creation_time"), ), }, }, @@ -80,6 +85,7 @@ func testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName string) strin return fmt.Sprintf(` resource "pagerduty_automation_actions_runner" "foo" { name = "%s" + description = "Runner created by TF" runner_type = "runbook" runbook_base_uri = "cat-cat" runbook_api_key = "cat-secret" From 81292e76b82f81b87f203c24c1daa78c54532895 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Wed, 7 Dec 2022 14:52:32 -0500 Subject: [PATCH 13/21] Code review fixes --- ...ata_source_pagerduty_automation_actions_runner.go | 5 ++++- ...ource_pagerduty_automation_actions_runner_test.go | 10 +++++----- .../resource_pagerduty_automation_actions_runner.go | 12 +----------- .../docs/d/automation_actions_runner.html.markdown | 1 - 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner.go b/pagerduty/data_source_pagerduty_automation_actions_runner.go index 734aa83cb..687f359b5 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner.go @@ -73,9 +73,12 @@ func dataSourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta d.Set("name", runner.Name) d.Set("type", runner.Type) d.Set("runner_type", runner.RunnerType) - d.Set("description", runner.Description) d.Set("creation_time", runner.CreationTime) + if runner.Description != nil { + d.Set("description", &runner.Description) + } + if runner.RunbookBaseUri != nil { d.Set("runbook_base_uri", &runner.RunbookBaseUri) } diff --git a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go index 3185e8872..c6e208fa5 100644 --- a/pagerduty/data_source_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/data_source_pagerduty_automation_actions_runner_test.go @@ -32,21 +32,21 @@ func testAccDataSourcePagerdutyAutomationActionsRunner(src, n string) resource.T srcR := s.RootModule().Resources[src] srcA := srcR.Primary.Attributes - r, ok := s.RootModule().Resources[n] + ds, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - a := r.Primary.Attributes + dsA := ds.Primary.Attributes - if a["id"] == "" { + if dsA["id"] == "" { return fmt.Errorf("No Runner ID is set") } testAtts := []string{"id", "name", "type", "runner_type", "creation_time", "last_seen", "description", "runbook_base_uri"} for _, att := range testAtts { - if a[att] != srcA[att] { - return fmt.Errorf("Expected the runner %s to be: %s, but got: %s", att, srcA[att], a[att]) + if dsA[att] != srcA[att] { + return fmt.Errorf("Expected the runner %s to be: %s, but got: %s", att, srcA[att], dsA[att]) } } diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go index 561309864..d8bf15468 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -48,10 +48,6 @@ func resourcePagerDutyAutomationActionsRunner() *schema.Resource { Optional: true, ForceNew: true, // Requires creation of new resource while support for update is not implemented }, - "id": { - Type: schema.TypeString, - Computed: true, - }, "type": { Type: schema.TypeString, Computed: true, @@ -95,13 +91,6 @@ func buildAutomationActionsRunnerStruct(d *schema.ResourceData) (*pagerduty.Auto return nil, errors.New("runbook_base_uri must be specified when creating a runbook runner") } - if attr, ok := d.GetOk("runbook_base_uri"); ok { - val := attr.(string) - automationActionsRunner.RunbookBaseUri = &val - } else { - return nil, errors.New("runbook_base_uri must be specified when creating a runbook runner") - } - if attr, ok := d.GetOk("runbook_api_key"); ok { val := attr.(string) automationActionsRunner.RunbookApiKey = &val @@ -128,6 +117,7 @@ func resourcePagerDutyAutomationActionsRunnerCreate(d *schema.ResourceData, meta retryErr := resource.Retry(10*time.Second, func() *resource.RetryError { if automationActionsRunner, _, err := client.AutomationActionsRunner.Create(automationActionsRunner); err != nil { if isErrCode(err, 400) || isErrCode(err, 429) { + time.Sleep(2 * time.Second) return resource.RetryableError(err) } diff --git a/website/docs/d/automation_actions_runner.html.markdown b/website/docs/d/automation_actions_runner.html.markdown index 3f23a9d90..5c83ee83e 100644 --- a/website/docs/d/automation_actions_runner.html.markdown +++ b/website/docs/d/automation_actions_runner.html.markdown @@ -36,6 +36,5 @@ The following attributes are exported: * `description` - (Optional) The description of the runner. * `last_seen` - (Optional) The last time runner has been seen. Represented as an ISO 8601 timestamp. * `runbook_base_uri` - (Optional) The base URI of the Runbook server to connect to. Applicable to `runbook` type runners only. -* `teams` - (Optional) Teams associated with the runner. Account must have the `teams` ability enabled. [1]: https://developer.pagerduty.com/api-reference/aace61f84cbd0-get-an-automation-action-runner From 7ba49315a58ed18662f4d0f3d2122b3a06506ebd Mon Sep 17 00:00:00 2001 From: mrdubr Date: Wed, 7 Dec 2022 15:13:40 -0500 Subject: [PATCH 14/21] Fixed id --- pagerduty/resource_pagerduty_automation_actions_runner.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go index d8bf15468..583f4fc32 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -148,7 +148,6 @@ func resourcePagerDutyAutomationActionsRunnerRead(d *schema.ResourceData, meta i time.Sleep(2 * time.Second) return resource.RetryableError(err) } else if automationActionsRunner != nil { - d.Set("id", automationActionsRunner.ID) d.Set("name", automationActionsRunner.Name) d.Set("type", automationActionsRunner.Type) d.Set("runner_type", automationActionsRunner.RunnerType) From cefbd14b5366d8bc1f430143ca059fbfc3e05b40 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Wed, 7 Dec 2022 15:37:15 -0500 Subject: [PATCH 15/21] update to replace heimweh/go-pagerduty by mrdubr/go-pagerduty `go mod edit -replace github.com/heimweh/go-pagerduty=github.com/mrdubr/go-pagerduty@a2getrunner-support && go mod tidy && go mod vendor` --- go.mod | 2 + go.sum | 2 + .../pagerduty/automation_actions_runner.go | 78 +++++++++++++++++++ .../go-pagerduty/pagerduty/pagerduty.go | 2 + vendor/modules.txt | 3 +- 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 vendor/github.com/heimweh/go-pagerduty/pagerduty/automation_actions_runner.go diff --git a/go.mod b/go.mod index 3d880fd9e..8c2919090 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/mrdubr/go-pagerduty v0.0.0-20221207010513-91b73ec9ea69 diff --git a/go.sum b/go.sum index 97f9525e5..3f721a6c8 100644 --- a/go.sum +++ b/go.sum @@ -294,6 +294,8 @@ github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/montanaflynn/stats v0.6.6 h1:Duep6KMIDpY4Yo11iFsvyqJDyfzLF9+sndUKT+v64GQ= github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= +github.com/mrdubr/go-pagerduty v0.0.0-20221207010513-91b73ec9ea69 h1:xNq1ArQXzUwNoWuUw3I4hIxCe52eXJOZ3o1XrubwT4s= +github.com/mrdubr/go-pagerduty v0.0.0-20221207010513-91b73ec9ea69/go.mod h1:t9vftsO1IjYHGdgJXeemZtomCWnxi2SRgu0PRcRb2oY= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758= github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs= diff --git a/vendor/github.com/heimweh/go-pagerduty/pagerduty/automation_actions_runner.go b/vendor/github.com/heimweh/go-pagerduty/pagerduty/automation_actions_runner.go new file mode 100644 index 000000000..2e4ac4aef --- /dev/null +++ b/vendor/github.com/heimweh/go-pagerduty/pagerduty/automation_actions_runner.go @@ -0,0 +1,78 @@ +package pagerduty + +import "fmt" + +// AutomationActionsRunner handles the communication with schedule +// related methods of the PagerDuty API. +type AutomationActionsRunnerService service + +type AutomationActionsRunner struct { + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + RunnerType string `json:"runner_type"` + CreationTime string `json:"creation_time"` + LastSeenTime *string `json:"last_seen,omitempty"` + Summary string `json:"summary,omitempty"` + Description *string `json:"description,omitempty"` + RunbookBaseUri *string `json:"runbook_base_uri,omitempty"` + RunbookApiKey *string `json:"runbook_api_key,omitempty"` + Teams []*TeamReference `json:"teams,omitempty"` + Privileges *AutomationActionsPrivileges `json:"privileges,omitempty"` +} + +type AutomationActionsPrivileges struct { + Permissions []*string `json:"permissions,omitempty"` +} + +type AutomationActionsRunnerPayload struct { + Runner *AutomationActionsRunner `json:"runner,omitempty"` +} + +// Create creates a new runner +func (s *AutomationActionsRunnerService) Create(runner *AutomationActionsRunner) (*AutomationActionsRunner, *Response, error) { + u := "/automation_actions/runners" + v := new(AutomationActionsRunnerPayload) + o := RequestOptions{ + Type: "header", + Label: "X-EARLY-ACCESS", + Value: "automation-actions-early-access", + } + + resp, err := s.client.newRequestDoOptions("POST", u, nil, &AutomationActionsRunnerPayload{Runner: runner}, &v, o) + if err != nil { + return nil, nil, err + } + + return v.Runner, resp, nil +} + +// Get retrieves information about a runner. +func (s *AutomationActionsRunnerService) Get(id string) (*AutomationActionsRunner, *Response, error) { + u := fmt.Sprintf("/automation_actions/runners/%s", id) + v := new(AutomationActionsRunnerPayload) + o := RequestOptions{ + Type: "header", + Label: "X-EARLY-ACCESS", + Value: "automation-actions-early-access", + } + + resp, err := s.client.newRequestDoOptions("GET", u, nil, nil, &v, o) + if err != nil { + return nil, nil, err + } + + return v.Runner, resp, nil +} + +// Delete deletes an existing runner. +func (s *AutomationActionsRunnerService) Delete(id string) (*Response, error) { + u := fmt.Sprintf("/automation_actions/runners/%s", id) + o := RequestOptions{ + Type: "header", + Label: "X-EARLY-ACCESS", + Value: "automation-actions-early-access", + } + + return s.client.newRequestDoOptions("DELETE", u, nil, nil, nil, o) +} diff --git a/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go b/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go index 48a89e8d0..3f547a340 100644 --- a/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go +++ b/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go @@ -59,6 +59,7 @@ type Client struct { WebhookSubscriptions *WebhookSubscriptionService BusinessServiceSubscribers *BusinessServiceSubscriberService OnCall *OnCallService + AutomationActionsRunner *AutomationActionsRunnerService } // Response is a wrapper around http.Response @@ -121,6 +122,7 @@ func NewClient(config *Config) (*Client, error) { c.WebhookSubscriptions = &WebhookSubscriptionService{c} c.BusinessServiceSubscribers = &BusinessServiceSubscriberService{c} c.OnCall = &OnCallService{c} + c.AutomationActionsRunner = &AutomationActionsRunnerService{c} InitCache(c) PopulateCache() diff --git a/vendor/modules.txt b/vendor/modules.txt index a21a95c07..5c628a2d5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -122,7 +122,7 @@ github.com/hashicorp/terraform-registry-address github.com/hashicorp/terraform-svchost # github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d github.com/hashicorp/yamux -# github.com/heimweh/go-pagerduty v0.0.0-20220907210255-0dcec1fd4239 +# github.com/heimweh/go-pagerduty v0.0.0-20220907210255-0dcec1fd4239 => github.com/mrdubr/go-pagerduty v0.0.0-20221207010513-91b73ec9ea69 ## explicit github.com/heimweh/go-pagerduty/pagerduty # github.com/klauspost/compress v1.15.9 @@ -349,3 +349,4 @@ google.golang.org/protobuf/types/known/anypb google.golang.org/protobuf/types/known/durationpb google.golang.org/protobuf/types/known/emptypb google.golang.org/protobuf/types/known/timestamppb +# github.com/heimweh/go-pagerduty => github.com/mrdubr/go-pagerduty v0.0.0-20221207010513-91b73ec9ea69 From f9bbf083b006a18e0a06863accfc0e109e0ab300 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Wed, 7 Dec 2022 16:56:31 -0500 Subject: [PATCH 16/21] Docs for the resource --- .../r/automation_actions_runner.html.markdown | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 website/docs/r/automation_actions_runner.html.markdown diff --git a/website/docs/r/automation_actions_runner.html.markdown b/website/docs/r/automation_actions_runner.html.markdown new file mode 100644 index 000000000..3b018e90d --- /dev/null +++ b/website/docs/r/automation_actions_runner.html.markdown @@ -0,0 +1,52 @@ +--- +layout: "pagerduty" +page_title: "PagerDuty: pagerduty_automation_actions_runner" +sidebar_current: "docs-pagerduty-resource-automation-actions-runner" +description: |- + Creates and manages an Automation Actions runner in PagerDuty. +--- + +# pagerduty\_automation\_actions\_runner + +An Automation Actions [runner](https://developer.pagerduty.com/api-reference/d78999fb7e863-create-an-automation-action-runner) is the method for how actions are executed. This can be done locally using an installed runner agent or as a connection to a PD Runbook Automation instance. + +-> Only Runbook Automation (runbook) runners can be created. + +## Example Usage + +```hcl +resource "pagerduty_automation_actions_runner" "example" { + name = "Production runner" + description = "Runner created by the SRE team" + runner_type = "runbook" + runbook_base_uri = "prod.cat" + runbook_api_key = "ABC123456789XYZ" +} +``` + +## Argument Reference + +The following arguments are supported: + + * `name` - (Required) The name of the runner. + * `description` - (Required) The description of the runner. + * `runner_type` - (Required) The type of runner. The only allowed values is `runbook`. + * `runbook_base_uri` - (Required) The subdomain for your Runbook Automation Instance. + * `runbook_api_key` - (Required) The unique User API Token created in Runbook Automation. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the runner. +* `type` - The type of object. The value returned will be `runner`. +* `creation_time` - The time runner was created. Represented as an ISO 8601 timestamp. +* `last_seen` - (Optional) The last time runner has been seen. Represented as an ISO 8601 timestamp. + +## Import + +Runners can be imported using the `id`, e.g. + +``` +$ terraform import pagerduty_automation_actions_runner.main 01DBJLIGED17S1DQK123 +``` From 07bfd39ef5401e2bf43e64bc566665cf2a38833a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Wed, 7 Dec 2022 20:04:28 -0300 Subject: [PATCH 17/21] wip implementing import for runners --- ...agerduty_automation_actions_runner_test.go | 46 +++++++++++++++++++ ...rce_pagerduty_automation_actions_runner.go | 17 +++++-- 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 pagerduty/import_pagerduty_automation_actions_runner_test.go diff --git a/pagerduty/import_pagerduty_automation_actions_runner_test.go b/pagerduty/import_pagerduty_automation_actions_runner_test.go new file mode 100644 index 000000000..654bbf9fd --- /dev/null +++ b/pagerduty/import_pagerduty_automation_actions_runner_test.go @@ -0,0 +1,46 @@ +package pagerduty + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccPagerDutyAutomationActionsRunner_import(t *testing.T) { + runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPagerDutyAutomationActionsRunnerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName), + }, + + { + Config: testAccCheckPagerDutyAutomationActionsRunnerConfig2(runnerName), + }, + + { + ResourceName: "pagerduty_automation_actions_runner.foo", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckPagerDutyAutomationActionsRunnerConfig2(runnerName string) string { + return fmt.Sprintf(` +resource "pagerduty_automation_actions_runner" "foo" { + name = "%s" + description = "Runner created by TF" + runner_type = "runbook" + runbook_base_uri = "cat-cat" + # runbook_api_key = "cat-secret" +} +`, runnerName) +} diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go index 583f4fc32..6f52d2e1a 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -3,6 +3,7 @@ package pagerduty import ( "errors" "log" + "strings" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -44,9 +45,17 @@ func resourcePagerDutyAutomationActionsRunner() *schema.Resource { ForceNew: true, // Requires creation of new resource while support for update is not implemented }, "runbook_api_key": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, // Requires creation of new resource while support for update is not implemented + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, // Requires creation of new resource while support for update is not implemented + Sensitive: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + if strings.Compare(old, new) < 0 { + return false + } + return true + }, }, "type": { Type: schema.TypeString, @@ -94,8 +103,6 @@ func buildAutomationActionsRunnerStruct(d *schema.ResourceData) (*pagerduty.Auto if attr, ok := d.GetOk("runbook_api_key"); ok { val := attr.(string) automationActionsRunner.RunbookApiKey = &val - } else { - return nil, errors.New("runbook_api_key must be specified when creating a runbook runner") } return &automationActionsRunner, nil From 4cc42347c545a6a7642db320a099512bca8dfbb2 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Thu, 8 Dec 2022 11:38:25 -0500 Subject: [PATCH 18/21] fixed the import test --- ...agerduty_automation_actions_runner_test.go | 24 ++++--------------- ...rce_pagerduty_automation_actions_runner.go | 10 ++------ 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/pagerduty/import_pagerduty_automation_actions_runner_test.go b/pagerduty/import_pagerduty_automation_actions_runner_test.go index 654bbf9fd..54a3744ed 100644 --- a/pagerduty/import_pagerduty_automation_actions_runner_test.go +++ b/pagerduty/import_pagerduty_automation_actions_runner_test.go @@ -19,28 +19,12 @@ func TestAccPagerDutyAutomationActionsRunner_import(t *testing.T) { { Config: testAccCheckPagerDutyAutomationActionsRunnerConfig(runnerName), }, - - { - Config: testAccCheckPagerDutyAutomationActionsRunnerConfig2(runnerName), - }, - { - ResourceName: "pagerduty_automation_actions_runner.foo", - ImportState: true, - ImportStateVerify: true, + ResourceName: "pagerduty_automation_actions_runner.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"runbook_api_key"}, }, }, }) } - -func testAccCheckPagerDutyAutomationActionsRunnerConfig2(runnerName string) string { - return fmt.Sprintf(` -resource "pagerduty_automation_actions_runner" "foo" { - name = "%s" - description = "Runner created by TF" - runner_type = "runbook" - runbook_base_uri = "cat-cat" - # runbook_api_key = "cat-secret" -} -`, runnerName) -} diff --git a/pagerduty/resource_pagerduty_automation_actions_runner.go b/pagerduty/resource_pagerduty_automation_actions_runner.go index 6f52d2e1a..adaf0a87d 100644 --- a/pagerduty/resource_pagerduty_automation_actions_runner.go +++ b/pagerduty/resource_pagerduty_automation_actions_runner.go @@ -3,7 +3,6 @@ package pagerduty import ( "errors" "log" - "strings" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -47,15 +46,8 @@ func resourcePagerDutyAutomationActionsRunner() *schema.Resource { "runbook_api_key": { Type: schema.TypeString, Optional: true, - Computed: true, ForceNew: true, // Requires creation of new resource while support for update is not implemented Sensitive: true, - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - if strings.Compare(old, new) < 0 { - return false - } - return true - }, }, "type": { Type: schema.TypeString, @@ -103,6 +95,8 @@ func buildAutomationActionsRunnerStruct(d *schema.ResourceData) (*pagerduty.Auto if attr, ok := d.GetOk("runbook_api_key"); ok { val := attr.(string) automationActionsRunner.RunbookApiKey = &val + } else { + return nil, errors.New("runbook_api_key must be specified when creating a runbook runner") } return &automationActionsRunner, nil From 1512d6f30d99fa65b2d3fb9fe908932f6f38e589 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Thu, 8 Dec 2022 12:51:21 -0500 Subject: [PATCH 19/21] import doc update --- .../r/automation_actions_runner.html.markdown | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/website/docs/r/automation_actions_runner.html.markdown b/website/docs/r/automation_actions_runner.html.markdown index 3b018e90d..dadb40671 100644 --- a/website/docs/r/automation_actions_runner.html.markdown +++ b/website/docs/r/automation_actions_runner.html.markdown @@ -15,12 +15,19 @@ An Automation Actions [runner](https://developer.pagerduty.com/api-reference/d78 ## Example Usage ```hcl +# Assumes TF_VAR_RUNBOOK_API_KEY variable is defined in the environment + +variable "RUNBOOK_API_KEY" { + type = string + sensitive = true +} + resource "pagerduty_automation_actions_runner" "example" { - name = "Production runner" - description = "Runner created by the SRE team" - runner_type = "runbook" - runbook_base_uri = "prod.cat" - runbook_api_key = "ABC123456789XYZ" + name = "Runner created via TF" + description = "Description of the Runner created via TF" + runner_type = "runbook" + runbook_base_uri = "rdcat.stg" + runbook_api_key = var.RUNBOOK_API_KEY } ``` @@ -48,5 +55,16 @@ The following attributes are exported: Runners can be imported using the `id`, e.g. ``` -$ terraform import pagerduty_automation_actions_runner.main 01DBJLIGED17S1DQK123 +resource "pagerduty_automation_actions_runner" "example" { + name = "Runner created via TF" + description = "Description of the Runner created via TF" + runner_type = "runbook" + runbook_base_uri = "rdcat.stg" +} ``` + +``` +$ terraform import pagerduty_automation_actions_runner.example 01DER7CUUBF7TH4116K0M4WKPU +``` + +-> The imported resource `runbook_api_key` attribute has been omitted to avoid resource replacement after the import. From 03ba9b3e25101e17bbec9144ed7a0b5859b7c3bf Mon Sep 17 00:00:00 2001 From: mrdubr Date: Thu, 8 Dec 2022 12:54:04 -0500 Subject: [PATCH 20/21] typo --- website/docs/r/automation_actions_runner.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/automation_actions_runner.html.markdown b/website/docs/r/automation_actions_runner.html.markdown index dadb40671..76f6ad957 100644 --- a/website/docs/r/automation_actions_runner.html.markdown +++ b/website/docs/r/automation_actions_runner.html.markdown @@ -15,7 +15,7 @@ An Automation Actions [runner](https://developer.pagerduty.com/api-reference/d78 ## Example Usage ```hcl -# Assumes TF_VAR_RUNBOOK_API_KEY variable is defined in the environment +# Assumes the TF_VAR_RUNBOOK_API_KEY variable is defined in the environment variable "RUNBOOK_API_KEY" { type = string From 05c9e8401e195a9f5aec9c03dd8bf2672667c356 Mon Sep 17 00:00:00 2001 From: mrdubr Date: Fri, 9 Dec 2022 10:42:49 -0500 Subject: [PATCH 21/21] Updated import docs --- website/docs/r/automation_actions_runner.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/website/docs/r/automation_actions_runner.html.markdown b/website/docs/r/automation_actions_runner.html.markdown index 76f6ad957..9bc6aec37 100644 --- a/website/docs/r/automation_actions_runner.html.markdown +++ b/website/docs/r/automation_actions_runner.html.markdown @@ -52,6 +52,8 @@ The following attributes are exported: ## Import +-> In the example below the `runbook_api_key` attribute has been omitted to avoid resource replacement after the import. + Runners can be imported using the `id`, e.g. ``` @@ -62,9 +64,8 @@ resource "pagerduty_automation_actions_runner" "example" { runbook_base_uri = "rdcat.stg" } ``` - ``` $ terraform import pagerduty_automation_actions_runner.example 01DER7CUUBF7TH4116K0M4WKPU ``` --> The imported resource `runbook_api_key` attribute has been omitted to avoid resource replacement after the import. +