2
Copyright 2015 The Camlistore Authors
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
17
// Package gceutil provides utility functions to help with instances on
18
// Google Compute Engine.
28
"google.golang.org/api/compute/v1"
31
// CoreOSImageURL returns the URL of the latest stable CoreOS image for running on Google Compute Engine.
32
func CoreOSImageURL(cl *http.Client) (string, error) {
33
resp, err := cl.Get("https://www.googleapis.com/compute/v1/projects/coreos-cloud/global/images")
37
defer resp.Body.Close()
39
type coreOSImage struct {
41
CreationTimestamp time.Time
45
type coreOSImageList struct {
49
imageList := &coreOSImageList{}
50
if err := json.NewDecoder(resp.Body).Decode(imageList); err != nil {
53
if imageList == nil || len(imageList.Items) == 0 {
54
return "", errors.New("no images list in response")
58
var max time.Time // latest stable image creation time
59
for _, v := range imageList.Items {
60
if !strings.HasPrefix(v.Name, "coreos-stable") {
63
if v.CreationTimestamp.After(max) {
64
max = v.CreationTimestamp
69
return "", errors.New("no stable coreOS image found")
74
// InstanceGroupAndManager contains both an InstanceGroup and
75
// its InstanceGroupManager, if any.
76
type InstanceGroupAndManager struct {
77
Group *compute.InstanceGroup
79
// Manager is the manager of the Group. It may be nil.
80
Manager *compute.InstanceGroupManager
83
// InstanceGroups returns all the instance groups in a project's zone, along
84
// with their associated InstanceGroupManagers.
85
// The returned map is keyed by the instance group identifier URL.
86
func InstanceGroups(svc *compute.Service, proj, zone string) (map[string]InstanceGroupAndManager, error) {
87
managerList, err := svc.InstanceGroupManagers.List(proj, zone).Do()
91
if managerList.NextPageToken != "" {
92
return nil, errors.New("too many managers; pagination not supported")
94
managedBy := make(map[string]*compute.InstanceGroupManager) // instance group URL -> its manager
95
for _, it := range managerList.Items {
96
managedBy[it.InstanceGroup] = it
98
groupList, err := svc.InstanceGroups.List(proj, zone).Do()
102
if groupList.NextPageToken != "" {
103
return nil, errors.New("too many instance groups; pagination not supported")
105
ret := make(map[string]InstanceGroupAndManager)
106
for _, it := range groupList.Items {
107
ret[it.SelfLink] = InstanceGroupAndManager{it, managedBy[it.SelfLink]}