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