Skip to content

Commit

Permalink
feat: organization add datasource (#2520)
Browse files Browse the repository at this point in the history
* feat: add datasource

* feat: add datasource

* feat: add datasource
  • Loading branch information
WeiMengXS authored Feb 5, 2024
1 parent cdf1faf commit 888be0a
Show file tree
Hide file tree
Showing 13 changed files with 2,231 additions and 219 deletions.
3 changes: 3 additions & 0 deletions .changelog/2520.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
tencentcloud_organization_org_share_area
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.651
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.777
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.770
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.856
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.814
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.815
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts v1.0.762
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/oceanus v1.0.831 h1:oya
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/oceanus v1.0.831/go.mod h1:2WuTlTnKCnZoa6l0JxY9GNfo0UG6nU7AEsljF8rMMsM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.770 h1:Ksu947uY/fafnlILemfMuyKqL/RxHh9dfVowWX8paeQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.770/go.mod h1:Ew8jug2Kq03q9Kkjzov7IMYgugGLEw3FGYPofuRqzKw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.856 h1:W1FLC178fBj5HigbNkAUIBGPPTit20RVycuXQ6u2lSg=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.856/go.mod h1:Ln9igPci1mCEe4gzekGpMMn/x9im6XlDh2bqvWnUMDo=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.814 h1:1XlGrQHRBJv419m64yWRNRWVUTvqBi3Go4ahndnZz9w=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.814/go.mod h1:ch9ZDGEWZXIpzOffmFMgCDKX5uL6x2NLJBiSG94WWEE=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.815 h1:zuOoiF957eIqonXgMW1qtsMnlYGIrgTSMM+yk+OHuWE=
Expand Down
52 changes: 24 additions & 28 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,10 @@ import (
"strconv"
"strings"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/wedata"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tmp"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tpulsar"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/trabbit"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/trocket"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tse"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tsf"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vod"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpn"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/waf"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tco"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcr"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tdcpg"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tem"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/teo"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tke"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tat"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcaplusdb"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcm"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcmg"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcmq"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/go-homedir"
sdkcommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
sdksts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
Expand Down Expand Up @@ -113,6 +85,29 @@ import (
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/ssl"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/ssm"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/sts"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tat"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcaplusdb"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcm"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcmg"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcmq"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tco"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcr"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tdcpg"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tem"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/teo"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tke"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tmp"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tpulsar"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/trabbit"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/trocket"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tse"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tsf"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vod"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpn"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/waf"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/wedata"
)

const (
Expand Down Expand Up @@ -982,6 +977,7 @@ func Provider() *schema.Provider {
"tencentcloud_clickhouse_spec": cdwch.DataSourceTencentCloudClickhouseSpec(),
"tencentcloud_clickhouse_instance_shards": cdwch.DataSourceTencentCloudClickhouseInstanceShards(),
"tencentcloud_clickhouse_instance_nodes": cdwch.DataSourceTencentCloudClickhouseInstanceNodes(),
"tencentcloud_organization_org_share_area": tco.DataSourceTencentCloudOrganizationOrgShareArea(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
1 change: 1 addition & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,7 @@ Tencent Cloud Organization (TCO)
tencentcloud_organization_org_financial_by_member
tencentcloud_organization_org_financial_by_month
tencentcloud_organization_org_financial_by_product
tencentcloud_organization_org_share_area
Resource
tencentcloud_organization_instance
tencentcloud_organization_org_node
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package tco

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func DataSourceTencentCloudOrganizationOrgShareArea() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudOrganizationOrgShareAreaRead,
Schema: map[string]*schema.Schema{
"lang": {
Optional: true,
Type: schema.TypeString,
Description: "Language.default zh.\nValid values:\n - `zh`: Chinese.\n - `en`: English.",
},

"items": {
Computed: true,
Type: schema.TypeList,
Description: "Area list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Region name.",
},
"area": {
Type: schema.TypeString,
Computed: true,
Description: "Region identifier.",
},
"area_id": {
Type: schema.TypeInt,
Computed: true,
Description: "Region ID.",
},
},
},
},

"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudOrganizationOrgShareAreaRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_organization_org_share_area.read")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)

paramMap := make(map[string]interface{})
if v, ok := d.GetOk("lang"); ok {
paramMap["Lang"] = helper.String(v.(string))
}

service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}

var items []*organization.ShareArea
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeOrganizationOrgShareAreaByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
items = result
return nil
})
if err != nil {
return err
}

ids := make([]string, 0, len(items))
tmpList := make([]map[string]interface{}, 0, len(items))

if items != nil {
for _, shareArea := range items {
shareAreaMap := map[string]interface{}{}

if shareArea.Name != nil {
shareAreaMap["name"] = shareArea.Name
}

if shareArea.Area != nil {
shareAreaMap["area"] = shareArea.Area
}

if shareArea.AreaId != nil {
shareAreaMap["area_id"] = shareArea.AreaId
}

ids = append(ids, *shareArea.Area)
tmpList = append(tmpList, shareAreaMap)
}

_ = d.Set("items", tmpList)
}

d.SetId(helper.DataResourceIdsHash(ids))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Use this data source to query detailed information of organization org_share_area

Example Usage

```hcl
data "tencentcloud_organization_org_share_area" "org_share_area" {
lang = "zh"
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tco_test

import (
"testing"

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

tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
)

func TestAccTencentCloudOrganizationOrgShareAreaDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{
{
Config: testAccOrganizationOrgShareAreaDataSource,
Check: resource.ComposeTestCheckFunc(tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_organization_org_share_area.org_share_area")),
},
},
})
}

const testAccOrganizationOrgShareAreaDataSource = `
data "tencentcloud_organization_org_share_area" "org_share_area" {
lang = "zh"
}
`
35 changes: 35 additions & 0 deletions tencentcloud/services/tco/service_tencentcloud_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,38 @@ func (me *OrganizationService) DescribeOrganizationMembersByFilter(ctx context.C

return
}

func (me *OrganizationService) DescribeOrganizationOrgShareAreaByFilter(ctx context.Context, param map[string]interface{}) (orgShareArea []*organization.ShareArea, errRet error) {
var (
logId = tccommon.GetLogId(ctx)
request = organization.NewDescribeShareAreasRequest()
)

defer func() {
if errRet != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
}
}()

for k, v := range param {
if k == "Lang" {
request.Lang = v.(*string)
}
}

ratelimit.Check(request.GetAction())

response, err := me.client.UseOrganizationClient().DescribeShareAreas(request)
if err != nil {
errRet = err
return
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if len(response.Response.Items) < 1 {
return
}

orgShareArea = response.Response.Items
return
}
Loading

0 comments on commit 888be0a

Please sign in to comment.