Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions cmd/sg.go
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have securitygroups, securitygroupandsg` all the options here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean awsctl get securitygroups ??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

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...)
}
34 changes: 34 additions & 0 deletions pkg/ec2/getSG.go
Original file line number Diff line number Diff line change
@@ -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

}