/
google.go
52 lines (42 loc) · 1.11 KB
/
google.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
package platform
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/otiai10/hotsub/params"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
)
const (
// DefaultGoogleInstanceTag ....
DefaultGoogleInstanceTag = "hotsub" + "-" + HotsubSecurityStructureVersion
// DefaultGoogleFirewallName ...
DefaultGoogleFirewallName = "hotsub-firewall" + "-" + HotsubSecurityStructureVersion
)
// GoogleCloudPlatform ...
type GoogleCloudPlatform struct{}
// Validate validates Google platform itself.
// 1) Create Firewall Rule for specific tag.
func (p GoogleCloudPlatform) Validate(ctx params.Context) error {
client, err := google.DefaultClient(context.Background())
if err != nil {
return err
}
service, err := compute.New(client)
if err != nil {
return err
}
if err := p.createGoogleFirewallIfNotExists(service, ctx); err != nil {
return err
}
return nil
}
func (p GoogleCloudPlatform) errorizeOperationError(ope *compute.OperationError) error {
if ope == nil {
return nil
}
buf := new(bytes.Buffer)
json.NewDecoder(buf).Decode(ope)
return fmt.Errorf(buf.String())
}