Skip to content

Commit

Permalink
Merge pull request #51 from riteshsonawane1372/main
Browse files Browse the repository at this point in the history
Added Instance state and solved #44
  • Loading branch information
surajnarwade authored May 31, 2023
2 parents acf6fe3 + fa8832f commit ea62ae1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 21 deletions.
10 changes: 6 additions & 4 deletions cmd/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@ func getEC2(cmd *cobra.Command, args []string) {
if Keys!="" && strings.Contains(Keys, "=") {
tags := strings.SplitN(Keys, "=", 2)
value := strings.Split(tags[1], ",")
ec2Instance = ec2.DescribeInstance(*&tags[0], value,cmd,args)
ec2Instance = ec2.DescribeInstance(tags[0], value,cmd,args)
} else if Keys!=""{
ec2Instance = ec2.DescribeInstance(*&Keys, nil,cmd,args)
ec2Instance = ec2.DescribeInstance(Keys, nil,cmd,args)
}else{
ec2Instance = ec2.DescribeInstance("", nil,cmd,args)
}

w := tabwriter.NewWriter(os.Stdout, 18, 5, 3, ' ', tabwriter.TabIndent)
defer w.Flush()

fmt.Fprintln(w, "NAME", "\t", "INSTANCE_ID", "\t", "INSTANCE_TYPE", "\t", "PRIVATE IP")
fmt.Fprintln(w, "NAME", "\t", "INSTANCE_ID", "\t", "INSTANCE_TYPE", "\t", "PRIVATE IP","\t", "INSTANCE_STATUS")
for _, i := range ec2Instance {
fmt.Fprintln(
w, i.InstanceName, "\t",
i.InstanceID, "\t",
i.InstanceType, "\t",
i.InstancePrivateIP)
i.InstancePrivateIP, "\t",
i.InstanceStatus,
)
}

}
Expand Down
10 changes: 0 additions & 10 deletions cmd/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,4 @@ func getS3(cmd *cobra.Command, args []string) error {

func init() {
getCmd.AddCommand(s3Cmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// s3Cmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// s3Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
6 changes: 3 additions & 3 deletions pkg/VPC/getVPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Vpc struct {

func GetVPC() ([]Vpc, error) {

var getVpc []Vpc
var vpcList []Vpc

ctx := context.TODO()
cfg, err := config.LoadDefaultConfig(ctx)
Expand All @@ -36,9 +36,9 @@ func GetVPC() ([]Vpc, error) {
for _, i := range info.Vpcs {
for _, j := range i.Tags {
if *j.Key == "Name" {
vpcList = append(vpcList, Vp
vpcList = append(vpcList, Vpc{
Name: aws.String(*j.Value),
State: aws.String(string(*&i.State)),
State: aws.String(string(i.State)),
Ipv4Cidr: aws.String(*i.CidrBlock),
Default: *i.IsDefault,
VpcID: aws.String(*i.VpcId),
Expand Down
11 changes: 7 additions & 4 deletions pkg/ec2/getEc2Info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type EC2Instance struct {
InstanceID string
InstanceType string
InstancePrivateIP string
InstanceStatus string
}

func DescribeInstance(tagName string, tagValue []string, cmd *cobra.Command, args []string) []EC2Instance {
Expand All @@ -39,17 +40,19 @@ func DescribeInstance(tagName string, tagValue []string, cmd *cobra.Command, arg
ec2Instance = append(ec2Instance, EC2Instance{
InstanceName: aws.ToString(j.Value),
InstanceID: aws.ToString(i.InstanceId),
InstanceType: *aws.String(string(*&i.InstanceType)),
InstancePrivateIP: aws.ToString(*&i.PrivateIpAddress),
InstanceType: *aws.String(string(i.InstanceType)),
InstancePrivateIP: aws.ToString(i.PrivateIpAddress),
InstanceStatus: aws.ToString((*string)(&i.State.Name)),
})
}
}
} else {
ec2Instance = append(ec2Instance, EC2Instance{
InstanceName: aws.ToString(j.Value),
InstanceID: aws.ToString(i.InstanceId),
InstanceType: *aws.String(string(*&i.InstanceType)),
InstancePrivateIP: aws.ToString(*&i.PrivateIpAddress),
InstanceType: *aws.String(string(i.InstanceType)),
InstancePrivateIP: aws.ToString(i.PrivateIpAddress),
InstanceStatus: aws.ToString((*string)(&i.State.Name)),
})
}
}
Expand Down
53 changes: 53 additions & 0 deletions pkg/vpc/getVPC.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vpc

import (
"context"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)

type Vpc struct {
Name *string
State *string
Ipv4Cidr *string
Default bool
VpcID *string
}

func GetVPC() ([]Vpc, error) {

var vpcList []Vpc

ctx := context.TODO()
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Fatal(err)
}

client := ec2.NewFromConfig(cfg)

info, err := client.DescribeVpcs(ctx, &ec2.DescribeVpcsInput{})
if err != nil {
return nil, err
}
for _, i := range info.Vpcs {
for _, j := range i.Tags {
if *j.Key == "Name" {
vpcList = append(vpcList, Vpc{
Name: aws.String(*j.Value),
State: aws.String(string(i.State)),
Ipv4Cidr: aws.String(*i.CidrBlock),
Default: *i.IsDefault,
VpcID: aws.String(*i.VpcId),
})
}

}
}

return vpcList, err

}

0 comments on commit ea62ae1

Please sign in to comment.