/
data_source_github_user_external_identity.go
91 lines (77 loc) · 2.54 KB
/
data_source_github_user_external_identity.go
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
package github
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/shurcooL/githubv4"
)
func dataSourceGithubUserExternalIdentity() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubUserExternalIdentityRead,
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: 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,
},
},
"login": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceGithubUserExternalIdentityRead(d *schema.ResourceData, meta interface{}) error {
username := d.Get("username").(string)
client := meta.(*Owner).v4client
orgName := meta.(*Owner).name
var query struct {
Organization struct {
SamlIdentityProvider struct {
ExternalIdentities `graphql:"externalIdentities(first: 1, login:$username)"` // There should only ever be one external identity configured
}
} `graphql:"organization(login: $orgName)"`
}
variables := map[string]interface{}{
"orgName": githubv4.String(orgName),
"username": githubv4.String(username),
}
err := client.Query(meta.(*Owner).StopContext, &query, variables)
if err != nil {
return err
}
if len(query.Organization.SamlIdentityProvider.ExternalIdentities.Edges) == 0 {
return fmt.Errorf("there was no external identity found for username %q in Organization %q", username, orgName)
}
externalIdentityNode := query.Organization.SamlIdentityProvider.ExternalIdentities.Edges[0].Node // There should only be one user in this list
samlIdentity := map[string]string{
"family_name": string(externalIdentityNode.SamlIdentity.FamilyName),
"given_name": string(externalIdentityNode.SamlIdentity.GivenName),
"name_id": string(externalIdentityNode.SamlIdentity.NameId),
"username": string(externalIdentityNode.SamlIdentity.Username),
}
scimIdentity := map[string]string{
"family_name": string(externalIdentityNode.ScimIdentity.FamilyName),
"given_name": string(externalIdentityNode.ScimIdentity.GivenName),
"username": string(externalIdentityNode.ScimIdentity.Username),
}
login := string(externalIdentityNode.User.Login)
d.SetId(fmt.Sprintf("%s/%s", orgName, username))
d.Set("saml_identity", samlIdentity)
d.Set("scim_identity", scimIdentity)
d.Set("login", login)
d.Set("username", login)
return nil
}