From 71a3b2e19c69eeeda99248ab0a92d411d890b52d Mon Sep 17 00:00:00 2001 From: Terrence Date: Fri, 10 Mar 2023 10:36:36 -0800 Subject: [PATCH] fix: configure_gce_instance error resolved IT was difficult to track down, so a bunch of clarity was added to errors that were being generated. --- gcloud/computeengine.go | 4 ++-- gcloud/computeengine_test.go | 5 +++-- gcloud/serviceusage.go | 13 ++++++++++--- gcloud/serviceusage_test.go | 19 ++++++++++++++----- tui/post_processors.go | 12 ++++++++++-- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/gcloud/computeengine.go b/gcloud/computeengine.go index 9893ad5..a2d80b3 100644 --- a/gcloud/computeengine.go +++ b/gcloud/computeengine.go @@ -177,13 +177,13 @@ func (c *Client) ImageLatestGet(project, imageproject, imagefamily string) (stri svc, err := c.getComputeService(project) if err != nil { - return resp, err + return resp, fmt.Errorf("ImageLatestGet: could not get compute service: %s", err) } filter := fmt.Sprintf("(family=\"%s\")", imagefamily) results, err := svc.Images.List(imageproject).Filter(filter).Do() if err != nil { - return resp, err + return resp, fmt.Errorf("ImageLatestGet: could not get filter list images: %s", err) } sort.Slice(results.Items, func(i, j int) bool { diff --git a/gcloud/computeengine_test.go b/gcloud/computeengine_test.go index 9c5aef5..297714c 100644 --- a/gcloud/computeengine_test.go +++ b/gcloud/computeengine_test.go @@ -538,8 +538,9 @@ func TestGetLatestImage(t *testing.T) { imageFamily string want string }{ - "debian": {projectID, "debian-cloud", "debian-11", getLatestImageByProjectFromFile(imgs, "debian-cloud", "debian-11")}, - "rhel": {projectID, "rhel-cloud", "rhel-9", getLatestImageByProjectFromFile(imgs, "rhel-cloud", "rhel-9")}, + "debian": {projectID, "debian-cloud", "debian-11", getLatestImageByProjectFromFile(imgs, "debian-cloud", "debian-11")}, + "rhel": {projectID, "rhel-cloud", "rhel-9", getLatestImageByProjectFromFile(imgs, "rhel-cloud", "rhel-9")}, + "default": {projectID, DefaultImageProject, DefaultImageFamily, getLatestImageByProjectFromFile(imgs, DefaultImageProject, DefaultImageFamily)}, } for name, tc := range tests { diff --git a/gcloud/serviceusage.go b/gcloud/serviceusage.go index c86b401..96c076e 100644 --- a/gcloud/serviceusage.go +++ b/gcloud/serviceusage.go @@ -26,6 +26,9 @@ import ( // permission to enable the service in the project or it's a nonexistent service name. var ErrorServiceNotExistOrNotAllowed = fmt.Errorf("Not found or permission denied for service") +// ErrorProjectRequired communicates that am empty project string has been passed +var ErrorProjectRequired = fmt.Errorf("Project may not be an empty string") + func (c *Client) getServiceUsageService() (*serviceusage.Service, error) { var err error svc := c.services.serviceUsage @@ -54,12 +57,12 @@ func (c *Client) ServiceEnable(project, service string) error { svc, err := c.getServiceUsageService() if err != nil { - return err + return fmt.Errorf("could not getServiceUsageService: %s", err) } enabled, err := c.ServiceIsEnabled(project, service) if err != nil { - return err + return fmt.Errorf("could not confirm if service is already enabled: %w", err) } if enabled { @@ -96,6 +99,10 @@ func (c *Client) ServiceEnable(project, service string) error { func (c *Client) ServiceIsEnabled(project, service string) (bool, error) { svc, err := c.getServiceUsageService() + if project == "" { + return false, ErrorProjectRequired + } + s := fmt.Sprintf("projects/%s/services/%s", project, service) current, err := svc.Services.Get(s).Do() if err != nil { @@ -103,7 +110,7 @@ func (c *Client) ServiceIsEnabled(project, service string) (bool, error) { return false, ErrorServiceNotExistOrNotAllowed } - return false, err + return false, fmt.Errorf("cannot get the service for resource (%s): %w", s, err) } if current.State == "ENABLED" { diff --git a/gcloud/serviceusage_test.go b/gcloud/serviceusage_test.go index d5b6815..cd98a5b 100644 --- a/gcloud/serviceusage_test.go +++ b/gcloud/serviceusage_test.go @@ -15,6 +15,7 @@ package gcloud import ( + "errors" "reflect" "testing" ) @@ -28,16 +29,24 @@ func TestServiceEnable(t *testing.T) { want bool disable bool }{ - "vault": {"vault.googleapis.com", projectID, nil, true, true}, - "compute": {"compute.googleapis.com", projectID, nil, true, false}, - "fakeservice": {"fakeservice.googleapis.com", projectID, ErrorServiceNotExistOrNotAllowed, false, false}, + "vault": {"vault.googleapis.com", projectID, nil, true, true}, + "compute": {"compute.googleapis.com", projectID, nil, true, false}, + "fakeservice": {"fakeservice.googleapis.com", projectID, ErrorServiceNotExistOrNotAllowed, false, false}, + "emptyproject": {"compute.googleapis.com", "", ErrorProjectRequired, false, false}, } for name, tc := range tests { t.Run(name, func(t *testing.T) { err := c.ServiceEnable(tc.project, tc.service) - if err != tc.err { - t.Fatalf("expected: %v got: %v", tc.err, err) + + if tc.err == nil && err != nil { + t.Fatalf("expected: no error got: %v", err) + } + + if tc.err != nil && err != nil { + if errors.Is(tc.err, err) { + t.Fatalf("expected: %v got: %v", tc.err, err) + } } got, err := c.ServiceIsEnabled(tc.project, tc.service) diff --git a/tui/post_processors.go b/tui/post_processors.go index 3dc414e..9d1a8dd 100644 --- a/tui/post_processors.go +++ b/tui/post_processors.go @@ -306,12 +306,20 @@ func validateGCEDefault(input string, q *Queue) tea.Cmd { return successMsg{} } - project := q.stack.GetSetting("project-id") + project := q.stack.GetSetting("project_id") + basename := q.stack.GetSetting("basename") defaultImage, err := q.client.ImageLatestGet(project, gcloud.DefaultImageProject, gcloud.DefaultImageFamily) if err != nil { - return errMsg{err: fmt.Errorf("validateGCEDefault: could not get DefaultImage default: %s", err)} + return errMsg{ + err: fmt.Errorf( + "validateGCEDefault: could not get DefaultImage for project(%s) family(%s): %s", + gcloud.DefaultImageProject, + gcloud.DefaultImageFamily, + err, + ), + } } defaultConfig := map[string]string{