|
| 1 | +package eps |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/go-multierror" |
| 7 | + "github.com/hashicorp/go-uuid" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + |
| 11 | + "github.com/chnsz/golangsdk" |
| 12 | + |
| 13 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 15 | +) |
| 16 | + |
| 17 | +// @API EPS GET /v1.0/enterprise-projects/quotas |
| 18 | +func DataSourceQuotas() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + ReadContext: dataSourceQuotasRead, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "region": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Computed: true, |
| 27 | + Description: `The region in which to query the resource quotas.`, |
| 28 | + }, |
| 29 | + |
| 30 | + // Attributes. |
| 31 | + "quotas": { |
| 32 | + Type: schema.TypeList, |
| 33 | + Computed: true, |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "quota": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Computed: true, |
| 39 | + Description: `The total number of the resource quota.`, |
| 40 | + }, |
| 41 | + "type": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + Description: `The resource type corresponding to quota.`, |
| 45 | + }, |
| 46 | + "used": { |
| 47 | + Type: schema.TypeInt, |
| 48 | + Computed: true, |
| 49 | + Description: `The used quota number.`, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + Description: `The list of the resource quotas.`, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func listResourceQuotas(client *golangsdk.ServiceClient, d *schema.ResourceData) ([]interface{}, error) { |
| 60 | + httpUrl := "v1.0/enterprise-projects/quotas" |
| 61 | + listPath := client.Endpoint + httpUrl |
| 62 | + |
| 63 | + opt := golangsdk.RequestOpts{ |
| 64 | + KeepResponseBody: true, |
| 65 | + MoreHeaders: map[string]string{ |
| 66 | + "Content-Type": "application/json", |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + requestResp, err := client.Request("GET", listPath, &opt) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + respBody, err := utils.FlattenResponse(requestResp) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + return utils.PathSearch("quotas.resources", respBody, make([]interface{}, 0)).([]interface{}), nil |
| 80 | +} |
| 81 | + |
| 82 | +func flattenResourceQuotas(resources []interface{}) []map[string]interface{} { |
| 83 | + if len(resources) < 1 { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + result := make([]map[string]interface{}, 0, len(resources)) |
| 88 | + for _, resource := range resources { |
| 89 | + result = append(result, map[string]interface{}{ |
| 90 | + "quota": utils.PathSearch("quota", resource, nil), |
| 91 | + "type": utils.PathSearch("type", resource, nil), |
| 92 | + "used": utils.PathSearch("used", resource, nil), |
| 93 | + }) |
| 94 | + } |
| 95 | + |
| 96 | + return result |
| 97 | +} |
| 98 | + |
| 99 | +func dataSourceQuotasRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 100 | + var ( |
| 101 | + cfg = meta.(*config.Config) |
| 102 | + region = cfg.GetRegion(d) |
| 103 | + ) |
| 104 | + client, err := cfg.NewServiceClient("eps", region) |
| 105 | + if err != nil { |
| 106 | + return diag.Errorf("error creating EPS client: %s", err) |
| 107 | + } |
| 108 | + |
| 109 | + staticRoutes, err := listResourceQuotas(client, d) |
| 110 | + if err != nil { |
| 111 | + return diag.Errorf("error querying resource quotas: %s", err) |
| 112 | + } |
| 113 | + |
| 114 | + randUUID, err := uuid.GenerateUUID() |
| 115 | + if err != nil { |
| 116 | + return diag.Errorf("unable to generate ID: %s", err) |
| 117 | + } |
| 118 | + d.SetId(randUUID) |
| 119 | + |
| 120 | + mErr := multierror.Append(nil, |
| 121 | + d.Set("region", region), |
| 122 | + d.Set("quotas", flattenResourceQuotas(staticRoutes)), |
| 123 | + ) |
| 124 | + |
| 125 | + if mErr.ErrorOrNil() != nil { |
| 126 | + return diag.Errorf("error saving data source fields of the static routes: %s", mErr) |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
0 commit comments