forked from hashicorp/terraform
/
image.go
43 lines (37 loc) · 1.08 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package google
import (
"strings"
"code.google.com/p/google-api-go-client/compute/v1"
)
// readImage finds the image with the given name.
func readImage(c *Config, name string) (*compute.Image, error) {
// First, always try ourselves first.
image, err := c.clientCompute.Images.Get(c.Project, name).Do()
if err == nil && image != nil && image.SelfLink != "" {
return image, nil
}
// This is a map of names to the project name where a public image is
// hosted. GCE doesn't have an API to simply look up an image without
// a project so we do this jank thing.
imageMap := map[string]string{
"centos": "centos-cloud",
"coreos": "coreos-cloud",
"debian": "debian-cloud",
"opensuse": "opensuse-cloud",
"rhel": "rhel-cloud",
"sles": "suse-cloud",
}
// If we match a lookup for an alternate project, then try that next.
// If not, we return the error.
var project string
for k, v := range imageMap {
if strings.Contains(name, k) {
project = v
break
}
}
if project == "" {
return nil, err
}
return c.clientCompute.Images.Get(project, name).Do()
}