Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed attached disk update test @ beta #640

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions google-beta/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 0 additions & 32 deletions google-beta/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 4 additions & 6 deletions google-beta/resource_compute_region_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,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 {
Expand Down
21 changes: 12 additions & 9 deletions google-beta/self_link_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}