From 35d934fce1f058cdf54def7898f194c1d5a746d5 Mon Sep 17 00:00:00 2001 From: yuwan Date: Tue, 11 Jun 2024 13:52:31 +0800 Subject: [PATCH] OCM-8693 | test: Add tag/untag role/policy functions --- pkg/aws/aws_client/policy.go | 25 +++++++++++++++++++++++++ pkg/aws/aws_client/role.go | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkg/aws/aws_client/policy.go b/pkg/aws/aws_client/policy.go index 18b6dcbd..74010527 100644 --- a/pkg/aws/aws_client/policy.go +++ b/pkg/aws/aws_client/policy.go @@ -161,3 +161,28 @@ func (client *AWSClient) CleanPolicies(cleanRule func(types.Policy) bool) error } return nil } + +func (client *AWSClient) TagPolicy(policyArn string, tags map[string]string) error { + var policyTags []types.Tag + for tagKey, tagValue := range tags { + policyTags = append(policyTags, types.Tag{ + Key: &tagKey, + Value: &tagValue, + }) + } + input := &iam.TagPolicyInput{ + PolicyArn: &policyArn, + Tags: policyTags, + } + _, err := client.IamClient.TagPolicy(context.TODO(), input) + return err +} + +func (client *AWSClient) UntagPolicy(policyArn string, tagKeys []string) error { + input := &iam.UntagPolicyInput{ + PolicyArn: &policyArn, + TagKeys: tagKeys, + } + _, err := client.IamClient.UntagPolicy(context.TODO(), input) + return err +} \ No newline at end of file diff --git a/pkg/aws/aws_client/role.go b/pkg/aws/aws_client/role.go index c7c00d8a..03d1e8ab 100644 --- a/pkg/aws/aws_client/role.go +++ b/pkg/aws/aws_client/role.go @@ -292,3 +292,28 @@ func completeRolePolicyDocument(statement map[string]interface{}) (string, error assumeRolePolicyDocument, err := json.Marshal(rolePolicyDocument) return string(assumeRolePolicyDocument), err } + +func (client *AWSClient) TagRole(roleName string, tags map[string]string) error { + var roleTags []types.Tag + for tagKey, tagValue := range tags { + roleTags = append(roleTags, types.Tag{ + Key: &tagKey, + Value: &tagValue, + }) + } + input := &iam.TagRoleInput{ + RoleName: &roleName, + Tags: roleTags, + } + _, err := client.IamClient.TagRole(context.TODO(), input) + return err +} + +func (client *AWSClient) UntagRole(roleName string, tagKeys []string) error { + input := &iam.UntagRoleInput{ + RoleName: &roleName, + TagKeys: tagKeys, + } + _, err := client.IamClient.UntagRole(context.TODO(), input) + return err +} \ No newline at end of file