From f3516211041aad7cd51999e6508bd6868164af0f Mon Sep 17 00:00:00 2001 From: brunopadz Date: Sun, 10 Oct 2021 22:39:27 -0300 Subject: [PATCH 1/2] feat: add initial work on inspect subcommand Signed-off-by: brunopadz --- cmd/aws.go | 7 ++++ cmd/awsInspect.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 cmd/awsInspect.go diff --git a/cmd/aws.go b/cmd/aws.go index 5de1108..0d88eb4 100644 --- a/cmd/aws.go +++ b/cmd/aws.go @@ -16,4 +16,11 @@ var awsCmd = &cobra.Command{ func init() { rootCmd.AddCommand(awsCmd) + + inspect.Flags().StringVarP(&ami, "ami", "a", "", "AMI ID") + inspect.Flags().StringVarP(®ion, "region", "r", "", "Region where the AMI was created") + + inspect.MarkFlagRequired("ami") + inspect.MarkFlagRequired("region") + } diff --git a/cmd/awsInspect.go b/cmd/awsInspect.go new file mode 100644 index 0000000..1b87da6 --- /dev/null +++ b/cmd/awsInspect.go @@ -0,0 +1,85 @@ +package cmd + +import ( + "context" + "fmt" + "log" + + "github.com/pterm/pterm" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + cfg "github.com/brunopadz/amictl/config" + aaws "github.com/brunopadz/amictl/pkg/providers/aws" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func init() { + awsCmd.AddCommand(inspect) +} + +var inspect = &cobra.Command{ + Use: "inspect", + Short: "Inspect AMI", + Long: `Inspect command shows additional info about AMIs`, + Example: ` amictl aws inspect`, + RunE: runInspect, +} + +var ( + ami string + region string +) + +func runInspect(cmd *cobra.Command, _ []string) error { + + var c cfg.Config + + err := viper.Unmarshal(&c) + if err != nil { + fmt.Println(err) + } + + s, err := aaws.New(region) + if err != nil { + log.Fatalln("Couldn't create a session to AWS.") + } + + a := ec2.NewFromConfig(s) + + input := &ec2.DescribeImagesInput{ + ImageIds: []string{ + ami, + }, + Owners: []string{ + viper.GetString("aws.account"), + }, + } + + o, err := a.DescribeImages(context.TODO(), input) + if err != nil { + log.Fatalln("Couldn't get AMI data.") + } + + for _, v := range o.Images { + pterm.FgLightCyan.Println("Displaying info for:", pterm.NewStyle(pterm.Bold).Sprint(aws.ToString(v.ImageId))) + pterm.FgDarkGray.Println("----------------------------------------------") + fmt.Println("Name:", aws.ToString(v.Name)) + fmt.Println("Description:", aws.ToString(v.Description)) + fmt.Println("Creation Date:", aws.ToString(v.CreationDate)) + fmt.Println("Deprecation Time:", aws.ToString(v.DeprecationTime)) + fmt.Println("Root Device Name:", aws.ToString(v.RootDeviceName)) + fmt.Println("RAM Disk ID:", aws.ToString(v.RamdiskId)) + fmt.Println("Kernel ID:", aws.ToString(v.KernelId)) + fmt.Println("Platform Details:", aws.ToString(v.PlatformDetails)) + fmt.Println("Public:", aws.ToBool(v.Public)) + fmt.Println("Tags:") + for _, t := range v.Tags { + fmt.Println(" ", aws.ToString(t.Key), "=", aws.ToString(t.Value)) + } + + } + + return nil +} From f596935185a014deac03e12d68debe65f020ba53 Mon Sep 17 00:00:00 2001 From: brunopadz Date: Mon, 11 Oct 2021 17:16:08 -0300 Subject: [PATCH 2/2] feat: add more information about ami Signed-off-by: brunopadz --- cmd/awsInspect.go | 154 +++++++++++++++++++++++++++++++++++++++++---- pkg/utils/empty.go | 10 +++ 2 files changed, 151 insertions(+), 13 deletions(-) create mode 100644 pkg/utils/empty.go diff --git a/cmd/awsInspect.go b/cmd/awsInspect.go index 1b87da6..4e9a530 100644 --- a/cmd/awsInspect.go +++ b/cmd/awsInspect.go @@ -5,6 +5,8 @@ import ( "fmt" "log" + "github.com/brunopadz/amictl/pkg/utils" + "github.com/pterm/pterm" "github.com/aws/aws-sdk-go-v2/aws" @@ -23,7 +25,7 @@ var inspect = &cobra.Command{ Use: "inspect", Short: "Inspect AMI", Long: `Inspect command shows additional info about AMIs`, - Example: ` amictl aws inspect`, + Example: ` amictl aws inspect --region 123123123123 --ami ami-0x00000000f`, RunE: runInspect, } @@ -48,7 +50,7 @@ func runInspect(cmd *cobra.Command, _ []string) error { a := ec2.NewFromConfig(s) - input := &ec2.DescribeImagesInput{ + i := &ec2.DescribeImagesInput{ ImageIds: []string{ ami, }, @@ -57,26 +59,152 @@ func runInspect(cmd *cobra.Command, _ []string) error { }, } - o, err := a.DescribeImages(context.TODO(), input) + o, err := a.DescribeImages(context.TODO(), i) if err != nil { log.Fatalln("Couldn't get AMI data.") } for _, v := range o.Images { + + var ( + arch string + image string + state string + device string + boot string + hypervisor string + virtType string + volumeType string + ) + + switch v.Architecture { + case "i386": + arch = "i386" + case "x86_64": + arch = "x86_64" + case "arm64": + arch = "arm64" + case "x86_64_mac": + arch = "x86_64_mac" + default: + arch = "-" + } + + switch v.ImageType { + case "machine": + image = "machine" + case "kernel": + image = "kernel" + case "ramdisk": + image = "ramdisk" + default: + image = "-" + } + + switch v.State { + case "available": + state = "available" + case "invalid": + state = "invalid" + case "deregistered": + state = "deregistered" + case "transient": + state = "transient" + case "failed": + state = "failed" + case "error": + state = "error" + default: + state = "-" + } + + switch v.RootDeviceType { + case "ebs": + device = "ebs" + case "instance-store": + device = "instance-store" + default: + device = "-" + } + + switch v.BootMode { + case "legacy-bios": + boot = "legacy-bios" + case "uefi": + boot = "uefi" + default: + boot = "-" + } + + switch v.Hypervisor { + case "ovm": + hypervisor = "ovm" + case "xen": + hypervisor = "xen" + default: + hypervisor = "-" + } + + switch v.VirtualizationType { + case "hvm": + virtType = "hvm" + case "paravirtual": + virtType = "paravirtual" + default: + virtType = "-" + } + pterm.FgLightCyan.Println("Displaying info for:", pterm.NewStyle(pterm.Bold).Sprint(aws.ToString(v.ImageId))) pterm.FgDarkGray.Println("----------------------------------------------") - fmt.Println("Name:", aws.ToString(v.Name)) - fmt.Println("Description:", aws.ToString(v.Description)) - fmt.Println("Creation Date:", aws.ToString(v.CreationDate)) - fmt.Println("Deprecation Time:", aws.ToString(v.DeprecationTime)) - fmt.Println("Root Device Name:", aws.ToString(v.RootDeviceName)) - fmt.Println("RAM Disk ID:", aws.ToString(v.RamdiskId)) - fmt.Println("Kernel ID:", aws.ToString(v.KernelId)) - fmt.Println("Platform Details:", aws.ToString(v.PlatformDetails)) + fmt.Println("Name:", utils.EmptyString(aws.ToString(v.Name))) + fmt.Println("Description:", utils.EmptyString(aws.ToString(v.Description))) + fmt.Println("Creation Date:", utils.EmptyString(aws.ToString(v.CreationDate))) + fmt.Println("Deprecation Time:", utils.EmptyString(aws.ToString(v.DeprecationTime))) + fmt.Println("Owner ID:", utils.EmptyString(aws.ToString(v.OwnerId))) + fmt.Println("Owner Alias:", utils.EmptyString(aws.ToString(v.ImageOwnerAlias))) + fmt.Println("State:", utils.EmptyString(state)) + fmt.Println("Root Device Name:", utils.EmptyString(aws.ToString(v.RootDeviceName))) + fmt.Println("Root Device Type:", utils.EmptyString(device)) + fmt.Println("RAM Disk ID:", utils.EmptyString(aws.ToString(v.RamdiskId))) + fmt.Println("Kernel ID:", utils.EmptyString(aws.ToString(v.KernelId))) + fmt.Println("Architecture:", utils.EmptyString(arch)) + fmt.Println("Platform Details:", utils.EmptyString(aws.ToString(v.PlatformDetails))) + fmt.Println("Image Type:", utils.EmptyString(image)) + fmt.Println("ENA Supported:", aws.ToBool(v.EnaSupport)) + fmt.Println("Boot Mode:", utils.EmptyString(boot)) + fmt.Println("Hypervisor:", utils.EmptyString(hypervisor)) + fmt.Println("Virtualization Type:", utils.EmptyString(virtType)) + fmt.Println("Block Device Mapping Info:") + for _, bdm := range v.BlockDeviceMappings { + switch bdm.Ebs.VolumeType { + case "gp2": + volumeType = "gp2" + case "gp3": + volumeType = "gp3" + case "io1": + volumeType = "io1" + case "io2": + volumeType = "io2" + case "st1": + volumeType = "st1" + case "sc1": + volumeType = "sc1" + } + fmt.Println(" Volume Size:", aws.ToInt32(bdm.Ebs.VolumeSize), "GB") + fmt.Println(" Volume Type:", utils.EmptyString(volumeType)) + fmt.Println(" Snapshot ID:", utils.EmptyString(aws.ToString(bdm.Ebs.SnapshotId))) + fmt.Println(" Encrypted:", aws.ToBool(bdm.Ebs.Encrypted)) + fmt.Println(" Delete on Termination:", aws.ToBool(bdm.Ebs.DeleteOnTermination)) + } + fmt.Println("SR-IOV Net Support:", utils.EmptyString(aws.ToString(v.SriovNetSupport))) fmt.Println("Public:", aws.ToBool(v.Public)) fmt.Println("Tags:") - for _, t := range v.Tags { - fmt.Println(" ", aws.ToString(t.Key), "=", aws.ToString(t.Value)) + if len(v.Tags) == 0 { + fmt.Println("-") + } else { + for _, t := range v.Tags { + fmt.Println(" ", aws.ToString(t.Key), "=", aws.ToString(t.Value)) + } } } diff --git a/pkg/utils/empty.go b/pkg/utils/empty.go new file mode 100644 index 0000000..944af49 --- /dev/null +++ b/pkg/utils/empty.go @@ -0,0 +1,10 @@ +package utils + +//EmptyString checks whether a string is empty or not +func EmptyString(v string) string { + if len(v) == 0 { + v = "-" + } + + return v +}