-
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.
Merge pull request #402 from stmcallister/tags-resource
Adding Tag and Tag Assignment resources. Also Tag Data Source.
- Loading branch information
Showing
38 changed files
with
1,490 additions
and
193 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
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,73 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"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 dataSourcePagerDutyTag() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourcePagerDutyTagRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"label": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The label of the tag to find in the PagerDuty API", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourcePagerDutyTagRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*pagerduty.Client) | ||
|
||
log.Printf("[INFO] Reading PagerDuty tag") | ||
|
||
searchTag := d.Get("label").(string) | ||
|
||
o := &pagerduty.ListTagsOptions{ | ||
Query: searchTag, | ||
} | ||
|
||
return resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
resp, _, err := client.Tags.List(o) | ||
if err != nil { | ||
if isErrCode(err, 429) { | ||
// Delaying retry by 30s as recommended by PagerDuty | ||
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit | ||
time.Sleep(30 * time.Second) | ||
return resource.RetryableError(err) | ||
} | ||
|
||
return resource.NonRetryableError(err) | ||
} | ||
|
||
var found *pagerduty.Tag | ||
|
||
for _, tag := range resp.Tags { | ||
if tag.Label == searchTag { | ||
found = tag | ||
break | ||
} | ||
} | ||
|
||
if found == nil { | ||
return resource.NonRetryableError( | ||
fmt.Errorf("Unable to locate any tag with label: %s", searchTag), | ||
) | ||
} | ||
|
||
d.SetId(found.ID) | ||
d.Set("label", found.Label) | ||
d.Set("summary", found.Summary) | ||
d.Set("html_url", found.HTMLURL) | ||
|
||
return nil | ||
}) | ||
} |
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,64 @@ | ||
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 TestAccDataSourcePagerDutyTag_Basic(t *testing.T) { | ||
tag := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourcePagerDutyTagConfig(tag), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourcePagerDutyTag("pagerduty_tag.test", "data.pagerduty_tag.by_label"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourcePagerDutyTag(src, n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
|
||
srcR := s.RootModule().Resources[src] | ||
srcA := srcR.Primary.Attributes | ||
|
||
r := s.RootModule().Resources[n] | ||
a := r.Primary.Attributes | ||
|
||
if a["id"] == "" { | ||
return fmt.Errorf("Expected to get a tag ID from PagerDuty") | ||
} | ||
|
||
testAtts := []string{"id", "label"} | ||
|
||
for _, att := range testAtts { | ||
if a[att] != srcA[att] { | ||
return fmt.Errorf("Expected the tag %s to be: %s, but got: %s", att, srcA[att], a[att]) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccDataSourcePagerDutyTagConfig(tag string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_tag" "test" { | ||
label = "%s" | ||
} | ||
data "pagerduty_tag" "by_label" { | ||
label = pagerduty_tag.test.label | ||
} | ||
`, tag) | ||
} |
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,37 @@ | ||
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 TestAccPagerDutyTagAssignment_import(t *testing.T) { | ||
tag := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
team := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPagerDutyTagAssignmentDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPagerDutyTagAssignmentTeamConfig(tag, team), | ||
}, | ||
|
||
{ | ||
ResourceName: "pagerduty_tag_assignment.foo", | ||
ImportStateIdFunc: testAccCheckPagerDutyTagAssignmentID, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPagerDutyTagAssignmentID(s *terraform.State) (string, error) { | ||
return fmt.Sprintf("%v.%v.%v", "teams", s.RootModule().Resources["pagerduty_team.foo"].Primary.ID, s.RootModule().Resources["pagerduty_tag.foo"].Primary.ID), nil | ||
} |
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 TestAccPagerDutyTag_import(t *testing.T) { | ||
tag := 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(tag), | ||
}, | ||
|
||
{ | ||
ResourceName: "pagerduty_tag.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
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,118 @@ | ||
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 resourcePagerDutyTag() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourcePagerDutyTagCreate, | ||
Read: resourcePagerDutyTagRead, | ||
Delete: resourcePagerDutyTagDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"label": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"summary": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"html_url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func buildTagStruct(d *schema.ResourceData) *pagerduty.Tag { | ||
tag := &pagerduty.Tag{ | ||
Label: d.Get("label").(string), | ||
Type: "tag", | ||
} | ||
|
||
if attr, ok := d.GetOk("summary"); ok { | ||
tag.Summary = attr.(string) | ||
} | ||
|
||
return tag | ||
} | ||
|
||
func resourcePagerDutyTagCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*pagerduty.Client) | ||
|
||
tag := buildTagStruct(d) | ||
|
||
log.Printf("[INFO] Creating PagerDuty tag %s", tag.Label) | ||
|
||
retryErr := resource.Retry(10*time.Second, func() *resource.RetryError { | ||
if tag, _, err := client.Tags.Create(tag); err != nil { | ||
if isErrCode(err, 400) || isErrCode(err, 429) { | ||
return resource.RetryableError(err) | ||
} | ||
|
||
return resource.NonRetryableError(err) | ||
} else if tag != nil { | ||
d.SetId(tag.ID) | ||
} | ||
return nil | ||
}) | ||
|
||
if retryErr != nil { | ||
return retryErr | ||
} | ||
|
||
return resourcePagerDutyTagRead(d, meta) | ||
|
||
} | ||
|
||
func resourcePagerDutyTagRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*pagerduty.Client) | ||
|
||
log.Printf("[INFO] Reading PagerDuty tag %s", d.Id()) | ||
|
||
return resource.Retry(30*time.Second, func() *resource.RetryError { | ||
if tag, _, err := client.Tags.Get(d.Id()); err != nil { | ||
time.Sleep(2 * time.Second) | ||
return resource.RetryableError(err) | ||
} else if tag != nil { | ||
log.Printf("Tag Type: %v", tag.Type) | ||
d.Set("label", tag.Label) | ||
d.Set("summary", tag.Summary) | ||
d.Set("html_url", tag.HTMLURL) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func resourcePagerDutyTagDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*pagerduty.Client) | ||
|
||
log.Printf("[INFO] Deleting PagerDuty tag %s", d.Id()) | ||
|
||
retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
if _, err := client.Tags.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 | ||
} |
Oops, something went wrong.