mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-04 17:51:11 +00:00
Allow specifying project or full URL when specifying image
This commit is contained in:
parent
4bc9add477
commit
d9add1b513
61
image.go
61
image.go
@ -1,22 +1,36 @@
|
|||||||
package google
|
package google
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.google.com/p/google-api-go-client/compute/v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// readImage finds the image with the given name.
|
// If the given name is a URL, return it.
|
||||||
func readImage(c *Config, name string) (*compute.Image, error) {
|
// If it is of the form project/name, use that URL.
|
||||||
// First, always try ourselves first.
|
// If it is of the form name then look in the configured project and then hosted image projects.
|
||||||
|
func resolveImage(c *Config, name string) (string, error) {
|
||||||
|
|
||||||
|
|
||||||
|
if strings.HasPrefix(name, "https://www.googleapis.com/compute/v1/") {
|
||||||
|
return name, nil
|
||||||
|
|
||||||
|
} else {
|
||||||
|
splitName := strings.Split(name, "/")
|
||||||
|
if len(splitName) == 1 {
|
||||||
|
|
||||||
|
// Must infer the project name:
|
||||||
|
|
||||||
|
// First, try the configured project.
|
||||||
image, err := c.clientCompute.Images.Get(c.Project, name).Do()
|
image, err := c.clientCompute.Images.Get(c.Project, name).Do()
|
||||||
if err == nil && image != nil && image.SelfLink != "" {
|
if err == nil {
|
||||||
return image, nil
|
return image.SelfLink, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a map of names to the project name where a public image is
|
// If we match a lookup for an alternate project, then try that next.
|
||||||
// hosted. GCE doesn't have an API to simply look up an image without
|
// If not, we return the original error.
|
||||||
// a project so we do this jank thing.
|
|
||||||
|
// If the image name contains the left hand side, we use the project from the right hand
|
||||||
|
// side.
|
||||||
imageMap := map[string]string{
|
imageMap := map[string]string{
|
||||||
"centos": "centos-cloud",
|
"centos": "centos-cloud",
|
||||||
"coreos": "coreos-cloud",
|
"coreos": "coreos-cloud",
|
||||||
@ -25,10 +39,8 @@ func readImage(c *Config, name string) (*compute.Image, error) {
|
|||||||
"rhel": "rhel-cloud",
|
"rhel": "rhel-cloud",
|
||||||
"sles": "suse-cloud",
|
"sles": "suse-cloud",
|
||||||
"ubuntu": "ubuntu-os-cloud",
|
"ubuntu": "ubuntu-os-cloud",
|
||||||
|
"windows": "windows-cloud",
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we match a lookup for an alternate project, then try that next.
|
|
||||||
// If not, we return the error.
|
|
||||||
var project string
|
var project string
|
||||||
for k, v := range imageMap {
|
for k, v := range imageMap {
|
||||||
if strings.Contains(name, k) {
|
if strings.Contains(name, k) {
|
||||||
@ -37,8 +49,27 @@ func readImage(c *Config, name string) (*compute.Image, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if project == "" {
|
if project == "" {
|
||||||
return nil, err
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// There was a match, but the image still may not exist, so check it:
|
||||||
|
image, err = c.clientCompute.Images.Get(project, name).Do()
|
||||||
|
if err == nil {
|
||||||
|
return image.SelfLink, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", err
|
||||||
|
|
||||||
|
} else if len(splitName) == 2 {
|
||||||
|
image, err := c.clientCompute.Images.Get(splitName[0], splitName[1]).Do()
|
||||||
|
if err == nil {
|
||||||
|
return image.SelfLink, nil
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf("Invalid image name, require URL, project/name, or just name: %s", name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.clientCompute.Images.Get(project, name).Do()
|
|
||||||
}
|
}
|
||||||
|
@ -70,15 +70,15 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error {
|
|||||||
|
|
||||||
// If we were given a source image, load that.
|
// If we were given a source image, load that.
|
||||||
if v, ok := d.GetOk("image"); ok {
|
if v, ok := d.GetOk("image"); ok {
|
||||||
log.Printf("[DEBUG] Loading image: %s", v.(string))
|
log.Printf("[DEBUG] Resolving image name: %s", v.(string))
|
||||||
image, err := readImage(config, v.(string))
|
imageUrl, err := resolveImage(config, v.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"Error loading image '%s': %s",
|
"Error resolving image name '%s': %s",
|
||||||
v.(string), err)
|
v.(string), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
disk.SourceImage = image.SelfLink
|
disk.SourceImage = imageUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := d.GetOk("type"); ok {
|
if v, ok := d.GetOk("type"); ok {
|
||||||
|
@ -231,15 +231,17 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
|||||||
// Load up the image for this disk if specified
|
// Load up the image for this disk if specified
|
||||||
if v, ok := d.GetOk(prefix + ".image"); ok {
|
if v, ok := d.GetOk(prefix + ".image"); ok {
|
||||||
imageName := v.(string)
|
imageName := v.(string)
|
||||||
image, err := readImage(config, imageName)
|
|
||||||
|
|
||||||
|
imageUrl, err := resolveImage(config, imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"Error loading image '%s': %s",
|
"Error resolving image name '%s': %s",
|
||||||
imageName, err)
|
imageName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
|
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
|
||||||
SourceImage: image.SelfLink,
|
SourceImage: imageUrl,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,6 +240,48 @@ resource "google_compute_instance" "foobar" {
|
|||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
const testAccComputeInstance_basic2 = `
|
||||||
|
resource "google_compute_instance" "foobar" {
|
||||||
|
name = "terraform-test"
|
||||||
|
machine_type = "n1-standard-1"
|
||||||
|
zone = "us-central1-a"
|
||||||
|
can_ip_forward = false
|
||||||
|
tags = ["foo", "bar"]
|
||||||
|
|
||||||
|
disk {
|
||||||
|
image = "debian-cloud/debian-7-wheezy-v20140814"
|
||||||
|
}
|
||||||
|
|
||||||
|
network {
|
||||||
|
source = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata {
|
||||||
|
foo = "bar"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
const testAccComputeInstance_basic3 = `
|
||||||
|
resource "google_compute_instance" "foobar" {
|
||||||
|
name = "terraform-test"
|
||||||
|
machine_type = "n1-standard-1"
|
||||||
|
zone = "us-central1-a"
|
||||||
|
can_ip_forward = false
|
||||||
|
tags = ["foo", "bar"]
|
||||||
|
|
||||||
|
disk {
|
||||||
|
image = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140814"
|
||||||
|
}
|
||||||
|
|
||||||
|
network {
|
||||||
|
source = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata {
|
||||||
|
foo = "bar"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
const testAccComputeInstance_update = `
|
const testAccComputeInstance_update = `
|
||||||
resource "google_compute_instance" "foobar" {
|
resource "google_compute_instance" "foobar" {
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
|
Loading…
Reference in New Issue
Block a user