-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproject.go
69 lines (54 loc) · 1.55 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package run
import (
"context"
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
const (
projectAPIBaseURL = "https://cloudresourcemanager.googleapis.com/v1/projects"
// ProjectStateActive is the state of active project.
ProjectStateActive = "ACTIVE"
)
type projectList struct {
Projects []*project `json:"projects"`
}
// Project represents GCP project.
type project struct {
Number string `json:"projectNumber"`
ID string `json:"projectId"`
State string `json:"lifecycleState"`
}
func getProjects(ctx context.Context) ([]*project, error) {
req, err := http.NewRequest(http.MethodGet, projectAPIBaseURL, nil)
if err != nil {
return nil, errors.Wrap(err, "error client creating request")
}
var list projectList
if err := apiClient.Get(ctx, req, &list); err != nil {
return nil, errors.Wrap(err, "error decoding response")
}
return list.Projects, nil
}
func isQualifiedProject(ctx context.Context, p *project, filterID string) bool {
log.Debug().Msgf("qualifying project: %s (%s - %s)", p.ID, p.Number, p.State)
if p.State != ProjectStateActive {
log.Debug().Msgf("skipping: %s (inactive)", p.ID)
return false
}
on, err := isAPIEnabled(ctx, p.Number, cloudRunAPI)
if err != nil {
log.Error().Err(err).Msgf("error checking Cloud Run API: %s", p.ID)
return false
}
if !on {
log.Debug().Msgf("skipping: %s (API not enabled)", p.ID)
return false
}
if filterID != "" && !strings.EqualFold(p.ID, filterID) {
log.Debug().Msgf("skipping: %s (filter: %s)", p.ID, filterID)
return false
}
return true
}