Skip to content

Commit

Permalink
Implement basepath patches in the provider.
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 Oct 11, 2019
1 parent 114d7fe commit c93ba7d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 30 deletions.
12 changes: 10 additions & 2 deletions google/data_source_google_compute_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ func dataSourceGoogleComputeZonesRead(d *schema.ResourceData, meta interface{})
return err
}

regionUrl := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s",
project, region)
// we want to share exactly the same base path as the compute client or the
// region string may mismatch, giving us no results
// note that the client's BasePath includes a `projects/` suffix, so that'll
// need to be added to the URL below if the source changes
computeClientBasePath := config.clientCompute.BasePath

regionUrl, err := replaceVars(d, config, fmt.Sprintf("%s%s/regions/%s", computeClientBasePath, project, region))
if err != nil {
return err
}
filter := fmt.Sprintf("(region eq %s)", regionUrl)

if s, ok := d.GetOk("status"); ok {
Expand Down
8 changes: 7 additions & 1 deletion google/resource_compute_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,13 @@ func expandComputeAutoscalerTarget(v interface{}, d TerraformResourceData, confi
if err != nil {
return nil, fmt.Errorf("Invalid value for target: %s", err)
}
return "https://www.googleapis.com/compute/v1/" + f.RelativeLink(), nil

url, err := replaceVars(d, config, "{{ComputeBasePath}}"+f.RelativeLink())
if err != nil {
return nil, err
}

return url, nil
}

func expandComputeAutoscalerZone(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
Expand Down
16 changes: 12 additions & 4 deletions google/resource_compute_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,12 @@ func expandComputeForwardingRuleBackendService(v interface{}, d TerraformResourc
// Anything that starts with a URL scheme is assumed to be a self link worth using.
return v, nil
} else if strings.HasPrefix(v.(string), "projects/") {
// If the self link references a project, we'll just stuck the compute v1 prefix on it.
return "https://www.googleapis.com/compute/v1/" + v.(string), nil
// If the self link references a project, we'll just stuck the compute prefix on it
url, err := replaceVars(d, config, "{{ComputeBasePath}}"+v.(string))
if err != nil {
return "", err
}
return url, nil
} else if strings.HasPrefix(v.(string), "regions/") || strings.HasPrefix(v.(string), "zones/") {
// For regional or zonal resources which include their region or zone, just put the project in front.
url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/")
Expand Down Expand Up @@ -682,8 +686,12 @@ func expandComputeForwardingRuleTarget(v interface{}, d TerraformResourceData, c
// Anything that starts with a URL scheme is assumed to be a self link worth using.
return v, nil
} else if strings.HasPrefix(v.(string), "projects/") {
// If the self link references a project, we'll just stuck the compute v1 prefix on it.
return "https://www.googleapis.com/compute/v1/" + v.(string), nil
// If the self link references a project, we'll just stuck the compute prefix on it
url, err := replaceVars(d, config, "{{ComputeBasePath}}"+v.(string))
if err != nil {
return "", err
}
return url, nil
} else if strings.HasPrefix(v.(string), "regions/") || strings.HasPrefix(v.(string), "zones/") {
// For regional or zonal resources which include their region or zone, just put the project in front.
url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/")
Expand Down
2 changes: 1 addition & 1 deletion google/resource_compute_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func getInstanceReferences(instanceUrls []string) (refs []*compute.InstanceRefer

func validInstanceURLs(instanceUrls []string) bool {
for _, v := range instanceUrls {
if !strings.HasPrefix(v, "https://www.googleapis.com/compute/v1/") {
if !strings.HasPrefix(v, "https://") {
return false
}
}
Expand Down
8 changes: 6 additions & 2 deletions google/resource_compute_target_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,12 @@ func expandComputeTargetInstanceInstance(v interface{}, d TerraformResourceData,
// Anything that starts with a URL scheme is assumed to be a self link worth using.
return v, nil
} else if strings.HasPrefix(v.(string), "projects/") {
// If the self link references a project, we'll just stuck the compute v1 prefix on it.
return "https://www.googleapis.com/compute/v1/" + v.(string), nil
// If the self link references a project, we'll just stuck the compute prefix on it
url, err := replaceVars(d, config, "{{ComputeBasePath}}"+v.(string))
if err != nil {
return "", err
}
return url, nil
} else if strings.HasPrefix(v.(string), "regions/") || strings.HasPrefix(v.(string), "zones/") {
// For regional or zonal resources which include their region or zone, just put the project in front.
url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/")
Expand Down
21 changes: 13 additions & 8 deletions google/resource_compute_target_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,25 @@ func convertHealthChecks(healthChecks []interface{}, d *schema.ResourceData, con

// Instances do not need to exist yet, so we simply generate URLs.
// Instances can be full URLS or zone/name
func convertInstancesToUrls(project string, names *schema.Set) ([]string, error) {
func convertInstancesToUrls(d *schema.ResourceData, config *Config, project string, names *schema.Set) ([]string, error) {
urls := make([]string, len(names.List()))
for i, nameI := range names.List() {
name := nameI.(string)
if strings.HasPrefix(name, "https://www.googleapis.com/compute/v1/") {
// assume that any URI will start with https://
if strings.HasPrefix(name, "https://") {
urls[i] = name
} else {
splitName := strings.Split(name, "/")
if len(splitName) != 2 {
return nil, fmt.Errorf("Invalid instance name, require URL or zone/name: %s", name)
} else {
urls[i] = fmt.Sprintf(
"https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances/%s",
project, splitName[0], splitName[1])
url, err := replaceVars(d, config, fmt.Sprintf(
"{{ComputeBasePath}}projects/%s/zones/%s/instances/%s",
project, splitName[0], splitName[1]))
if err != nil {
return nil, err
}
urls[i] = url
}
}
}
Expand All @@ -174,7 +179,7 @@ func resourceComputeTargetPoolCreate(d *schema.ResourceData, meta interface{}) e
return err
}

instanceUrls, err := convertInstancesToUrls(project, d.Get("instances").(*schema.Set))
instanceUrls, err := convertInstancesToUrls(d, config, project, d.Get("instances").(*schema.Set))
if err != nil {
return err
}
Expand Down Expand Up @@ -310,11 +315,11 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e
old := old_.(*schema.Set)
new := new_.(*schema.Set)

addUrls, err := convertInstancesToUrls(project, new.Difference(old))
addUrls, err := convertInstancesToUrls(d, config, project, new.Difference(old))
if err != nil {
return err
}
removeUrls, err := convertInstancesToUrls(project, old.Difference(new))
removeUrls, err := convertInstancesToUrls(d, config, project, old.Difference(new))
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion google/resource_compute_vpn_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,13 @@ func expandComputeVpnTunnelRouter(v interface{}, d TerraformResourceData, config
if err != nil {
return nil, fmt.Errorf("Invalid value for router: %s", err)
}
return "https://www.googleapis.com/compute/v1/" + f.RelativeLink(), nil

url, err := replaceVars(d, config, "{{ComputeBasePath}}"+f.RelativeLink())
if err != nil {
return nil, err
}

return url, nil
}

func expandComputeVpnTunnelPeerIp(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
Expand Down
12 changes: 4 additions & 8 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
instanceGroupManagerURL = regexp.MustCompile(fmt.Sprintf("^https://www.googleapis.com/compute/v1/projects/(%s)/zones/([a-z0-9-]*)/instanceGroupManagers/([^/]*)", ProjectRegex))
instanceGroupManagerURL = regexp.MustCompile(fmt.Sprintf("projects/(%s)/zones/([a-z0-9-]*)/instanceGroupManagers/([^/]*)", ProjectRegex))

networkConfig = &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -1647,13 +1647,9 @@ func waitForContainerClusterReady(config *Config, project, location, clusterName
})
}

// container engine's API currently mistakenly returns the instance group manager's
// URL instead of the instance group's URL in its responses. This shim detects that
// error, and corrects it, by fetching the instance group manager URL and retrieving
// the instance group manager, then using that to look up the instance group URL, which
// is then substituted.
//
// This should be removed when the API response is fixed.
// container engine's API returns the instance group manager's URL instead of the instance
// group's URL in its responses, while the field is named as if it should have been the group
// and not the manager. This shim should be supported for backwards compatibility reasons.
func getInstanceGroupUrlsFromManagerUrls(config *Config, igmUrls []string) ([]string, error) {
instanceGroupURLs := make([]string, 0, len(igmUrls))
for _, u := range igmUrls {
Expand Down
9 changes: 6 additions & 3 deletions google/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error enabling the Compute Engine API required to delete the default network: %s", err)
}

if err = forceDeleteComputeNetwork(project.ProjectId, "default", config); err != nil {
if err = forceDeleteComputeNetwork(d, config, project.ProjectId, "default"); err != nil {
if isGoogleApiErrorWithCode(err, 404) {
log.Printf("[DEBUG] Default network not found for project %q, no need to delete it", project.ProjectId)
} else {
Expand Down Expand Up @@ -495,8 +495,11 @@ func resourceProjectImportState(d *schema.ResourceData, meta interface{}) ([]*sc
}

// Delete a compute network along with the firewall rules inside it.
func forceDeleteComputeNetwork(projectId, networkName string, config *Config) error {
networkLink := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, networkName)
func forceDeleteComputeNetwork(d *schema.ResourceData, config *Config, projectId, networkName string) error {
networkLink, err := replaceVars(d, config, fmt.Sprintf("{{ComputeBasePath}}%s/global/networks/%s", projectId, networkName))
if err != nil {
return err
}

token := ""
for paginate := true; paginate; {
Expand Down

0 comments on commit c93ba7d

Please sign in to comment.