|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 5 | + "github.com/shurcooL/githubv4" |
| 6 | +) |
| 7 | + |
| 8 | +func dataSourceGithubOrganizationExternalIdentities() *schema.Resource { |
| 9 | + return &schema.Resource{ |
| 10 | + Read: dataSourceGithubOrganizationExternalIdentitiesRead, |
| 11 | + |
| 12 | + Schema: map[string]*schema.Schema{ |
| 13 | + "identities": { |
| 14 | + Type: schema.TypeList, |
| 15 | + Computed: true, |
| 16 | + Elem: &schema.Resource{ |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "login": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Computed: true, |
| 21 | + }, |
| 22 | + "saml_identity": { |
| 23 | + Type: schema.TypeMap, |
| 24 | + Computed: true, |
| 25 | + Elem: &schema.Schema{ |
| 26 | + Type: schema.TypeString, |
| 27 | + }, |
| 28 | + }, |
| 29 | + "scim_identity": { |
| 30 | + Type: schema.TypeMap, |
| 31 | + Computed: true, |
| 32 | + Elem: &schema.Schema{ |
| 33 | + Type: schema.TypeString, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func dataSourceGithubOrganizationExternalIdentitiesRead(d *schema.ResourceData, meta interface{}) error { |
| 44 | + name := meta.(*Owner).name |
| 45 | + |
| 46 | + client4 := meta.(*Owner).v4client |
| 47 | + ctx := meta.(*Owner).StopContext |
| 48 | + |
| 49 | + var query struct { |
| 50 | + Organization struct { |
| 51 | + SamlIdentityProvider struct { |
| 52 | + ExternalIdentities struct { |
| 53 | + Edges []struct { |
| 54 | + Node struct { |
| 55 | + User struct { |
| 56 | + Login githubv4.String |
| 57 | + } |
| 58 | + SamlIdentity struct { |
| 59 | + NameId githubv4.String |
| 60 | + Username githubv4.String |
| 61 | + GivenName githubv4.String |
| 62 | + FamilyName githubv4.String |
| 63 | + } |
| 64 | + ScimIdentity struct { |
| 65 | + Username githubv4.String |
| 66 | + GivenName githubv4.String |
| 67 | + FamilyName githubv4.String |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + PageInfo struct { |
| 72 | + EndCursor githubv4.String |
| 73 | + HasNextPage bool |
| 74 | + } |
| 75 | + } `graphql:"externalIdentities(first: 100, after: $after)"` |
| 76 | + } |
| 77 | + } `graphql:"organization(login: $login)"` |
| 78 | + } |
| 79 | + variables := map[string]interface{}{ |
| 80 | + "login": githubv4.String(name), |
| 81 | + "after": (*githubv4.String)(nil), |
| 82 | + } |
| 83 | + |
| 84 | + var identities []map[string]interface{} |
| 85 | + |
| 86 | + for { |
| 87 | + err := client4.Query(ctx, &query, variables) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + for _, edge := range query.Organization.SamlIdentityProvider.ExternalIdentities.Edges { |
| 92 | + identity := map[string]interface{}{ |
| 93 | + "login": string(edge.Node.User.Login), |
| 94 | + "saml_identity": nil, |
| 95 | + "scim_identity": nil, |
| 96 | + } |
| 97 | + |
| 98 | + if edge.Node.SamlIdentity.NameId != "" { |
| 99 | + identity["saml_identity"] = map[string]string{ |
| 100 | + "name_id": string(edge.Node.SamlIdentity.NameId), |
| 101 | + "username": string(edge.Node.SamlIdentity.Username), |
| 102 | + "given_name": string(edge.Node.SamlIdentity.GivenName), |
| 103 | + "family_name": string(edge.Node.SamlIdentity.FamilyName), |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if edge.Node.ScimIdentity.Username != "" { |
| 108 | + identity["scim_identity"] = map[string]string{ |
| 109 | + "username": string(edge.Node.ScimIdentity.Username), |
| 110 | + "given_name": string(edge.Node.ScimIdentity.GivenName), |
| 111 | + "family_name": string(edge.Node.ScimIdentity.FamilyName), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + identities = append(identities, identity) |
| 116 | + } |
| 117 | + if !query.Organization.SamlIdentityProvider.ExternalIdentities.PageInfo.HasNextPage { |
| 118 | + break |
| 119 | + } |
| 120 | + variables["after"] = githubv4.NewString(query.Organization.SamlIdentityProvider.ExternalIdentities.PageInfo.EndCursor) |
| 121 | + } |
| 122 | + |
| 123 | + d.SetId(name) |
| 124 | + d.Set("identities", identities) |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
0 commit comments