~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/go4/cloud/google/gceutil/gceutil.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2015 The Camlistore Authors
 
3
 
 
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
 
7
 
 
8
     http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
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.
 
15
*/
 
16
 
 
17
// Package gceutil provides utility functions to help with instances on
 
18
// Google Compute Engine.
 
19
package gceutil
 
20
 
 
21
import (
 
22
        "encoding/json"
 
23
        "errors"
 
24
        "net/http"
 
25
        "strings"
 
26
        "time"
 
27
 
 
28
        "google.golang.org/api/compute/v1"
 
29
)
 
30
 
 
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")
 
34
        if err != nil {
 
35
                return "", err
 
36
        }
 
37
        defer resp.Body.Close()
 
38
 
 
39
        type coreOSImage struct {
 
40
                SelfLink          string
 
41
                CreationTimestamp time.Time
 
42
                Name              string
 
43
        }
 
44
 
 
45
        type coreOSImageList struct {
 
46
                Items []coreOSImage
 
47
        }
 
48
 
 
49
        imageList := &coreOSImageList{}
 
50
        if err := json.NewDecoder(resp.Body).Decode(imageList); err != nil {
 
51
                return "", err
 
52
        }
 
53
        if imageList == nil || len(imageList.Items) == 0 {
 
54
                return "", errors.New("no images list in response")
 
55
        }
 
56
 
 
57
        imageURL := ""
 
58
        var max time.Time // latest stable image creation time
 
59
        for _, v := range imageList.Items {
 
60
                if !strings.HasPrefix(v.Name, "coreos-stable") {
 
61
                        continue
 
62
                }
 
63
                if v.CreationTimestamp.After(max) {
 
64
                        max = v.CreationTimestamp
 
65
                        imageURL = v.SelfLink
 
66
                }
 
67
        }
 
68
        if imageURL == "" {
 
69
                return "", errors.New("no stable coreOS image found")
 
70
        }
 
71
        return imageURL, nil
 
72
}
 
73
 
 
74
// InstanceGroupAndManager contains both an InstanceGroup and
 
75
// its InstanceGroupManager, if any.
 
76
type InstanceGroupAndManager struct {
 
77
        Group *compute.InstanceGroup
 
78
 
 
79
        // Manager is the manager of the Group. It may be nil.
 
80
        Manager *compute.InstanceGroupManager
 
81
}
 
82
 
 
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()
 
88
        if err != nil {
 
89
                return nil, err
 
90
        }
 
91
        if managerList.NextPageToken != "" {
 
92
                return nil, errors.New("too many managers; pagination not supported")
 
93
        }
 
94
        managedBy := make(map[string]*compute.InstanceGroupManager) // instance group URL -> its manager
 
95
        for _, it := range managerList.Items {
 
96
                managedBy[it.InstanceGroup] = it
 
97
        }
 
98
        groupList, err := svc.InstanceGroups.List(proj, zone).Do()
 
99
        if err != nil {
 
100
                return nil, err
 
101
        }
 
102
        if groupList.NextPageToken != "" {
 
103
                return nil, errors.New("too many instance groups; pagination not supported")
 
104
        }
 
105
        ret := make(map[string]InstanceGroupAndManager)
 
106
        for _, it := range groupList.Items {
 
107
                ret[it.SelfLink] = InstanceGroupAndManager{it, managedBy[it.SelfLink]}
 
108
        }
 
109
        return ret, nil
 
110
}