generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.go
56 lines (49 loc) · 1.71 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
package iap
import "github.com/pkg/errors"
type Project int
const (
DspDevopsSuperProd Project = iota
DspToolsK8s
)
func ParseProject(s string) (Project, error) {
switch s {
case "dsp-devops-super-prod":
return DspDevopsSuperProd, nil
case "dsp-tools-k8s":
return DspToolsK8s, nil
}
return -1, errors.Errorf("unknown project: %s", s)
}
// ProjectID returns the project ID for the given project.
// This is the alphanumeric/hyphenated ID, not the one that can contain spaces or the one that is all numbers.
func (p Project) ProjectID() (string, error) {
switch p {
case DspDevopsSuperProd:
return "dsp-devops-super-prod", nil
case DspToolsK8s:
return "dsp-tools-k8s", nil
}
return "unknown", errors.Errorf("unknown project enum: %d", p)
}
// tokenKey generates the credentials.TokenProvider key for the given project.
// By way of explicit example, for DspDevopsSuperProd, this function returns "dsp-devops-super-prod-iap-oauth-token",
// which means that a DSP_DEVOPS_SUPER_PROD_IAP_OAUTH_TOKEN environment variable would override that token generation
// and so on.
func (p Project) tokenKey() (string, error) {
projectID, err := p.ProjectID()
if err != nil {
return "", err
} else {
return projectID + "-iap-oauth-token", nil
}
}
// oauthCredentials returns the client ID and client secret (in that order) for the given project.
func (p Project) oauthCredentials(cfg iapConfig) (string, string, error) {
switch p {
case DspDevopsSuperProd:
return cfg.Projects.DspDevopsSuperProd.ClientID, cfg.Projects.DspDevopsSuperProd.ClientSecret, nil
case DspToolsK8s:
return cfg.Projects.DspToolsK8s.ClientID, cfg.Projects.DspToolsK8s.ClientSecret, nil
}
return "", "", errors.Errorf("unknown project enum: %d", p)
}