Skip to content

Commit

Permalink
add generic table for EBS resource
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaysomani07 committed Feb 9, 2023
1 parent a7353f7 commit 9666649
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 9 deletions.
26 changes: 21 additions & 5 deletions internal/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"strconv"
"time"

"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -28,8 +29,6 @@ func (e ByLaunchTime) Less(i, j int) bool {
return e[i].Instance.LaunchTime.Before(*e[j].Instance.LaunchTime)
}



func GetInstances(sess session.Session) ([]EC2Resp, error) {
var ec2Info []EC2Resp
ec2Serv := *ec2.New(&sess)
Expand Down Expand Up @@ -98,14 +97,31 @@ func GetSingleSecGrp(sess session.Session, sgId string) *ec2.DescribeSecurityGro
Volumes(ebs) are region specific
Localstack doesn't have default volumes, so at some regions, there won't be any volumes.
*/
func GetVolumes(sess session.Session) []*ec2.Volume {
func GetVolumes(sess session.Session) ([]EBSResp, error) {
var volumes []EBSResp
ec2Serv := *ec2.New(&sess)
result, err := ec2Serv.DescribeVolumes(&ec2.DescribeVolumesInput{})
if err != nil {
fmt.Println("Error in fetching Volumes: ", " err: ", err)
return nil
return nil, err
}
for _, v := range result.Volumes {
launchTime := v.CreateTime
loc, _ := time.LoadLocation("Asia/Kolkata")
IST := launchTime.In(loc)
IST.Format("Mon Jan _2 15:04:05 2006")
volume := EBSResp{
VolumeId: *v.VolumeId,
Size: strconv.Itoa(int(*v.Size)) + " GB",
VolumeType: *v.VolumeType,
State: *v.State,
AvailabilityZone: *v.AvailabilityZone,
Snapshot: *v.SnapshotId,
CreationTime: IST.String(),
}
volumes = append(volumes, volume)
}
return result.Volumes
return volumes, nil
}

func GetSingleVolume(sess session.Session, vId string) *ec2.Volume {
Expand Down
14 changes: 12 additions & 2 deletions internal/aws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ type IAMUSerResp struct {
}

type IAMUSerPolicyResponse struct {
PolicyArn string
PolicyName string
PolicyArn string
PolicyName string
}

type EBSResp struct {
VolumeId string
Size string
VolumeType string
State string
AvailabilityZone string
Snapshot string
CreationTime string
}
5 changes: 3 additions & 2 deletions internal/config/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ func (a *Aliases) declare(key string, aliases ...string) {
func (a *Aliases) loadDefaultAliases() {
a.mx.Lock()
defer a.mx.Unlock()

a.declare("ec2", "Ec2", "EC2")
a.declare("s3", "S3")
a.declare("sg", "SG")
a.declare("iam:u","IAM:U")
a.declare("iam:u", "IAM:U")
a.declare("ebs", "EBS")

a.declare("help", "h", "?")
a.declare("quit", "q", "q!", "Q")
Expand Down
49 changes: 49 additions & 0 deletions internal/dao/ebs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 EBS struct {
Accessor
ctx context.Context
}

func (ebs *EBS) Init(ctx context.Context) {
ebs.ctx = ctx
}

func (ebs *EBS) 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))
}
vols, err := aws.GetVolumes(*sess)
if err != nil {
return nil, err
}
objs := make([]Object, len(vols))
for i, obj := range vols {
objs[i] = obj
}
return objs, nil
}

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

func (ebs *EBS) Describe(volId string) (string, error) {
sess, ok := ebs.ctx.Value(internal.KeySession).(*session.Session)
if !ok {
log.Err(fmt.Errorf("conversion err: Expected session.session but got %v", sess))
}
res := aws.GetSingleVolume(*sess, volId)
return res.GoString(), nil
}
4 changes: 4 additions & 0 deletions internal/model/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ var Registry = map[string]ResourceMeta{
DAO: &dao.IAMUP{},
Renderer: &render.IamUserPloicy{},
},
"ebs": {
DAO: &dao.EBS{},
Renderer: &render.EBS{},
},
}
45 changes: 45 additions & 0 deletions internal/render/ebs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package render

import (
"fmt"

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

type EBS struct {
}

// Header returns a header row.
func (ebs EBS) Header() Header {
return Header{
HeaderColumn{Name: "Volume-Id", SortIndicatorIdx: 7, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Size", SortIndicatorIdx: 0, Align: tview.AlignRight, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Volume-Type", SortIndicatorIdx: 0, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "State", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Availability-Zone", SortIndicatorIdx: 13, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Snapshot", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: true},
HeaderColumn{Name: "Creation-Time", SortIndicatorIdx: 9, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
}
}

func (ebs EBS) Render(o interface{}, ns string, row *Row) error {
ebsResp, ok := o.(aws.EBSResp)

if !ok {
return fmt.Errorf("Expected EBS response, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
ebsResp.VolumeId,
ebsResp.Size,
ebsResp.VolumeType,
ebsResp.State,
ebsResp.AvailabilityZone,
ebsResp.Snapshot,
ebsResp.CreationTime,
}

return nil
}
41 changes: 41 additions & 0 deletions internal/view/ebs.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 EBS struct {
ResourceViewer
}

func NewEBS(resource string) ResourceViewer {
var ebs EBS
ebs.ResourceViewer = NewBrowser(resource)
ebs.AddBindKeysFn(ebs.bindKeys)
// s3.GetTable().SetEnterFn(s3.describeInstace)
return &ebs
}
func (ebs *EBS) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyShiftI: ui.NewKeyAction("Sort Volume-Id", ebs.GetTable().SortColCmd("Volume-Id", true), true),
ui.KeyShiftS: ui.NewKeyAction("Sort Size", ebs.GetTable().SortColCmd("Size", true), true),
ui.KeyShiftV: ui.NewKeyAction("Sort Volume-Type", ebs.GetTable().SortColCmd("Volume-Type", true), true),
ui.KeyShiftZ: ui.NewKeyAction("Sort Availability-Zone", ebs.GetTable().SortColCmd("Availability-Zone", true), true),
ui.KeyShiftT: ui.NewKeyAction("Sort Creation-Time", ebs.GetTable().SortColCmd("Creation-Time", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", ebs.App().PrevCmd, true),
tcell.KeyEnter: ui.NewKeyAction("View", ebs.enterCmd, true),
})
}

func (ebs *EBS) enterCmd(evt *tcell.EventKey) *tcell.EventKey {
volId := ebs.GetTable().GetSelectedItem()
ebs.App().Flash().Info("volume id: " + volId)

f := describeResource
if ebs.GetTable().enterFn != nil {
f = ebs.GetTable().enterFn
}
f(ebs.App(), ebs.GetTable().GetModel(), ebs.Resource(), volId)
return nil
}
3 changes: 3 additions & 0 deletions internal/view/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ func coreViewers(vv MetaViewers) {
vv["iam:u"] = MetaViewer{
viewerFn: NewIAMU,
}
vv["ebs"] = MetaViewer{
viewerFn: NewEBS,
}
}

0 comments on commit 9666649

Please sign in to comment.