-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
111 lines (95 loc) · 2.88 KB
/
client.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2022 Jared Allard <jared@rgst.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package gcp
import (
"context"
"errors"
"net/http"
"github.com/golang/glog"
"github.com/jaredallard/minecraft-preempt/pkg/cloud"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
var (
// ErrNotStopped is an error that is thrown when an instance is attempted
// to be stopped but is found to be not running
ErrNotStopped = errors.New("not stopped")
)
// Client is a gcs client
type Client struct {
context context.Context
gclient *http.Client
compute *compute.Service
project string
zone string
}
// NewClient creates a new client
func NewClient(ctx context.Context, project, zone string) (*Client, error) {
client, err := google.DefaultClient(ctx, compute.ComputeScope)
if err != nil {
return nil, err
}
comp, err := compute.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return nil, err
}
return &Client{
context: context.Background(),
gclient: client,
compute: comp,
project: project,
zone: zone,
}, nil
}
// Status returns the status of an instance
func (c *Client) Status(ctx context.Context, instanceID string) (cloud.ProviderStatus, error) {
gr := c.compute.Instances.Get(c.project, c.zone, instanceID)
i, err := gr.Do()
if err != nil {
return "", err
}
// HACK: handle invalid statuses
st := cloud.ProviderStatus(i.Status)
switch st {
case cloud.StatusRunning, cloud.StatusStarting, cloud.StatusStopping, cloud.StatusStopped:
case "TERMINATED":
// Terminated is a special case, it's not really stopped
// but can be treated as such
st = cloud.StatusStopped
default:
glog.Info("Unknown status: ", i.Status)
st = cloud.StatusUnknown
}
// convert some of the types to "Starting"
if i.Status == "STAGING" || i.Status == "PROVISIONING" {
st = cloud.StatusStarting
}
return st, nil
}
// Start a instance if it's not already running
func (c *Client) Start(ctx context.Context, instanceID string) error {
gr := c.compute.Instances.Get(c.project, c.zone, instanceID)
i, err := gr.Do()
if err != nil {
return err
}
if i.Status != "STOPPED" && i.Status != "TERMINATED" {
return ErrNotStopped
}
sr := c.compute.Instances.Start(c.project, c.zone, instanceID)
_, err = sr.Do()
return err
}