From 67ee33ba87614c73f9b78e2cd9e806aac04a57dc Mon Sep 17 00:00:00 2001 From: chinmaysomani Date: Tue, 31 Jan 2023 17:14:30 +0530 Subject: [PATCH] use ctrl+A for Display Lifecycle Rules --- internal/aws/s3.go | 18 ++++++++++-- internal/view/app.go | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/internal/aws/s3.go b/internal/aws/s3.go index 2ab7bac..d30dd1d 100644 --- a/internal/aws/s3.go +++ b/internal/aws/s3.go @@ -53,14 +53,12 @@ func ListBuckets(sess session.Session) ([]BucketResp, error) { } func GetInfoAboutBucket(sess session.Session, bucketName string, delimiter string, prefix string) *s3.ListObjectsV2Output { - // fmt.Println("Bucket Name: ", bucketName) s3Serv := *s3.New(&sess) result, err := s3Serv.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(bucketName), Delimiter: aws.String(delimiter), Prefix: aws.String(prefix)}) if err != nil { fmt.Println("Error is:", err) return nil } - //fmt.Println(result) return result } @@ -77,3 +75,19 @@ func PutObjects(sess session.Session) { } fmt.Println("uploaded object") } + +func GetBuckEncryption(sess session.Session, bucketName *string) *s3.ServerSideEncryptionConfiguration { + s3Serv := *s3.New(&sess) + sse, _ := s3Serv.GetBucketEncryption(&s3.GetBucketEncryptionInput{ + Bucket: bucketName, + }) + return sse.ServerSideEncryptionConfiguration +} + +func GetBuckLifecycle(sess session.Session, bucketName string) *s3.GetBucketLifecycleConfigurationOutput { + s3Serv := *s3.New(&sess) + blc, _ := s3Serv.GetBucketLifecycleConfiguration(&s3.GetBucketLifecycleConfigurationInput{ + Bucket: aws.String(bucketName), + }) + return blc +} diff --git a/internal/view/app.go b/internal/view/app.go index fb9118c..5a9fd05 100644 --- a/internal/view/app.go +++ b/internal/view/app.go @@ -330,6 +330,7 @@ func (a *App) DisplayEc2Instances(ins []aws.EC2Resp, sess *session.Session) *tvi table.SetCell((i + 1), 7, tview.NewTableCell(in.LaunchTime).SetAlign(tview.AlignCenter)) } table.SetSelectable(true, false) + a.Application.SetFocus(table) table.Select(1, 1).SetFixed(1, 1) table.Select(1, 1).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) { if table.GetCell(1, 1).Text != "" { @@ -475,6 +476,13 @@ func (a *App) DisplayS3Buckets(sess *session.Session, buckets []aws.BucketResp) s3DataT.Select(1, 1).SetFixed(1, 1) } } + } else if event.Key() == tcell.KeyCtrlA { + r, _ := table.GetSelection() + cell := table.GetCell(r, 0) + flex.Clear() + s3DataT.Clear() + a.Main.RemovePage("s3data") + a.DisplayLifecycleRules(table, flex, cell.Text, *sess) } return event }) @@ -552,6 +560,65 @@ func (a *App) DisplayS3Objects(s3DataTable *tview.Table, flex *tview.Flex, folde a.Main.AddAndSwitchToPage("s3dataView", flex, true) } } +func (a *App) DisplayLifecycleRules(table *tview.Table, flex *tview.Flex, bucketName string, sess session.Session) { + lifeCycleTable := tview.NewTable() + lifeCycleTable.SetBorder(true) + lifeCycle := aws.GetBuckLifecycle(sess, bucketName) + rules := lifeCycle.Rules + a.setTableHeaderForLifecycle(lifeCycleTable, bucketName) + a.setTableContentorLifecycle(lifeCycleTable, rules) + flex.AddItem(a.Views()["pAndRMenu"], 0, 2, false) + inputPrompt := tview.NewInputField(). + SetLabel("🐶>"). + SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool { + return true // accept any input + }) + inputPrompt.SetFieldBackgroundColor(tcell.ColorBlack) + inputPrompt.SetBorder(true) + flex.AddItem(inputPrompt, 0, 1, false) + flex.AddItem(lifeCycleTable, 0, 9, true) + lifeCycleTable.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { //Tiger + if event.Key() == tcell.KeyESC { + flex.Clear() + a.Main.RemovePage("lifeCycleDataView") + a.Main.SwitchToPage("main") + a.Application.SetFocus(table) + } + return event + }) + + a.Application.SetFocus(lifeCycleTable) + lifeCycleTable.SetSelectable(false, false) + a.Main.AddAndSwitchToPage("lifeCycleDataView", flex, true) +} + +func (a *App) setTableHeaderForLifecycle(lifeCycleTable *tview.Table, bucketName string) *tview.Table { + lifeCycleTable.SetTitle(bucketName) + lifeCycleTable.SetTitleColor(tcell.ColorYellow) + lifeCycleTable.SetCell(0, 0, tview.NewTableCell("Lifecycle-Id").SetSelectable(false).SetTextColor(tcell.ColorOrangeRed).SetAlign(tview.AlignCenter)) + lifeCycleTable.SetCell(0, 1, tview.NewTableCell("Status").SetSelectable(false).SetTextColor(tcell.ColorOrangeRed).SetAlign(tview.AlignCenter)) + lifeCycleTable.SetCell(0, 2, tview.NewTableCell("Expiration-Days").SetSelectable(false).SetTextColor(tcell.ColorOrangeRed).SetAlign(tview.AlignCenter)) + lifeCycleTable.SetCell(0, 3, tview.NewTableCell("Transition-Days").SetSelectable(false).SetTextColor(tcell.ColorOrangeRed).SetAlign(tview.AlignCenter)) + lifeCycleTable.SetCell(0, 4, tview.NewTableCell("Transition-StorageClass").SetSelectable(false).SetTextColor(tcell.ColorOrangeRed).SetAlign(tview.AlignCenter)) + + return lifeCycleTable +} + +func (a *App) setTableContentorLifecycle(table *tview.Table, rules []*s3.LifecycleRule) *tview.Table { + indx := 0 + for _, rule := range rules { + table.SetCell((indx + 2), 0, tview.NewTableCell(*rule.ID).SetTextColor(tcell.ColorYellow).SetAlign(tview.AlignCenter)) + table.SetCell((indx + 2), 1, tview.NewTableCell(*rule.Status).SetTextColor(tcell.ColorYellow).SetAlign(tview.AlignCenter)) + table.SetCell((indx + 2), 2, tview.NewTableCell(fmt.Sprintf("%v", *rule.Expiration.Days)).SetTextColor(tcell.ColorYellow).SetAlign(tview.AlignCenter)) + table.SetCell((indx + 2), 3, tview.NewTableCell(strconv.Itoa(int(*rule.Transitions[0].Days))).SetTextColor(tcell.ColorYellow).SetAlign(tview.AlignCenter)) + table.SetCell((indx + 2), 4, tview.NewTableCell(*rule.Transitions[0].StorageClass).SetTextColor(tcell.ColorYellow).SetAlign(tview.AlignCenter)) + indx++ + } + + table.SetBorderFocusColor(tcell.ColorSpringGreen) + + return table +} func (a *App) DisplayS3ObjectForEmptyBuc(s3DataT *tview.Table, flex *tview.Flex, bucketName string, sess session.Session) { s3DataT.SetTitle(bucketName)