/
project.go
53 lines (44 loc) · 1.7 KB
/
project.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
// Copyright © 2019 Circonus, Inc. <support@circonus.com>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package gcpservice
import (
"github.com/circonus-labs/circonus-cloud-agent/internal/circonus"
"github.com/pkg/errors"
"golang.org/x/oauth2/google"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/option"
)
// loadProjectMeta retrieves project meta data from gcp api
// https://godoc.org/google.golang.org/api/cloudresourcemanager/v1#Project
func (inst *Instance) loadProjectMeta() error {
conf, err := google.JWTConfigFromJSON(inst.cfg.GCP.credentialData, "https://www.googleapis.com/auth/cloud-platform.read-only")
if err != nil {
return errors.Wrap(err, "initializing gcp credentials")
}
client := conf.Client(inst.ctx)
// svc, err := cloudresourcemanager.New(client)
svc, err := cloudresourcemanager.NewService(inst.ctx, option.WithHTTPClient(client))
if err != nil {
return errors.Wrap(err, "initializing resource manager service")
}
resp, err := svc.Projects.Get(inst.cfg.GCP.projectID).Context(inst.ctx).Do()
if err != nil {
return errors.Wrap(err, "retrieving project meta data")
}
// ensure project is active
if resp.LifecycleState != "ACTIVE" {
return errors.Errorf("project %s is not active (%s)", resp.Name, resp.LifecycleState)
}
// save the project name (for use in check bundle display name)
inst.cfg.GCP.projectName = resp.Name
// use project labels as base tags
inst.baseTags = circonus.Tags{
circonus.Tag{Category: "project_id", Value: inst.cfg.GCP.projectID},
}
for k, v := range resp.Labels {
inst.baseTags = append(inst.baseTags, circonus.Tag{Category: k, Value: v})
}
return nil
}