-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytags.go
130 lines (116 loc) · 2.76 KB
/
mytags.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"os"
_ "strings"
)
const (
AppVersion = "0.0.2"
)
type Tags struct {
Tags []Tag `json:"tags"`
}
type Tag struct {
Key string `json:"key"`
Value string `json:"value"`
}
var (
argProfile = flag.String("profile", "", "Profile 名を指定.")
argRegion = flag.String("region", "ap-northeast-1", "Region 名を指定.")
argEndpoint = flag.String("endpoint", "", "AWS API のエンドポイントを指定.")
argKey = flag.String("key", "", "取得したいタグのキーを指定..")
argVersion = flag.Bool("version", false, "バージョンを出力.")
)
func awsEc2Client(profile string, region string) *ec2.EC2 {
var config aws.Config
if profile != "" {
creds := credentials.NewSharedCredentials("", profile)
config = aws.Config{Region: aws.String(region), Credentials: creds, Endpoint: aws.String(*argEndpoint)}
} else {
config = aws.Config{Region: aws.String(region), Endpoint: aws.String(*argEndpoint)}
}
sess := session.New(&config)
ec2Client := ec2.New(sess)
return ec2Client
}
func myInstanceid() (instanceId string) {
sess := session.Must(session.NewSession())
svc := ec2metadata.New(sess)
doc, _ := svc.GetInstanceIdentityDocument()
instanceId = doc.InstanceID
return instanceId
}
func myTags(ec2Client *ec2.EC2, instanceId string) [][]string {
var instanceIds []*string
instanceIds = append(instanceIds, aws.String(instanceId))
input := &ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: instanceIds,
},
},
}
result, err := ec2Client.DescribeTags(input)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var tags [][]string
var tag []string
for _, t := range result.Tags {
tag = []string{
*t.Key,
*t.Value,
}
tags = append(tags, tag)
}
return tags
}
func outputSingleTag(data [][]string, key string) {
var v string
for _, record := range data {
if key == record[0] {
v = record[1]
}
}
fmt.Printf(v)
}
func outputJson(data [][]string) {
var rs []Tag
for _, record := range data {
r := Tag{Key: record[0], Value: record[1]}
rs = append(rs, r)
}
rj := Tags{
Tags: rs,
}
b, err := json.Marshal(rj)
if err != nil {
fmt.Println("JSON Marshal error:", err)
return
}
os.Stdout.Write(b)
}
func main() {
flag.Parse()
if *argVersion {
fmt.Println(AppVersion)
os.Exit(0)
}
ec2Client := awsEc2Client(*argProfile, *argRegion)
instanceId := myInstanceid()
allTags := myTags(ec2Client, instanceId)
if *argKey != "" {
outputSingleTag(allTags, *argKey)
} else {
outputJson(allTags)
}
}