Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Security Group #61

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 55 additions & 0 deletions cmd/sg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"fmt"
"log"
"os"
"text/tabwriter"

"github.com/spf13/cobra"
"github.com/surajincloud/awsctl/pkg/ec2"
)



var sgCmd = &cobra.Command{

Use: "sgrp",
Short: "Print Security Groups",
Long: `
For Example: $ awsctl get sgrp
`,
Run: getSG,

}

func getSG(cmd *cobra.Command, args[]string){

var sgroup []ec2.SECURITYGRP

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(sgCmd)
}
riteshsonawane1372 marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 42 additions & 0 deletions pkg/ec2/getSG.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 SECURITYGRP struct {
Copy link
Member

Choose a reason for hiding this comment

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

use camel case and split the security group and ssh in two different PRs.

each PR should have only one feature command. it's easy to review and focus.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay


SGName string
SGId string
SGDescription string

}


func DescribeSecurityGroup(cmd *cobra.Command, args []string) ([]SECURITYGRP,error){

var securityGrp []SECURITYGRP

ctx,client:= Ec2Client(cmd,args)
input:= &ec2.DescribeSecurityGroupsInput{}
info,err:= client.DescribeSecurityGroups(ctx,input)


for _, i:= range info.SecurityGroups{
securityGrp=append(securityGrp, SECURITYGRP{
SGName: aws.ToString(i.GroupName),
SGId: aws.ToString(i.GroupId),
SGDescription: aws.ToString(i.Description),

})
}

return securityGrp,err

}