-
Notifications
You must be signed in to change notification settings - Fork 951
Expand file tree
/
Copy pathdata_source_github_team_external_groups.go
More file actions
82 lines (72 loc) · 1.89 KB
/
data_source_github_team_external_groups.go
File metadata and controls
82 lines (72 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package github
import (
"context"
"encoding/json"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubTeamExternalGroups() *schema.Resource {
return &schema.Resource{
Description: "Retrieve external groups for a specific GitHub team.",
ReadContext: dataSourceGithubTeamExternalGroupsRead,
Schema: map[string]*schema.Schema{
"slug": {
Type: schema.TypeString,
Required: true,
Description: "The slug of the GitHub team.",
},
"external_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeInt,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceGithubTeamExternalGroupsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
err := checkOrganization(meta)
if err != nil {
return diag.FromErr(err)
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
slug := d.Get("slug").(string)
externalGroups, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, slug)
if err != nil {
return diag.FromErr(err)
}
// convert to JSON in order to marshal to format we can return
jsonGroups, err := json.Marshal(externalGroups.Groups)
if err != nil {
return diag.FromErr(err)
}
groupsState := make([]map[string]any, 0)
err = json.Unmarshal(jsonGroups, &groupsState)
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("external_groups", groupsState); err != nil {
return diag.FromErr(err)
}
id, err := buildID(orgName, slug)
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
return nil
}