Skip to content

Commit

Permalink
Update central config reader to use defaults
Browse files Browse the repository at this point in the history
Co-Authored-by: Anuj Chaudhari <[email protected]>
Signed-off-by: Vui Lam <[email protected]>
  • Loading branch information
vuil and anujc25 committed Aug 30, 2024
1 parent 56ae7a9 commit d46034e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pkg/centralconfig/central_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type CentralConfig interface {
GetTanzuPlatformEndpointToServiceEndpointMap() (TanzuPlatformEndpointToServiceEndpointMap, error)
// GetTanzuPlatformSaaSEndpointList returns list of tanzu platform saas endpoints which can be a regular
// expression. When comparing the result please make sure to use regex match instead of string comparison
GetTanzuPlatformSaaSEndpointList() ([]string, error)
GetTanzuPlatformSaaSEndpointList() []string
// GetTanzuConfigEndpointUpdateVersion returns current version for the local configuration file update
// If the version specified here does not match with the local version stored in the datastore that means
// the local configuration file endpoint updates are required
Expand Down
5 changes: 5 additions & 0 deletions pkg/centralconfig/central_config_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ var (
// It serves as a fallback default only if reading the central configuration fails.
DefaultTanzuPlatformEndpoint = "https://api.tanzu.cloud.vmware.com"

defaultSaaSEndpoints = []string{
"https://(www.)?platform(.)*.tanzu.broadcom.com",
"https://api.tanzu(.)*.cloud.vmware.com",
}

// DefaultCentralConfigReader is a pre-initialized default central configuration reader instance.
// This global object can be used directly to read configuration from the default central configuration.
DefaultCentralConfigReader = newDefaultCentralConfigReader()
Expand Down
7 changes: 5 additions & 2 deletions pkg/centralconfig/central_config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ func (c *centralConfigYamlReader) GetTanzuPlatformEndpointToServiceEndpointMap()

// GetTanzuPlatformSaaSEndpointList returns list of tanzu platform saas endpoints which can be a regular
// expression. When comparing the result please make sure to use regex match instead of string comparison
func (c *centralConfigYamlReader) GetTanzuPlatformSaaSEndpointList() ([]string, error) {
func (c *centralConfigYamlReader) GetTanzuPlatformSaaSEndpointList() []string {
saasEndpointList := []string{}
err := c.GetCentralConfigEntry(KeyTanzuPlatformSaaSEndpointsAsRegularExpression, &saasEndpointList)
return saasEndpointList, err
if err != nil {
return defaultSaaSEndpoints
}
return saasEndpointList
}

// GetTanzuConfigEndpointUpdateVersion returns current version for the local configuration file update
Expand Down
20 changes: 5 additions & 15 deletions pkg/centralconfig/central_config_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,11 @@ func TestGetTanzuPlatformSaaSEndpointList(t *testing.T) {
name string
cfgContent string
tpSaaSEndpoints []string
expectError bool
}{
{
name: "when saas endpoint list does not exist",
name: "when saas endpoint list does not exist, it should return default endpoints from local variable",
cfgContent: "",
tpSaaSEndpoints: []string{},
expectError: true,
tpSaaSEndpoints: defaultSaaSEndpoints,
},
{
name: "when saas endpoint list exists",
Expand All @@ -188,16 +186,14 @@ cli.core.tanzu_cli_platform_saas_endpoints_as_regular_expression:
- https://api.tanzu*.example.com
`,
tpSaaSEndpoints: []string{"https://platform*.fake.example.com", "https://api.fake.example.com", "https://api.tanzu*.example.com"},
expectError: false,
},
{
name: "when saas endpoint list exists but the format is different and cannot be parsed",
name: "when saas endpoint list exists but the format is different and cannot be parsed, it should return default endpoints from local variable",
cfgContent: `
cli.core.tanzu_cli_platform_saas_endpoints_as_regular_expression:
https://platform*.fake.example.com
`,
tpSaaSEndpoints: []string{},
expectError: true,
tpSaaSEndpoints: defaultSaaSEndpoints,
},
}

Expand All @@ -217,13 +213,7 @@ cli.core.tanzu_cli_platform_saas_endpoints_as_regular_expression:
err = os.WriteFile(path, []byte(tc.cfgContent), 0644)
assert.Nil(t, err)

tpSaaSEndpoints, err := reader.GetTanzuPlatformSaaSEndpointList()
if tc.expectError {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}

tpSaaSEndpoints := reader.GetTanzuPlatformSaaSEndpointList()
assert.Equal(t, tc.tpSaaSEndpoints, tpSaaSEndpoints)
})
}
Expand Down
23 changes: 9 additions & 14 deletions pkg/centralconfig/fakes/central_config_fake.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ var _ = Describe("create new context", func() {

// Mock the default central configuration reader
fakeDefaultCentralConfigReader := fakes.CentralConfig{}
fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns([]string{fakeTanzuEndpoint}, nil)
fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns([]string{fakeTanzuEndpoint})
fakeDefaultCentralConfigReader.GetTanzuPlatformEndpointToServiceEndpointMapReturns(centralconfig.TanzuPlatformEndpointToServiceEndpointMap{}, nil)
centralconfig.DefaultCentralConfigReader = &fakeDefaultCentralConfigReader
})
Expand Down
10 changes: 1 addition & 9 deletions pkg/command/login_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,8 @@ func isTanzuPlatformSaaSEndpoint(tpEndpoint string) bool {
return false
}

defaultSaaSEndpoints := []string{
"https://(www.)?platform(.)*.tanzu.broadcom.com",
"https://api.tanzu(.)*.cloud.vmware.com",
}
saasEndpointRegularExpressions, _ := centralconfig.DefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointList()
saasEndpointRegularExpressions = append(saasEndpointRegularExpressions, defaultSaaSEndpoints...)
saasEndpointRegularExpressions := centralconfig.DefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointList()
for _, endpointRegex := range saasEndpointRegularExpressions {
if endpointRegex == "" {
continue
}
// Create a regular expression pattern
re, err := regexp.Compile(endpointRegex)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/command/login_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestConfigureTanzuPlatformServiceEndpoints(t *testing.T) {
}()

fakeDefaultCentralConfigReader := fakes.CentralConfig{}
fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns(tt.saasEndpointListInCC, nil)
fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns(tt.saasEndpointListInCC)
fakeDefaultCentralConfigReader.GetTanzuPlatformEndpointToServiceEndpointMapReturns(tt.saasEndpointToServiceEndpointMapInCC, nil)
centralconfig.DefaultCentralConfigReader = &fakeDefaultCentralConfigReader

Expand Down Expand Up @@ -191,7 +191,7 @@ func TestIsTanzuPlatformSaaSEndpoint(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeDefaultCentralConfigReader := fakes.CentralConfig{}
fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns(tt.saasEndpointListInCentralConfig, nil)
fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns(tt.saasEndpointListInCentralConfig)
centralconfig.DefaultCentralConfigReader = &fakeDefaultCentralConfigReader

actual := isTanzuPlatformSaaSEndpoint(tt.tpEndpoint)
Expand Down
5 changes: 5 additions & 0 deletions pkg/command/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/vmware-tanzu/tanzu-cli/pkg/centralconfig"
"github.com/vmware-tanzu/tanzu-cli/pkg/centralconfig/fakes"
)

func TestPrepareTanzuContextName(t *testing.T) {
Expand Down Expand Up @@ -58,6 +59,10 @@ func TestPrepareTanzuContextName(t *testing.T) {

for _, tc := range testCases {
forceCSP = tc.forceCSP
fakeDefaultCentralConfigReader := fakes.CentralConfig{}
fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns([]string{centralconfig.DefaultTanzuPlatformEndpoint})
centralconfig.DefaultCentralConfigReader = &fakeDefaultCentralConfigReader

actual := prepareTanzuContextName(tc.orgName, tc.endpoint, tc.isStaging)
if actual != tc.expected {
t.Errorf("orgName: %s, endpoint: %s, isStaging: %t - expected: %s, got: %s", tc.orgName, tc.endpoint, tc.isStaging, tc.expected, actual)
Expand Down

0 comments on commit d46034e

Please sign in to comment.