terraform-provider-google/google/self_link_helpers.go
Riley Karson 232cb87c7a Add versioned Beta support to google_compute_instance_group_manager (#234)
* Vendor GCP Compute Beta client library.

* Refactor resource_compute_instance_group_manager for multi version support (#129)

* Refactor resource_compute_instance_group_manager for multi version support.
* Minor changes based on review.
* Removed type-specific API version conversion functions.

* Add support for Beta operations.

* Add v0beta support to google_compute_instance_group_manager.

* Renamed Key to Feature, added comments & updated some parameter names.

* Fix code and tests for version finder to match fields that don't have a change.

* Store non-v1 resources' self links as v1 so that dependent single-version resources don't see diffs.

* Fix weird change to vendor.json from merge.

* Add a note that Convert loses ForceSendFields, fix failing test.

* Moved nil type to a switch case in compute_shared_operation.go.

* Move base api version declaration above schema.
2017-07-26 13:37:59 -07:00

50 lines
1.1 KiB
Go

package google
import (
"fmt"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
// Compare only the relative path of two self links.
func compareSelfLinkRelativePaths(k, old, new string, d *schema.ResourceData) bool {
oldStripped, err := getRelativePath(old)
if err != nil {
return false
}
newStripped, err := getRelativePath(new)
if err != nil {
return false
}
if oldStripped == newStripped {
return true
}
return false
}
// Hash the relative path of a self link.
func selfLinkRelativePathHash(selfLink interface{}) int {
path, _ := getRelativePath(selfLink.(string))
return hashcode.String(path)
}
func getRelativePath(selfLink string) (string, error) {
stringParts := strings.SplitAfterN(selfLink, "projects/", 2)
if len(stringParts) != 2 {
return "", fmt.Errorf("String was not a self link: %s", selfLink)
}
return "projects/" + stringParts[1], nil
}
func ConvertSelfLinkToV1(link string) string {
reg := regexp.MustCompile("/compute/[a-zA-Z0-9]*/projects/")
return reg.ReplaceAllString(link, "/compute/v1/projects/")
}