|
| 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 dataSourceGithubEnterpriseSCIMGroups() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Description: "Lookup SCIM groups provisioned for a GitHub enterprise.", |
| 16 | + ReadContext: dataSourceGithubEnterpriseSCIMGroupsRead, |
| 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 groups.", |
| 60 | + Type: schema.TypeList, |
| 61 | + Computed: true, |
| 62 | + Elem: &schema.Resource{ |
| 63 | + Schema: enterpriseSCIMGroupSchema(), |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func dataSourceGithubEnterpriseSCIMGroupsRead(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 | + groups, first, err := enterpriseSCIMListAllGroups(ctx, client, enterprise, filter, count) |
| 78 | + if err != nil { |
| 79 | + return diag.FromErr(err) |
| 80 | + } |
| 81 | + |
| 82 | + flat := make([]any, 0, len(groups)) |
| 83 | + for _, g := range groups { |
| 84 | + flat = append(flat, flattenEnterpriseSCIMGroup(g)) |
| 85 | + } |
| 86 | + |
| 87 | + id := fmt.Sprintf("%s/scim-groups", 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 enterpriseSCIMMetaSchema() map[string]*schema.Schema { |
| 124 | + return map[string]*schema.Schema{ |
| 125 | + "resource_type": { |
| 126 | + Type: schema.TypeString, |
| 127 | + Computed: true, |
| 128 | + Description: "The SCIM resource type.", |
| 129 | + }, |
| 130 | + "created": { |
| 131 | + Type: schema.TypeString, |
| 132 | + Computed: true, |
| 133 | + Description: "The creation timestamp.", |
| 134 | + }, |
| 135 | + "last_modified": { |
| 136 | + Type: schema.TypeString, |
| 137 | + Computed: true, |
| 138 | + Description: "The lastModified timestamp.", |
| 139 | + }, |
| 140 | + "location": { |
| 141 | + Type: schema.TypeString, |
| 142 | + Computed: true, |
| 143 | + Description: "The resource location.", |
| 144 | + }, |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func enterpriseSCIMGroupSchema() map[string]*schema.Schema { |
| 149 | + return map[string]*schema.Schema{ |
| 150 | + "schemas": { |
| 151 | + Type: schema.TypeList, |
| 152 | + Computed: true, |
| 153 | + Description: "SCIM schemas for this group.", |
| 154 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 155 | + }, |
| 156 | + "id": { |
| 157 | + Type: schema.TypeString, |
| 158 | + Computed: true, |
| 159 | + Description: "The SCIM group ID.", |
| 160 | + }, |
| 161 | + "external_id": { |
| 162 | + Type: schema.TypeString, |
| 163 | + Computed: true, |
| 164 | + Description: "The external ID for the group.", |
| 165 | + }, |
| 166 | + "display_name": { |
| 167 | + Type: schema.TypeString, |
| 168 | + Computed: true, |
| 169 | + Description: "The SCIM group displayName.", |
| 170 | + }, |
| 171 | + "members": { |
| 172 | + Type: schema.TypeList, |
| 173 | + Computed: true, |
| 174 | + Description: "Group members.", |
| 175 | + Elem: &schema.Resource{Schema: enterpriseSCIMGroupMemberSchema()}, |
| 176 | + }, |
| 177 | + "meta": { |
| 178 | + Type: schema.TypeList, |
| 179 | + Computed: true, |
| 180 | + Description: "Resource metadata.", |
| 181 | + Elem: &schema.Resource{Schema: enterpriseSCIMMetaSchema()}, |
| 182 | + }, |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func enterpriseSCIMGroupMemberSchema() map[string]*schema.Schema { |
| 187 | + return map[string]*schema.Schema{ |
| 188 | + "value": { |
| 189 | + Type: schema.TypeString, |
| 190 | + Computed: true, |
| 191 | + Description: "Member identifier.", |
| 192 | + }, |
| 193 | + "ref": { |
| 194 | + Type: schema.TypeString, |
| 195 | + Computed: true, |
| 196 | + Description: "Member reference URL.", |
| 197 | + }, |
| 198 | + "display_name": { |
| 199 | + Type: schema.TypeString, |
| 200 | + Computed: true, |
| 201 | + Description: "Member display name.", |
| 202 | + }, |
| 203 | + } |
| 204 | +} |
0 commit comments