Skip to content

Commit

Permalink
add show subnets belong to a VPC and describe subnet functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrar-Ahmed7 committed Feb 15, 2023
1 parent ecc0646 commit ca8c8d3
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 5 deletions.
25 changes: 22 additions & 3 deletions internal/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
Expand Down Expand Up @@ -229,14 +230,32 @@ func GetSingleVPC(sess session.Session, vpcId string) *ec2.Vpc {
return result.Vpcs[0]
}

func GetSubnets(sess session.Session) []*ec2.Subnet {
func GetSubnets(sess session.Session, vpcId string) []SubnetResp {
ec2Serv := *ec2.New(&sess)
result, err := ec2Serv.DescribeSubnets(&ec2.DescribeSubnetsInput{})
result, err := ec2Serv.DescribeSubnets(&ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcId)},
},
},
})
if err != nil {
fmt.Println("Error in fetching Subnets: ", " err: ", err)
return nil
}
return result.Subnets
var subnets []SubnetResp
for _, s := range result.Subnets {
subnet := SubnetResp{
SubnetId: *s.SubnetId,
OwnerId: *s.OwnerId,
CidrBlock: *s.CidrBlock,
AvailabilityZone: *s.AvailabilityZone,
State: *s.State,
}
subnets = append(subnets, subnet)
}
return subnets
}

func GetSingleSubnet(sess session.Session, sId string) *ec2.Subnet {
Expand Down
8 changes: 8 additions & 0 deletions internal/aws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,11 @@ type LambdaResp struct {
CodeSize string
LastModified string
}

type SubnetResp struct {
SubnetId string
OwnerId string
CidrBlock string
AvailabilityZone string
State string
}
1 change: 1 addition & 0 deletions internal/config/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (a *Aliases) loadDefaultAliases() {
a.declare("ec2:i", "EC2:I")
a.declare("sqs", "SQS")
a.declare("vpc", "VPC")
a.declare("subnet", "SUBNET")
a.declare("lambda", "LAMBDA")

a.declare("help", "h", "?")
Expand Down
47 changes: 47 additions & 0 deletions internal/dao/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dao

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/aws"
"github.com/rs/zerolog/log"
)

type Subnet struct {
Accessor
ctx context.Context
}

func (sn *Subnet) Init(ctx context.Context) {
sn.ctx = ctx
}

func (sn *Subnet) List(ctx context.Context) ([]Object, error) {
sess, ok := ctx.Value(internal.KeySession).(*session.Session)
if !ok {
log.Err(fmt.Errorf("conversion err: Expected session.session but got %v", sess))
}
vpcId := fmt.Sprintf("%v", ctx.Value(internal.VpcId))
subnets := aws.GetSubnets(*sess, vpcId)
objs := make([]Object, len(subnets))
for i, obj := range subnets {
objs[i] = obj
}
return objs, nil
}

func (sn *Subnet) Get(ctx context.Context, path string) (Object, error) {
return nil, nil
}

func (sn *Subnet) Describe(vpcId string) (string, error) {
sess, ok := sn.ctx.Value(internal.KeySession).(*session.Session)
if !ok {
log.Err(fmt.Errorf("conversion err: Expected session.session but got %v", sess))
}
res := aws.GetSingleSubnet(*sess, vpcId).GoString()
return res, nil
}
2 changes: 1 addition & 1 deletion internal/dao/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (v *VPC) List(ctx context.Context) ([]Object, error) {
return objs, nil
}

func (ei *VPC) Get(ctx context.Context, path string) (Object, error) {
func (v *VPC) Get(ctx context.Context, path string) (Object, error) {
return nil, nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
UserName ContextKey = "user_name"
GroupName ContextKey = "group_name"
RoleName ContextKey = "role_name"
VpcId ContextKey = "vpc_id"
)

// TODO rename the file name to constants.go
Expand Down
6 changes: 6 additions & 0 deletions internal/model/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ var Registry = map[string]ResourceMeta{
DAO: &dao.VPC{},
Renderer: &render.VPC{},
},

"subnet": {
DAO: &dao.Subnet{},
Renderer: &render.Subnet{},
},

"lambda": {
DAO: &dao.Lambda{},
Renderer: &render.Lambda{},
Expand Down
38 changes: 38 additions & 0 deletions internal/render/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package render

import (
"fmt"

"github.com/derailed/tview"
"github.com/one2nc/cloud-lens/internal/aws"
)

type Subnet struct {
}

func (sn Subnet) Header() Header {
return Header{
HeaderColumn{Name: "Subnet-Id", SortIndicatorIdx: 7, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Owner-Id", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Cidr Block", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Availability Zone", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Subnet-State", SortIndicatorIdx: 7, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
}
}

func (sn Subnet) Render(o interface{}, ns string, row *Row) error {
snResp, ok := o.(aws.SubnetResp)
if !ok {
return fmt.Errorf("expected vpc, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
snResp.SubnetId,
snResp.OwnerId,
snResp.CidrBlock,
snResp.AvailabilityZone,
snResp.State,
}
return nil
}
3 changes: 3 additions & 0 deletions internal/view/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func coreViewers(vv MetaViewers) {
vv["vpc"] = MetaViewer{
viewerFn: NewVPC,
}
vv["subnet"] = MetaViewer{
viewerFn: NewSubnet,
}
vv["lambda"] = MetaViewer{
viewerFn: NewLambda,
}
Expand Down
41 changes: 41 additions & 0 deletions internal/view/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package view

import (
"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal/ui"
)

type Subnet struct {
ResourceViewer
}

// NewPod returns a new viewer.
func NewSubnet(resource string) ResourceViewer {
var sn Subnet
sn.ResourceViewer = NewBrowser(resource)
sn.AddBindKeysFn(sn.bindKeys)
return &sn
}

func (sn *Subnet) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyShiftI: ui.NewKeyAction("Sort Subnet-Id", sn.GetTable().SortColCmd("Subnet-Id", true), true),
ui.KeyShiftS: ui.NewKeyAction("Sort Subnet-State", sn.GetTable().SortColCmd("Subnet-State", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", sn.App().PrevCmd, false),
tcell.KeyEnter: ui.NewKeyAction("View", sn.enterCmd, true),
})
}

func (sn *Subnet) enterCmd(evt *tcell.EventKey) *tcell.EventKey {
subnetId := sn.GetTable().GetSelectedItem()
if subnetId != "" {
f := describeResource
if sn.GetTable().enterFn != nil {
f = sn.GetTable().enterFn
}
f(sn.App(), sn.GetTable().GetModel(), sn.Resource(), subnetId)
sn.App().Flash().Info("Subnet Id: " + subnetId)
}

return nil
}
18 changes: 17 additions & 1 deletion internal/view/vpc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package view

import (
"context"

"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/ui"
)

Expand All @@ -22,7 +25,8 @@ func (v *VPC) bindKeys(aa ui.KeyActions) {
ui.KeyShiftI: ui.NewKeyAction("Sort VPC-Id", v.GetTable().SortColCmd("VPC-Id", true), true),
ui.KeyShiftS: ui.NewKeyAction("Sort VPC-State", v.GetTable().SortColCmd("VPC-State", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", v.App().PrevCmd, false),
tcell.KeyEnter: ui.NewKeyAction("View", v.enterCmd, false),
tcell.KeyEnter: ui.NewKeyAction("View", v.enterCmd, true),
ui.KeyS: ui.NewKeyAction("View Subnets", v.subnetCmd, true),
})
}

Expand All @@ -39,3 +43,15 @@ func (v *VPC) enterCmd(evt *tcell.EventKey) *tcell.EventKey {

return nil
}

func (iamug *VPC) subnetCmd(evt *tcell.EventKey) *tcell.EventKey {
vpcId := iamug.GetTable().GetSelectedItem()
if vpcId != "" {
sn := NewSubnet("subnet")
ctx := context.WithValue(iamug.App().GetContext(), internal.VpcId, vpcId)
iamug.App().SetContext(ctx)
iamug.App().Flash().Info("VPC ID: " + vpcId)
iamug.App().inject(sn)
}
return nil
}

0 comments on commit ca8c8d3

Please sign in to comment.