|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | + "github.com/shurcooL/githubv4" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceGithubUserExternalIdentity() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceGithubUserExternalIdentityRead, |
| 13 | + |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "username": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + }, |
| 19 | + "saml_identity": { |
| 20 | + Type: schema.TypeMap, |
| 21 | + Computed: true, |
| 22 | + Elem: &schema.Schema{ |
| 23 | + Type: schema.TypeString, |
| 24 | + }, |
| 25 | + }, |
| 26 | + "scim_identity": { |
| 27 | + Type: schema.TypeMap, |
| 28 | + Computed: true, |
| 29 | + Elem: &schema.Schema{ |
| 30 | + Type: schema.TypeString, |
| 31 | + }, |
| 32 | + }, |
| 33 | + "login": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func dataSourceGithubUserExternalIdentityRead(d *schema.ResourceData, meta interface{}) error { |
| 42 | + username := d.Get("username").(string) |
| 43 | + |
| 44 | + client := meta.(*Owner).v4client |
| 45 | + orgName := meta.(*Owner).name |
| 46 | + |
| 47 | + var query struct { |
| 48 | + Organization struct { |
| 49 | + SamlIdentityProvider struct { |
| 50 | + ExternalIdentities `graphql:"externalIdentities(first: 1, login:$username)"` // There should only ever be one external identity configured |
| 51 | + } |
| 52 | + } `graphql:"organization(login: $orgName)"` |
| 53 | + } |
| 54 | + |
| 55 | + variables := map[string]interface{}{ |
| 56 | + "orgName": githubv4.String(orgName), |
| 57 | + "username": githubv4.String(username), |
| 58 | + } |
| 59 | + |
| 60 | + err := client.Query(meta.(*Owner).StopContext, &query, variables) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + if len(query.Organization.SamlIdentityProvider.ExternalIdentities.Edges) == 0 { |
| 65 | + return fmt.Errorf("there was no external identity found for username %q in Organization %q", username, orgName) |
| 66 | + } |
| 67 | + |
| 68 | + externalIdentityNode := query.Organization.SamlIdentityProvider.ExternalIdentities.Edges[0].Node // There should only be one user in this list |
| 69 | + |
| 70 | + samlIdentity := map[string]string{ |
| 71 | + "family_name": string(externalIdentityNode.SamlIdentity.FamilyName), |
| 72 | + "given_name": string(externalIdentityNode.SamlIdentity.GivenName), |
| 73 | + "name_id": string(externalIdentityNode.SamlIdentity.NameId), |
| 74 | + "username": string(externalIdentityNode.SamlIdentity.Username), |
| 75 | + } |
| 76 | + |
| 77 | + scimIdentity := map[string]string{ |
| 78 | + "family_name": string(externalIdentityNode.ScimIdentity.FamilyName), |
| 79 | + "given_name": string(externalIdentityNode.ScimIdentity.GivenName), |
| 80 | + "username": string(externalIdentityNode.ScimIdentity.Username), |
| 81 | + } |
| 82 | + |
| 83 | + login := string(externalIdentityNode.User.Login) |
| 84 | + |
| 85 | + d.SetId(fmt.Sprintf("%s/%s", orgName, username)) |
| 86 | + d.Set("saml_identity", samlIdentity) |
| 87 | + d.Set("scim_identity", scimIdentity) |
| 88 | + d.Set("login", login) |
| 89 | + d.Set("username", login) |
| 90 | + return nil |
| 91 | +} |
0 commit comments