企业路由器 ER-批量添加删除资源标签:Go

时间:2023-12-06 15:04:41

Go

  • 批量添加资源标签。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        er "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/er/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/er/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/er/v3/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := er.NewErClient(
            er.ErClientBuilder().
                WithRegion(region.ValueOf("cn-north-4")).
                WithCredential(auth).
                Build())
    
        request := &model.BatchCreateResourceTagsRequest{}
    	keyTags:= "key1"
    	valueTags:= "value1"
    	keyTags1:= "key2"
    	valueTags1:= "value2"
    	var listTagsbody = []model.Tag{
            {
                Key: &keyTags,
                Value: &valueTags,
            },
            {
                Key: &keyTags1,
                Value: &valueTags1,
            },
        }
    	request.Body = &model.BatchOperateResourceTagsRequestBody{
    		Tags: &listTagsbody,
    		Action: model.GetBatchOperateResourceTagsRequestBodyActionEnum().CREATE,
    	}
    	response, err := client.BatchCreateResourceTags(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 批量删除资源标签。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        er "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/er/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/er/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/er/v3/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := er.NewErClient(
            er.ErClientBuilder().
                WithRegion(region.ValueOf("cn-north-4")).
                WithCredential(auth).
                Build())
    
        request := &model.BatchCreateResourceTagsRequest{}
    	keyTags:= "key1"
    	keyTags1:= "key2"
    	valueTags:= "value3"
    	var listTagsbody = []model.Tag{
            {
                Key: &keyTags,
            },
            {
                Key: &keyTags1,
                Value: &valueTags,
            },
        }
    	request.Body = &model.BatchOperateResourceTagsRequestBody{
    		Tags: &listTagsbody,
    		Action: model.GetBatchOperateResourceTagsRequestBodyActionEnum().DELETE,
    	}
    	response, err := client.BatchCreateResourceTags(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
support.huaweicloud.com/api-er/BatchCreateResourceTags.html