Skip to content

Commit

Permalink
Add dropdown to select zone for gcp resources
Browse files Browse the repository at this point in the history
  • Loading branch information
PratikJethe committed Nov 3, 2023
1 parent a088932 commit a7539ce
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
KeyActiveProfile ContextKey = "active-profile"
KeyActiveRegion ContextKey = "active-region"
KeyActiveProject ContextKey = "active-project"
KeyActiveZone ContextKey = "active-zone"
KeySession ContextKey = "session"
BucketName ContextKey = "bucket_name"
StorageBucketName ContextKey = "storage_bucket_name"
Expand Down
6 changes: 3 additions & 3 deletions internal/gcp/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (

compute "cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/compute/apiv1/computepb"
"github.com/one2nc/cloudlens/internal"
"google.golang.org/api/iterator"
)

func ListInstances(ctx context.Context) ([]VMResp, error) {
var vmResp = []VMResp{}
projectID := "projectID"
zone := "zone"

projectID := ctx.Value(internal.KeyActiveProject).(string)
zone := ctx.Value(internal.KeyActiveZone).(string)
instancesClient, err := compute.NewInstancesRESTClient(ctx)
if err != nil {
return vmResp, err
Expand Down Expand Up @@ -41,7 +42,6 @@ func ListInstances(ctx context.Context) ([]VMResp, error) {
AvailabilityZone: *instance.Zone,
InstanceState: *instance.Status,
LaunchTime: *instance.CreationTimestamp,

}
vmResp = append(vmResp, vm)
}
Expand Down
37 changes: 37 additions & 0 deletions internal/gcp/zones.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gcp

import (
"context"

"github.com/one2nc/cloudlens/internal"
"github.com/rs/zerolog/log"
"google.golang.org/api/compute/v1"
)

func FecthZones(ctx context.Context) ([]string, error) {


zonesResp := []string{}

// Create a client with default credentials.
client, err := compute.NewService(ctx)
if err != nil {
log.Printf("Failed to create compute service client: %v", err)
return zonesResp, err

}

// List available zones in your project.
projectID := ctx.Value(internal.KeyActiveProject).(string) // Replace with your GCP project ID.
zones, err := client.Zones.List(projectID).Context(ctx).Do()
if err != nil {
log.Printf("Failed to list zones: %v", err)
return zonesResp, err
}

for _, zone := range zones.Items {
zonesResp = append(zonesResp, zone.Name)
}

return zonesResp, nil
}
30 changes: 30 additions & 0 deletions internal/view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,27 @@ func (a *App) handleGCP() error {
}

ctx = context.WithValue(ctx, internal.KeyActiveProject, serviceAccount.ProjectID)
zones, err := gcp.FecthZones(ctx)

if err != nil {
go func() {
<-time.After(splashDelay)
dialog.ShowError(a.Content.Pages, "Invalid path to google credentials")

}()
}
ctx = context.WithValue(ctx, internal.KeyActiveZone, zones[0])

p := ui.NewDropDown("Projects:", []string{serviceAccount.ProjectID})
p.SetSelectedFunc(a.projectchanged)
a.Views()["project"] = p
z := ui.NewDropDown("Zones:", zones)
z.SetSelectedFunc(a.zonechanged)
a.Views()["zone"] = z

infoData := map[string]tview.Primitive{
"project": a.project(),
"zone": a.zone(),
}
a.Views()["info"] = ui.NewInfo(infoData)
a.SetContext(ctx)
Expand Down Expand Up @@ -217,6 +231,7 @@ func (a *App) handleCloudSelection(seletedCloud string) error {
a.Main.SwitchToPage(internal.GCP_SCREEN)
}
a.command = NewCommand(a)
a.bindKeys()
if err := a.command.Init(); err != nil {
log.Print(err)
return err
Expand Down Expand Up @@ -408,6 +423,10 @@ func (a *App) projectchanged(project string, index int) {
a.refreshProject(project)
}

func (a *App) zonechanged(zone string, index int) {
a.refreshZone(zone)
}

func (a *App) regionChanged(region string, index int) {
profile := a.GetContext().Value(internal.KeyActiveProfile).(string)
a.refreshSession(profile, region)
Expand Down Expand Up @@ -441,6 +460,14 @@ func (a *App) refreshProject(project string) {
a.App.Flash().Infof("Refreshing %v...", stackedViews[0])
}

func (a *App) refreshZone(zone string) {
ctx := context.WithValue(a.GetContext(), internal.KeyActiveZone, zone)
a.SetContext(ctx)
stackedViews := a.Content.Pages.Stack.Flatten()
a.gotoResource(stackedViews[0], "", true)
a.App.Flash().Infof("Refreshing %v...", stackedViews[0])
}

func (a *App) gotoResource(cmd, path string, clearStack bool) {
err := a.command.run(cmd, path, clearStack)
if err != nil {
Expand Down Expand Up @@ -497,6 +524,9 @@ func (a *App) profile() *ui.DropDown {
func (a *App) project() *ui.DropDown {
return a.Views()["project"].(*ui.DropDown)
}
func (a *App) zone() *ui.DropDown {
return a.Views()["zone"].(*ui.DropDown)
}

func (a *App) region() *ui.DropDown {
return a.Views()["region"].(*ui.DropDown)
Expand Down
5 changes: 3 additions & 2 deletions internal/view/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (t *Tab) Add() {
t.items = append(t.items, t.App.region())
case internal.GCP:
t.items = append(t.items, t.App.project())
t.items = append(t.items, t.App.zone())
}
}

Expand All @@ -39,8 +40,8 @@ func (t *Tab) tabAction(event *tcell.EventKey) *tcell.EventKey {

focusIdx := t.currentFocusIdx()

if event.Key() == tcell.KeyTAB {
if focusIdx + 1 == len(t.items) {
if event.Key() == tcell.KeyTAB {
if focusIdx+1 == len(t.items) {
t.App.Application.SetFocus(t.Content.Pages.Current())
return event
}
Expand Down

0 comments on commit a7539ce

Please sign in to comment.