Skip to content

Commit

Permalink
fix: changed service enablements to enums to make them more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
tpryan committed Mar 11, 2023
1 parent bf2c29e commit c9473fd
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gcloud/cloudbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *Client) getCloudBuildService(project string) (*cloudbuild.Service, erro
return svc, nil
}

if err := c.ServiceEnable(project, "cloudbuild.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, CloudBuild); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion gcloud/clouddomains.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Client) getDomainsClient(project string) (*domains.Client, error) {
return svc, nil
}

if err := c.ServiceEnable(project, "domains.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, Domains); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
4 changes: 2 additions & 2 deletions gcloud/cloudfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *Client) getCloudFunctionsService(project string) (*cloudfunctions.Servi
return svc, nil
}

if err := c.ServiceEnable(project, "cloudfunctions.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, CloudFunctions); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand All @@ -48,7 +48,7 @@ func (c *Client) getCloudFunctionsService(project string) (*cloudfunctions.Servi
func (c *Client) FunctionRegionList(project string) ([]string, error) {
resp := []string{}

if err := c.ServiceEnable(project, "cloudfunctions.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, CloudFunctions); err != nil {
return resp, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion gcloud/cloudrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *Client) getRunService(project string) (*run.APIService, error) {
return svc, nil
}

if err := c.ServiceEnable(project, "run.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, Run); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion gcloud/computeengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Client) getComputeService(project string) (*compute.Service, error) {
return svc, nil
}

if err := c.ServiceEnable(project, "compute.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, Compute); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion gcloud/gcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func TestBreakServicesServiceUsage(t *testing.T) {
client.services.serviceUsage.BasePath = "nonsenseshouldbreak"
},
errorfunc: func() (interface{}, error) {
return client.ServiceIsEnabled(projectID, "example.com")
return client.ServiceIsEnabled(projectID, Compute)
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion gcloud/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *Client) getIAMService(project string) (*iam.Service, error) {
return svc, nil
}

if err := c.ServiceEnable(project, "domains.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, IAM); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion gcloud/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *Client) getSchedulerService(project string) (*scheduler.CloudSchedulerC
return svc, nil
}

if err := c.ServiceEnable(project, "cloudscheduler.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, CloudScheduler); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion gcloud/secretmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *Client) getSecretManagerService(project string) (*secretmanager.Service
return svc, nil
}

if err := c.ServiceEnable(project, "secretmanager.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, SecretManager); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down
75 changes: 68 additions & 7 deletions gcloud/serviceusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,67 @@ import (
"google.golang.org/api/serviceusage/v1"
)

// Service is an enum that makes it easy to reference Google Cloud Services
type Service int64

const (
// Compute is the service name for enabling Compute Engine
Compute Service = iota + 1
// CloudResourceManager is the service name for enabling
// CloudResourceManager this is ultimately about manipulating projects
CloudResourceManager
// CloudBuild is the service name for enabling Cloud Build
CloudBuild
// CloudScheduler is the service name for enabling Cloud Scheduler
CloudScheduler
// Domains is the service name for enabling Cloud Domains
Domains
// CloudFunctions is the service name for enabling Cloud Functions
CloudFunctions
// Run is the service name for enabling Cloud Run
Run
// IAM is the service name for enabling Cloud IAM
IAM
// SecretManager is the service name for enabling Cloud Secret Manager
SecretManager
// Storage is the service name for enabling Cloud Storage
Storage
// Vault is the service name for enabling Cloud Vault
Vault
)

func (s Service) String() string {
apistring := "googleapis.com"
svc := ""
switch s {
case Compute:
svc = "compute"
case CloudResourceManager:
svc = "cloudresourcemanager"
case CloudBuild:
svc = "cloudbuild"
case Domains:
svc = "domains"
case CloudFunctions:
svc = "cloudfunctions"
case Run:
svc = "run"
case IAM:
svc = "iam"
case SecretManager:
svc = "secretmanager"
case Storage:
svc = "storage"
case CloudScheduler:
svc = "cloudscheduler"
case Vault:
svc = "vault"
default:
svc = "unknown"
}
return fmt.Sprintf("%s.%s", svc, apistring)
}

// ErrorServiceNotExistOrNotAllowed occurs when the user running this code doesn't have
// 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")
Expand Down Expand Up @@ -50,8 +111,8 @@ func (c *Client) getServiceUsageService() (*serviceusage.Service, error) {

// ServiceEnable enable a service in the selected project so that query calls
// to various lists will work.
func (c *Client) ServiceEnable(project, service string) error {
if _, ok := c.enabledServices[service]; ok {
func (c *Client) ServiceEnable(project string, service Service) error {
if _, ok := c.enabledServices[service.String()]; ok {
return nil
}

Expand All @@ -66,7 +127,7 @@ func (c *Client) ServiceEnable(project, service string) error {
}

if enabled {
c.enabledServices[service] = true
c.enabledServices[service.String()] = true
return nil
}

Expand All @@ -83,20 +144,20 @@ func (c *Client) ServiceEnable(project, service string) error {
return err
}
if enabled {
c.enabledServices[service] = true
c.enabledServices[service.String()] = true
return nil
}
time.Sleep(1 * time.Second)
}
}

c.enabledServices[service] = true
c.enabledServices[service.String()] = true
return nil
}

// ServiceIsEnabled checks to see if the existing service is already enabled
// in the project we are trying to enable it in.
func (c *Client) ServiceIsEnabled(project, service string) (bool, error) {
func (c *Client) ServiceIsEnabled(project string, service Service) (bool, error) {
svc, err := c.getServiceUsageService()

if project == "" {
Expand All @@ -121,7 +182,7 @@ func (c *Client) ServiceIsEnabled(project, service string) (bool, error) {
}

// ServiceDisable disables a service in the selected project
func (c *Client) ServiceDisable(project, service string) error {
func (c *Client) ServiceDisable(project string, service Service) error {
svc, err := c.getServiceUsageService()
if err != nil {
return err
Expand Down
18 changes: 10 additions & 8 deletions gcloud/serviceusage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ import (
"testing"
)

const FAKESERVICE Service = 1000004

func TestServiceEnable(t *testing.T) {
c := NewClient(ctx, defaultUserAgent)
tests := map[string]struct {
service string
service Service
project string
err error
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},
"emptyproject": {"compute.googleapis.com", "", ErrorProjectRequired, false, false},
"vault": {Vault, projectID, nil, true, true},
"compute": {Compute, projectID, nil, true, false},
"emptyproject": {Compute, "", ErrorProjectRequired, false, false},
"fakeservice": {FAKESERVICE, projectID, ErrorServiceNotExistOrNotAllowed, false, false},
}

for name, tc := range tests {
Expand Down Expand Up @@ -67,13 +69,13 @@ func TestServiceEnable(t *testing.T) {
func TestServiceDisable(t *testing.T) {
c := NewClient(ctx, defaultUserAgent)
tests := map[string]struct {
service string
service Service
project string
err error
want bool
}{
"vault": {"vault.googleapis.com", projectID, nil, false},
"fakeservice": {"fakeservice.googleapis.com", projectID, ErrorServiceNotExistOrNotAllowed, false},
"vault": {Vault, projectID, nil, false},
"fakeservice": {FAKESERVICE, projectID, ErrorServiceNotExistOrNotAllowed, false},
}

for name, tc := range tests {
Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *Client) getStorageService(project string) (*storage.Client, error) {
return svc, nil
}

if err := c.ServiceEnable(project, "storage.googleapis.com"); err != nil {
if err := c.ServiceEnable(project, Storage); err != nil {
return nil, fmt.Errorf("error activating service for polling: %s", err)
}

Expand Down

0 comments on commit c9473fd

Please sign in to comment.