-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathami.go
More file actions
146 lines (111 loc) · 4.09 KB
/
ami.go
File metadata and controls
146 lines (111 loc) · 4.09 KB
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
package window
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
type (
AMI struct {
// The architecture of the image.
Architecture string
// Any block device mapping entries.
BlockDeviceMappings []*ec2.BlockDeviceMapping
// The date and time the image was created.
CreationDate time.Time
// The description of the AMI that was provided during image creation.
Description string
// The hypervisor type of the image.
Hypervisor string
// The ID of the AMI.
ImageId string
// The location of the AMI.
ImageLocation string
// The AWS account alias (for example, amazon, self) or the AWS account ID of
// the AMI owner.
ImageOwnerAlias string
// The type of image.
ImageType string
// The kernel associated with the image, if any. Only applicable for machine
// images.
KernelId string
// The name of the AMI that was provided during image creation.
Name string
// The AWS account ID of the image owner.
OwnerId string
// The value is Windows for Windows AMIs; otherwise blank.
Platform string
// Any product codes associated with the AMI.
ProductCodes []*ec2.ProductCode
// Indicates whether the image has public launch permissions. The value is true
// if this image has public launch permissions or false if it has only implicit
// and explicit launch permissions.
Public bool
// The RAM disk associated with the image, if any. Only applicable for machine
// images.
RamdiskId string
// The device name of the root device (for example, /dev/sda1 or /dev/xvda).
RootDeviceName string
// The type of root device used by the AMI. The AMI can use an EBS volume or
// an instance store volume.
RootDeviceType string
// Specifies whether enhanced networking is enabled.
SriovNetSupport string
// The current state of the AMI. If the state is available, the image is successfully
// registered and can be used to launch an instance.
State string
// The reason for the state change.
StateReason *ec2.StateReason
// Any tags assigned to the image.
Tags []*ec2.Tag
// The type of virtualization of the AMI.
VirtualizationType string
Id string
Instances []*Instance
}
AMIByNameAsc []*AMI
)
func (a AMIByNameAsc) Len() int { return len(a) }
func (a AMIByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a AMIByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func LoadAMIs(input *ec2.DescribeImagesInput) (map[string]*AMI, error) {
resp, err := EC2Client.DescribeImages(input)
if err != nil {
return nil, err
}
images := make(map[string]*AMI, len(resp.Images))
for _, img := range resp.Images {
ami := &AMI{
Architecture: aws.StringValue(img.Architecture),
BlockDeviceMappings: img.BlockDeviceMappings,
Description: aws.StringValue(img.Description),
Hypervisor: aws.StringValue(img.Hypervisor),
ImageId: aws.StringValue(img.ImageId),
ImageLocation: aws.StringValue(img.ImageLocation),
ImageOwnerAlias: aws.StringValue(img.ImageOwnerAlias),
ImageType: aws.StringValue(img.ImageType),
KernelId: aws.StringValue(img.KernelId),
Name: aws.StringValue(img.Name),
OwnerId: aws.StringValue(img.OwnerId),
Platform: aws.StringValue(img.Platform),
ProductCodes: img.ProductCodes,
Public: aws.BoolValue(img.Public),
RamdiskId: aws.StringValue(img.RamdiskId),
RootDeviceName: aws.StringValue(img.RootDeviceName),
RootDeviceType: aws.StringValue(img.RootDeviceType),
SriovNetSupport: aws.StringValue(img.SriovNetSupport),
State: aws.StringValue(img.State),
StateReason: img.StateReason,
Tags: img.Tags,
VirtualizationType: aws.StringValue(img.VirtualizationType),
}
ami.CreationDate, _ = time.Parse(time.RFC3339Nano, aws.StringValue(img.CreationDate))
ami.Id = "ami:" + ami.ImageId
images[ami.ImageId] = ami
}
return images, nil
}
func (ami *AMI) Inactive() bool {
return len(ami.Instances) == 0
}