Skip to content

Commit 4a0e475

Browse files
committed
ore aliyun: add ability to tag images
Add ability to tag images to facilate resource management.
1 parent 197d830 commit 4a0e475

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

mantle/cmd/ore/aliyun/tag-image.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2017 CoreOS, Inc.
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 aliyun
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"strings"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var (
26+
cmdTagImage = &cobra.Command{
27+
Use: "tag-image --id <id> --tags foo=bar ...",
28+
Short: "Tag an image",
29+
Run: runTagImage,
30+
}
31+
id string
32+
tags []string
33+
region string
34+
)
35+
36+
func init() {
37+
// Initialize the command and its flags
38+
Aliyun.AddCommand(cmdTagImage)
39+
cmdTagImage.Flags().StringVar(&id, "id", "", "Aliyun Image ID")
40+
cmdTagImage.Flags().StringVar(&region, "region", "", "Region")
41+
cmdTagImage.Flags().StringSliceVar(&tags, "tags", []string{}, "list of key=value tags to attach to the Aliyun image")
42+
}
43+
44+
func runTagImage(cmd *cobra.Command, args []string) {
45+
if id == "" {
46+
fmt.Fprintf(os.Stderr, "Provide --id to tag\n")
47+
os.Exit(1)
48+
}
49+
50+
if region == "" {
51+
fmt.Fprintf(os.Stderr, "Provide --region\n")
52+
os.Exit(1)
53+
}
54+
55+
tagMap := make(map[string]string)
56+
for _, tag := range tags {
57+
splitTag := strings.SplitN(tag, "=", 2)
58+
if len(splitTag) != 2 {
59+
fmt.Fprintf(os.Stderr, "invalid tag format; should be key=value, not %v\n", tag)
60+
os.Exit(1)
61+
}
62+
key, value := splitTag[0], splitTag[1]
63+
tagMap[key] = value
64+
}
65+
66+
err := API.CreateTags(id, tagMap, region)
67+
if err != nil {
68+
fmt.Fprintf(os.Stderr, "couldn't create image tags: %v", err)
69+
os.Exit(1)
70+
}
71+
}

mantle/platform/api/aliyun/api.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,32 @@ func (a *API) DeleteImage(id string, force bool) error {
370370
return errs.AsError()
371371
}
372372

373+
func (a *API) CreateTags(resource string, tags map[string]string, region string) error {
374+
request := ecs.CreateAddTagsRequest()
375+
request.SetConnectTimeout(defaultConnectTimeout)
376+
request.SetReadTimeout(defaultReadTimeout)
377+
request.ResourceId = resource
378+
request.RegionId = region
379+
request.ResourceType = "image"
380+
381+
tagObjs := make([]ecs.AddTagsTag, 0, len(tags))
382+
for key, value := range tags {
383+
tagObjs = append(tagObjs, ecs.AddTagsTag{
384+
Key: key,
385+
Value: value,
386+
})
387+
}
388+
389+
request.Tag = &tagObjs
390+
response, err := a.ecs.AddTags(request)
391+
plog.Infof("response %v", response)
392+
if err != nil {
393+
return fmt.Errorf("unable to add tags: %v", err)
394+
}
395+
396+
return err
397+
}
398+
373399
// DeleteSnapshot deletes a snapshot
374400
func (a *API) DeleteSnapshot(id string, force bool) error {
375401
request := ecs.CreateDeleteSnapshotRequest()

0 commit comments

Comments
 (0)