github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/image.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // If the given name is a URL, return it.
     9  // If it is of the form project/name, use that URL.
    10  // If it is of the form name then look in the configured project and then hosted image projects.
    11  func resolveImage(c *Config, name string) (string, error) {
    12  
    13  	if strings.HasPrefix(name, "https://www.googleapis.com/compute/v1/") {
    14  		return name, nil
    15  
    16  	} else {
    17  		splitName := strings.Split(name, "/")
    18  		if len(splitName) == 1 {
    19  
    20  			// Must infer the project name:
    21  
    22  			// First, try the configured project.
    23  			image, err := c.clientCompute.Images.Get(c.Project, name).Do()
    24  			if err == nil {
    25  				return image.SelfLink, nil
    26  			}
    27  
    28  			// If we match a lookup for an alternate project, then try that next.
    29  			// If not, we return the original error.
    30  
    31  			// If the image name contains the left hand side, we use the project from the right hand
    32  			// side.
    33  			imageMap := map[string]string{
    34  				"centos":   "centos-cloud",
    35  				"coreos":   "coreos-cloud",
    36  				"debian":   "debian-cloud",
    37  				"opensuse": "opensuse-cloud",
    38  				"rhel":     "rhel-cloud",
    39  				"sles":     "suse-cloud",
    40  				"ubuntu":   "ubuntu-os-cloud",
    41  				"windows":  "windows-cloud",
    42  			}
    43  			var project string
    44  			for k, v := range imageMap {
    45  				if strings.Contains(name, k) {
    46  					project = v
    47  					break
    48  				}
    49  			}
    50  			if project == "" {
    51  				return "", err
    52  			}
    53  
    54  			// There was a match, but the image still may not exist, so check it:
    55  			image, err = c.clientCompute.Images.Get(project, name).Do()
    56  			if err == nil {
    57  				return image.SelfLink, nil
    58  			}
    59  
    60  			return "", err
    61  
    62  		} else if len(splitName) == 2 {
    63  			image, err := c.clientCompute.Images.Get(splitName[0], splitName[1]).Do()
    64  			if err == nil {
    65  				return image.SelfLink, nil
    66  			}
    67  			return "", err
    68  
    69  		} else {
    70  			return "", fmt.Errorf("Invalid image name, require URL, project/name, or just name: %s", name)
    71  		}
    72  	}
    73  
    74  }