diff --git a/cmd/sg.go b/cmd/sg.go new file mode 100644 index 0000000..b305a7b --- /dev/null +++ b/cmd/sg.go @@ -0,0 +1,71 @@ +package cmd + +import ( + "fmt" + "log" + "os" + "text/tabwriter" + + "github.com/spf13/cobra" + "github.com/surajincloud/awsctl/pkg/ec2" +) + +var sgCmd = &cobra.Command{ + + Use: "sg", + Short: "Print Security Groups", + Long: ` + For Example: $ awsctl get securitygroup + $ awsctl get securitygroups + $ awsctl get sg + `, + Run: getSG, +} + +var securitygrp=&cobra.Command{ + Use: "securitygroup", + Short: "Print Security Groups", + Run: getSG, +} +var securitygrps=&cobra.Command{ + Use:"securitygroups", + Short: "Print Security Groups", + Run:getSG, +} + +var group=[]*cobra.Command{ + securitygrp, + securitygrps, + sgCmd, +} + +func getSG(cmd *cobra.Command, args []string) { + + var sgroup []ec2.SecurityGroup + + sgroup, err := ec2.DescribeSecurityGroup(cmd, args) + if err != nil { + log.Fatal("Unable to get Security Group") + } + + w := tabwriter.NewWriter(os.Stdout, 18, 5, 3, ' ', tabwriter.TabIndent) + defer w.Flush() + + fmt.Fprintln(w, "NAME", "\t", "GROUP ID", "\t", "DESCRIPTION") + + for _, i := range sgroup { + + fmt.Fprintln(w, i.SGName, "\t", + i.SGId, "\t", + i.SGDescription, "\t", + ) + + } + +} + + + +func init() { + getCmd.AddCommand(group...) +} diff --git a/pkg/ec2/getSG.go b/pkg/ec2/getSG.go new file mode 100644 index 0000000..68596b2 --- /dev/null +++ b/pkg/ec2/getSG.go @@ -0,0 +1,34 @@ +package ec2 + +import ( + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/spf13/cobra" +) + +type SecurityGroup struct { + SGName string + SGId string + SGDescription string +} + +func DescribeSecurityGroup(cmd *cobra.Command, args []string) ([]SecurityGroup, error) { + + var securityGrp []SecurityGroup + + ctx, client := Ec2Client(cmd, args) + input := &ec2.DescribeSecurityGroupsInput{} + info, err := client.DescribeSecurityGroups(ctx, input) + + for _, i := range info.SecurityGroups { + securityGrp = append(securityGrp, SecurityGroup{ + SGName: aws.ToString(i.GroupName), + SGId: aws.ToString(i.GroupId), + SGDescription: aws.ToString(i.Description), + }) + } + + return securityGrp, err + +} +