|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/google/go-github/v81/github" |
| 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 | + Description: "Use this data source to retrieve a list of enterprise cost centers.", |
| 15 | + ReadContext: dataSourceGithubEnterpriseCostCentersRead, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "enterprise_slug": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + Description: "The slug of the enterprise.", |
| 22 | + }, |
| 23 | + "state": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"active", "deleted"}, false)), |
| 27 | + Description: "Filter cost centers by state.", |
| 28 | + }, |
| 29 | + "cost_centers": { |
| 30 | + Type: schema.TypeSet, |
| 31 | + Computed: true, |
| 32 | + Description: "The list of cost centers.", |
| 33 | + Elem: &schema.Resource{ |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: "The cost center ID.", |
| 39 | + }, |
| 40 | + "name": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "The name of the cost center.", |
| 44 | + }, |
| 45 | + "state": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "The state of the cost center.", |
| 49 | + }, |
| 50 | + "azure_subscription": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "The Azure subscription associated with the cost center.", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func dataSourceGithubEnterpriseCostCentersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 63 | + client := meta.(*Owner).v3client |
| 64 | + enterpriseSlug := d.Get("enterprise_slug").(string) |
| 65 | + var state *string |
| 66 | + if v, ok := d.GetOk("state"); ok { |
| 67 | + state = github.Ptr(v.(string)) |
| 68 | + } |
| 69 | + |
| 70 | + result, _, err := client.Enterprise.ListCostCenters(ctx, enterpriseSlug, &github.ListCostCenterOptions{State: state}) |
| 71 | + if err != nil { |
| 72 | + return diag.FromErr(err) |
| 73 | + } |
| 74 | + |
| 75 | + items := make([]any, 0, len(result.CostCenters)) |
| 76 | + for _, cc := range result.CostCenters { |
| 77 | + if cc == nil { |
| 78 | + continue |
| 79 | + } |
| 80 | + items = append(items, map[string]any{ |
| 81 | + "id": cc.ID, |
| 82 | + "name": cc.Name, |
| 83 | + "state": cc.GetState(), |
| 84 | + "azure_subscription": cc.GetAzureSubscription(), |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + stateStr := "all" |
| 89 | + if state != nil { |
| 90 | + stateStr = *state |
| 91 | + } |
| 92 | + d.SetId(buildTwoPartID(enterpriseSlug, stateStr)) |
| 93 | + if err := d.Set("cost_centers", items); err != nil { |
| 94 | + return diag.FromErr(err) |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
0 commit comments