-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsettings.go
More file actions
218 lines (185 loc) · 5.61 KB
/
settings.go
File metadata and controls
218 lines (185 loc) · 5.61 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package api
import (
"net/url"
"os"
"strings"
configMediator "github.com/ActiveState/cli/internal/mediators/config"
"github.com/ActiveState/cli/pkg/projectfile"
"github.com/ActiveState/cli/internal/condition"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/logging"
)
func init() {
configMediator.RegisterOption(constants.APIHostConfig, configMediator.String, "")
}
// Service records available api services
type Service string
const (
// ServiceMono is our main service for api services, "Mono" refers to its monolithic nature, one that we're trying to get away from
ServiceMono Service = "platform"
// ServiceSecrets is our service that's used purely for setting and storing secrets
ServiceSecrets = "secrets"
// ServiceHeadChef is our service that's used to kick off and track builds
ServiceHeadChef = "headchef"
// ServiceHeadChefWS is the websocket service on headchef
BuildLogStreamer = "buildlog-streamer"
// ServiceInventory is our service that's used to query available inventory and dependencies
ServiceInventory = "inventory"
// ServiceGraphQL is our service that's used as a graphql endpoint for platform requests
ServiceGraphQL = "platform-graphql"
// ServiceMediator is our mediator service used to query build graph data
ServiceMediator = "mediator"
// ServiceRequirementsImport is our service that processes requirements.txt files.
ServiceRequirementsImport = "requirements-import"
// ServiceBuildPlanner is our service that processes build plans.
ServiceBuildPlanner = "build-planner"
// ServiceVulnerabilities is Data Acquisition's Hasura service for vulnerability (CVE) information.
ServiceVulnerabilities = "vulnerabilities"
// ServiceHasuraInventory is the Hasura service for inventory information.
ServiceHasuraInventory = "hasura-inventory"
// ServiceUpdateInfo is the service for update info
ServiceUpdateInfo = "update-info"
// TestingPlatform is the API host used by tests so-as not to affect production.
TestingPlatform = ".testing.tld"
)
var urlsByService = map[Service]*url.URL{
ServiceMono: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.MonoAPIPath,
},
ServiceSecrets: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.SecretsAPIPath,
},
ServiceHeadChef: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.HeadChefAPIPath,
},
BuildLogStreamer: {
Scheme: "wss",
Host: constants.DefaultAPIHost,
Path: constants.BuildLogStreamerPath,
},
ServiceInventory: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.InventoryAPIPath,
},
ServiceGraphQL: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.GraphqlAPIPath,
},
ServiceMediator: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.MediatorAPIPath,
},
ServiceRequirementsImport: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.RequirementsImportAPIPath,
},
ServiceBuildPlanner: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.BuildPlannerAPIPath,
},
ServiceVulnerabilities: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.VulnerabilitiesAPIPath,
},
ServiceHasuraInventory: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.HasuraInventoryAPIPath,
},
ServiceUpdateInfo: {
Scheme: "https",
Host: constants.DefaultAPIHost,
Path: constants.UpdateInfoAPIPath,
},
}
// GetServiceURL returns the URL for the given service
func GetServiceURL(service Service) *url.URL {
serviceURL, validService := urlsByService[service]
if !validService {
logging.Panic("Invalid service: %s", string(service))
}
if host := getProjectHost(service); host != nil {
serviceURL.Host = *host
}
if insecure := getProjectHostFromEnv(); insecure == "true" {
if serviceURL.Scheme == "https" || serviceURL.Scheme == "wss" {
serviceURL.Scheme = strings.TrimRight(serviceURL.Scheme, "s")
}
}
sname := strings.Replace(strings.ToUpper(string(service)), "-", "_", -1)
envname := constants.APIServiceOverrideEnvVarName + sname
if override := os.Getenv(envname); override != "" {
u, err := url.Parse(override)
if err != nil {
logging.Error("Could not apply %s: %s", envname, err)
} else {
return u
}
}
return serviceURL
}
func getProjectHost(service Service) *string {
if host := HostOverride(); host != "" {
logging.Debug("Using host override: %s", host)
return &host
}
if condition.InUnitTest() {
testingPlatform := string(service) + TestingPlatform
return &testingPlatform
}
pj, err := projectfile.FromEnv()
if err != nil {
return nil
}
url, err := url.Parse(pj.Project)
if err != nil {
logging.Error("Could not parse project url: %s", pj.Project)
return nil
}
return &url.Host
}
func getProjectHostFromConfig() string {
cfg, err := config.New()
if err != nil {
return ""
}
return cfg.GetString(constants.APIHostConfig)
}
func getProjectHostFromEnv() string {
return os.Getenv(constants.APIHostEnvVarName)
}
func HostOverride() string {
if apiHost := getProjectHostFromEnv(); apiHost != "" {
return apiHost
}
if apiHost := getProjectHostFromConfig(); apiHost != "" {
return apiHost
}
return ""
}
// GetPlatformURL returns a generic Platform URL for the given path.
// This is for retrieving non-service URLs (e.g. signup URL).
func GetPlatformURL(path string) *url.URL {
host := constants.DefaultAPIHost
if hostOverride := HostOverride(); hostOverride != "" {
host = hostOverride
}
return &url.URL{
Scheme: "https",
Host: host,
Path: path,
}
}