-
Notifications
You must be signed in to change notification settings - Fork 143
feat(tco): [120686077] support custom policies #2962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment | ||
``` |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
...o/resource_tc_identity_center_role_configuration_permission_custom_policies_attachment.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
package tco | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" | ||
|
||
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" | ||
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" | ||
) | ||
|
||
func ResourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachment() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachmentCreate, | ||
Read: resourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachmentRead, | ||
Delete: resourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachmentDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"zone_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Space ID.", | ||
}, | ||
|
||
"role_configuration_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Permission configuration ID.", | ||
}, | ||
|
||
"policies": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"role_policy_document": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Role policy document.", | ||
}, | ||
|
||
"role_policy_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Role policy name.", | ||
}, | ||
"role_policy_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Role policy type.", | ||
}, | ||
|
||
"add_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Role policy add time.", | ||
}, | ||
}, | ||
}, | ||
Description: "Policies.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachmentCreate(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment.create")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
logId := tccommon.GetLogId(tccommon.ContextNil) | ||
|
||
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) | ||
|
||
var ( | ||
zoneId string | ||
roleConfigurationId string | ||
rolePolicyNames []string | ||
) | ||
var ( | ||
request = organization.NewAddPermissionPolicyToRoleConfigurationRequest() | ||
response = organization.NewAddPermissionPolicyToRoleConfigurationResponse() | ||
) | ||
|
||
if v, ok := d.GetOk("zone_id"); ok { | ||
zoneId = v.(string) | ||
request.ZoneId = helper.String(zoneId) | ||
} | ||
|
||
if v, ok := d.GetOk("role_configuration_id"); ok { | ||
roleConfigurationId = v.(string) | ||
request.RoleConfigurationId = helper.String(roleConfigurationId) | ||
} | ||
|
||
if v, ok := d.GetOk("policies"); ok { | ||
policies := v.(*schema.Set).List() | ||
for _, poilcy := range policies { | ||
policyMap := poilcy.(map[string]interface{}) | ||
rolePolicyName := policyMap["role_policy_name"] | ||
rolePolicyDocument := policyMap["role_policy_document"] | ||
rolePolicyNames = append(rolePolicyNames, rolePolicyName.(string)) | ||
request.RolePolicyNames = append(request.RolePolicyNames, helper.String(rolePolicyName.(string))) | ||
request.CustomPolicyDocuments = append(request.CustomPolicyDocuments, helper.String(rolePolicyDocument.(string))) | ||
} | ||
} | ||
|
||
request.RolePolicyType = helper.String("Custom") | ||
|
||
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { | ||
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().AddPermissionPolicyToRoleConfigurationWithContext(ctx, request) | ||
if e != nil { | ||
return tccommon.RetryError(e) | ||
} else { | ||
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) | ||
} | ||
response = result | ||
return nil | ||
}) | ||
if err != nil { | ||
log.Printf("[CRITAL]%s create identity center role configuration permission policy attachment failed, reason:%+v", logId, err) | ||
return err | ||
} | ||
|
||
_ = response | ||
|
||
rolePolicyNameStr := strings.Join(rolePolicyNames, tccommon.COMMA_SP) | ||
d.SetId(strings.Join([]string{zoneId, roleConfigurationId, rolePolicyNameStr}, tccommon.FILED_SP)) | ||
|
||
return resourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachmentRead(d, meta) | ||
} | ||
|
||
func resourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachmentRead(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment.read")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
logId := tccommon.GetLogId(tccommon.ContextNil) | ||
|
||
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) | ||
|
||
service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} | ||
|
||
idSplit := strings.Split(d.Id(), tccommon.FILED_SP) | ||
if len(idSplit) != 3 { | ||
return fmt.Errorf("id is broken,%s", d.Id()) | ||
} | ||
zoneId := idSplit[0] | ||
roleConfigurationId := idSplit[1] | ||
rolePolicyNames := strings.Split(idSplit[2], tccommon.COMMA_SP) | ||
rolePolicyNameSet := make(map[string]struct{}) | ||
|
||
for _, rolePolicyName := range rolePolicyNames { | ||
rolePolicyNameSet[rolePolicyName] = struct{}{} | ||
} | ||
|
||
_ = d.Set("zone_id", zoneId) | ||
_ = d.Set("role_configuration_id", roleConfigurationId) | ||
|
||
respData, err := service.DescribeIdentityCenterRoleConfigurationPermissionPolicyAttachmentById(ctx, zoneId, roleConfigurationId, "Custom") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if respData == nil { | ||
d.SetId("") | ||
log.Printf("[WARN]%s resource `identity_center_role_configuration_permission_policy_attachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) | ||
return nil | ||
} | ||
|
||
if respData.RolePolicies != nil { | ||
policies := make([]interface{}, 0) | ||
for _, r := range respData.RolePolicies { | ||
if _, ok := rolePolicyNameSet[*r.RolePolicyName]; ok { | ||
policyMap := make(map[string]interface{}) | ||
|
||
if r.RolePolicyName != nil { | ||
policyMap["role_policy_name"] = *r.RolePolicyName | ||
} | ||
|
||
if r.RolePolicyType != nil { | ||
policyMap["role_policy_type"] = *r.RolePolicyType | ||
} | ||
|
||
if r.RolePolicyDocument != nil { | ||
policyMap["role_policy_document"] = *r.RolePolicyDocument | ||
} | ||
|
||
if r.AddTime != nil { | ||
policyMap["add_time"] = *r.AddTime | ||
} | ||
|
||
policies = append(policies, policyMap) | ||
} | ||
} | ||
_ = d.Set("policies", policies) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceTencentCloudIdentityCenterRoleConfigurationPermissionCustomPoliciesAttachmentDelete(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment.delete")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
logId := tccommon.GetLogId(tccommon.ContextNil) | ||
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) | ||
|
||
idSplit := strings.Split(d.Id(), tccommon.FILED_SP) | ||
if len(idSplit) != 3 { | ||
return fmt.Errorf("id is broken,%s", d.Id()) | ||
} | ||
zoneId := idSplit[0] | ||
roleConfigurationId := idSplit[1] | ||
rolePolicyNames := strings.Split(idSplit[2], tccommon.COMMA_SP) | ||
|
||
var ( | ||
request = organization.NewRemovePermissionPolicyFromRoleConfigurationRequest() | ||
) | ||
|
||
request.ZoneId = helper.String(zoneId) | ||
|
||
request.RoleConfigurationId = helper.String(roleConfigurationId) | ||
|
||
request.RolePolicyType = helper.String("Custom") | ||
|
||
request.RolePolicyId = helper.Int64(0) | ||
|
||
for _, rolePolicyName := range rolePolicyNames { | ||
request.RolePolicyName = helper.String(rolePolicyName) | ||
|
||
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { | ||
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().RemovePermissionPolicyFromRoleConfigurationWithContext(ctx, request) | ||
if e != nil { | ||
return tccommon.RetryError(e) | ||
} else { | ||
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
log.Printf("[CRITAL]%s delete identity center role configuration permission policy attachment failed, reason:%+v", logId, err) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
57 changes: 57 additions & 0 deletions
57
..._tc_identity_center_role_configuration_permission_custom_policies_attachment.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Provides a resource to create a organization tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment | ||
|
||
Example Usage | ||
|
||
```hcl | ||
resource "tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment" "identity_center_role_configuration_permission_custom_policies_attachment" { | ||
zone_id = "z-xxxxxx" | ||
role_configuration_id = "rc-xxxxxx" | ||
policies { | ||
role_policy_name = "CustomPolicy2" | ||
role_policy_document = <<-EOF | ||
{ | ||
"version": "2.0", | ||
"statement": [ | ||
{ | ||
"effect": "allow", | ||
"action": [ | ||
"vpc:AcceptAttachCcnInstances" | ||
], | ||
"resource": [ | ||
"*" | ||
] | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
policies { | ||
role_policy_name = "CustomPolicy1" | ||
role_policy_document = <<-EOF | ||
{ | ||
"version": "2.0", | ||
"statement": [ | ||
{ | ||
"effect": "allow", | ||
"action": [ | ||
"vpc:AcceptAttachCcnInstances" | ||
], | ||
"resource": [ | ||
"*" | ||
] | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
} | ||
``` | ||
|
||
Import | ||
|
||
organization tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment can be imported using the id, e.g. | ||
|
||
``` | ||
terraform import tencentcloud_identity_center_role_configuration_permission_custom_policies_attachment.identity_center_role_configuration_permission_custom_policies_attachment ${zoneId}#${roleConfigurationId}#${rolePolicyName1},...${rolePolicyNameN} | ||
``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.这个不支持修改?如果set中,有一个想移除,其它的都要先删除,然后重新绑定么?
2.这个是set,会顺序确定是无感的?