-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #607 from mrdubr/add_runner_team_assoc
Add support for Automation Actions' Runner association with a Team
- Loading branch information
Showing
11 changed files
with
395 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
pagerduty/import_pagerduty_automation_actions_runner_team_association_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccPagerDutyAutomationActionsRunnerTeamAssociation_import(t *testing.T) { | ||
runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
teamName := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationConfig(runnerName, teamName), | ||
}, | ||
{ | ||
ResourceName: "pagerduty_automation_actions_runner_team_association.foo", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
pagerduty/resource_pagerduty_automation_actions_runner_team_association.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourcePagerDutyAutomationActionsRunnerTeamAssociation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourcePagerDutyAutomationActionsRunnerTeamAssociationCreate, | ||
Read: resourcePagerDutyAutomationActionsRunnerTeamAssociationRead, | ||
Delete: resourcePagerDutyAutomationActionsRunnerTeamAssociationDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"runner_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"team_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePagerDutyAutomationActionsRunnerTeamAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
client, err := meta.(*Config).Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
runnerID := d.Get("runner_id").(string) | ||
teamID := d.Get("team_id").(string) | ||
|
||
log.Printf("[INFO] Creating PagerDuty AutomationActionsRunnerTeamAssociation %s:%s", d.Get("runner_id").(string), d.Get("team_id").(string)) | ||
|
||
retryErr := resource.Retry(10*time.Second, func() *resource.RetryError { | ||
if teamRef, _, err := client.AutomationActionsRunner.AssociateToTeam(runnerID, teamID); err != nil { | ||
if isErrCode(err, 429) { | ||
time.Sleep(2 * time.Second) | ||
return resource.RetryableError(err) | ||
} | ||
|
||
return resource.NonRetryableError(err) | ||
} else if teamRef != nil { | ||
d.SetId(fmt.Sprintf("%s:%s", runnerID, teamID)) | ||
} | ||
return nil | ||
}) | ||
|
||
if retryErr != nil { | ||
return retryErr | ||
} | ||
|
||
return fetchPagerDutyAutomationActionsRunnerTeamAssociation(d, meta, handleNotFoundError) | ||
} | ||
|
||
func fetchPagerDutyAutomationActionsRunnerTeamAssociation(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { | ||
client, err := meta.(*Config).Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
runnerID, teamID := resourcePagerDutyParseColonCompoundID(d.Id()) | ||
return resource.Retry(30*time.Second, func() *resource.RetryError { | ||
resp, _, err := client.AutomationActionsRunner.GetAssociationToTeam(runnerID, teamID) | ||
if err != nil { | ||
errResp := errCallback(err, d) | ||
if errResp != nil { | ||
time.Sleep(2 * time.Second) | ||
return resource.RetryableError(errResp) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
if resp.Team.ID != teamID { | ||
log.Printf("[WARN] Removing %s since the user: %s is not a member of: %s", d.Id(), runnerID, teamID) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("runner_id", runnerID) | ||
d.Set("team_id", resp.Team.ID) | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func resourcePagerDutyAutomationActionsRunnerTeamAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
return fetchPagerDutyAutomationActionsRunnerTeamAssociation(d, meta, handleNotFoundError) | ||
} | ||
|
||
func resourcePagerDutyAutomationActionsRunnerTeamAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
client, err := meta.(*Config).Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
runnerID, teamID := resourcePagerDutyParseColonCompoundID(d.Id()) | ||
log.Printf("[INFO] Deleting PagerDuty AutomationActionsRunnerTeamAssociation %s", d.Id()) | ||
|
||
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
if _, err := client.AutomationActionsRunner.DissociateFromTeam(runnerID, teamID); 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 | ||
} |
103 changes: 103 additions & 0 deletions
103
pagerduty/resource_pagerduty_automation_actions_runner_team_association_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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_runner_team_association", &resource.Sweeper{ | ||
Name: "pagerduty_automation_actions_runner_team_association", | ||
F: testSweepAutomationActionsRunnerTeamAssociation, | ||
}) | ||
} | ||
|
||
func testSweepAutomationActionsRunnerTeamAssociation(region string) error { | ||
return nil | ||
} | ||
|
||
func TestAccPagerDutyAutomationActionsRunnerTeamAssociation_Basic(t *testing.T) { | ||
runnerName := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
teamName := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationConfig(runnerName, teamName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationExists("pagerduty_automation_actions_runner_team_association.foo"), | ||
resource.TestCheckResourceAttrSet("pagerduty_automation_actions_runner_team_association.foo", "runner_id"), | ||
resource.TestCheckResourceAttrSet("pagerduty_automation_actions_runner_team_association.foo", "team_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationDestroy(s *terraform.State) error { | ||
client, _ := testAccProvider.Meta().(*Config).Client() | ||
for _, r := range s.RootModule().Resources { | ||
if r.Type != "pagerduty_automation_actions_runner_team_association" { | ||
continue | ||
} | ||
runnerID, teamID := resourcePagerDutyParseColonCompoundID(r.Primary.ID) | ||
if _, _, err := client.AutomationActionsRunner.GetAssociationToTeam(runnerID, teamID); err == nil { | ||
return fmt.Errorf("Automation Actions Runner Team association still exists") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationExists(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() | ||
runnerID, teamID := resourcePagerDutyParseColonCompoundID(rs.Primary.ID) | ||
found, _, err := client.AutomationActionsRunner.GetAssociationToTeam(runnerID, teamID) | ||
if err != nil { | ||
return err | ||
} | ||
if fmt.Sprintf("%s:%s", runnerID, found.Team.ID) != rs.Primary.ID { | ||
return fmt.Errorf("Automation Actions Runner association to team not found: %v - %v", rs.Primary.ID, found) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckPagerDutyAutomationActionsRunnerTeamAssociationConfig(teamName, runnerName string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_team" "foo" { | ||
name = "%s" | ||
description = "foo" | ||
} | ||
resource "pagerduty_automation_actions_runner" "foo" { | ||
name = "%s runner" | ||
description = "Runner created by TF" | ||
runner_type = "runbook" | ||
runbook_base_uri = "cat-cat" | ||
runbook_api_key = "cat-secret" | ||
} | ||
resource "pagerduty_automation_actions_runner_team_association" "foo" { | ||
runner_id = pagerduty_automation_actions_runner.foo.id | ||
team_id = pagerduty_team.foo.id | ||
} | ||
`, teamName, runnerName) | ||
} |
40 changes: 40 additions & 0 deletions
40
vendor/github.com/heimweh/go-pagerduty/pagerduty/automation_actions_action.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
vendor/github.com/heimweh/go-pagerduty/pagerduty/automation_actions_runner.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.