Skip to content

Commit a026b8c

Browse files
committed
feat(tco): add resource_tc_organization_org_manage_policy_config
1 parent 5b54b98 commit a026b8c

5 files changed

+58
-32
lines changed

tencentcloud/services/tco/resource_tc_organization_org_manage_policy.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package tco
22

33
import (
44
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
59
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
610
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
711
organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"
8-
"log"
912

1013
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
1114
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
@@ -45,6 +48,12 @@ func ResourceTencentCloudOrganizationOrgManagePolicy() *schema.Resource {
4548
Type: schema.TypeString,
4649
Description: "Policy description.",
4750
},
51+
52+
"policy_id": {
53+
Computed: true,
54+
Type: schema.TypeString,
55+
Description: "Policy Id.",
56+
},
4857
},
4958
}
5059
}
@@ -56,8 +65,9 @@ func resourceTencentCloudOrganizationOrgManagePolicyCreate(d *schema.ResourceDat
5665
logId := tccommon.GetLogId(tccommon.ContextNil)
5766

5867
var (
59-
request = organization.NewCreatePolicyRequest()
60-
response = organization.NewCreatePolicyResponse()
68+
policyType string
69+
request = organization.NewCreatePolicyRequest()
70+
response = organization.NewCreatePolicyResponse()
6171
)
6272
if v, ok := d.GetOk("name"); ok {
6373
request.Name = helper.String(v.(string))
@@ -68,6 +78,7 @@ func resourceTencentCloudOrganizationOrgManagePolicyCreate(d *schema.ResourceDat
6878
}
6979

7080
if v, ok := d.GetOk("type"); ok {
81+
policyType = v.(string)
7182
request.Type = helper.String(v.(string))
7283
}
7384

@@ -90,8 +101,7 @@ func resourceTencentCloudOrganizationOrgManagePolicyCreate(d *schema.ResourceDat
90101
return err
91102
}
92103

93-
d.SetId(helper.UInt64ToStr(*response.Response.PolicyId))
94-
104+
d.SetId(strings.Join([]string{helper.UInt64ToStr(*response.Response.PolicyId), policyType}, tccommon.FILED_SP))
95105
return resourceTencentCloudOrganizationOrgManagePolicyRead(d, meta)
96106
}
97107

@@ -105,11 +115,12 @@ func resourceTencentCloudOrganizationOrgManagePolicyRead(d *schema.ResourceData,
105115

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

108-
policyId := d.Id()
109-
var policyType string
110-
if v, ok := d.GetOk("type"); ok {
111-
policyType = v.(string)
118+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
119+
if len(idSplit) != 2 {
120+
return fmt.Errorf("id is broken,%s", d.Id())
112121
}
122+
policyId := idSplit[0]
123+
policyType := idSplit[1]
113124

114125
OrgManagePolicy, err := service.DescribeOrganizationOrgManagePolicyById(ctx, policyId, policyType)
115126
if err != nil {
@@ -137,6 +148,8 @@ func resourceTencentCloudOrganizationOrgManagePolicyRead(d *schema.ResourceData,
137148
if OrgManagePolicy.Description != nil {
138149
_ = d.Set("description", OrgManagePolicy.Description)
139150
}
151+
_ = d.Set("policy_id", policyId)
152+
140153
return nil
141154
}
142155

@@ -148,7 +161,11 @@ func resourceTencentCloudOrganizationOrgManagePolicyUpdate(d *schema.ResourceDat
148161

149162
request := organization.NewUpdatePolicyRequest()
150163

151-
policyId := d.Id()
164+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
165+
if len(idSplit) != 2 {
166+
return fmt.Errorf("id is broken,%s", d.Id())
167+
}
168+
policyId := idSplit[0]
152169

153170
request.PolicyId = helper.StrToInt64Point(policyId)
154171

@@ -201,11 +218,12 @@ func resourceTencentCloudOrganizationOrgManagePolicyDelete(d *schema.ResourceDat
201218
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
202219

203220
service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
204-
policyId := d.Id()
205-
var policyType string
206-
if v, ok := d.GetOk("type"); ok {
207-
policyType = v.(string)
221+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
222+
if len(idSplit) != 2 {
223+
return fmt.Errorf("id is broken,%s", d.Id())
208224
}
225+
policyId := idSplit[0]
226+
policyType := idSplit[1]
209227

210228
if err := service.DeleteOrganizationOrgManagePolicyById(ctx, policyId, policyType); err != nil {
211229
return err

tencentcloud/services/tco/resource_tc_organization_org_manage_policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Example Usage
55
```hcl
66
resource "tencentcloud_organization_org_manage_policy" "org_manage_policy" {
77
name = "FullAccessPolicy"
8-
content = "{"version":"2.0","statement":[{"effect":"allow","action":"*","resource":"*"}]}"
8+
content = "{\"version\":\"2.0\",\"statement\":[{\"effect\":\"allow\",\"action\":\"*\",\"resource\":\"*\"}]}"
99
type = "SERVICE_CONTROL_POLICY"
1010
description = "Full access policy"
1111
}

tencentcloud/services/tco/resource_tc_organization_org_manage_policy_target_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ func TestAccTencentCloudOrganizationOrgManagePolicyTargetResource_basic(t *testi
1212
t.Parallel()
1313
resource.Test(t, resource.TestCase{
1414
PreCheck: func() {
15-
tcacctest.AccPreCheck(t)
15+
tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_ORGANIZATION)
1616
},
1717
Providers: tcacctest.AccProviders,
1818
Steps: []resource.TestStep{
1919
{
2020
Config: testAccOrganizationOrgManagePolicyTarget,
21-
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_organization_org_manage_policy_target.org_manage_policy_target", "id")),
21+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_organization_org_manage_policy_target.org_manage_policy_target", "id"),
22+
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy_target.org_manage_policy_target", "target_id", "100034649025"),
23+
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy_target.org_manage_policy_target", "target_type", "MEMBER"),
24+
resource.TestCheckResourceAttrSet("tencentcloud_organization_org_manage_policy_target.org_manage_policy_target", "policy_id"),
25+
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy_target.org_manage_policy_target", "policy_type", "SERVICE_CONTROL_POLICY")),
2226
},
2327
{
2428
ResourceName: "tencentcloud_organization_org_manage_policy_target.org_manage_policy_target",
@@ -30,23 +34,22 @@ func TestAccTencentCloudOrganizationOrgManagePolicyTargetResource_basic(t *testi
3034
}
3135

3236
const testAccOrganizationOrgManagePolicyTarget = `
33-
3437
resource "tencentcloud_organization_org_manage_policy" "org_manage_policy" {
35-
name = "FullAccessPolicy2"
36-
content = "{\"tags\":{\"Region\":{\"tag_value\":{\"@@assign\":[\"ap-guangzhou\",\"ap-beijing\"]},\"tag_key\":{\"@@assign\":\"Region\"}}}}"
37-
type = "TAG_POLICY"
38+
name = "example-service"
39+
content = "{\"version\":\"2.0\",\"statement\":[{\"effect\":\"allow\",\"action\":\"*\",\"resource\":\"*\"}]}"
40+
type = "SERVICE_CONTROL_POLICY"
3841
description = "Full access policy"
3942
depends_on = [tencentcloud_organization_org_manage_policy_config.org_manage_policy_config]
4043
}
4144
resource "tencentcloud_organization_org_manage_policy_config" "org_manage_policy_config" {
42-
organization_id = 201408
43-
policy_type = "TAG_POLICY"
45+
organization_id = 45155
46+
policy_type = "SERVICE_CONTROL_POLICY"
4447
}
4548
resource "tencentcloud_organization_org_manage_policy_target" "org_manage_policy_target" {
46-
target_id = 100020337821
49+
target_id = 100034649025
4750
target_type = "MEMBER"
48-
policy_id = tencentcloud_organization_org_manage_policy.org_manage_policy.id
49-
policy_type = "TAG_POLICY"
51+
policy_id = tencentcloud_organization_org_manage_policy.org_manage_policy.policy_id
52+
policy_type = "SERVICE_CONTROL_POLICY"
5053
}
5154
5255
`

tencentcloud/services/tco/resource_tc_organization_org_manage_policy_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ func TestAccTencentCloudOrganizationOrgManagePolicyResource_basic(t *testing.T)
1212
t.Parallel()
1313
resource.Test(t, resource.TestCase{
1414
PreCheck: func() {
15-
tcacctest.AccPreCheck(t)
15+
tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_ORGANIZATION)
1616
},
1717
Providers: tcacctest.AccProviders,
1818
Steps: []resource.TestStep{
1919
{
2020
Config: testAccOrganizationOrgManagePolicy,
2121
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_organization_org_manage_policy.org_manage_policy", "id"),
22-
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "name", "iac-example"),
22+
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "name", "iac-example-svc"),
2323
resource.TestCheckResourceAttrSet("tencentcloud_organization_org_manage_policy.org_manage_policy", "content"),
2424
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "type", "SERVICE_CONTROL_POLICY"),
2525
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "description", "Full access policy"),
@@ -28,7 +28,7 @@ func TestAccTencentCloudOrganizationOrgManagePolicyResource_basic(t *testing.T)
2828
{
2929
Config: testAccOrganizationOrgManagePolicyUpdate,
3030
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_organization_org_manage_policy.org_manage_policy", "id"),
31-
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "name", "iac-example-1"),
31+
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "name", "iac-example-svc1"),
3232
resource.TestCheckResourceAttrSet("tencentcloud_organization_org_manage_policy.org_manage_policy", "content"),
3333
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "type", "SERVICE_CONTROL_POLICY"),
3434
resource.TestCheckResourceAttr("tencentcloud_organization_org_manage_policy.org_manage_policy", "description", "Full access policy"),
@@ -46,7 +46,7 @@ func TestAccTencentCloudOrganizationOrgManagePolicyResource_basic(t *testing.T)
4646
const testAccOrganizationOrgManagePolicy = `
4747
4848
resource "tencentcloud_organization_org_manage_policy" "org_manage_policy" {
49-
name = "iac-example"
49+
name = "iac-example-svc"
5050
content = "{\"version\":\"2.0\",\"statement\":[{\"effect\":\"allow\",\"action\":\"*\",\"resource\":\"*\"}]}"
5151
type = "SERVICE_CONTROL_POLICY"
5252
description = "Full access policy"
@@ -56,7 +56,7 @@ resource "tencentcloud_organization_org_manage_policy" "org_manage_policy" {
5656
const testAccOrganizationOrgManagePolicyUpdate = `
5757
5858
resource "tencentcloud_organization_org_manage_policy" "org_manage_policy" {
59-
name = "iac-example-1"
59+
name = "iac-example-svc1"
6060
content = "{\"version\":\"2.0\",\"statement\":[{\"effect\":\"allow\",\"action\":\"*\",\"resource\":\"*\"}]}"
6161
type = "SERVICE_CONTROL_POLICY"
6262
description = "Full access policy"

tencentcloud/services/tco/service_tencentcloud_organization.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,12 @@ func (me *OrganizationService) DescribeOrganizationOrgManagePolicyTargetById(ctx
10971097
request := organization.NewListTargetsForPolicyRequest()
10981098
request.PolicyType = &policyType
10991099
request.PolicyId = helper.StrToUint64Point(policyId)
1100-
request.TargetType = &targetType
1100+
switch targetType {
1101+
case "NODE":
1102+
request.TargetType = helper.String("Node")
1103+
case "MEMBER":
1104+
request.TargetType = helper.String("User")
1105+
}
11011106

11021107
defer func() {
11031108
if errRet != nil {

0 commit comments

Comments
 (0)