|
| 1 | +package workspace |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-multierror" |
| 8 | + "github.com/hashicorp/go-uuid" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + |
| 12 | + "github.com/chnsz/golangsdk" |
| 13 | + |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 16 | +) |
| 17 | + |
| 18 | +// @API Workspace GET /v2/{project_id}/common/timezones |
| 19 | +func DataSourceTimeZones() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + ReadContext: dataSourceTimeZonesRead, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "region": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Computed: true, |
| 27 | + Description: `The region where the time zones are located.`, |
| 28 | + }, |
| 29 | + |
| 30 | + // Attributes. |
| 31 | + "time_zones": { |
| 32 | + Type: schema.TypeList, |
| 33 | + Computed: true, |
| 34 | + Description: `The list of time zones.`, |
| 35 | + Elem: &schema.Resource{ |
| 36 | + Schema: map[string]*schema.Schema{ |
| 37 | + "name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + Description: `The name of the time zone.`, |
| 41 | + }, |
| 42 | + "offset": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: `The offset of the time zone.`, |
| 46 | + }, |
| 47 | + "us_description": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: `The English description of the time zone.`, |
| 51 | + }, |
| 52 | + "cn_description": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: `The Chinese description of the time zone.`, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Currently, the filter parameter time_zone_name is not available. |
| 65 | +func listTimeZones(client *golangsdk.ServiceClient) ([]interface{}, error) { |
| 66 | + var ( |
| 67 | + httpUrl = "v2/{project_id}/common/timezones" |
| 68 | + ) |
| 69 | + |
| 70 | + listPath := client.Endpoint + httpUrl |
| 71 | + listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID) |
| 72 | + |
| 73 | + opt := golangsdk.RequestOpts{ |
| 74 | + KeepResponseBody: true, |
| 75 | + MoreHeaders: map[string]string{ |
| 76 | + "Content-Type": "application/json", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + requestResp, err := client.Request("GET", listPath, &opt) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + respBody, err := utils.FlattenResponse(requestResp) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + return utils.PathSearch("time_zones", respBody, make([]interface{}, 0)).([]interface{}), nil |
| 90 | +} |
| 91 | + |
| 92 | +func dataSourceTimeZonesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 93 | + cfg := meta.(*config.Config) |
| 94 | + region := cfg.GetRegion(d) |
| 95 | + client, err := cfg.NewServiceClient("workspace", region) |
| 96 | + if err != nil { |
| 97 | + return diag.Errorf("error creating Workspace client: %s", err) |
| 98 | + } |
| 99 | + |
| 100 | + resp, err := listTimeZones(client) |
| 101 | + if err != nil { |
| 102 | + return diag.Errorf("error querying Workspace time zones: %s", err) |
| 103 | + } |
| 104 | + |
| 105 | + randomUUID, err := uuid.GenerateUUID() |
| 106 | + if err != nil { |
| 107 | + return diag.Errorf("unable to generate ID: %s", err) |
| 108 | + } |
| 109 | + d.SetId(randomUUID) |
| 110 | + |
| 111 | + mErr := multierror.Append(nil, |
| 112 | + d.Set("region", region), |
| 113 | + d.Set("time_zones", flattenTimeZones(resp)), |
| 114 | + ) |
| 115 | + return diag.FromErr(mErr.ErrorOrNil()) |
| 116 | +} |
| 117 | + |
| 118 | +func flattenTimeZones(items []interface{}) []map[string]interface{} { |
| 119 | + if len(items) < 1 { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + result := make([]map[string]interface{}, 0, len(items)) |
| 124 | + for _, item := range items { |
| 125 | + result = append(result, map[string]interface{}{ |
| 126 | + "name": utils.PathSearch("time_zone_name", item, nil), |
| 127 | + "offset": utils.PathSearch("time_zone", item, nil), |
| 128 | + "us_description": utils.PathSearch("time_zone_desc_us", item, nil), |
| 129 | + "cn_description": utils.PathSearch("time_zone_desc_cn", item, nil), |
| 130 | + }) |
| 131 | + } |
| 132 | + |
| 133 | + return result |
| 134 | +} |
0 commit comments