Skip to content

Commit

Permalink
Allow overriding the base path for a URL with custom endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
rileykarson authored and modular-magician committed Jun 7, 2019
1 parent e42df41 commit 3ef40c7
Show file tree
Hide file tree
Showing 166 changed files with 659 additions and 371 deletions.
37 changes: 26 additions & 11 deletions google-beta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"regexp"
"time"

"github.com/hashicorp/terraform/helper/logging"
Expand Down Expand Up @@ -65,10 +66,13 @@ type Config struct {

tokenSource oauth2.TokenSource

clientBilling *cloudbilling.APIService
clientBuild *cloudbuild.Service
clientComposer *composer.Service
clientCompute *compute.Service
clientBilling *cloudbilling.APIService
clientBuild *cloudbuild.Service
clientComposer *composer.Service

ComputeBasePath string
clientCompute *compute.Service

clientComputeBeta *computeBeta.Service
clientContainer *container.Service
clientContainerBeta *containerBeta.Service
Expand All @@ -93,11 +97,13 @@ type Config struct {
clientServiceMan *servicemanagement.APIService
clientServiceUsage *serviceusage.Service
clientBigQuery *bigquery.Service
clientCloudFunctions *cloudfunctions.Service
clientCloudIoT *cloudiot.Service
clientAppEngine *appengine.APIService
clientServiceNetworking *servicenetworking.APIService
clientStorageTransfer *storagetransfer.Service

CloudFunctionsBasePath string
clientCloudFunctions *cloudfunctions.Service
clientCloudIoT *cloudiot.Service
clientAppEngine *appengine.APIService
clientServiceNetworking *servicenetworking.APIService
clientStorageTransfer *storagetransfer.Service

bigtableClientFactory *BigtableClientFactory
}
Expand Down Expand Up @@ -137,12 +143,14 @@ func (c *Config) LoadAndValidate() error {

context := context.Background()

log.Printf("[INFO] Instantiating GCE client...")
computeClientBasePath := removeBasePathVersion(c.ComputeBasePath) + "v1/projects/"
log.Printf("[INFO] Instantiating GCE client for path %s", computeClientBasePath)
c.clientCompute, err = compute.NewService(context, option.WithHTTPClient(client))
if err != nil {
return err
}
c.clientCompute.UserAgent = userAgent
c.clientCompute.BasePath = computeClientBasePath

log.Printf("[INFO] Instantiating GCE Beta client...")
c.clientComputeBeta, err = computeBeta.NewService(context, option.WithHTTPClient(client))
Expand Down Expand Up @@ -298,12 +306,14 @@ func (c *Config) LoadAndValidate() error {
}
c.clientBigQuery.UserAgent = userAgent

log.Printf("[INFO] Instantiating Google Cloud CloudFunctions Client...")
cloudFunctionsClientBasePath := removeBasePathVersion(c.CloudFunctionsBasePath)
log.Printf("[INFO] Instantiating Google Cloud CloudFunctions Client for path %s", cloudFunctionsClientBasePath)
c.clientCloudFunctions, err = cloudfunctions.NewService(context, option.WithHTTPClient(client))
if err != nil {
return err
}
c.clientCloudFunctions.UserAgent = userAgent
c.clientCloudFunctions.BasePath = cloudFunctionsClientBasePath

c.bigtableClientFactory = &BigtableClientFactory{
UserAgent: userAgent,
Expand Down Expand Up @@ -408,3 +418,8 @@ func (c *Config) getTokenSource(clientScopes []string) (oauth2.TokenSource, erro
log.Printf("[INFO] -- Scopes: %s", clientScopes)
return googleoauth.DefaultTokenSource(context.Background(), clientScopes...)
}

// Remove the `/{{version}}/` from a base path, replacing it with `/`
func removeBasePathVersion(url string) string {
return regexp.MustCompile(`/[^/]+/$`).ReplaceAllString(url, "/")
}
2 changes: 1 addition & 1 deletion google-beta/data_source_google_composer_image_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func dataSourceGoogleComposerImageVersionsRead(d *schema.ResourceData, meta inte
return err
}

url, err := replaceVars(d, config, "https://composer.googleapis.com/v1/projects/{{project}}/locations/{{region}}/imageVersions")
url, err := replaceVars(d, config, "{{ComposerBasePath}}projects/{{project}}/locations/{{region}}/imageVersions")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion google-beta/data_source_tpu_tensorflow_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func dataSourceTpuTensorFlowVersionsRead(d *schema.ResourceData, meta interface{
return err
}

url, err := replaceVars(d, config, "https://tpu.googleapis.com/v1/projects/{{project}}/locations/{{zone}}/tensorflowVersions")
url, err := replaceVars(d, config, "{{TpuBasePath}}projects/{{project}}/locations/{{zone}}/tensorflowVersions")
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ func Provider() terraform.ResourceProvider {
"CLOUDSDK_COMPUTE_ZONE",
}, nil),
},

"scopes": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"compute_base_path": ComputeBasePathEntry,
"cloud_functions_base_path": CloudFunctionsBasePathEntry,
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -313,6 +317,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config.Scopes[i] = scope.(string)
}

config.ComputeBasePath = d.Get("compute_base_path").(string)
config.CloudFunctionsBasePath = d.Get("cloud_functions_base_path").(string)

if err := config.LoadAndValidate(); err != nil {
return nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions google-beta/provider_access_context_manager_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var AccessContextManagerDefaultBasePath = "https://accesscontextmanager.googleapis.com/v1/"

var AccessContextManagerBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_ACCESS_CONTEXT_MANAGER_BASE_PATH",
}, AccessContextManagerDefaultBasePath),
}

var GeneratedAccessContextManagerResourcesMap = map[string]*schema.Resource{
"google_access_context_manager_access_policy": resourceAccessContextManagerAccessPolicy(),
"google_access_context_manager_access_level": resourceAccessContextManagerAccessLevel(),
Expand Down
10 changes: 10 additions & 0 deletions google-beta/provider_app_engine_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var AppEngineDefaultBasePath = "https://appengine.googleapis.com/v1/"

var AppEngineBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_APP_ENGINE_BASE_PATH",
}, AppEngineDefaultBasePath),
}

var GeneratedAppEngineResourcesMap = map[string]*schema.Resource{
"google_app_engine_firewall_rule": resourceAppEngineFirewallRule(),
}
10 changes: 10 additions & 0 deletions google-beta/provider_binary_authorization_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var BinaryAuthorizationDefaultBasePath = "https://binaryauthorization.googleapis.com/v1beta1/"

var BinaryAuthorizationBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_BINARY_AUTHORIZATION_BASE_PATH",
}, BinaryAuthorizationDefaultBasePath),
}

var GeneratedBinaryAuthorizationResourcesMap = map[string]*schema.Resource{
"google_binary_authorization_attestor": resourceBinaryAuthorizationAttestor(),
"google_binary_authorization_policy": resourceBinaryAuthorizationPolicy(),
Expand Down
10 changes: 10 additions & 0 deletions google-beta/provider_cloud_build_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var CloudBuildDefaultBasePath = "https://cloudbuild.googleapis.com/v1/"

var CloudBuildBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_CLOUD_BUILD_BASE_PATH",
}, CloudBuildDefaultBasePath),
}

var GeneratedCloudBuildResourcesMap = map[string]*schema.Resource{
"google_cloudbuild_trigger": resourceCloudBuildTrigger(),
}
10 changes: 10 additions & 0 deletions google-beta/provider_cloud_scheduler_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var CloudSchedulerDefaultBasePath = "https://cloudscheduler.googleapis.com/v1/"

var CloudSchedulerBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_CLOUD_SCHEDULER_BASE_PATH",
}, CloudSchedulerDefaultBasePath),
}

var GeneratedCloudSchedulerResourcesMap = map[string]*schema.Resource{
"google_cloud_scheduler_job": resourceCloudSchedulerJob(),
}
10 changes: 10 additions & 0 deletions google-beta/provider_compute_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var ComputeDefaultBasePath = "https://www.googleapis.com/compute/beta/"

var ComputeBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_COMPUTE_BASE_PATH",
}, ComputeDefaultBasePath),
}

var GeneratedComputeResourcesMap = map[string]*schema.Resource{
"google_compute_address": resourceComputeAddress(),
"google_compute_autoscaler": resourceComputeAutoscaler(),
Expand Down
10 changes: 10 additions & 0 deletions google-beta/provider_container_analysis_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var ContainerAnalysisDefaultBasePath = "https://containeranalysis.googleapis.com/v1beta1/"

var ContainerAnalysisBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_CONTAINER_ANALYSIS_BASE_PATH",
}, ContainerAnalysisDefaultBasePath),
}

var GeneratedContainerAnalysisResourcesMap = map[string]*schema.Resource{
"google_container_analysis_note": resourceContainerAnalysisNote(),
}
10 changes: 10 additions & 0 deletions google-beta/provider_dns_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var DnsDefaultBasePath = "https://www.googleapis.com/dns/v1beta2/"

var DnsBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_DNS_BASE_PATH",
}, DnsDefaultBasePath),
}

var GeneratedDnsResourcesMap = map[string]*schema.Resource{
"google_dns_managed_zone": resourceDnsManagedZone(),
"google_dns_policy": resourceDnsPolicy(),
Expand Down
10 changes: 10 additions & 0 deletions google-beta/provider_filestore_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var FilestoreDefaultBasePath = "https://file.googleapis.com/v1/"

var FilestoreBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_FILESTORE_BASE_PATH",
}, FilestoreDefaultBasePath),
}

var GeneratedFilestoreResourcesMap = map[string]*schema.Resource{
"google_filestore_instance": resourceFilestoreInstance(),
}
10 changes: 10 additions & 0 deletions google-beta/provider_firestore_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var FirestoreDefaultBasePath = "https://firestore.googleapis.com/v1/"

var FirestoreBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_FIRESTORE_BASE_PATH",
}, FirestoreDefaultBasePath),
}

var GeneratedFirestoreResourcesMap = map[string]*schema.Resource{
"google_firestore_index": resourceFirestoreIndex(),
}
18 changes: 18 additions & 0 deletions google-beta/provider_handwritten_base_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package google

import (
"github.com/hashicorp/terraform/helper/schema"
)

// For generated resources, base path entries live in product-specific provider
// files. Collect handwritten ones here.

var CloudFunctionsDefaultBasePath = "https://cloudfunctions.googleapis.com/v1/"

var CloudFunctionsBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_CLOUD_FUNCTIONS_BASE_PATH",
}, CloudFunctionsDefaultBasePath),
}
10 changes: 10 additions & 0 deletions google-beta/provider_kms_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var KmsDefaultBasePath = "https://cloudkms.googleapis.com/v1/"

var KmsBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_KMS_BASE_PATH",
}, KmsDefaultBasePath),
}

var GeneratedKmsResourcesMap = map[string]*schema.Resource{
"google_kms_key_ring": resourceKmsKeyRing(),
}
10 changes: 10 additions & 0 deletions google-beta/provider_logging_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var LoggingDefaultBasePath = "https://logging.googleapis.com/v2/"

var LoggingBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_LOGGING_BASE_PATH",
}, LoggingDefaultBasePath),
}

var GeneratedLoggingResourcesMap = map[string]*schema.Resource{
"google_logging_metric": resourceLoggingMetric(),
}
10 changes: 10 additions & 0 deletions google-beta/provider_monitoring_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var MonitoringDefaultBasePath = "https://monitoring.googleapis.com/v3/"

var MonitoringBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_MONITORING_BASE_PATH",
}, MonitoringDefaultBasePath),
}

var GeneratedMonitoringResourcesMap = map[string]*schema.Resource{
"google_monitoring_alert_policy": resourceMonitoringAlertPolicy(),
"google_monitoring_group": resourceMonitoringGroup(),
Expand Down
10 changes: 10 additions & 0 deletions google-beta/provider_pubsub_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var PubsubDefaultBasePath = "https://pubsub.googleapis.com/v1/"

var PubsubBasePathEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_PUBSUB_BASE_PATH",
}, PubsubDefaultBasePath),
}

var GeneratedPubsubResourcesMap = map[string]*schema.Resource{
"google_pubsub_topic": resourcePubsubTopic(),
"google_pubsub_subscription": resourcePubsubSubscription(),
Expand Down
Loading

0 comments on commit 3ef40c7

Please sign in to comment.