Skip to content

Commit 1a88547

Browse files
authored
Merge pull request #3717 from ActiveState/miked/PIF-1050
Allow configuration of API host
2 parents c963853 + 792555c commit 1a88547

File tree

13 files changed

+188
-8
lines changed

13 files changed

+188
-8
lines changed

cmd/state-svc/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ActiveState/cli/internal/rollbar"
3030
"github.com/ActiveState/cli/internal/runbits/panics"
3131
"github.com/ActiveState/cli/internal/svcctl"
32+
"github.com/ActiveState/cli/pkg/platform/api"
3233
"github.com/ActiveState/cli/pkg/platform/authentication"
3334
"github.com/inconshreveable/mousetrap"
3435
)
@@ -70,6 +71,8 @@ func main() {
7071
rollbar.SetupRollbar(constants.StateServiceRollbarToken)
7172
rollbar.SetConfig(cfg)
7273

74+
api.SetConfig(cfg)
75+
7376
if os.Getenv("VERBOSE") == "true" {
7477
logging.CurrentHandler().SetVerbose(true)
7578
}

cmd/state/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/ActiveState/cli/internal/runbits/panics"
3838
"github.com/ActiveState/cli/internal/subshell"
3939
"github.com/ActiveState/cli/internal/svcctl"
40+
"github.com/ActiveState/cli/pkg/platform/api"
4041
secretsapi "github.com/ActiveState/cli/pkg/platform/api/secrets"
4142
"github.com/ActiveState/cli/pkg/platform/authentication"
4243
"github.com/ActiveState/cli/pkg/platform/model"
@@ -91,6 +92,7 @@ func main() {
9192
return
9293
}
9394
rollbar.SetConfig(cfg)
95+
api.SetConfig(cfg)
9496

9597
// Configuration options
9698
// This should only be used if the config option is not exclusive to one package.

internal/constants/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ const VulnerabilitiesAPIPath = "/v13s/v1/graphql"
261261
// HasuraInventoryAPIPath is the path used for the hasura inventory api
262262
const HasuraInventoryAPIPath = "/sv/hasura-inventory/v1/graphql"
263263

264+
// UpdateInfoAPIPath is the path used for the update info api
265+
const UpdateInfoAPIPath = "/sv/state-update/api/v1"
266+
264267
// NotificationsInfoURL is the URL we check against to see what versions are deprecated
265268
const NotificationsInfoURL = "https://state-tool.s3.amazonaws.com/messages.json"
266269

@@ -409,6 +412,9 @@ const SecurityPromptConfig = "security.prompt.enabled"
409412
// SecurityPromptLevelConfig is the config key used to determine the level of security prompts
410413
const SecurityPromptLevelConfig = "security.prompt.level"
411414

415+
// APIHostConfig is the config key used to determine the api host
416+
const APIHostConfig = "api.host"
417+
412418
// SvcAppName is the name we give our state-svc application
413419
const SvcAppName = "State Service"
414420

internal/runbits/checkout/checkout.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ActiveState/cli/internal/runbits/buildscript"
1515
"github.com/ActiveState/cli/internal/runbits/git"
1616
"github.com/ActiveState/cli/pkg/localcommit"
17+
"github.com/ActiveState/cli/pkg/platform/api"
1718
"github.com/ActiveState/cli/pkg/platform/api/mono/mono_models"
1819
"github.com/ActiveState/cli/pkg/platform/authentication"
1920
"github.com/ActiveState/cli/pkg/platform/model"
@@ -193,6 +194,7 @@ func CreateProjectFiles(checkoutPath, cachePath, owner, name, branch, commitID,
193194
Language: language,
194195
Cache: cachePath,
195196
Portable: portable,
197+
Host: api.HostOverride(),
196198
})
197199
if err != nil {
198200
if osutils.IsAccessDeniedError(err) {

internal/runbits/runtime/runtime.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/ActiveState/cli/internal/runbits/runtime/trigger"
2828
"github.com/ActiveState/cli/pkg/buildplan"
2929
"github.com/ActiveState/cli/pkg/localcommit"
30+
"github.com/ActiveState/cli/pkg/platform/api"
3031
"github.com/ActiveState/cli/pkg/platform/model"
3132
bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner"
3233
"github.com/ActiveState/cli/pkg/project"
@@ -264,7 +265,7 @@ func Update(
264265
// Build progress URL is of the form
265266
// https://<host>/<owner>/<project>/distributions?branch=<branch>&commitID=<commitID>
266267
host := constants.DefaultAPIHost
267-
if hostOverride := os.Getenv(constants.APIHostEnvVarName); hostOverride != "" {
268+
if hostOverride := api.HostOverride(); hostOverride != "" {
268269
host = hostOverride
269270
}
270271
path, err := url.JoinPath(proj.Owner(), proj.Name(), constants.BuildProgressUrlPathName)

internal/runners/initialize/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/ActiveState/cli/internal/runbits/runtime"
2626
"github.com/ActiveState/cli/internal/runbits/runtime/trigger"
2727
"github.com/ActiveState/cli/pkg/localcommit"
28+
"github.com/ActiveState/cli/pkg/platform/api"
2829
"github.com/ActiveState/cli/pkg/platform/authentication"
2930
"github.com/ActiveState/cli/pkg/platform/model"
3031
bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner"
@@ -186,6 +187,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
186187
Language: lang.String(),
187188
Directory: path,
188189
Private: params.Private,
190+
Host: api.HostOverride(),
189191
}
190192

191193
pjfile, err := projectfile.Create(createParams)

internal/testhelpers/e2e/session.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Session struct {
6565
spawned []*SpawnedCmd
6666
ignoreLogErrors bool
6767
cache keyCache
68+
cfg *config.Instance
6869
}
6970

7071
type keyCache map[string]string
@@ -203,6 +204,7 @@ func new(t *testing.T, retainDirs, updatePath bool, extraEnv ...string) *Session
203204
if err := cfg.Set(constants.SecurityPromptConfig, false); err != nil {
204205
require.NoError(session.T, err)
205206
}
207+
session.cfg = cfg
206208

207209
return session
208210
}
@@ -790,6 +792,14 @@ func (s *Session) SetupRCFileCustom(subshell subshell.SubShell) {
790792
require.NoError(s.T, err)
791793
}
792794

795+
func (s *Session) SetConfig(key string, value interface{}) {
796+
require.NoError(s.T, s.cfg.Set(key, value))
797+
}
798+
799+
func (s *Session) GetConfig(key string) interface{} {
800+
return s.cfg.Get(key)
801+
}
802+
793803
func RunningOnCI() bool {
794804
return condition.OnCI()
795805
}

internal/testhelpers/tagsuite/tagsuite.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
const (
1414
Activate = "activate"
1515
Analytics = "analytics"
16+
API = "api"
1617
Artifacts = "artifacts"
1718
Auth = "auth"
1819
Automation = "automation"

pkg/platform/api/settings.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ import (
55
"os"
66
"strings"
77

8+
configMediator "github.com/ActiveState/cli/internal/mediators/config"
89
"github.com/ActiveState/cli/pkg/projectfile"
910

1011
"github.com/ActiveState/cli/internal/condition"
12+
"github.com/ActiveState/cli/internal/config"
1113
"github.com/ActiveState/cli/internal/constants"
1214
"github.com/ActiveState/cli/internal/logging"
1315
)
1416

17+
func init() {
18+
configMediator.RegisterOption(constants.APIHostConfig, configMediator.String, "")
19+
}
20+
1521
// Service records available api services
1622
type Service string
1723

@@ -49,6 +55,9 @@ const (
4955
// ServiceHasuraInventory is the Hasura service for inventory information.
5056
ServiceHasuraInventory = "hasura-inventory"
5157

58+
// ServiceUpdateInfo is the service for update info
59+
ServiceUpdateInfo = "update-info"
60+
5261
// TestingPlatform is the API host used by tests so-as not to affect production.
5362
TestingPlatform = ".testing.tld"
5463
)
@@ -109,6 +118,24 @@ var urlsByService = map[Service]*url.URL{
109118
Host: constants.DefaultAPIHost,
110119
Path: constants.HasuraInventoryAPIPath,
111120
},
121+
ServiceUpdateInfo: {
122+
Scheme: "https",
123+
Host: constants.DefaultAPIHost,
124+
Path: constants.UpdateInfoAPIPath,
125+
},
126+
}
127+
128+
var configuredAPIHost string
129+
130+
func registerConfigListener(cfg *config.Instance) {
131+
configMediator.AddListener(constants.APIHostConfig, func() {
132+
configuredAPIHost = cfg.GetString(constants.APIHostConfig)
133+
})
134+
}
135+
136+
func SetConfig(cfg *config.Instance) {
137+
configuredAPIHost = cfg.GetString(constants.APIHostConfig)
138+
registerConfigListener(cfg)
112139
}
113140

114141
// GetServiceURL returns the URL for the given service
@@ -121,7 +148,7 @@ func GetServiceURL(service Service) *url.URL {
121148
serviceURL.Host = *host
122149
}
123150

124-
if insecure := os.Getenv(constants.APIInsecureEnvVarName); insecure == "true" {
151+
if insecure := os.Getenv(constants.APIHostEnvVarName); insecure == "true" {
125152
if serviceURL.Scheme == "https" || serviceURL.Scheme == "wss" {
126153
serviceURL.Scheme = strings.TrimRight(serviceURL.Scheme, "s")
127154
}
@@ -142,8 +169,9 @@ func GetServiceURL(service Service) *url.URL {
142169
}
143170

144171
func getProjectHost(service Service) *string {
145-
if apiHost := os.Getenv(constants.APIHostEnvVarName); apiHost != "" {
146-
return &apiHost
172+
if host := HostOverride(); host != "" {
173+
logging.Debug("Using host override: %s", host)
174+
return &host
147175
}
148176

149177
if condition.InUnitTest() {
@@ -164,11 +192,30 @@ func getProjectHost(service Service) *string {
164192
return &url.Host
165193
}
166194

195+
func getProjectHostFromConfig() string {
196+
if configuredAPIHost != "" {
197+
return configuredAPIHost
198+
}
199+
return ""
200+
}
201+
202+
func HostOverride() string {
203+
if apiHost := os.Getenv(constants.APIHostEnvVarName); apiHost != "" {
204+
return apiHost
205+
}
206+
207+
if apiHost := getProjectHostFromConfig(); apiHost != "" {
208+
return apiHost
209+
}
210+
211+
return ""
212+
}
213+
167214
// GetPlatformURL returns a generic Platform URL for the given path.
168215
// This is for retrieving non-service URLs (e.g. signup URL).
169216
func GetPlatformURL(path string) *url.URL {
170217
host := constants.DefaultAPIHost
171-
if hostOverride := os.Getenv(constants.APIHostEnvVarName); hostOverride != "" {
218+
if hostOverride := HostOverride(); hostOverride != "" {
172219
host = hostOverride
173220
}
174221
return &url.URL{

pkg/projectfile/create_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func Test_Create(t *testing.T) {
3838
Project: tt.args.project,
3939
Directory: tt.args.directory,
4040
Language: tt.args.language,
41+
Host: "test.example.com",
4142
})
4243
assert.NoError(t, err)
4344
configFile := filepath.Join(tempDir, constants.ConfigFileName)

0 commit comments

Comments
 (0)