Skip to content

Commit

Permalink
Minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaysomani07 committed Feb 14, 2023
2 parents 16ffdc9 + 346045a commit 11579e9
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 5 deletions.
35 changes: 31 additions & 4 deletions internal/aws/ec2.go
Expand Up @@ -120,14 +120,30 @@ func GetSingleVolume(sess session.Session, vId string) *ec2.Volume {
Snapshots are region specific
Localstack does have default snapshots, so we can see some of the snapshots that we never created
*/
func GetSnapshots(sess session.Session) []*ec2.Snapshot {
func GetSnapshots(sess session.Session) []Snapshot {
ec2Serv := *ec2.New(&sess)
result, err := ec2Serv.DescribeSnapshots(&ec2.DescribeSnapshotsInput{})
if err != nil {
fmt.Println("Error in fetching Snapshots: ", " err: ", err)
return nil
}
return result.Snapshots
var snapshots []Snapshot
for _, s := range result.Snapshots {
launchTime := s.StartTime
loc, _ := time.LoadLocation("Asia/Kolkata")
IST := launchTime.In(loc)
IST.Format("Mon Jan _2 15:04:05 2006")
snapshot := Snapshot{
SnapshotId: *s.SnapshotId,
OwnerId: *s.OwnerId,
VolumeId: *s.VolumeId,
VolumeSize: strconv.Itoa(int(*s.VolumeSize)),
StartTime: IST.String(),
State: *s.State,
}
snapshots = append(snapshots, snapshot)
}
return snapshots
}

func GetSingleSnapshot(sess session.Session, sId string) *ec2.Snapshot {
Expand All @@ -147,14 +163,25 @@ func GetSingleSnapshot(sess session.Session, sId string) *ec2.Snapshot {
Localstack does have default some AMIs, so we can see some of the AMIs that we never created
*/

func GetAMIs(sess session.Session) []*ec2.Image {
func GetAMIs(sess session.Session) []ImageResp {
ec2Serv := *ec2.New(&sess)
result, err := ec2Serv.DescribeImages(&ec2.DescribeImagesInput{})
if err != nil {
fmt.Println("Error in fetching AMIs: ", " err: ", err)
return nil
}
return result.Images
var images []ImageResp
for _, i := range result.Images {
image := ImageResp{
ImageId: *i.ImageId,
OwnerId: *i.OwnerId,
ImageLocation: *i.ImageLocation,
Name: *i.Name,
ImageType: *i.ImageType,
}
images = append(images, image)
}
return images
}

func GetSingleAMI(sess session.Session, amiId string) *ec2.Image {
Expand Down
1 change: 0 additions & 1 deletion internal/aws/sqs.go
Expand Up @@ -84,7 +84,6 @@ func CreateLambdaFunction(sess session.Session) {
}

fmt.Printf("Function ARN: %s\n", aws.StringValue(resp.FunctionArn))

}

func GetAllLambdaFunctions(sess session.Session) {
Expand Down
17 changes: 17 additions & 0 deletions internal/aws/types.go
Expand Up @@ -81,3 +81,20 @@ type QueueResp struct {
Encryption string
MaxMessageSize string
}

type Snapshot struct {
SnapshotId string
OwnerId string
VolumeId string
VolumeSize string
StartTime string
State string
}

type ImageResp struct {
ImageId string
OwnerId string
ImageLocation string
Name string
ImageType string
}
2 changes: 2 additions & 0 deletions internal/config/alias.go
Expand Up @@ -136,6 +136,8 @@ func (a *Aliases) loadDefaultAliases() {
a.declare("iam:u", "IAM:U")
a.declare("iam:g", "IAM:G")
a.declare("iam:r", "IAM:R")
a.declare("ec2:s", "EC2:S")
a.declare("ec2:i", "EC2:I")

a.declare("help", "h", "?")
a.declare("quit", "q", "q!", "Q")
Expand Down
37 changes: 37 additions & 0 deletions internal/dao/ec2_image.go
@@ -0,0 +1,37 @@
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 EC2I struct {
Accessor
ctx context.Context
}

func (ei *EC2I) Init(ctx context.Context) {
ei.ctx = ctx
}

func (ei *EC2I) 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))
}
ins := aws.GetAMIs(*sess)
objs := make([]Object, len(ins))
for i, obj := range ins {
objs[i] = obj
}
return objs, nil
}

func (ei *EC2I) Get(ctx context.Context, path string) (Object, error) {
return nil, nil
}
37 changes: 37 additions & 0 deletions internal/dao/ec2_snapshot.go
@@ -0,0 +1,37 @@
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 EC2S struct {
Accessor
ctx context.Context
}

func (es *EC2S) Init(ctx context.Context) {
es.ctx = ctx
}

func (es *EC2S) 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))
}
ins := aws.GetSnapshots(*sess)
objs := make([]Object, len(ins))
for i, obj := range ins {
objs[i] = obj
}
return objs, nil
}

func (es *EC2S) Get(ctx context.Context, path string) (Object, error) {
return nil, nil
}
8 changes: 8 additions & 0 deletions internal/model/registry.go
Expand Up @@ -54,4 +54,12 @@ var Registry = map[string]ResourceMeta{
DAO: &dao.IamGroupUser{},
Renderer: &render.IamGroupUser{},
},
"ec2:s": {
DAO: &dao.EC2S{},
Renderer: &render.EC2S{},
},
"ec2:i": {
DAO: &dao.EC2I{},
Renderer: &render.EC2I{},
},
}
38 changes: 38 additions & 0 deletions internal/render/ec2_image.go
@@ -0,0 +1,38 @@
package render

import (
"fmt"

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

type EC2I struct {
}

func (ei EC2I) Header() Header {
return Header{
HeaderColumn{Name: "Image-Id", SortIndicatorIdx: 6, 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: "Image-Location", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Name", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Image-Type", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
}
}

func (ei EC2I) Render(o interface{}, ns string, row *Row) error {
eiResp, ok := o.(aws.ImageResp)
if !ok {
return fmt.Errorf("Expected eiResp, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
eiResp.ImageId,
eiResp.OwnerId,
eiResp.ImageLocation,
eiResp.Name,
eiResp.ImageType,
}
return nil
}
40 changes: 40 additions & 0 deletions internal/render/ec2_snapshot.go
@@ -0,0 +1,40 @@
package render

import (
"fmt"

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

type EC2S struct {
}

func (es EC2S) Header() Header {
return Header{
HeaderColumn{Name: "Snapshot-Id", SortIndicatorIdx: 9, 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: "Volume-Id", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Volume-Size", SortIndicatorIdx: 7, 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: "Start-Time", SortIndicatorIdx: 6, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: true},
}
}

func (es EC2S) Render(o interface{}, ns string, row *Row) error {
esResp, ok := o.(aws.Snapshot)
if !ok {
return fmt.Errorf("Expected esResp, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
esResp.SnapshotId,
esResp.OwnerId,
esResp.VolumeId,
esResp.VolumeSize,
esResp.State,
esResp.StartTime,
}
return nil
}
24 changes: 24 additions & 0 deletions internal/view/ec2_image.go
@@ -0,0 +1,24 @@
package view

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

type EC2I struct {
ResourceViewer
}

func NewEC2I(resource string) ResourceViewer {
var es EC2I
es.ResourceViewer = NewBrowser(resource)
es.AddBindKeysFn(es.bindKeys)
return &es
}

func (ei *EC2I) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyShiftI: ui.NewKeyAction("Sort Image-Id", ei.GetTable().SortColCmd("Image-Id", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", ei.App().PrevCmd, true),
})
}
26 changes: 26 additions & 0 deletions internal/view/ec2_snapshot.go
@@ -0,0 +1,26 @@
package view

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

type EC2S struct {
ResourceViewer
}

func NewEC2S(resource string) ResourceViewer {
var es EC2S
es.ResourceViewer = NewBrowser(resource)
es.AddBindKeysFn(es.bindKeys)
return &es
}

func (es *EC2S) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyShiftI: ui.NewKeyAction("Sort Snapshot-Id", es.GetTable().SortColCmd("Snapshot-Id", true), true),
ui.KeyShiftV: ui.NewKeyAction("Sort Volume-Size", es.GetTable().SortColCmd("Volume-Size", true), true),
ui.KeyShiftT: ui.NewKeyAction("Sort Start-Time", es.GetTable().SortColCmd("Start-Time", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", es.App().PrevCmd, true),
})
}
6 changes: 6 additions & 0 deletions internal/view/registrar.go
Expand Up @@ -29,4 +29,10 @@ func coreViewers(vv MetaViewers) {
vv["iam:r"] = MetaViewer{
viewerFn: NewIamRole,
}
vv["ec2:s"] = MetaViewer{
viewerFn: NewEC2S,
}
vv["ec2:i"] = MetaViewer{
viewerFn: NewEC2I,
}
}

0 comments on commit 11579e9

Please sign in to comment.