forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_organization_external_identities.go
More file actions
131 lines (116 loc) · 3.16 KB
/
data_source_github_organization_external_identities.go
File metadata and controls
131 lines (116 loc) · 3.16 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package github
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/shurcooL/githubv4"
)
type ExternalIdentities struct {
Edges []struct {
Node struct {
User struct {
Login githubv4.String
}
SamlIdentity struct {
NameId githubv4.String
Username githubv4.String
GivenName githubv4.String
FamilyName githubv4.String
}
ScimIdentity struct {
Username githubv4.String
GivenName githubv4.String
FamilyName githubv4.String
}
}
}
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
}
func dataSourceGithubOrganizationExternalIdentities() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubOrganizationExternalIdentitiesRead,
Schema: map[string]*schema.Schema{
"identities": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"login": {
Type: schema.TypeString,
Computed: true,
},
"saml_identity": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"scim_identity": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func dataSourceGithubOrganizationExternalIdentitiesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
name := meta.(*Owner).name
client4 := meta.(*Owner).v4client
var query struct {
Organization struct {
SamlIdentityProvider struct {
ExternalIdentities `graphql:"externalIdentities(first: 100, after: $after)"`
}
} `graphql:"organization(login: $login)"`
}
variables := map[string]any{
"login": githubv4.String(name),
"after": (*githubv4.String)(nil),
}
var identities []map[string]any
for {
err := client4.Query(ctx, &query, variables)
if err != nil {
return diag.FromErr(err)
}
for _, edge := range query.Organization.SamlIdentityProvider.Edges {
identity := map[string]any{
"login": string(edge.Node.User.Login),
"saml_identity": nil,
"scim_identity": nil,
}
if edge.Node.SamlIdentity.NameId != "" {
identity["saml_identity"] = map[string]string{
"name_id": string(edge.Node.SamlIdentity.NameId),
"username": string(edge.Node.SamlIdentity.Username),
"given_name": string(edge.Node.SamlIdentity.GivenName),
"family_name": string(edge.Node.SamlIdentity.FamilyName),
}
}
if edge.Node.ScimIdentity.Username != "" {
identity["scim_identity"] = map[string]string{
"username": string(edge.Node.ScimIdentity.Username),
"given_name": string(edge.Node.ScimIdentity.GivenName),
"family_name": string(edge.Node.ScimIdentity.FamilyName),
}
}
identities = append(identities, identity)
}
if !query.Organization.SamlIdentityProvider.PageInfo.HasNextPage {
break
}
variables["after"] = new(query.Organization.SamlIdentityProvider.PageInfo.EndCursor)
}
d.SetId(name)
_ = d.Set("identities", identities)
return nil
}