This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_source_tc_cam_group_policy_attachments.go
175 lines (161 loc) · 5.43 KB
/
data_source_tc_cam_group_policy_attachments.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Use this data source to query detailed information of CAM group policy attachments
Example Usage
```hcl
# query by group_id
data "tencentcloud_cam_group_policy_attachments" "foo" {
group_id = tencentcloud_cam_group.foo.id
}
# query by group_id and policy_id
data "tencentcloud_cam_group_policy_attachments" "bar" {
group_id = tencentcloud_cam_group.foo.id
policy_id = tencentcloud_cam_policy.foo.id
}
```
*/
package tencentcloud
import (
"context"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func dataSourceTencentCloudCamGroupPolicyAttachments() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCamGroupPolicyAttachmentsRead,
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Required: true,
Description: "Id of the attached CAM group to be queried.",
},
"policy_id": {
Type: schema.TypeString,
Optional: true,
Description: "Id of CAM policy to be queried.",
},
"create_mode": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validateAllowedIntValue([]int{1, 2}),
Description: "Mode of Creation of the CAM user policy attachment. 1 means the cam policy attachment is created by production, and the others indicate syntax strategy ways.",
},
"policy_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAllowedStringValue(CAM_POLICY_CREATE_STRATEGY),
Description: "Type of the policy strategy. 'User' means customer strategy and 'QCS' means preset strategy.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"group_policy_attachment_list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of CAM group policy attachments. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Computed: true,
Description: "Id of CAM group.",
},
"policy_id": {
Type: schema.TypeString,
Computed: true,
Description: "Name of CAM group.",
},
"create_mode": {
Type: schema.TypeInt,
Computed: true,
Description: "Mode of Creation of the CAM group policy attachment. 1 means the cam policy attachment is created by production, and the others indicate syntax strategy ways.",
},
"policy_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the policy strategy. 'User' means customer strategy and 'QCS' means preset strategy.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Create time of the CAM group policy attachment.",
},
"policy_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the policy.",
},
},
},
},
},
}
}
func dataSourceTencentCloudCamGroupPolicyAttachmentsRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_cam_group_policy_attachments.read")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
params := make(map[string]interface{})
groupId := d.Get("group_id").(string)
params["group_id"] = groupId
if v, ok := d.GetOk("policy_id"); ok {
policyId, err := strconv.Atoi(v.(string))
if err != nil {
return err
}
params["policy_id"] = uint64(policyId)
}
if v, ok := d.GetOk("policy_type"); ok {
params["policy_type"] = v.(string)
}
if v, ok := d.GetOk("create_mode"); ok {
params["create_mode"] = v.(int)
}
camService := CamService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
var policyOfGroups []*cam.AttachPolicyInfo
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
results, e := camService.DescribeGroupPolicyAttachmentsByFilter(ctx, params)
if e != nil {
return retryError(e)
}
policyOfGroups = results
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CAM group policy attachments failed, reason:%s\n", logId, err.Error())
return err
}
policyOfGroupList := make([]map[string]interface{}, 0, len(policyOfGroups))
ids := make([]string, 0, len(policyOfGroups))
for _, policy := range policyOfGroups {
mapping := map[string]interface{}{
"group_id": groupId,
"policy_id": strconv.Itoa(int(*policy.PolicyId)),
"create_time": *policy.AddTime,
"create_mode": *policy.CreateMode,
"policy_type": *policy.PolicyType,
"policy_name": *policy.PolicyName,
}
policyOfGroupList = append(policyOfGroupList, mapping)
ids = append(ids, groupId+"#"+strconv.Itoa(int(*policy.PolicyId)))
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("group_policy_attachment_list", policyOfGroupList); e != nil {
log.Printf("[CRITAL]%s provider set group polilcy attachment list fail, reason:%s\n", logId, e.Error())
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := writeToFile(output.(string), policyOfGroupList); e != nil {
return e
}
}
return nil
}