From 111a6053481843a1e8f287283dc7df57f4c6b13b Mon Sep 17 00:00:00 2001 From: Tom Withers Date: Fri, 11 Jun 2021 19:59:26 +0000 Subject: [PATCH 1/3] Initial Commit, still need to write tests --- pagerduty/data_source_pagerduty_team.go | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pagerduty/data_source_pagerduty_team.go b/pagerduty/data_source_pagerduty_team.go index 6cf122090..1cdc4f51a 100644 --- a/pagerduty/data_source_pagerduty_team.go +++ b/pagerduty/data_source_pagerduty_team.go @@ -28,6 +28,18 @@ func dataSourcePagerDutyTeam() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "members": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, }, } } @@ -71,6 +83,25 @@ func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error ) } + if found != nil { + mo := &pagerduty.GetMembersOptions{} + resp, _, err := client.Teams.GetMembers(searchTeam, mo) + 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) + } + + if err := d.Set("members", dataSourceMembersRead(resp.Members)); err != nil { + return fmt.Errorf("error setting users: %w", err) + } + } + d.SetId(found.ID) d.Set("name", found.Name) d.Set("description", found.Description) @@ -79,3 +110,13 @@ func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error return nil }) } + +func dataSourceMembersRead(m []*pagerduty.Member) []map[string]interface{} { + members := make([]map[string]interface{}, 0, len(m)) + for _, i := range m { + m := make(map[string]interface{}) + m["id"] = i.User.ID + members = append(members, m) + } + return members +} From a50b57632001ef63622575ff625ceb9936ea3140 Mon Sep 17 00:00:00 2001 From: Tom Withers Date: Fri, 11 Jun 2021 20:01:34 +0000 Subject: [PATCH 2/3] Update docs --- website/docs/d/team.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/docs/d/team.html.markdown b/website/docs/d/team.html.markdown index 7fa45c400..3766c5f89 100644 --- a/website/docs/d/team.html.markdown +++ b/website/docs/d/team.html.markdown @@ -50,4 +50,8 @@ The following arguments are supported: * `description` - A description of the found team. * `parent` - ID of the parent team. This is available to accounts with the Team Hierarchy feature enabled. Please contact your account manager for more information. +`members` + +* `id` - The ID specifying the PagerDuty user. + [1]: https://v1.developer.pagerduty.com/documentation/rest/teams/list From 87dfaed77d31d4753bc0b1e47ac2849e5a242531 Mon Sep 17 00:00:00 2001 From: Tom Withers Date: Fri, 11 Jun 2021 20:06:46 +0000 Subject: [PATCH 3/3] return RetryableError --- pagerduty/data_source_pagerduty_team.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pagerduty/data_source_pagerduty_team.go b/pagerduty/data_source_pagerduty_team.go index 1cdc4f51a..b9bacf529 100644 --- a/pagerduty/data_source_pagerduty_team.go +++ b/pagerduty/data_source_pagerduty_team.go @@ -98,7 +98,7 @@ func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error } if err := d.Set("members", dataSourceMembersRead(resp.Members)); err != nil { - return fmt.Errorf("error setting users: %w", err) + return resource.RetryableError(err) } }