-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/google: upgrade our image resolution logic #12223
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
59e6324
Start adding tests for image resolution.
paddycarver 95e01ad
Merge branch 'master' into paddy_10984_better_image_resolution
paddycarver 73565af
Merge branch 'master' into paddy_10984_better_image_resolution
paddycarver 90254b9
provider/google: update image resolution code.
paddycarver 6868ba7
Update the docs for resolveImage.
paddycarver 4c41729
Update with @danawillow's feedback.
paddycarver 72bfc43
Update typo.
paddycarver ec93fb5
Update the documentation.
paddycarver 32c1bb4
Make all image string templates code formatted.
paddycarver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
compute "google.golang.org/api/compute/v1" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccComputeImage_resolveImage(t *testing.T) { | ||
var image compute.Image | ||
rand := acctest.RandString(10) | ||
name := fmt.Sprintf("test-image-%s", rand) | ||
fam := fmt.Sprintf("test-image-family-%s", rand) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckComputeImageDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccComputeImage_resolving(name, fam), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeImageExists( | ||
"google_compute_image.foobar", &image), | ||
testAccCheckComputeImageResolution("google_compute_image.foobar"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckComputeImageResolution(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
project := config.Project | ||
|
||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Resource not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
if rs.Primary.Attributes["name"] == "" { | ||
return fmt.Errorf("No image name is set") | ||
} | ||
if rs.Primary.Attributes["family"] == "" { | ||
return fmt.Errorf("No image family is set") | ||
} | ||
if rs.Primary.Attributes["self_link"] == "" { | ||
return fmt.Errorf("No self_link is set") | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
family := rs.Primary.Attributes["family"] | ||
link := rs.Primary.Attributes["self_link"] | ||
|
||
images := map[string]string{ | ||
"family/debian-8": "projects/debian-cloud/global/images/family/debian-8", | ||
"projects/debian-cloud/global/images/debian-8-jessie-v20170110": "projects/debian-cloud/global/images/debian-8-jessie-v20170110", | ||
"debian-8": "projects/debian-cloud/global/images/family/debian-8", | ||
"debian-8-jessie-v20170110": "projects/debian-cloud/global/images/debian-8-jessie-v20170110", | ||
"https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20170110": "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20170110", | ||
|
||
"global/images/" + name: "global/images/" + name, | ||
"global/images/family/" + family: "global/images/family/" + family, | ||
name: "global/images/" + name, | ||
family: "global/images/family/" + family, | ||
"family/" + family: "global/images/family/" + family, | ||
project + "/" + name: "projects/" + project + "/global/images/" + name, | ||
project + "/" + family: "projects/" + project + "/global/images/family/" + family, | ||
link: link, | ||
} | ||
|
||
for input, expectation := range images { | ||
result, err := resolveImage(config, input) | ||
if err != nil { | ||
return fmt.Errorf("Error resolving input %s to image: %+v\n", input, err) | ||
} | ||
if result != expectation { | ||
return fmt.Errorf("Expected input '%s' to resolve to '%s', it resolved to '%s' instead.\n", input, expectation, result) | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccComputeImage_resolving(name, family string) string { | ||
return fmt.Sprintf(` | ||
resource "google_compute_disk" "foobar" { | ||
name = "%s" | ||
zone = "us-central1-a" | ||
image = "debian-8-jessie-v20160803" | ||
} | ||
resource "google_compute_image" "foobar" { | ||
name = "%s" | ||
family = "%s" | ||
source_disk = "${google_compute_disk.foobar.self_link}" | ||
} | ||
`, name, name, family) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the following three checks all follow the same format. It might be a bit cleaner to loop over a slice of structs containing the regex, the expected length, and the result URL (although if we can't do a similar thing elsewhere it might look out of place- use your judgment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of doing that, but wasn't super convinced of the benefit--the indirection didn't make it feel any clearer, and because the format of the code would be essentially the same (define a case, find the matches, check the lengths, handle errors, build a string) it seemed to me like it would be adding more code, not removing code.
I'm happy to reconsider on this one, though, if you have any suggestions or pointers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look to see if I could quickly do my own suggestion and it looks like you're right that it would add rather than remove code.