Fixed attached disk update test @ beta (#3506)

Signed-off-by: Modular Magician <magic-modules@google.com>
This commit is contained in:
The Magician 2019-04-30 14:28:55 -07:00 committed by Riley Karson
parent 754ff5787a
commit 6345dd2dc5
4 changed files with 20 additions and 62 deletions

View File

@ -18,7 +18,6 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"strings"
"time"
@ -29,14 +28,6 @@ import (
"google.golang.org/api/googleapi"
)
const (
computeDiskUserRegexString = "^(?:https://www.googleapis.com/compute/v1/projects/)?(" + ProjectRegex + ")/zones/([-_a-zA-Z0-9]*)/instances/([-_a-zA-Z0-9]*)$"
)
var (
computeDiskUserRegex = regexp.MustCompile(computeDiskUserRegexString)
)
// Is the new disk size smaller than the old one?
func isDiskShrinkage(old, new, _ interface{}) bool {
// It's okay to remove size entirely.
@ -775,13 +766,11 @@ func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error {
for _, instance := range convertStringArr(v) {
self := d.Get("self_link").(string)
if !computeDiskUserRegex.MatchString(instance) {
return fmt.Errorf("Unknown user %q of disk %q", instance, self)
instanceProject, instanceZone, instanceName, err := GetLocationalResourcePropertiesFromSelfLinkString(instance)
if err != nil {
return err
}
matches := computeDiskUserRegex.FindStringSubmatch(instance)
instanceProject := matches[1]
instanceZone := matches[2]
instanceName := matches[3]
i, err := config.clientCompute.Instances.Get(instanceProject, instanceZone, instanceName).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {

View File

@ -437,38 +437,6 @@ func TestAccComputeDisk_deleteDetachIGM(t *testing.T) {
})
}
func TestAccComputeDisk_computeDiskUserRegex(t *testing.T) {
shouldPass := []string{
"https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1/instances/123",
"https://www.googleapis.com/compute/v1/projects/123123/zones/us-central1/instances/123",
"https://www.googleapis.com/compute/v1/projects/hashicorptest.net:project-123/zones/us-central1/instances/123",
"https://www.googleapis.com/compute/v1/projects/123/zones/456/instances/789",
}
shouldFail := []string{
"https://www.googleapis.com/compute/v1/projects/project#/zones/us-central1/instances/123",
"https://www.googleapis.com/compute/v1/projects/project/zones/us-central#/instances/123",
"https://www.googleapis.com/compute/v1/projects/project/zones/us-central1/instances/?",
"https://www.googleapis.com/compute/v1/projects/foo.com:bar:baz/zones/us-central1/instances/?",
"https://www.googleapis.com/compute/v1/projects/foo.com:/zones/us-central1/instances/?",
}
for _, element := range shouldPass {
if !computeDiskUserRegex.MatchString(element) {
t.Error("computeDiskUserRegex should match on '" + element + "' but doesn't")
}
}
for _, element := range shouldFail {
if computeDiskUserRegex.MatchString(element) {
t.Error("computeDiskUserRegex shouldn't match on '" + element + "' but does")
}
}
}
func testAccCheckComputeDiskExists(n, p string, disk *compute.Disk) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

View File

@ -510,13 +510,11 @@ func resourceComputeRegionDiskDelete(d *schema.ResourceData, meta interface{}) e
for _, instance := range convertStringArr(v) {
self := d.Get("self_link").(string)
if !computeDiskUserRegex.MatchString(instance) {
return fmt.Errorf("Unknown user %q of disk %q", instance, self)
instanceProject, instanceZone, instanceName, err := GetLocationalResourcePropertiesFromSelfLinkString(instance)
if err != nil {
return err
}
matches := computeDiskUserRegex.FindStringSubmatch(instance)
instanceProject := matches[1]
instanceZone := matches[2]
instanceName := matches[3]
i, err := config.clientCompute.Instances.Get(instanceProject, instanceZone, instanceName).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {

View File

@ -106,15 +106,7 @@ func GetRegionalResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, c
func getResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *Config, locationType LocationType) (string, string, string, error) {
if selfLink, ok := d.GetOk("self_link"); ok {
parsed, err := url.Parse(selfLink.(string))
if err != nil {
return "", "", "", err
}
s := strings.Split(parsed.Path, "/")
// https://www.googleapis.com/compute/beta/projects/project_name/regions/region_name/instanceGroups/foobarbaz
// => project_name, region_name, foobarbaz
return s[4], s[6], s[8], nil
return GetLocationalResourcePropertiesFromSelfLinkString(selfLink.(string))
} else {
project, err := getProject(d, config)
if err != nil {
@ -142,3 +134,14 @@ func getResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *C
return project, location, name, nil
}
}
// given a full locational (non-global) self link, returns the project + region/zone + name or an error
func GetLocationalResourcePropertiesFromSelfLinkString(selfLink string) (string, string, string, error) {
parsed, err := url.Parse(selfLink)
if err != nil {
return "", "", "", err
}
s := strings.Split(parsed.Path, "/")
return s[4], s[6], s[8], nil
}