|
| 1 | +package rocketmq |
| 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 RocketMQ GET /v2/available-zones |
| 18 | +func DataSourceRocketMQAvailabilityZones() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + ReadContext: dataSourceRocketMQAvailabilityZonesRead, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "region": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Computed: true, |
| 27 | + Description: "The region where the availability zones are located.", |
| 28 | + }, |
| 29 | + "availability_zones": { |
| 30 | + Type: schema.TypeList, |
| 31 | + Computed: true, |
| 32 | + Elem: &schema.Resource{ |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + Description: "The ID of the availability zone.", |
| 38 | + }, |
| 39 | + "name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "The name of the availability zone.", |
| 43 | + }, |
| 44 | + "code": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: "The code of the availability zone.", |
| 48 | + }, |
| 49 | + "port": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "The port number of the availability zone.", |
| 53 | + }, |
| 54 | + "sold_out": { |
| 55 | + Type: schema.TypeBool, |
| 56 | + Computed: true, |
| 57 | + Description: "Whether the availability zone is sold out.", |
| 58 | + }, |
| 59 | + "resource_availability": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Whether there are available resources in the availability zone.", |
| 63 | + }, |
| 64 | + "default_az": { |
| 65 | + Type: schema.TypeBool, |
| 66 | + Computed: true, |
| 67 | + Description: "Whether the availability zone is default.", |
| 68 | + }, |
| 69 | + "remain_time": { |
| 70 | + Type: schema.TypeFloat, |
| 71 | + Computed: true, |
| 72 | + Description: "The remaining time of the availability zone.", |
| 73 | + }, |
| 74 | + "ipv6_enable": { |
| 75 | + Type: schema.TypeBool, |
| 76 | + Computed: true, |
| 77 | + Description: "Whether IPv6 is supported in the availability zone.", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + Description: "The list of availability zones.", |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func dataSourceRocketMQAvailabilityZonesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 88 | + var ( |
| 89 | + cfg = meta.(*config.Config) |
| 90 | + region = cfg.GetRegion(d) |
| 91 | + httpUrl = "v2/available-zones" |
| 92 | + listOpt = golangsdk.RequestOpts{ |
| 93 | + KeepResponseBody: true, |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + client, err := cfg.NewServiceClient("dmsv2", region) |
| 98 | + if err != nil { |
| 99 | + return diag.Errorf("error creating DMS client: %s", err) |
| 100 | + } |
| 101 | + |
| 102 | + listPath := client.Endpoint + httpUrl |
| 103 | + resp, err := client.Request("GET", listPath, &listOpt) |
| 104 | + if err != nil { |
| 105 | + return diag.Errorf("error querying availability zones: %s", err) |
| 106 | + } |
| 107 | + |
| 108 | + respBody, err := utils.FlattenResponse(resp) |
| 109 | + if err != nil { |
| 110 | + return diag.FromErr(err) |
| 111 | + } |
| 112 | + |
| 113 | + randomUUID, err := uuid.GenerateUUID() |
| 114 | + if err != nil { |
| 115 | + return diag.Errorf("unable to generate ID: %s", err) |
| 116 | + } |
| 117 | + |
| 118 | + d.SetId(randomUUID) |
| 119 | + |
| 120 | + mErr := multierror.Append( |
| 121 | + d.Set("region", region), |
| 122 | + d.Set("availability_zones", flattenRocketMQAvailabilityZones(utils.PathSearch("available_zones", |
| 123 | + respBody, make([]interface{}, 0)).([]interface{}))), |
| 124 | + ) |
| 125 | + |
| 126 | + return diag.FromErr(mErr.ErrorOrNil()) |
| 127 | +} |
| 128 | + |
| 129 | +func flattenRocketMQAvailabilityZones(azs []interface{}) []map[string]interface{} { |
| 130 | + result := make([]map[string]interface{}, len(azs)) |
| 131 | + for i, az := range azs { |
| 132 | + result[i] = map[string]interface{}{ |
| 133 | + "id": utils.PathSearch("id", az, nil), |
| 134 | + "name": utils.PathSearch("name", az, nil), |
| 135 | + "code": utils.PathSearch("code", az, nil), |
| 136 | + "port": utils.PathSearch("port", az, nil), |
| 137 | + "sold_out": utils.PathSearch("soldOut", az, nil), |
| 138 | + "resource_availability": utils.PathSearch("resource_availability", az, nil), |
| 139 | + "default_az": utils.PathSearch("default_az", az, nil), |
| 140 | + "remain_time": utils.PathSearch("remain_time", az, nil), |
| 141 | + "ipv6_enable": utils.PathSearch("ipv6_enable", az, nil), |
| 142 | + } |
| 143 | + } |
| 144 | + return result |
| 145 | +} |
0 commit comments