diff --git a/gcloud/cloudbuild.go b/gcloud/cloudbuild.go index de64992..8b0fbea 100644 --- a/gcloud/cloudbuild.go +++ b/gcloud/cloudbuild.go @@ -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) } diff --git a/gcloud/clouddomains.go b/gcloud/clouddomains.go index fbe78fd..358d791 100644 --- a/gcloud/clouddomains.go +++ b/gcloud/clouddomains.go @@ -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) } diff --git a/gcloud/cloudfunctions.go b/gcloud/cloudfunctions.go index e828617..c1a1baa 100644 --- a/gcloud/cloudfunctions.go +++ b/gcloud/cloudfunctions.go @@ -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) } @@ -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) } diff --git a/gcloud/cloudrun.go b/gcloud/cloudrun.go index 1595ef2..fa1e048 100644 --- a/gcloud/cloudrun.go +++ b/gcloud/cloudrun.go @@ -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) } diff --git a/gcloud/computeengine.go b/gcloud/computeengine.go index a2d80b3..61371fb 100644 --- a/gcloud/computeengine.go +++ b/gcloud/computeengine.go @@ -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) } diff --git a/gcloud/gcloud_test.go b/gcloud/gcloud_test.go index b40c985..0b1f270 100644 --- a/gcloud/gcloud_test.go +++ b/gcloud/gcloud_test.go @@ -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) }, }, } diff --git a/gcloud/iam.go b/gcloud/iam.go index f71629e..e23ae2d 100644 --- a/gcloud/iam.go +++ b/gcloud/iam.go @@ -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) } diff --git a/gcloud/scheduler.go b/gcloud/scheduler.go index 51fa896..6a4cbeb 100644 --- a/gcloud/scheduler.go +++ b/gcloud/scheduler.go @@ -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) } diff --git a/gcloud/secretmanager.go b/gcloud/secretmanager.go index 9d307ad..0ac34cd 100644 --- a/gcloud/secretmanager.go +++ b/gcloud/secretmanager.go @@ -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) } diff --git a/gcloud/serviceusage.go b/gcloud/serviceusage.go index 96c076e..8c4526e 100644 --- a/gcloud/serviceusage.go +++ b/gcloud/serviceusage.go @@ -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") @@ -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 } @@ -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 } @@ -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 == "" { @@ -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 diff --git a/gcloud/serviceusage_test.go b/gcloud/serviceusage_test.go index cd98a5b..81c7f67 100644 --- a/gcloud/serviceusage_test.go +++ b/gcloud/serviceusage_test.go @@ -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 { @@ -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 { diff --git a/gcloud/storage.go b/gcloud/storage.go index a5d9228..be88695 100644 --- a/gcloud/storage.go +++ b/gcloud/storage.go @@ -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) }