github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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, search the specified project first, then
    10  // search image families in the specified project.
    11  // If it is of the form name then look in the configured project, then hosted
    12  // image projects, and lastly at image families in hosted image projects.
    13  func resolveImage(c *Config, name string) (string, error) {
    14  
    15  	if strings.HasPrefix(name, "https://www.googleapis.com/compute/v1/") {
    16  		return name, nil
    17  
    18  	} else {
    19  		splitName := strings.Split(name, "/")
    20  		if len(splitName) == 1 {
    21  
    22  			// Must infer the project name:
    23  
    24  			// First, try the configured project.
    25  			image, err := c.clientCompute.Images.Get(c.Project, name).Do()
    26  			if err == nil {
    27  				return image.SelfLink, nil
    28  			}
    29  
    30  			// If we match a lookup for an alternate project, then try that next.
    31  			// If not, we return the original error.
    32  
    33  			// If the image name contains the left hand side, we use the project from
    34  			// the right hand side.
    35  			imageMap := map[string]string{
    36  				"centos":   "centos-cloud",
    37  				"coreos":   "coreos-cloud",
    38  				"debian":   "debian-cloud",
    39  				"opensuse": "opensuse-cloud",
    40  				"rhel":     "rhel-cloud",
    41  				"sles":     "suse-cloud",
    42  				"ubuntu":   "ubuntu-os-cloud",
    43  				"windows":  "windows-cloud",
    44  			}
    45  			var project string
    46  			for k, v := range imageMap {
    47  				if strings.Contains(name, k) {
    48  					project = v
    49  					break
    50  				}
    51  			}
    52  			if project == "" {
    53  				return "", err
    54  			}
    55  
    56  			// There was a match, but the image still may not exist, so check it:
    57  			image, err = c.clientCompute.Images.Get(project, name).Do()
    58  			if err == nil {
    59  				return image.SelfLink, nil
    60  			}
    61  
    62  			// If it doesn't exist, try to see if it works as an image family:
    63  			image, err = c.clientCompute.Images.GetFromFamily(project, name).Do()
    64  			if err == nil {
    65  				return image.SelfLink, nil
    66  			}
    67  
    68  			return "", err
    69  
    70  		} else if len(splitName) == 2 {
    71  
    72  			// Check if image exists in the specified project:
    73  			image, err := c.clientCompute.Images.Get(splitName[0], splitName[1]).Do()
    74  			if err == nil {
    75  				return image.SelfLink, nil
    76  			}
    77  
    78  			// If it doesn't, check if it exists as an image family:
    79  			image, err = c.clientCompute.Images.GetFromFamily(splitName[0], splitName[1]).Do()
    80  			if err == nil {
    81  				return image.SelfLink, nil
    82  			}
    83  
    84  			return "", err
    85  
    86  		} else {
    87  			return "", fmt.Errorf("Invalid image name, require URL, project/name, or just name: %s", name)
    88  		}
    89  	}
    90  
    91  }