-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip adding support for a2action team association
- Loading branch information
1 parent
ec4edbe
commit 9cf9ed0
Showing
5 changed files
with
262 additions
and
38 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
128 changes: 128 additions & 0 deletions
128
pagerduty/resource_pagerduty_automation_actions_action_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,128 @@ | ||
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 resourcePagerDutyAutomationActionsActionTeamAssociation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourcePagerDutyAutomationActionsActionTeamAssociationCreate, | ||
Read: resourcePagerDutyAutomationActionsActionTeamAssociationRead, | ||
Delete: resourcePagerDutyAutomationActionsActionTeamAssociationDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"action_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"team_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePagerDutyAutomationActionsActionTeamAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
client, err := meta.(*Config).Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
actionID := d.Get("action_id").(string) | ||
teamID := d.Get("team_id").(string) | ||
|
||
log.Printf("[INFO] Creating PagerDuty AutomationActionsActionTeamAssociation %s:%s", d.Get("actionID").(string), d.Get("team_id").(string)) | ||
|
||
retryErr := resource.Retry(10*time.Second, func() *resource.RetryError { | ||
if teamRef, _, err := client.AutomationActionsAction.AssociateToTeam(actionID, 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", actionID, teamID)) | ||
} | ||
return nil | ||
}) | ||
|
||
if retryErr != nil { | ||
return retryErr | ||
} | ||
|
||
return fetchPagerDutyAutomationActionsActionTeamAssociation(d, meta, handleNotFoundError) | ||
} | ||
|
||
func fetchPagerDutyAutomationActionsActionTeamAssociation(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { | ||
client, err := meta.(*Config).Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
actionID, teamID := resourcePagerDutyParseColonCompoundID(d.Id()) | ||
log.Printf("[DEBUG] Reading a2 action: %s from team: %s", actionID, teamID) | ||
return resource.Retry(30*time.Second, func() *resource.RetryError { | ||
teamRef, _, err := client.AutomationActionsAction.GetAssociationToTeam(actionID, teamID) | ||
if err != nil { | ||
errResp := errCallback(err, d) | ||
if errResp != nil { | ||
time.Sleep(2 * time.Second) | ||
return resource.RetryableError(errResp) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
if teamRef.ID != teamID { | ||
log.Printf("[WARN] Removing %s since the user: %s is not a member of: %s", d.Id(), actionID, teamID) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("action_id", actionID) | ||
d.Set("team_id", teamRef.ID) | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func resourcePagerDutyAutomationActionsActionTeamAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
return fetchPagerDutyAutomationActionsActionTeamAssociation(d, meta, handleNotFoundError) | ||
} | ||
|
||
func resourcePagerDutyAutomationActionsActionTeamAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
client, err := meta.(*Config).Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
actionID, teamID := resourcePagerDutyParseColonCompoundID(d.Id()) | ||
log.Printf("[INFO] Deleting PagerDuty AutomationActionsActionTeamAssociation %s", d.Id()) | ||
|
||
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
if _, err := client.AutomationActionsAction.DissociateToTeam(actionID, 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 | ||
} |
96 changes: 96 additions & 0 deletions
96
pagerduty/resource_pagerduty_automation_actions_action_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,96 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func init() { | ||
resource.AddTestSweepers("pagerduty_automation_actions_action_team_association", &resource.Sweeper{ | ||
Name: "pagerduty_automation_actions_action_team_association", | ||
F: testSweepAutomationActionsActionTeamAssociation, | ||
}) | ||
} | ||
|
||
func testSweepAutomationActionsActionTeamAssociation(region string) error { | ||
return nil | ||
} | ||
|
||
func TestAccPagerDutyAutomationActionsActionTeamAssociation_Basic(t *testing.T) { | ||
teamName := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPagerDutyAutomationActionsActionTeamAssociationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPagerDutyAutomationActionsActionTeamAssociationConfig(teamName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyAutomationActionsActionTeamAssociationExists("pagerduty_automation_actions_action_team_association.foo"), | ||
resource.TestCheckResourceAttrSet("pagerduty_automation_actions_action_team_association.foo", "action_id"), | ||
resource.TestCheckResourceAttrSet("pagerduty_automation_actions_action_team_association.foo", "team_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPagerDutyAutomationActionsActionTeamAssociationDestroy(s *terraform.State) error { | ||
client, _ := testAccProvider.Meta().(*Config).Client() | ||
for _, r := range s.RootModule().Resources { | ||
if r.Type != "pagerduty_automation_actions_action_team_association" { | ||
continue | ||
} | ||
actionID, teamID := resourcePagerDutyParseColonCompoundID(r.Primary.ID) | ||
if _, _, err := client.AutomationActionsAction.GetAssociationToTeam(actionID, teamID); err == nil { | ||
return fmt.Errorf("Automation Actions Runner still exists") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccCheckPagerDutyAutomationActionsActionTeamAssociationExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No Automation Actions Runner ID is set") | ||
} | ||
|
||
client, _ := testAccProvider.Meta().(*Config).Client() | ||
actionID, teamID := resourcePagerDutyParseColonCompoundID(rs.Primary.ID) | ||
found, _, err := client.AutomationActionsAction.GetAssociationToTeam(actionID, teamID) | ||
if err != nil { | ||
return err | ||
} | ||
if found.ID != rs.Primary.ID { | ||
return fmt.Errorf("Automation Actions Action association to team not found: %v - %v", rs.Primary.ID, found) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckPagerDutyAutomationActionsActionTeamAssociationConfig(teamName string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_team" "foo" { | ||
name = "%s" | ||
description = "foo" | ||
} | ||
# expecting action resource to be here | ||
resource "pagerduty_automation_actions_action_team_association" "foo" { | ||
action_id = "action_id_will_be_here" | ||
team_id = pagerduty_team.foo.id | ||
} | ||
`, teamName) | ||
} |
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