Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/data-sources/workspace_timezones.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "Workspace"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_workspace_timezones"
description: |-
Use this data source to get the list of Workspace time zones within HuaweiCloud.
---

# huaweicloud_workspace_timezones

Use this data source to get the list of Workspace time zones within HuaweiCloud.

## Example Usage

```hcl
data "huaweicloud_workspace_timezones" "test" {}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region where the time zones are located.
If omitted, the provider-level region will be used.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `time_zones` - The list of time zones.
The [time_zones](#workspace_time_zones_attr) structure is documented below.

<a name="workspace_time_zones_attr"></a>
The `time_zones` block supports:

* `name` - The name of the time zone.

* `offset` - The offset of the time zone.

* `us_description` - The English description of the time zone.

* `cn_description` - The Chinese description of the time zone.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,7 @@ func Provider() *schema.Provider {
"huaweicloud_workspace_policy_groups": workspace.DataSourcePolicyGroups(),
"huaweicloud_workspace_service": workspace.DataSourceService(),
"huaweicloud_workspace_tags": workspace.DataSourceTags(),
"huaweicloud_workspace_timezones": workspace.DataSourceTimeZones(),
"huaweicloud_workspace_users": workspace.DataSourceUsers(),
"huaweicloud_workspace_volume_products": workspace.DataSourceVolumeProducts(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package workspace

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceTimezones_basic(t *testing.T) {
var (
dataSourceName = "data.huaweicloud_workspace_timezones.test"
dc = acceptance.InitDataSourceCheck(dataSourceName)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceTimezones_basic,
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestMatchResourceAttr(dataSourceName, "time_zones.#", regexp.MustCompile(`^[0-9]+$`)),
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.name"),
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.offset"),
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.us_description"),
resource.TestCheckResourceAttrSet(dataSourceName, "time_zones.0.cn_description"),
),
},
},
})
}

const testAccDataSourceTimezones_basic = `
data "huaweicloud_workspace_timezones" "test" {}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package workspace

import (
"context"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

// @API Workspace GET /v2/{project_id}/common/timezones
func DataSourceTimeZones() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceTimeZonesRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The region where the time zones are located.`,
},

// Attributes.
"time_zones": {
Type: schema.TypeList,
Computed: true,
Description: `The list of time zones.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the time zone.`,
},
"offset": {
Type: schema.TypeString,
Computed: true,
Description: `The offset of the time zone.`,
},
"us_description": {
Type: schema.TypeString,
Computed: true,
Description: `The English description of the time zone.`,
},
"cn_description": {
Type: schema.TypeString,
Computed: true,
Description: `The Chinese description of the time zone.`,
},
},
},
},
},
}
}

// Currently, the filter parameter time_zone_name is not available.
func listTimeZones(client *golangsdk.ServiceClient) ([]interface{}, error) {
var (
httpUrl = "v2/{project_id}/common/timezones"
)

listPath := client.Endpoint + httpUrl
listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID)

opt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{
"Content-Type": "application/json",
},
}

requestResp, err := client.Request("GET", listPath, &opt)
if err != nil {
return nil, err
}
respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return nil, err
}

return utils.PathSearch("time_zones", respBody, make([]interface{}, 0)).([]interface{}), nil
}

func dataSourceTimeZonesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("workspace", region)
if err != nil {
return diag.Errorf("error creating Workspace client: %s", err)
}

resp, err := listTimeZones(client)
if err != nil {
return diag.Errorf("error querying Workspace time zones: %s", err)
}

randomUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(randomUUID)

mErr := multierror.Append(nil,
d.Set("region", region),
d.Set("time_zones", flattenTimeZones(resp)),
)
return diag.FromErr(mErr.ErrorOrNil())
}

func flattenTimeZones(items []interface{}) []map[string]interface{} {
if len(items) < 1 {
return nil
}

result := make([]map[string]interface{}, 0, len(items))
for _, item := range items {
result = append(result, map[string]interface{}{
"name": utils.PathSearch("time_zone_name", item, nil),
"offset": utils.PathSearch("time_zone", item, nil),
"us_description": utils.PathSearch("time_zone_desc_us", item, nil),
"cn_description": utils.PathSearch("time_zone_desc_cn", item, nil),
})
}

return result
}
Loading