|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceGithubEnterpriseCostCenters() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + ReadContext: dataSourceGithubEnterpriseCostCentersRead, |
| 15 | + |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "enterprise_slug": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + Description: "The slug of the enterprise.", |
| 21 | + }, |
| 22 | + "state": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Optional: true, |
| 25 | + ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{"active", "deleted"}, false), "state"), |
| 26 | + Description: "Filter cost centers by state.", |
| 27 | + }, |
| 28 | + "cost_centers": { |
| 29 | + Type: schema.TypeSet, |
| 30 | + Computed: true, |
| 31 | + Elem: &schema.Resource{ |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "state": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "azure_subscription": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "resources": { |
| 50 | + Type: schema.TypeList, |
| 51 | + Computed: true, |
| 52 | + Elem: &schema.Resource{ |
| 53 | + Schema: map[string]*schema.Schema{ |
| 54 | + "type": {Type: schema.TypeString, Computed: true}, |
| 55 | + "name": {Type: schema.TypeString, Computed: true}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func dataSourceGithubEnterpriseCostCentersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 67 | + client := meta.(*Owner).v3client |
| 68 | + enterpriseSlug := d.Get("enterprise_slug").(string) |
| 69 | + state := "" |
| 70 | + if v, ok := d.GetOk("state"); ok { |
| 71 | + state = v.(string) |
| 72 | + } |
| 73 | + |
| 74 | + ctx = context.WithValue(ctx, ctxId, fmt.Sprintf("%s/cost-centers", enterpriseSlug)) |
| 75 | + centers, err := enterpriseCostCentersList(ctx, client, enterpriseSlug, state) |
| 76 | + if err != nil { |
| 77 | + return diag.FromErr(err) |
| 78 | + } |
| 79 | + |
| 80 | + items := make([]any, 0, len(centers)) |
| 81 | + for _, cc := range centers { |
| 82 | + resources := make([]map[string]any, 0) |
| 83 | + for _, r := range cc.Resources { |
| 84 | + resources = append(resources, map[string]any{"type": r.Type, "name": r.Name}) |
| 85 | + } |
| 86 | + items = append(items, map[string]any{ |
| 87 | + "id": cc.ID, |
| 88 | + "name": cc.Name, |
| 89 | + "state": cc.State, |
| 90 | + "azure_subscription": cc.AzureSubscription, |
| 91 | + "resources": resources, |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + d.SetId(fmt.Sprintf("%s/%s", enterpriseSlug, state)) |
| 96 | + _ = d.Set("cost_centers", items) |
| 97 | + return nil |
| 98 | +} |
0 commit comments