github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 for a specific image:
    25  			image, err := c.clientCompute.Images.Get(c.Project, name).Do()
    26  			if err == nil {
    27  				return image.SelfLink, nil
    28  			}
    29  
    30  			// If it doesn't exist, try to see if it works as an image family:
    31  			image, err = c.clientCompute.Images.GetFromFamily(c.Project, name).Do()
    32  			if err == nil {
    33  				return image.SelfLink, nil
    34  			}
    35  
    36  			// If we match a lookup for an alternate project, then try that next.
    37  			// If not, we return the original error.
    38  
    39  			// If the image name contains the left hand side, we use the project from
    40  			// the right hand side.
    41  			imageMap := map[string]string{
    42  				"centos":   "centos-cloud",
    43  				"coreos":   "coreos-cloud",
    44  				"debian":   "debian-cloud",
    45  				"opensuse": "opensuse-cloud",
    46  				"rhel":     "rhel-cloud",
    47  				"sles":     "suse-cloud",
    48  				"ubuntu":   "ubuntu-os-cloud",
    49  				"windows":  "windows-cloud",
    50  			}
    51  			var project string
    52  			for k, v := range imageMap {
    53  				if strings.Contains(name, k) {
    54  					project = v
    55  					break
    56  				}
    57  			}
    58  			if project == "" {
    59  				return "", err
    60  			}
    61  
    62  			// There was a match, but the image still may not exist, so check it:
    63  			image, err = c.clientCompute.Images.Get(project, name).Do()
    64  			if err == nil {
    65  				return image.SelfLink, nil
    66  			}
    67  
    68  			// If it doesn't exist, try to see if it works as an image family:
    69  			image, err = c.clientCompute.Images.GetFromFamily(project, name).Do()
    70  			if err == nil {
    71  				return image.SelfLink, nil
    72  			}
    73  
    74  			return "", err
    75  
    76  		} else if len(splitName) == 2 {
    77  
    78  			// Check if image exists in the specified project:
    79  			image, err := c.clientCompute.Images.Get(splitName[0], splitName[1]).Do()
    80  			if err == nil {
    81  				return image.SelfLink, nil
    82  			}
    83  
    84  			// If it doesn't, check if it exists as an image family:
    85  			image, err = c.clientCompute.Images.GetFromFamily(splitName[0], splitName[1]).Do()
    86  			if err == nil {
    87  				return image.SelfLink, nil
    88  			}
    89  
    90  			return "", err
    91  
    92  		} else {
    93  			return "", fmt.Errorf("Invalid image name, require URL, project/name, or just name: %s", name)
    94  		}
    95  	}
    96  
    97  }