Skip to content

Commit 6f32492

Browse files
authored
Merge pull request #35 from brunopadz/feat/inspect-amis
feat: add inspect subcommand
2 parents 09b3d4d + f596935 commit 6f32492

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

cmd/aws.go

+7
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ var awsCmd = &cobra.Command{
1616

1717
func init() {
1818
rootCmd.AddCommand(awsCmd)
19+
20+
inspect.Flags().StringVarP(&ami, "ami", "a", "", "AMI ID")
21+
inspect.Flags().StringVarP(&region, "region", "r", "", "Region where the AMI was created")
22+
23+
inspect.MarkFlagRequired("ami")
24+
inspect.MarkFlagRequired("region")
25+
1926
}

cmd/awsInspect.go

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/brunopadz/amictl/pkg/utils"
9+
10+
"github.com/pterm/pterm"
11+
12+
"github.com/aws/aws-sdk-go-v2/aws"
13+
"github.com/aws/aws-sdk-go-v2/service/ec2"
14+
cfg "github.com/brunopadz/amictl/config"
15+
aaws "github.com/brunopadz/amictl/pkg/providers/aws"
16+
"github.com/spf13/cobra"
17+
"github.com/spf13/viper"
18+
)
19+
20+
func init() {
21+
awsCmd.AddCommand(inspect)
22+
}
23+
24+
var inspect = &cobra.Command{
25+
Use: "inspect",
26+
Short: "Inspect AMI",
27+
Long: `Inspect command shows additional info about AMIs`,
28+
Example: ` amictl aws inspect --region 123123123123 --ami ami-0x00000000f`,
29+
RunE: runInspect,
30+
}
31+
32+
var (
33+
ami string
34+
region string
35+
)
36+
37+
func runInspect(cmd *cobra.Command, _ []string) error {
38+
39+
var c cfg.Config
40+
41+
err := viper.Unmarshal(&c)
42+
if err != nil {
43+
fmt.Println(err)
44+
}
45+
46+
s, err := aaws.New(region)
47+
if err != nil {
48+
log.Fatalln("Couldn't create a session to AWS.")
49+
}
50+
51+
a := ec2.NewFromConfig(s)
52+
53+
i := &ec2.DescribeImagesInput{
54+
ImageIds: []string{
55+
ami,
56+
},
57+
Owners: []string{
58+
viper.GetString("aws.account"),
59+
},
60+
}
61+
62+
o, err := a.DescribeImages(context.TODO(), i)
63+
if err != nil {
64+
log.Fatalln("Couldn't get AMI data.")
65+
}
66+
67+
for _, v := range o.Images {
68+
69+
var (
70+
arch string
71+
image string
72+
state string
73+
device string
74+
boot string
75+
hypervisor string
76+
virtType string
77+
volumeType string
78+
)
79+
80+
switch v.Architecture {
81+
case "i386":
82+
arch = "i386"
83+
case "x86_64":
84+
arch = "x86_64"
85+
case "arm64":
86+
arch = "arm64"
87+
case "x86_64_mac":
88+
arch = "x86_64_mac"
89+
default:
90+
arch = "-"
91+
}
92+
93+
switch v.ImageType {
94+
case "machine":
95+
image = "machine"
96+
case "kernel":
97+
image = "kernel"
98+
case "ramdisk":
99+
image = "ramdisk"
100+
default:
101+
image = "-"
102+
}
103+
104+
switch v.State {
105+
case "available":
106+
state = "available"
107+
case "invalid":
108+
state = "invalid"
109+
case "deregistered":
110+
state = "deregistered"
111+
case "transient":
112+
state = "transient"
113+
case "failed":
114+
state = "failed"
115+
case "error":
116+
state = "error"
117+
default:
118+
state = "-"
119+
}
120+
121+
switch v.RootDeviceType {
122+
case "ebs":
123+
device = "ebs"
124+
case "instance-store":
125+
device = "instance-store"
126+
default:
127+
device = "-"
128+
}
129+
130+
switch v.BootMode {
131+
case "legacy-bios":
132+
boot = "legacy-bios"
133+
case "uefi":
134+
boot = "uefi"
135+
default:
136+
boot = "-"
137+
}
138+
139+
switch v.Hypervisor {
140+
case "ovm":
141+
hypervisor = "ovm"
142+
case "xen":
143+
hypervisor = "xen"
144+
default:
145+
hypervisor = "-"
146+
}
147+
148+
switch v.VirtualizationType {
149+
case "hvm":
150+
virtType = "hvm"
151+
case "paravirtual":
152+
virtType = "paravirtual"
153+
default:
154+
virtType = "-"
155+
}
156+
157+
pterm.FgLightCyan.Println("Displaying info for:", pterm.NewStyle(pterm.Bold).Sprint(aws.ToString(v.ImageId)))
158+
pterm.FgDarkGray.Println("----------------------------------------------")
159+
fmt.Println("Name:", utils.EmptyString(aws.ToString(v.Name)))
160+
fmt.Println("Description:", utils.EmptyString(aws.ToString(v.Description)))
161+
fmt.Println("Creation Date:", utils.EmptyString(aws.ToString(v.CreationDate)))
162+
fmt.Println("Deprecation Time:", utils.EmptyString(aws.ToString(v.DeprecationTime)))
163+
fmt.Println("Owner ID:", utils.EmptyString(aws.ToString(v.OwnerId)))
164+
fmt.Println("Owner Alias:", utils.EmptyString(aws.ToString(v.ImageOwnerAlias)))
165+
fmt.Println("State:", utils.EmptyString(state))
166+
fmt.Println("Root Device Name:", utils.EmptyString(aws.ToString(v.RootDeviceName)))
167+
fmt.Println("Root Device Type:", utils.EmptyString(device))
168+
fmt.Println("RAM Disk ID:", utils.EmptyString(aws.ToString(v.RamdiskId)))
169+
fmt.Println("Kernel ID:", utils.EmptyString(aws.ToString(v.KernelId)))
170+
fmt.Println("Architecture:", utils.EmptyString(arch))
171+
fmt.Println("Platform Details:", utils.EmptyString(aws.ToString(v.PlatformDetails)))
172+
fmt.Println("Image Type:", utils.EmptyString(image))
173+
fmt.Println("ENA Supported:", aws.ToBool(v.EnaSupport))
174+
fmt.Println("Boot Mode:", utils.EmptyString(boot))
175+
fmt.Println("Hypervisor:", utils.EmptyString(hypervisor))
176+
fmt.Println("Virtualization Type:", utils.EmptyString(virtType))
177+
fmt.Println("Block Device Mapping Info:")
178+
for _, bdm := range v.BlockDeviceMappings {
179+
switch bdm.Ebs.VolumeType {
180+
case "gp2":
181+
volumeType = "gp2"
182+
case "gp3":
183+
volumeType = "gp3"
184+
case "io1":
185+
volumeType = "io1"
186+
case "io2":
187+
volumeType = "io2"
188+
case "st1":
189+
volumeType = "st1"
190+
case "sc1":
191+
volumeType = "sc1"
192+
}
193+
fmt.Println(" Volume Size:", aws.ToInt32(bdm.Ebs.VolumeSize), "GB")
194+
fmt.Println(" Volume Type:", utils.EmptyString(volumeType))
195+
fmt.Println(" Snapshot ID:", utils.EmptyString(aws.ToString(bdm.Ebs.SnapshotId)))
196+
fmt.Println(" Encrypted:", aws.ToBool(bdm.Ebs.Encrypted))
197+
fmt.Println(" Delete on Termination:", aws.ToBool(bdm.Ebs.DeleteOnTermination))
198+
}
199+
fmt.Println("SR-IOV Net Support:", utils.EmptyString(aws.ToString(v.SriovNetSupport)))
200+
fmt.Println("Public:", aws.ToBool(v.Public))
201+
fmt.Println("Tags:")
202+
if len(v.Tags) == 0 {
203+
fmt.Println("-")
204+
} else {
205+
for _, t := range v.Tags {
206+
fmt.Println(" ", aws.ToString(t.Key), "=", aws.ToString(t.Value))
207+
}
208+
}
209+
210+
}
211+
212+
return nil
213+
}

pkg/utils/empty.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package utils
2+
3+
//EmptyString checks whether a string is empty or not
4+
func EmptyString(v string) string {
5+
if len(v) == 0 {
6+
v = "-"
7+
}
8+
9+
return v
10+
}

0 commit comments

Comments
 (0)