Skip to content

Commit 36bc864

Browse files
committed
add data source
1 parent a545977 commit 36bc864

6 files changed

+484
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Use this data source to query detailed information of chdfs access_groups
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_chdfs_access_groups" "access_groups" {
8+
vpc_id = "vpc-pewdpc0d"
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
19+
chdfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs/v20201112"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudChdfsAccessGroups() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudChdfsAccessGroupsRead,
26+
Schema: map[string]*schema.Schema{
27+
"vpc_id": {
28+
Optional: true,
29+
Type: schema.TypeString,
30+
Description: "get groups belongs to the vpc id, must set but only can use one of VpcId and OwnerUin to get the groups.",
31+
},
32+
33+
"owner_uin": {
34+
Optional: true,
35+
Type: schema.TypeInt,
36+
Description: "get groups belongs to the owner uin, must set but only can use one of VpcId and OwnerUin to get the groups.",
37+
},
38+
39+
"access_groups": {
40+
Computed: true,
41+
Type: schema.TypeList,
42+
Description: "access group list.",
43+
Elem: &schema.Resource{
44+
Schema: map[string]*schema.Schema{
45+
"access_group_id": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
Description: "access group id.",
49+
},
50+
"access_group_name": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
Description: "access group name.",
54+
},
55+
"description": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
Description: "access group description.",
59+
},
60+
"create_time": {
61+
Type: schema.TypeString,
62+
Computed: true,
63+
Description: "create time.",
64+
},
65+
"vpc_type": {
66+
Type: schema.TypeInt,
67+
Computed: true,
68+
Description: "vpc network type(1:CVM, 2:BM 1.0).",
69+
},
70+
"vpc_id": {
71+
Type: schema.TypeString,
72+
Computed: true,
73+
Description: "VPC ID.",
74+
},
75+
},
76+
},
77+
},
78+
79+
"result_output_file": {
80+
Type: schema.TypeString,
81+
Optional: true,
82+
Description: "Used to save results.",
83+
},
84+
},
85+
}
86+
}
87+
88+
func dataSourceTencentCloudChdfsAccessGroupsRead(d *schema.ResourceData, meta interface{}) error {
89+
defer logElapsed("data_source.tencentcloud_chdfs_access_groups.read")()
90+
defer inconsistentCheck(d, meta)()
91+
92+
logId := getLogId(contextNil)
93+
94+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
95+
96+
paramMap := make(map[string]interface{})
97+
if v, ok := d.GetOk("vpc_id"); ok {
98+
paramMap["vpc_id"] = helper.String(v.(string))
99+
}
100+
101+
if v, ok := d.GetOkExists("owner_uin"); ok {
102+
paramMap["owner_uin"] = helper.IntUint64(v.(int))
103+
}
104+
105+
service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn}
106+
107+
var accessGroups []*chdfs.AccessGroup
108+
109+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
110+
result, e := service.DescribeChdfsAccessGroupsByFilter(ctx, paramMap)
111+
if e != nil {
112+
return retryError(e)
113+
}
114+
accessGroups = result
115+
return nil
116+
})
117+
if err != nil {
118+
return err
119+
}
120+
121+
ids := make([]string, 0, len(accessGroups))
122+
tmpList := make([]map[string]interface{}, 0, len(accessGroups))
123+
124+
if accessGroups != nil {
125+
for _, accessGroup := range accessGroups {
126+
accessGroupMap := map[string]interface{}{}
127+
128+
if accessGroup.AccessGroupId != nil {
129+
accessGroupMap["access_group_id"] = accessGroup.AccessGroupId
130+
}
131+
132+
if accessGroup.AccessGroupName != nil {
133+
accessGroupMap["access_group_name"] = accessGroup.AccessGroupName
134+
}
135+
136+
if accessGroup.Description != nil {
137+
accessGroupMap["description"] = accessGroup.Description
138+
}
139+
140+
if accessGroup.CreateTime != nil {
141+
accessGroupMap["create_time"] = accessGroup.CreateTime
142+
}
143+
144+
if accessGroup.VpcType != nil {
145+
accessGroupMap["vpc_type"] = accessGroup.VpcType
146+
}
147+
148+
if accessGroup.VpcId != nil {
149+
accessGroupMap["vpc_id"] = accessGroup.VpcId
150+
}
151+
152+
ids = append(ids, *accessGroup.AccessGroupId)
153+
tmpList = append(tmpList, accessGroupMap)
154+
}
155+
156+
_ = d.Set("access_groups", tmpList)
157+
}
158+
159+
d.SetId(helper.DataResourceIdsHash(ids))
160+
output, ok := d.GetOk("result_output_file")
161+
if ok && output.(string) != "" {
162+
if e := writeToFile(output.(string), tmpList); e != nil {
163+
return e
164+
}
165+
}
166+
return nil
167+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudChdfsAccessGroupsDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccChdfsAccessGroupsDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_chdfs_access_groups.access_groups")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccChdfsAccessGroupsDataSource = `
26+
27+
data "tencentcloud_chdfs_access_groups" "access_groups" {
28+
vpc_id = "vpc-pewdpc0d"
29+
}
30+
31+
`
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
Use this data source to query detailed information of chdfs mount_points
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_chdfs_mount_points" "mount_points" {
8+
file_system_id = "f14mpfy5lh4e"
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
19+
chdfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs/v20201112"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudChdfsMountPoints() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudChdfsMountPointsRead,
26+
Schema: map[string]*schema.Schema{
27+
"file_system_id": {
28+
Optional: true,
29+
Type: schema.TypeString,
30+
Description: "get mount points belongs to file system id, only can use one of the AccessGroupId,FileSystemId,OwnerUin paramaters.",
31+
},
32+
33+
"access_group_id": {
34+
Optional: true,
35+
Type: schema.TypeString,
36+
Description: "get mount points belongs to access group id, only can use one of the AccessGroupId,FileSystemId,OwnerUin paramaters.",
37+
},
38+
39+
"owner_uin": {
40+
Optional: true,
41+
Type: schema.TypeInt,
42+
Description: "get mount points belongs to owner uin, only can use one of the AccessGroupId,FileSystemId,OwnerUin paramaters.",
43+
},
44+
45+
"mount_points": {
46+
Computed: true,
47+
Type: schema.TypeList,
48+
Description: "mount point list.",
49+
Elem: &schema.Resource{
50+
Schema: map[string]*schema.Schema{
51+
"mount_point_id": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
Description: "mount point id.",
55+
},
56+
"mount_point_name": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
Description: "mount point name.",
60+
},
61+
"file_system_id": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "mounted file system id.",
65+
},
66+
"status": {
67+
Type: schema.TypeInt,
68+
Computed: true,
69+
Description: "mount point status.",
70+
},
71+
"create_time": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "create time.",
75+
},
76+
"access_group_ids": {
77+
Type: schema.TypeSet,
78+
Elem: &schema.Schema{
79+
Type: schema.TypeString,
80+
},
81+
Computed: true,
82+
Description: "associated group ids.",
83+
},
84+
},
85+
},
86+
},
87+
88+
"result_output_file": {
89+
Type: schema.TypeString,
90+
Optional: true,
91+
Description: "Used to save results.",
92+
},
93+
},
94+
}
95+
}
96+
97+
func dataSourceTencentCloudChdfsMountPointsRead(d *schema.ResourceData, meta interface{}) error {
98+
defer logElapsed("data_source.tencentcloud_chdfs_mount_points.read")()
99+
defer inconsistentCheck(d, meta)()
100+
101+
logId := getLogId(contextNil)
102+
103+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
104+
105+
paramMap := make(map[string]interface{})
106+
if v, ok := d.GetOk("file_system_id"); ok {
107+
paramMap["file_system_id"] = helper.String(v.(string))
108+
}
109+
110+
if v, ok := d.GetOk("access_group_id"); ok {
111+
paramMap["access_group_id"] = helper.String(v.(string))
112+
}
113+
114+
if v, ok := d.GetOkExists("owner_uin"); ok {
115+
paramMap["owner_uin"] = helper.IntUint64(v.(int))
116+
}
117+
118+
service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn}
119+
120+
var mountPoints []*chdfs.MountPoint
121+
122+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
123+
result, e := service.DescribeChdfsMountPointsByFilter(ctx, paramMap)
124+
if e != nil {
125+
return retryError(e)
126+
}
127+
mountPoints = result
128+
return nil
129+
})
130+
if err != nil {
131+
return err
132+
}
133+
134+
ids := make([]string, 0, len(mountPoints))
135+
tmpList := make([]map[string]interface{}, 0, len(mountPoints))
136+
137+
if mountPoints != nil {
138+
for _, mountPoint := range mountPoints {
139+
mountPointMap := map[string]interface{}{}
140+
141+
if mountPoint.MountPointId != nil {
142+
mountPointMap["mount_point_id"] = mountPoint.MountPointId
143+
}
144+
145+
if mountPoint.MountPointName != nil {
146+
mountPointMap["mount_point_name"] = mountPoint.MountPointName
147+
}
148+
149+
if mountPoint.FileSystemId != nil {
150+
mountPointMap["file_system_id"] = mountPoint.FileSystemId
151+
}
152+
153+
if mountPoint.Status != nil {
154+
mountPointMap["status"] = mountPoint.Status
155+
}
156+
157+
if mountPoint.CreateTime != nil {
158+
mountPointMap["create_time"] = mountPoint.CreateTime
159+
}
160+
161+
if mountPoint.AccessGroupIds != nil {
162+
mountPointMap["access_group_ids"] = mountPoint.AccessGroupIds
163+
}
164+
165+
ids = append(ids, *mountPoint.MountPointId)
166+
tmpList = append(tmpList, mountPointMap)
167+
}
168+
169+
_ = d.Set("mount_points", tmpList)
170+
}
171+
172+
d.SetId(helper.DataResourceIdsHash(ids))
173+
output, ok := d.GetOk("result_output_file")
174+
if ok && output.(string) != "" {
175+
if e := writeToFile(output.(string), tmpList); e != nil {
176+
return e
177+
}
178+
}
179+
return nil
180+
}

0 commit comments

Comments
 (0)