-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
40 lines (32 loc) · 1.12 KB
/
main.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
package main
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
"gopkg.in/alecthomas/kingpin.v2"
)
func CreateNetwork(ctx context.Context, s *compute.NetworksService, project, name string) error {
network := &compute.Network{Name: name}
_, err := s.Insert(project, network).Context(ctx).Do()
return err
}
func main() {
var (
app = kingpin.New(filepath.Base(os.Args[0]), "A test harness.").DefaultEnvars()
creds = app.Flag("creds", "GCP credentials JSON.").Default("creds.json").ExistingFile()
project = app.Arg("project", "GCP project.").String()
name = app.Arg("name", "GCP network name.").String()
)
kingpin.MustParse(app.Parse(os.Args[1:]))
c, err := ioutil.ReadFile(*creds)
kingpin.FatalIfError(err, "cannot read credentials file")
ctx := context.Background()
s, err := compute.NewService(ctx,
option.WithCredentialsJSON(c),
option.WithScopes(compute.ComputeScope))
kingpin.FatalIfError(err, "cannot create compute networks service")
kingpin.FatalIfError(CreateNetwork(ctx, s.Networks, *project, *name), "cannot create network")
}