|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceGithubEnterpriseSCIMUsers() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Description: "Lookup SCIM users provisioned for a GitHub enterprise.", |
| 16 | + ReadContext: dataSourceGithubEnterpriseSCIMUsersRead, |
| 17 | + |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "enterprise": { |
| 20 | + Description: "The enterprise slug.", |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + }, |
| 24 | + "filter": { |
| 25 | + Description: "Optional SCIM filter. See GitHub SCIM enterprise docs for supported filters.", |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + }, |
| 29 | + "results_per_page": { |
| 30 | + Description: "Number of results per request (mapped to SCIM 'count'). Used while auto-fetching all pages.", |
| 31 | + Type: schema.TypeInt, |
| 32 | + Optional: true, |
| 33 | + Default: 100, |
| 34 | + ValidateFunc: validation.IntBetween(1, 100), |
| 35 | + }, |
| 36 | + |
| 37 | + "schemas": { |
| 38 | + Description: "SCIM response schemas.", |
| 39 | + Type: schema.TypeList, |
| 40 | + Computed: true, |
| 41 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 42 | + }, |
| 43 | + "total_results": { |
| 44 | + Description: "The total number of results returned by the SCIM endpoint.", |
| 45 | + Type: schema.TypeInt, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "start_index": { |
| 49 | + Description: "The startIndex from the first SCIM page.", |
| 50 | + Type: schema.TypeInt, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + "items_per_page": { |
| 54 | + Description: "The itemsPerPage from the first SCIM page.", |
| 55 | + Type: schema.TypeInt, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "resources": { |
| 59 | + Description: "All SCIM users.", |
| 60 | + Type: schema.TypeList, |
| 61 | + Computed: true, |
| 62 | + Elem: &schema.Resource{ |
| 63 | + Schema: enterpriseSCIMUserSchema(), |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func dataSourceGithubEnterpriseSCIMUsersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 71 | + client := meta.(*Owner).v3client |
| 72 | + |
| 73 | + enterprise := d.Get("enterprise").(string) |
| 74 | + filter := d.Get("filter").(string) |
| 75 | + count := d.Get("results_per_page").(int) |
| 76 | + |
| 77 | + users, first, err := enterpriseSCIMListAllUsers(ctx, client, enterprise, filter, count) |
| 78 | + if err != nil { |
| 79 | + return diag.FromErr(err) |
| 80 | + } |
| 81 | + |
| 82 | + flat := make([]any, 0, len(users)) |
| 83 | + for _, u := range users { |
| 84 | + flat = append(flat, flattenEnterpriseSCIMUser(u)) |
| 85 | + } |
| 86 | + |
| 87 | + id := fmt.Sprintf("%s/scim-users", enterprise) |
| 88 | + if filter != "" { |
| 89 | + id = fmt.Sprintf("%s?filter=%s", id, url.QueryEscape(filter)) |
| 90 | + } |
| 91 | + |
| 92 | + d.SetId(id) |
| 93 | + |
| 94 | + if err := d.Set("schemas", first.Schemas); err != nil { |
| 95 | + return diag.FromErr(err) |
| 96 | + } |
| 97 | + if first.TotalResults != nil { |
| 98 | + if err := d.Set("total_results", *first.TotalResults); err != nil { |
| 99 | + return diag.FromErr(err) |
| 100 | + } |
| 101 | + } |
| 102 | + startIndex := 1 |
| 103 | + if first.StartIndex != nil && *first.StartIndex > 0 { |
| 104 | + startIndex = *first.StartIndex |
| 105 | + } |
| 106 | + if err := d.Set("start_index", startIndex); err != nil { |
| 107 | + return diag.FromErr(err) |
| 108 | + } |
| 109 | + itemsPerPage := count |
| 110 | + if first.ItemsPerPage != nil && *first.ItemsPerPage > 0 { |
| 111 | + itemsPerPage = *first.ItemsPerPage |
| 112 | + } |
| 113 | + if err := d.Set("items_per_page", itemsPerPage); err != nil { |
| 114 | + return diag.FromErr(err) |
| 115 | + } |
| 116 | + if err := d.Set("resources", flat); err != nil { |
| 117 | + return diag.FromErr(err) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func enterpriseSCIMUserSchema() map[string]*schema.Schema { |
| 124 | + return map[string]*schema.Schema{ |
| 125 | + "schemas": { |
| 126 | + Type: schema.TypeList, |
| 127 | + Computed: true, |
| 128 | + Description: "SCIM schemas for this user.", |
| 129 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 130 | + }, |
| 131 | + "id": { |
| 132 | + Type: schema.TypeString, |
| 133 | + Computed: true, |
| 134 | + Description: "The SCIM user ID.", |
| 135 | + }, |
| 136 | + "external_id": { |
| 137 | + Type: schema.TypeString, |
| 138 | + Computed: true, |
| 139 | + Description: "The external ID for the user.", |
| 140 | + }, |
| 141 | + "user_name": { |
| 142 | + Type: schema.TypeString, |
| 143 | + Computed: true, |
| 144 | + Description: "The SCIM userName.", |
| 145 | + }, |
| 146 | + "display_name": { |
| 147 | + Type: schema.TypeString, |
| 148 | + Computed: true, |
| 149 | + Description: "The SCIM displayName.", |
| 150 | + }, |
| 151 | + "active": { |
| 152 | + Type: schema.TypeBool, |
| 153 | + Computed: true, |
| 154 | + Description: "Whether the user is active.", |
| 155 | + }, |
| 156 | + "name": { |
| 157 | + Type: schema.TypeList, |
| 158 | + Computed: true, |
| 159 | + Description: "User name object.", |
| 160 | + Elem: &schema.Resource{Schema: enterpriseSCIMUserNameSchema()}, |
| 161 | + }, |
| 162 | + "emails": { |
| 163 | + Type: schema.TypeList, |
| 164 | + Computed: true, |
| 165 | + Description: "User emails.", |
| 166 | + Elem: &schema.Resource{Schema: enterpriseSCIMUserEmailSchema()}, |
| 167 | + }, |
| 168 | + "roles": { |
| 169 | + Type: schema.TypeList, |
| 170 | + Computed: true, |
| 171 | + Description: "User roles.", |
| 172 | + Elem: &schema.Resource{Schema: enterpriseSCIMUserRoleSchema()}, |
| 173 | + }, |
| 174 | + "meta": { |
| 175 | + Type: schema.TypeList, |
| 176 | + Computed: true, |
| 177 | + Description: "Resource metadata.", |
| 178 | + Elem: &schema.Resource{Schema: enterpriseSCIMMetaSchema()}, |
| 179 | + }, |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func enterpriseSCIMUserNameSchema() map[string]*schema.Schema { |
| 184 | + return map[string]*schema.Schema{ |
| 185 | + "formatted": { |
| 186 | + Type: schema.TypeString, |
| 187 | + Computed: true, |
| 188 | + Description: "Formatted name.", |
| 189 | + }, |
| 190 | + "family_name": { |
| 191 | + Type: schema.TypeString, |
| 192 | + Computed: true, |
| 193 | + Description: "Family name.", |
| 194 | + }, |
| 195 | + "given_name": { |
| 196 | + Type: schema.TypeString, |
| 197 | + Computed: true, |
| 198 | + Description: "Given name.", |
| 199 | + }, |
| 200 | + "middle_name": { |
| 201 | + Type: schema.TypeString, |
| 202 | + Computed: true, |
| 203 | + Description: "Middle name.", |
| 204 | + }, |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +func enterpriseSCIMUserEmailSchema() map[string]*schema.Schema { |
| 209 | + return map[string]*schema.Schema{ |
| 210 | + "value": { |
| 211 | + Type: schema.TypeString, |
| 212 | + Computed: true, |
| 213 | + Description: "Email address.", |
| 214 | + }, |
| 215 | + "type": { |
| 216 | + Type: schema.TypeString, |
| 217 | + Computed: true, |
| 218 | + Description: "Email type.", |
| 219 | + }, |
| 220 | + "primary": { |
| 221 | + Type: schema.TypeBool, |
| 222 | + Computed: true, |
| 223 | + Description: "Whether this email is primary.", |
| 224 | + }, |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +func enterpriseSCIMUserRoleSchema() map[string]*schema.Schema { |
| 229 | + return map[string]*schema.Schema{ |
| 230 | + "value": { |
| 231 | + Type: schema.TypeString, |
| 232 | + Computed: true, |
| 233 | + Description: "Role value.", |
| 234 | + }, |
| 235 | + "display": { |
| 236 | + Type: schema.TypeString, |
| 237 | + Computed: true, |
| 238 | + Description: "Role display.", |
| 239 | + }, |
| 240 | + "type": { |
| 241 | + Type: schema.TypeString, |
| 242 | + Computed: true, |
| 243 | + Description: "Role type.", |
| 244 | + }, |
| 245 | + "primary": { |
| 246 | + Type: schema.TypeBool, |
| 247 | + Computed: true, |
| 248 | + Description: "Whether this role is primary.", |
| 249 | + }, |
| 250 | + } |
| 251 | +} |
0 commit comments