Skip to content

Commit

Permalink
modify for multiresources
Browse files Browse the repository at this point in the history
  • Loading branch information
zhurenzhu.zrz committed May 25, 2023
1 parent bc736c0 commit ce5e71f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
22 changes: 21 additions & 1 deletion client_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewProjectTags(project string, tags []ResourceTag) *ResourceTags {
}
}

// NewProjectUnTags create a project tags
// NewProjectUnTags delete a project tags
func NewProjectUnTags(project string, tags []string) *ResourceUnTags {
return &ResourceUnTags{
ResourceType: "project",
Expand All @@ -59,6 +59,22 @@ func NewProjectUnTags(project string, tags []string) *ResourceUnTags {
}
}

func NewResourceTags(resourceType string, resourceId string, tags []ResourceTag) *ResourceTags {
return &ResourceTags{
ResourceType: resourceType,
ResourceID: []string{resourceId},
Tags: tags,
}
}

func NewResourceUnTags(resourceType string, resourceId string, tags []string) *ResourceUnTags {
return &ResourceUnTags{
ResourceType: resourceType,
ResourceID: []string{resourceId},
Tags: tags,
}
}

// TagResources tag specific resource
func (c *Client) TagResources(project string, tags *ResourceTags) error {
body, err := json.Marshal(tags)
Expand Down Expand Up @@ -142,3 +158,7 @@ func (c *Client) ListTagResources(project string,
}
return listTagResp.TagResource, listTagResp.NextToken, err
}

func GenResourceId(project string, subResourceId string) string {
return project + "#" + subResourceId
}
60 changes: 60 additions & 0 deletions example/tag/subresource_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/example/util"
)

func main() {
fmt.Println("Tag Resource")
resouceId := sls.GenResourceId(util.ProjectName, util.LogStoreName)
resourceTags := sls.NewResourceTags("logstore", resouceId, []sls.ResourceTag{
{
Key: "the-tag",
Value: "aliyun-log-go-sdk",
},
{
Key: "the-tag-2",
Value: "aliyun log go sdk",
},
})
err := util.Client.TagResources(util.ProjectName, resourceTags)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Tag Resource success")
}
listTags("logstore")

fmt.Println("UnTag Resource")
resouceUnTags := sls.NewResourceUnTags("logstore", resouceId, []string{"the-tag"})

err = util.Client.UnTagResources(util.ProjectName, resouceUnTags)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("UnTag Resource success")
}
listTags("logstore")
}

func listTags(resourceType string) {
fmt.Println("tag list: ")
respTags, _, err := util.Client.ListTagResources(
util.ProjectName,
resourceType,
[]string{sls.GenResourceId(util.ProjectName, util.LogStoreName)},
[]sls.ResourceFilterTag{},
"")
if err != nil {
panic(err)
}
for _, tag := range respTags {
fmt.Printf(" resourceType : %s, resourceID : %s, tagKey : %s, tagValue : %s\n",
tag.ResourceType,
tag.ResourceID,
tag.TagKey,
tag.TagValue)
}
}

0 comments on commit ce5e71f

Please # to comment.