From 15cde73000fdc580fe4f4097abb251b1beec438c Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Mon, 5 Dec 2016 17:42:58 -0500 Subject: [PATCH] Add pagerduty_user data source (#10541) --- .../pagerduty/data_source_pagerduty_user.go | 62 ++++++++++++++++++ .../data_source_pagerduty_user_test.go | 64 +++++++++++++++++++ builtin/providers/pagerduty/provider.go | 1 + .../providers/pagerduty/d/user.html.markdown | 44 +++++++++++++ website/source/layouts/pagerduty.erb | 3 + 5 files changed, 174 insertions(+) create mode 100644 builtin/providers/pagerduty/data_source_pagerduty_user.go create mode 100644 builtin/providers/pagerduty/data_source_pagerduty_user_test.go create mode 100644 website/source/docs/providers/pagerduty/d/user.html.markdown diff --git a/builtin/providers/pagerduty/data_source_pagerduty_user.go b/builtin/providers/pagerduty/data_source_pagerduty_user.go new file mode 100644 index 000000000..392a8a184 --- /dev/null +++ b/builtin/providers/pagerduty/data_source_pagerduty_user.go @@ -0,0 +1,62 @@ +package pagerduty + +import ( + "fmt" + "log" + + pagerduty "github.com/PagerDuty/go-pagerduty" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourcePagerDutyUser() *schema.Resource { + return &schema.Resource{ + Read: dataSourcePagerDutyUserRead, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "email": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func dataSourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + log.Printf("[INFO] Reading PagerDuty user") + + searchEmail := d.Get("email").(string) + + o := &pagerduty.ListUsersOptions{ + Query: searchEmail, + } + + resp, err := client.ListUsers(*o) + if err != nil { + return err + } + + var found *pagerduty.User + + for _, user := range resp.Users { + if user.Email == searchEmail { + found = &user + break + } + } + + if found == nil { + return fmt.Errorf("Unable to locate any user with the email: %s", searchEmail) + } + + d.SetId(found.ID) + d.Set("name", found.Name) + d.Set("email", found.Email) + + return nil +} diff --git a/builtin/providers/pagerduty/data_source_pagerduty_user_test.go b/builtin/providers/pagerduty/data_source_pagerduty_user_test.go new file mode 100644 index 000000000..e2ab336f0 --- /dev/null +++ b/builtin/providers/pagerduty/data_source_pagerduty_user_test.go @@ -0,0 +1,64 @@ +package pagerduty + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourcePagerDutyUser_Basic(t *testing.T) { + rName := acctest.RandString(5) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourcePagerDutyUserConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccDataSourcePagerDutyUser("pagerduty_user.test", "data.pagerduty_user.by_email"), + ), + }, + }, + }) +} + +func testAccDataSourcePagerDutyUser(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 user ID from PagerDuty") + } + + testAtts := []string{"id", "name", "email"} + + for _, att := range testAtts { + if a[att] != srcA[att] { + return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att]) + } + } + + return nil + } +} + +func testAccDataSourcePagerDutyUserConfig(rName string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "test" { + name = "TF User %[1]s" + email = "tf.%[1]s@example.com" +} + +data "pagerduty_user" "by_email" { + email = "${pagerduty_user.test.email}" +} +`, rName) +} diff --git a/builtin/providers/pagerduty/provider.go b/builtin/providers/pagerduty/provider.go index 93a09ebb0..c117f8dff 100644 --- a/builtin/providers/pagerduty/provider.go +++ b/builtin/providers/pagerduty/provider.go @@ -19,6 +19,7 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ + "pagerduty_user": dataSourcePagerDutyUser(), "pagerduty_vendor": dataSourcePagerDutyVendor(), }, diff --git a/website/source/docs/providers/pagerduty/d/user.html.markdown b/website/source/docs/providers/pagerduty/d/user.html.markdown new file mode 100644 index 000000000..4ab713745 --- /dev/null +++ b/website/source/docs/providers/pagerduty/d/user.html.markdown @@ -0,0 +1,44 @@ +--- +layout: "pagerduty" +page_title: "PagerDuty: pagerduty_user" +sidebar_current: "docs-pagerduty-datasource-user" +description: |- + Get information about a user that you can use for a service integration (e.g Amazon Cloudwatch, Splunk, Datadog). +--- + +# pagerduty\_user + +Use this data source to get information about a specific [user][1] that you can use for other Pager Duty resources. + +## Example Usage + +``` +data "pagerduty_user" "me" { + email = "me@example.com" +} + +resource "pagerduty_escalation_policy" "foo" { + name = "Engineering Escalation Policy" + num_loops = 2 + + rule { + escalation_delay_in_minutes = 10 + + target { + type = "user" + id = "${data.pagerduty_user.me.id}" + } + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `email` - (Required) The email to use to find a user in the PagerDuty API. + +## Attributes Reference +* `name` - The short name of the found user. + +[1]: https://v2.developer.pagerduty.com/v2/page/api-reference#!/Users/get_users diff --git a/website/source/layouts/pagerduty.erb b/website/source/layouts/pagerduty.erb index 66d02fade..a34e5df6a 100644 --- a/website/source/layouts/pagerduty.erb +++ b/website/source/layouts/pagerduty.erb @@ -13,6 +13,9 @@ > Data Sources