Skip to content

Commit 188a4e4

Browse files
WeiMengXSWeiMengXS
and
WeiMengXS
authored
Feat/organization member policy (#2243)
* feat: member policy * feat: doc * feat: doc * feat: test --------- Co-authored-by: WeiMengXS <[email protected]>
1 parent e8530de commit 188a4e4

8 files changed

+281
-0
lines changed

.changelog/2243.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_organization_org_member_policy_attachment
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,7 @@ Tencent Cloud Organization (TCO)
15151515
tencentcloud_organization_org_member_email
15161516
tencentcloud_organization_org_member_auth_identity_attachment
15171517
tencentcloud_organization_policy_sub_account_attachment
1518+
tencentcloud_organization_org_member_policy_attachment
15181519
tencentcloud_organization_quit_organization_operation
15191520
15201521
TDSQL-C for PostgreSQL(TDCPG)
@@ -3278,6 +3279,7 @@ func Provider() *schema.Provider {
32783279
"tencentcloud_organization_instance": resourceTencentCloudOrganizationOrganization(),
32793280
"tencentcloud_organization_policy_sub_account_attachment": resourceTencentCloudOrganizationPolicySubAccountAttachment(),
32803281
"tencentcloud_organization_org_member_auth_identity_attachment": resourceTencentCloudOrganizationOrgMemberAuthIdentityAttachment(),
3282+
"tencentcloud_organization_org_member_policy_attachment": resourceTencentCloudOrganizationOrgMemberPolicyAttachment(),
32813283
"tencentcloud_dbbrain_sql_filter": resourceTencentCloudDbbrainSqlFilter(),
32823284
"tencentcloud_dbbrain_security_audit_log_export_task": resourceTencentCloudDbbrainSecurityAuditLogExportTask(),
32833285
"tencentcloud_dbbrain_db_diag_report_task": resourceTencentCloudDbbrainDbDiagReportTask(),

tencentcloud/provider_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
ACCOUNT_TYPE_SES = "SES"
2727
ACCOUNT_TYPE_TSF = "TSF"
2828
ACCOUNT_TYPE_SSL = "SSL"
29+
ACCOUNT_TYPE_ORGANIZATION = "ORGANIZATION"
2930
INTERNATIONAL_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_INTERNATIONAL"
3031
INTERNATIONAL_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_INTERNATIONAL"
3132
PREPAY_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_PREPAY"
@@ -42,6 +43,8 @@ const (
4243
TSF_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_TSF"
4344
SSL_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_SSL"
4445
SSL_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_SSL"
46+
ORGANIZATION_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_ORGANIZATION"
47+
ORGANIZATION_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_ORGANIZATION"
4548
)
4649

4750
func init() {
@@ -165,6 +168,14 @@ func testAccPreCheckCommon(t *testing.T, accountType string) {
165168
}
166169
os.Setenv(PROVIDER_SECRET_ID, secretId)
167170
os.Setenv(PROVIDER_SECRET_KEY, secretKey)
171+
case accountType == ACCOUNT_TYPE_ORGANIZATION:
172+
secretId := os.Getenv(ORGANIZATION_PROVIDER_SECRET_ID)
173+
secretKey := os.Getenv(ORGANIZATION_PROVIDER_SECRET_KEY)
174+
if secretId == "" || secretKey == "" {
175+
t.Fatalf("%v and %v must be set for acceptance tests\n", ORGANIZATION_PROVIDER_SECRET_ID, ORGANIZATION_PROVIDER_SECRET_KEY)
176+
}
177+
os.Setenv(PROVIDER_SECRET_ID, secretId)
178+
os.Setenv(PROVIDER_SECRET_KEY, secretKey)
168179
default:
169180
if v := os.Getenv(PROVIDER_SECRET_ID); v == "" {
170181
t.Fatalf("%v must be set for acceptance tests\n", PROVIDER_SECRET_ID)
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
Provides a resource to create a organization org_member_policy_attachment
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_organization_org_member_policy_attachment" "org_member_policy_attachment" {
8+
member_uins = [100033905366,100033905356]
9+
policy_name = "example-iac"
10+
identity_id = 1
11+
}
12+
```
13+
14+
Import
15+
16+
organization org_member_policy_attachment can be imported using the id, e.g.
17+
18+
```
19+
terraform import tencentcloud_organization_org_member_policy_attachment.org_member_policy_attachment org_member_policy_attachment_id
20+
```
21+
*/
22+
package tencentcloud
23+
24+
import (
25+
"context"
26+
"fmt"
27+
"log"
28+
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
31+
organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"
32+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
33+
)
34+
35+
func resourceTencentCloudOrganizationOrgMemberPolicyAttachment() *schema.Resource {
36+
return &schema.Resource{
37+
Create: resourceTencentCloudOrganizationOrgMemberPolicyAttachmentCreate,
38+
Read: resourceTencentCloudOrganizationOrgMemberPolicyAttachmentRead,
39+
Delete: resourceTencentCloudOrganizationOrgMemberPolicyAttachmentDelete,
40+
Importer: &schema.ResourceImporter{
41+
State: schema.ImportStatePassthrough,
42+
},
43+
Schema: map[string]*schema.Schema{
44+
"member_uins": {
45+
Required: true,
46+
ForceNew: true,
47+
Type: schema.TypeSet,
48+
Elem: &schema.Schema{
49+
Type: schema.TypeInt,
50+
},
51+
Description: "Member Uin list. Up to 10.",
52+
},
53+
54+
"policy_name": {
55+
Required: true,
56+
ForceNew: true,
57+
Type: schema.TypeString,
58+
Description: "Policy name.The maximum length is 128 characters, supporting English letters, numbers, and symbols +=,.@_-.",
59+
},
60+
61+
"identity_id": {
62+
Required: true,
63+
ForceNew: true,
64+
Type: schema.TypeInt,
65+
Description: "Organization identity ID.",
66+
},
67+
68+
"description": {
69+
Optional: true,
70+
ForceNew: true,
71+
Type: schema.TypeString,
72+
Description: "Notes.The maximum length is 128 characters.",
73+
},
74+
},
75+
}
76+
}
77+
78+
func resourceTencentCloudOrganizationOrgMemberPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
79+
defer logElapsed("resource.tencentcloud_organization_org_member_policy_attachment.create")()
80+
defer inconsistentCheck(d, meta)()
81+
82+
logId := getLogId(contextNil)
83+
84+
var (
85+
request = organization.NewCreateOrganizationMembersPolicyRequest()
86+
response = organization.NewCreateOrganizationMembersPolicyResponse()
87+
)
88+
if v, ok := d.GetOk("member_uins"); ok {
89+
memberUinsSet := v.(*schema.Set).List()
90+
for i := range memberUinsSet {
91+
memberUins := memberUinsSet[i].(int)
92+
request.MemberUins = append(request.MemberUins, helper.IntInt64(memberUins))
93+
}
94+
}
95+
96+
if v, ok := d.GetOk("policy_name"); ok {
97+
request.PolicyName = helper.String(v.(string))
98+
}
99+
100+
if v, ok := d.GetOkExists("identity_id"); ok {
101+
request.IdentityId = helper.IntInt64(v.(int))
102+
}
103+
104+
if v, ok := d.GetOk("description"); ok {
105+
request.Description = helper.String(v.(string))
106+
}
107+
108+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
109+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseOrganizationClient().CreateOrganizationMembersPolicy(request)
110+
if e != nil {
111+
return retryError(e)
112+
} else {
113+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
114+
}
115+
response = result
116+
return nil
117+
})
118+
if err != nil {
119+
log.Printf("[CRITAL]%s create organization orgMemberPolicyAttachment failed, reason:%+v", logId, err)
120+
return err
121+
}
122+
123+
if response == nil || response.Response == nil || response.Response.PolicyId == nil {
124+
return fmt.Errorf("policy id is null")
125+
}
126+
policyId := *response.Response.PolicyId
127+
d.SetId(helper.Int64ToStr(policyId))
128+
129+
return resourceTencentCloudOrganizationOrgMemberPolicyAttachmentRead(d, meta)
130+
}
131+
132+
func resourceTencentCloudOrganizationOrgMemberPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
133+
defer logElapsed("resource.tencentcloud_organization_org_member_policy_attachment.read")()
134+
defer inconsistentCheck(d, meta)()
135+
136+
return nil
137+
}
138+
139+
func resourceTencentCloudOrganizationOrgMemberPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
140+
defer logElapsed("resource.tencentcloud_organization_org_member_policy_attachment.delete")()
141+
defer inconsistentCheck(d, meta)()
142+
143+
logId := getLogId(contextNil)
144+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
145+
146+
service := OrganizationService{client: meta.(*TencentCloudClient).apiV3Conn}
147+
orgMemberPolicyAttachmentId := d.Id()
148+
149+
if err := service.DeleteOrganizationOrgMemberPolicyAttachmentById(ctx, orgMemberPolicyAttachmentId); err != nil {
150+
return err
151+
}
152+
153+
return nil
154+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudOrganizationOrgMemberPolicyAttachmentResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheckCommon(t, ACCOUNT_TYPE_ORGANIZATION)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccOrganizationOrgMemberPolicyAttachment,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_organization_org_member_policy_attachment.org_member_policy_attachment", "id"),
20+
resource.TestCheckResourceAttrSet("tencentcloud_organization_org_member_policy_attachment.org_member_policy_attachment", "member_uins.#"),
21+
resource.TestCheckResourceAttr("tencentcloud_organization_org_member_policy_attachment.org_member_policy_attachment", "policy_name", "example-iac"),
22+
resource.TestCheckResourceAttr("tencentcloud_organization_org_member_policy_attachment.org_member_policy_attachment", "identity_id", "1"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
const testAccOrganizationOrgMemberPolicyAttachment = `
30+
31+
resource "tencentcloud_organization_org_member_policy_attachment" "org_member_policy_attachment" {
32+
member_uins = [100033905366,100033905356]
33+
policy_name = "example-iac"
34+
identity_id = 1
35+
}
36+
`

tencentcloud/service_tencentcloud_organization.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,27 @@ func (me *OrganizationService) DeleteOrganizationOrgIdentityById(ctx context.Con
685685

686686
return
687687
}
688+
689+
func (me *OrganizationService) DeleteOrganizationOrgMemberPolicyAttachmentById(ctx context.Context, policyId string) (errRet error) {
690+
logId := getLogId(ctx)
691+
692+
request := organization.NewDeleteOrganizationMembersPolicyRequest()
693+
request.PolicyId = helper.StrToUint64Point(policyId)
694+
695+
defer func() {
696+
if errRet != nil {
697+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
698+
}
699+
}()
700+
701+
ratelimit.Check(request.GetAction())
702+
703+
response, err := me.client.UseOrganizationClient().DeleteOrganizationMembersPolicy(request)
704+
if err != nil {
705+
errRet = err
706+
return
707+
}
708+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
709+
710+
return
711+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
subcategory: "Tencent Cloud Organization (TCO)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_organization_org_member_policy_attachment"
5+
sidebar_current: "docs-tencentcloud-resource-organization_org_member_policy_attachment"
6+
description: |-
7+
Provides a resource to create a organization org_member_policy_attachment
8+
---
9+
10+
# tencentcloud_organization_org_member_policy_attachment
11+
12+
Provides a resource to create a organization org_member_policy_attachment
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_organization_org_member_policy_attachment" "org_member_policy_attachment" {
18+
member_uins = [100033905366, 100033905356]
19+
policy_name = "example-iac"
20+
identity_id = 1
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
* `identity_id` - (Required, Int, ForceNew) Organization identity ID.
29+
* `member_uins` - (Required, Set: [`Int`], ForceNew) Member Uin list. Up to 10.
30+
* `policy_name` - (Required, String, ForceNew) Policy name.The maximum length is 128 characters, supporting English letters, numbers, and symbols +=,.@_-.
31+
* `description` - (Optional, String, ForceNew) Notes.The maximum length is 128 characters.
32+
33+
## Attributes Reference
34+
35+
In addition to all arguments above, the following attributes are exported:
36+
37+
* `id` - ID of the resource.
38+
39+
40+
41+
## Import
42+
43+
organization org_member_policy_attachment can be imported using the id, e.g.
44+
45+
```
46+
terraform import tencentcloud_organization_org_member_policy_attachment.org_member_policy_attachment org_member_policy_attachment_id
47+
```
48+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,6 +3739,9 @@
37393739
<li>
37403740
<a href="/docs/providers/tencentcloud/r/organization_org_member_email.html">tencentcloud_organization_org_member_email</a>
37413741
</li>
3742+
<li>
3743+
<a href="/docs/providers/tencentcloud/r/organization_org_member_policy_attachment.html">tencentcloud_organization_org_member_policy_attachment</a>
3744+
</li>
37423745
<li>
37433746
<a href="/docs/providers/tencentcloud/r/organization_org_node.html">tencentcloud_organization_org_node</a>
37443747
</li>

0 commit comments

Comments
 (0)