Skip to content

Commit 03b8820

Browse files
committed
feat(workspace): add new data source to query time zones
1 parent 517038d commit 03b8820

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Workspace"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_workspace_timezones"
5+
description: |-
6+
Use this data source to get the list of Workspace time zones within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_workspace_timezones
10+
11+
Use this data source to get the list of Workspace time zones within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_workspace_timezones" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region where the time zones are located.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attribute Reference
27+
28+
In addition to all arguments above, the following attributes are exported:
29+
30+
* `id` - The data source ID.
31+
32+
* `time_zones` - The list of time zones.
33+
The [time_zones](#workspace_time_zones_attr) structure is documented below.
34+
35+
<a name="workspace_time_zones_attr"></a>
36+
The `time_zones` block supports:
37+
38+
* `name` - The name of the time zone.
39+
40+
* `offset` - The offset of the time zone.
41+
42+
* `us_description` - The English description of the time zone.
43+
44+
* `cn_description` - The Chinese description of the time zone.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,7 @@ func Provider() *schema.Provider {
21042104
"huaweicloud_workspace_policy_groups": workspace.DataSourcePolicyGroups(),
21052105
"huaweicloud_workspace_service": workspace.DataSourceService(),
21062106
"huaweicloud_workspace_tags": workspace.DataSourceTags(),
2107+
"huaweicloud_workspace_timezones": workspace.DataSourceTimeZones(),
21072108
"huaweicloud_workspace_users": workspace.DataSourceUsers(),
21082109
"huaweicloud_workspace_volume_products": workspace.DataSourceVolumeProducts(),
21092110

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package workspace
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourceTimezones_basic(t *testing.T) {
13+
var (
14+
dataSourceName = "data.huaweicloud_workspace_timezones.test"
15+
dc = acceptance.InitDataSourceCheck(dataSourceName)
16+
)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
20+
ProviderFactories: acceptance.TestAccProviderFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccDataSourceTimezones_basic,
24+
Check: resource.ComposeTestCheckFunc(
25+
dc.CheckResourceExists(),
26+
resource.TestMatchResourceAttr(dataSourceName, "time_zones.#", regexp.MustCompile(`^[0-9]+$`)),
27+
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.name"),
28+
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.offset"),
29+
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.us_description"),
30+
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.cn_description"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
const testAccDataSourceTimezones_basic = `
38+
data "huaweicloud_workspace_timezones" "test" {}
39+
`
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)