-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pagerduty_user data source (#10541)
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
64 changes: 64 additions & 0 deletions
64
builtin/providers/pagerduty/data_source_pagerduty_user_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,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][email protected]" | ||
} | ||
data "pagerduty_user" "by_email" { | ||
email = "${pagerduty_user.test.email}" | ||
} | ||
`, rName) | ||
} |
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
44 changes: 44 additions & 0 deletions
44
website/source/docs/providers/pagerduty/d/user.html.markdown
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,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 = "[email protected]" | ||
} | ||
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 |
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