This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
aws_ec2.go
93 lines (78 loc) · 2.25 KB
/
aws_ec2.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
package resources
import (
"bytes"
"github.com/aws/aws-sdk-go/service/ec2"
)
// Ec2Instance represents resource for ec2 instances
type Ec2Instance struct {
NativeObject ec2.Instance
ShortOutputTemplate string
LongOutputTemplate string
}
// NewEc2Instance creates Ec2Instnace var with default render templates
func NewEc2Instance() *Ec2Instance {
return &Ec2Instance{
ShortOutputTemplate: DefaultShortOutputTemplate,
LongOutputTemplate: DefaultLongOutputTemplate,
}
}
// Name returns resource name
func (i *Ec2Instance) Name() string {
return i.GetTag("Name")
}
// ConnectIdentifier returns the identifier (eg. IP or DNS name) which will be used when connecting to instances
func (i *Ec2Instance) ConnectIdentifier(usePrivateID bool, useDNS bool) string {
if usePrivateID || i.NativeObject.PublicDnsName == nil || i.NativeObject.PublicIpAddress == nil {
if useDNS {
return *i.NativeObject.PrivateDnsName
}
return *i.NativeObject.PrivateIpAddress
}
// else we're using public ID
if useDNS {
return *i.NativeObject.PublicDnsName
}
return *i.NativeObject.PublicIpAddress
}
// ResourceID returns unique resource ID
func (i *Ec2Instance) ResourceID() string {
return *i.NativeObject.InstanceId
}
// GetTag returns tag value or empty string if not found
func (i *Ec2Instance) GetTag(tagName string) string {
for _, tag := range i.NativeObject.Tags {
if *tag.Key == tagName {
return *tag.Value
}
}
return ""
}
// RenderShortOutput renders the list output for resource
func (i *Ec2Instance) RenderShortOutput() string {
var tpl bytes.Buffer
t := ParseTemplate(i.ShortOutputTemplate)
// fmt.Print("%v",i.NativeObject)
err := t.Execute(&tpl, i.NativeObject)
if err != nil {
panic("ERROR WHILE PARSING TEMPLATE")
}
return tpl.String()
}
// RenderLongOutput renders the detailed output for resource
func (i *Ec2Instance) RenderLongOutput() string {
var tpl bytes.Buffer
t := ParseTemplate(i.LongOutputTemplate)
err := t.Execute(&tpl, i.NativeObject)
if err != nil {
panic("ERROR WHILE PARSING TEMPLATE")
}
return tpl.String()
}
// String returns resource attributes as string
func (i *Ec2Instance) String() string {
return i.NativeObject.String()
}
// SortKey returns sort key
func (i *Ec2Instance) SortKey() string {
return i.GetTag("Name")
}