-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag.go
152 lines (124 loc) · 3.6 KB
/
tag.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Proof of Concepts for the Cloud-Barista Multi-Cloud Project.
// * Cloud-Barista: https://github.com/cloud-barista
//
// This code is based on the following material: https://github.com/mindjiver/gopherstack (MIT License)
//
// KT Cloud SDK go
//
// by ETRI, 2021.07.
package ktcloudsdk
import (
"net/url"
"strconv"
"strings"
)
type CreateTagsReqInfo struct {
ResourceType string `json:"resourcetype`
ResourceIds []string `json:"resourceids"`
Tags []TagArg `json:"tags`
}
type ListTagsReqInfo struct {
Account string `json:"account"`
DomainId string `json:"domainid"`
Key string `json:"key"`
Value string `json:"value`
ResourceType string `json:"resourcetype`
ResourceIds string `json:"resourceids"`
}
type DeleteTagsReqInfo struct {
ResourceType string `json:"resourcetype`
ResourceIds []string `json:"resourceids"`
Tags []TagArg `json:"tags`
}
type TagArg struct {
Key string `json:"key"`
Value string `json:"value"`
}
// Add tags to specified resources
func (c KtCloudClient) CreateTags(options *CreateTagsReqInfo) (CreateTagsResponse, error) {
var resp CreateTagsResponse
params := url.Values{}
params.Set("resourceids", strings.Join(options.ResourceIds, ","))
params.Set("resourcetype", options.ResourceType)
for j, tag := range options.Tags {
params.Set("tags["+strconv.Itoa(j+1)+"].key", tag.Key)
params.Set("tags["+strconv.Itoa(j+1)+"].value", tag.Value)
}
response, err := NewRequest(c, "createTags", params)
if err != nil {
return resp, err
}
resp = response.(CreateTagsResponse)
return resp, err
}
// Returns all items with a particular tag
func (c KtCloudClient) ListTags(options *ListTagsReqInfo) (ListTagsResponse, error) {
var resp ListTagsResponse
params := url.Values{}
if options.Account != "" {
params.Set("account", options.Account)
}
if options.DomainId != "" {
params.Set("domainid", options.DomainId)
}
if options.Key != "" {
params.Set("key", options.Key)
}
if options.Value != "" {
params.Set("value", options.Value)
}
if options.ResourceIds != "" {
params.Set("resourceid", options.ResourceIds)
}
if options.ResourceType != "" {
params.Set("resourcetype", options.ResourceType)
}
response, err := NewRequest(c, "listTags", params)
if err != nil {
return resp, err
}
resp = response.(ListTagsResponse)
return resp, err
}
// Remove tags from specified resources
func (c KtCloudClient) DeleteTags(options *DeleteTagsReqInfo) (DeleteTagsResponse, error) {
var resp DeleteTagsResponse
params := url.Values{}
params.Set("resourceids", strings.Join(options.ResourceIds, ","))
params.Set("resourcetype", options.ResourceType)
for j, tag := range options.Tags {
params.Set("tags["+strconv.Itoa(j+1)+"].key", tag.Key)
params.Set("tags["+strconv.Itoa(j+1)+"].value", tag.Value)
}
response, err := NewRequest(c, "deleteTags", params)
if err != nil {
return resp, err
}
resp = response.(DeleteTagsResponse)
return resp, err
}
type Tag struct {
Key string `json:"key"`
Value string `json:"value`
ResourceType string `json:"resourcetype`
ResourceId string `json:"resourceid"`
Account string `json:"account"`
DomainId string `json:"domainid"`
Domain string `json:"domain"`
}
type CreateTagsResponse struct {
Createtagsresponse struct {
JobId string `json:"jobid"`
} `json:"createtagsresponse"`
}
type ListTagsResponse struct {
Listtagsresponse struct {
Count int `json:"count"`
Tag []Tag `json:"tag"`
} `json:"listtagsresponse"`
}
type DeleteTagsResponse struct {
Deletetagsresponse struct {
JobId string `json:"jobid"`
} `json:"deletetagsresponse"`
}