/
data_source_tc_cfs_access_groups.go
135 lines (123 loc) · 3.7 KB
/
data_source_tc_cfs_access_groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Use this data source to query the detail information of CFS access group.
Example Usage
```hcl
data "tencentcloud_cfs_access_groups" "access_groups" {
access_group_id = "pgroup-7nx89k7l"
name = "test"
}
```
*/
package tencentcloud
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
cfs "github.com/liucy1983/tencentcloud-sdk-go/tencentcloud/cfs/v20190719"
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func dataSourceTencentCloudCfsAccessGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCfsAccessGroupsRead,
Schema: map[string]*schema.Schema{
"access_group_id": {
Type: schema.TypeString,
Optional: true,
Description: "A specified access group ID used to query.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "A access group Name used to query.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"access_group_list": {
Type: schema.TypeList,
Computed: true,
Description: "An information list of CFS access group. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the access group.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the access group.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of the access group.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time of the access group.",
},
},
},
},
},
}
}
func dataSourceTencentCloudCfsAccessGroupsRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_cfs_access_groups.read")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
cfsService := CfsService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
var accessGroupId string
var name string
if v, ok := d.GetOk("access_group_id"); ok {
accessGroupId = v.(string)
}
if v, ok := d.GetOk("name"); ok {
name = v.(string)
}
var accessGroups []*cfs.PGroupInfo
var errRet error
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
accessGroups, errRet = cfsService.DescribeAccessGroup(ctx, accessGroupId, name)
if errRet != nil {
return retryError(errRet, InternalError)
}
return nil
})
if err != nil {
return err
}
accessGroupList := make([]map[string]interface{}, 0, len(accessGroups))
ids := make([]string, 0, len(accessGroups))
for _, accessGroup := range accessGroups {
mapping := map[string]interface{}{
"access_group_id": accessGroup.PGroupId,
"name": accessGroup.Name,
"description": accessGroup.DescInfo,
"create_time": accessGroup.CDate,
}
accessGroupList = append(accessGroupList, mapping)
ids = append(ids, *accessGroup.PGroupId)
}
d.SetId(helper.DataResourceIdsHash(ids))
err = d.Set("access_group_list", accessGroupList)
if err != nil {
log.Printf("[CRITAL]%s provider set cfs access group list fail, reason:%s\n ", logId, err.Error())
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if err := writeToFile(output.(string), accessGroupList); err != nil {
return err
}
}
return nil
}