yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/resource_tag.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aws
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  )
    27  
    28  func (self *SRegion) TagResources(arns []string, tags map[string]string) error {
    29  	if len(tags) == 0 {
    30  		return nil
    31  	}
    32  	client, err := self.getResourceGroupTagClient()
    33  	if err != nil {
    34  		return errors.Wrap(err, "self.getResourceGroupTagClient()")
    35  	}
    36  	params := resourcegroupstaggingapi.TagResourcesInput{}
    37  	arnInput := []*string{}
    38  	for i := range arns {
    39  		arnInput = append(arnInput, &arns[i])
    40  	}
    41  	tagsInput := make(map[string]*string)
    42  	tagValues := []string{}
    43  	for k, v := range tags {
    44  		tagValues = append(tagValues, v)
    45  		tagsInput[k] = &tagValues[len(tagValues)-1]
    46  	}
    47  	params.SetResourceARNList(arnInput)
    48  	params.SetTags(tagsInput)
    49  	out, err := client.TagResources(&params)
    50  	if err != nil {
    51  		return errors.Wrapf(err, "client.TagResources(%s)", jsonutils.Marshal(params).String())
    52  	}
    53  	if out != nil && len(out.FailedResourcesMap) > 0 {
    54  		return errors.Wrapf(cloudprovider.ErrNotSupported, "client.TagResources(%s),error:%s", jsonutils.Marshal(params).String(), jsonutils.Marshal(out).String())
    55  	}
    56  	return nil
    57  }
    58  
    59  func (self *SRegion) UntagResources(arns []string, tagKeys []string) error {
    60  	if len(tagKeys) == 0 {
    61  		return nil
    62  	}
    63  	client, err := self.getResourceGroupTagClient()
    64  	if err != nil {
    65  		return errors.Wrap(err, "self.getResourceGroupTagClient()")
    66  	}
    67  	params := resourcegroupstaggingapi.UntagResourcesInput{}
    68  	arnInput := []*string{}
    69  	for i := range arns {
    70  		arnInput = append(arnInput, &arns[i])
    71  	}
    72  	delTagKeysInput := []*string{}
    73  	for i := range tagKeys {
    74  		delTagKeysInput = append(delTagKeysInput, &tagKeys[i])
    75  	}
    76  	params.SetResourceARNList(arnInput)
    77  	params.SetTagKeys(delTagKeysInput)
    78  	out, err := client.UntagResources(&params)
    79  	if err != nil {
    80  		return errors.Wrapf(err, "client.UntagResources(%s)", jsonutils.Marshal(params).String())
    81  	}
    82  	if out != nil && len(out.FailedResourcesMap) > 0 {
    83  		return errors.Wrapf(cloudprovider.ErrNotSupported, "client.UntagResources(%s),error:%s", jsonutils.Marshal(params).String(), jsonutils.Marshal(out).String())
    84  	}
    85  	return nil
    86  }
    87  
    88  func (self *SRegion) UpdateResourceTags(arn string, oldTags, tags map[string]string, replace bool) error {
    89  	addTags := map[string]string{}
    90  	for k, v := range tags {
    91  		if strings.HasPrefix(k, "aws:") {
    92  			return errors.Wrap(cloudprovider.ErrNotSupported, "The aws: prefix is reserved for AWS use")
    93  		}
    94  		if _, ok := oldTags[k]; !ok {
    95  			addTags[k] = v
    96  		} else {
    97  			if oldTags[k] != v {
    98  				addTags[k] = v
    99  			}
   100  		}
   101  	}
   102  	delTags := []string{}
   103  	if replace {
   104  		for k := range oldTags {
   105  			if _, ok := tags[k]; !ok {
   106  				if !strings.HasPrefix(k, "aws:") {
   107  					delTags = append(delTags, k)
   108  				}
   109  			}
   110  		}
   111  	}
   112  	err := self.UntagResources([]string{arn}, delTags)
   113  	if err != nil {
   114  		return errors.Wrapf(err, "self.host.zone.region.UntagResources([]string{%s}, %s)", arn, jsonutils.Marshal(delTags).String())
   115  	}
   116  	err = self.TagResources([]string{arn}, addTags)
   117  	if err != nil {
   118  		return errors.Wrapf(err, "self.host.zone.region.TagResources([]string{%s}, %s)", arn, jsonutils.Marshal(addTags).String())
   119  	}
   120  	return nil
   121  }