Skip to content

Commit

Permalink
fix: configure_gce_instance error resolved
Browse files Browse the repository at this point in the history
IT was difficult to track down, so a bunch of clarity was added to errors that were being generated.
  • Loading branch information
tpryan committed Mar 10, 2023
1 parent d337dbe commit 71a3b2e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions gcloud/computeengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions gcloud/computeengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions gcloud/serviceusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -96,14 +99,18 @@ 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 {
if strings.Contains(err.Error(), "Not found or permission denied for service") {
return false, ErrorServiceNotExistOrNotAllowed
}

return false, err
return false, fmt.Errorf("cannot get the service for resource (%s): %w", s, err)
}

if current.State == "ENABLED" {
Expand Down
19 changes: 14 additions & 5 deletions gcloud/serviceusage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package gcloud

import (
"errors"
"reflect"
"testing"
)
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions tui/post_processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 71a3b2e

Please sign in to comment.