Skip to content

Commit

Permalink
feat(cf): support load cf config v4 or later
Browse files Browse the repository at this point in the history
* sync latest config data from upstream cf cli project
  * remove `LoggregatorEndpoint` property and corresponding codes
  * add `UAAGrantType` property, but not setter/getter
* remove compatibility check and purge logic, we should delegate it
  to CF CLI
* set default config version to `4` if not set
* add unit test

resolve #218
  • Loading branch information
MING ZHE HUANG committed Sep 16, 2020
1 parent ce9315a commit 2d2b94c
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 39 deletions.
58 changes: 21 additions & 37 deletions bluemix/configuration/core_config/cf_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@ import (
)

type CFConfigData struct {
ConfigVersion int
Target string
AccessToken string
APIVersion string
AsyncTimeout uint
AuthorizationEndpoint string
LoggregatorEndpoint string
ColorEnabled string
ConfigVersion int
DopplerEndpoint string
UaaEndpoint string
RoutingAPIEndpoint string
LoginAt time.Time
AccessToken string
RefreshToken string
UAAOAuthClient string
UAAOAuthClientSecret string
SSHOAuthClient string
Locale string
LogCacheEndPoint string
MinCLIVersion string
MinRecommendedCLIVersion string
OrganizationFields models.OrganizationFields
PluginRepos []models.PluginRepo
RefreshToken string
RoutingAPIEndpoint string
SpaceFields models.SpaceFields
SSHOAuthClient string
SSLDisabled bool
AsyncTimeout uint
Target string
Trace string
ColorEnabled string
Locale string
PluginRepos []models.PluginRepo
MinCLIVersion string
MinRecommendedCLIVersion string
UaaEndpoint string
LoginAt time.Time
UAAGrantType string
UAAOAuthClient string
UAAOAuthClientSecret string
raw raw
}

Expand All @@ -49,7 +50,9 @@ func NewCFConfigData() *CFConfigData {
}

func (data *CFConfigData) Marshal() ([]byte, error) {
data.ConfigVersion = 3
if data.ConfigVersion == 0 {
data.ConfigVersion = 4
}
return json.MarshalIndent(data, "", " ")
}

Expand All @@ -59,11 +62,6 @@ func (data *CFConfigData) Unmarshal(bytes []byte) error {
return err
}

if data.ConfigVersion != 3 {
*data = CFConfigData{raw: make(map[string]interface{})}
return nil
}

var raw raw
err = json.Unmarshal(bytes, &raw)
if err != nil {
Expand Down Expand Up @@ -184,13 +182,6 @@ func (c *cfConfig) DopplerEndpoint() (endpoint string) {
return
}

func (c *cfConfig) LoggregatorEndpoint() (endpoint string) {
c.read(func() {
endpoint = c.data.LoggregatorEndpoint
})
return
}

func (c *cfConfig) UAAEndpoint() (endpoint string) {
c.read(func() {
endpoint = c.data.UaaEndpoint
Expand Down Expand Up @@ -356,12 +347,6 @@ func (c *cfConfig) SetAuthenticationEndpoint(endpoint string) {
})
}

func (c *cfConfig) SetLoggregatorEndpoint(endpoint string) {
c.write(func() {
c.data.LoggregatorEndpoint = endpoint
})
}

func (c *cfConfig) SetDopplerEndpoint(endpoint string) {
c.write(func() {
c.data.DopplerEndpoint = endpoint
Expand Down Expand Up @@ -455,7 +440,6 @@ func (c *cfConfig) UnsetAPI() {
c.data.AuthorizationEndpoint = ""
c.data.UaaEndpoint = ""
c.data.RoutingAPIEndpoint = ""
c.data.LoggregatorEndpoint = ""
c.data.DopplerEndpoint = ""
})
}
Expand Down
140 changes: 140 additions & 0 deletions bluemix/configuration/core_config/cf_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package core_config_test

import (
"testing"

"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config"
"github.com/stretchr/testify/assert"
)

func TestNewConfig(t *testing.T) {
c := core_config.NewCFConfigData()

assert.Equal(t, "cf", c.UAAOAuthClient)
assert.Equal(t, "", c.UAAOAuthClientSecret)

}

var v4JSON = `{
"AccessToken": "foo",
"APIVersion": "5",
"AsyncTimeout": 0,
"AuthorizationEndpoint": "iam.test.cloud.ibm.com",
"ColorEnabled": "",
"ConfigVersion": 4,
"DopplerEndpoint": "",
"Locale": "",
"LogCacheEndPoint": "",
"MinCLIVersion": "",
"MinRecommendedCLIVersion": "",
"OrganizationFields": {
"GUID": "",
"Name": "",
"QuotaDefinition": {
"name": "",
"memory_limit": 0,
"instance_memory_limit": 0,
"total_routes": 0,
"total_services": 0,
"non_basic_services_allowed": false,
"app_instance_limit": 0
}
},
"PluginRepos": null,
"RefreshToken": "",
"RoutingAPIEndpoint": "",
"SpaceFields": {
"GUID": "",
"Name": "",
"AllowSSH": false
},
"SSHOAuthClient": "",
"SSLDisabled": false,
"Target": "",
"Trace": "",
"UaaEndpoint": "",
"LoginAt": "0001-01-01T00:00:00Z",
"UAAGrantType": "",
"UAAOAuthClient": "cf",
"UAAOAuthClientSecret": ""
}`

func TestMarshal(t *testing.T) {
c := core_config.NewCFConfigData()
c.APIVersion = "5"
c.AccessToken = "foo"
c.AuthorizationEndpoint = "iam.test.cloud.ibm.com"

bytes, err := c.Marshal()
assert.NoError(t, err)
assert.Equal(t, string(bytes), v4JSON)
}

func TestUnmarshalV3(t *testing.T) {
c := core_config.NewCFConfigData()
assert.NoError(t, c.Unmarshal([]byte(v4JSON)))

assert.Equal(t, 4, c.ConfigVersion)
assert.Equal(t, "5", c.APIVersion)
assert.Equal(t, "foo", c.AccessToken)
assert.Equal(t, "iam.test.cloud.ibm.com", c.AuthorizationEndpoint)
}

func TestUnmarshalV4(t *testing.T) {
var v3JSON = `{
"AccessToken": "bar",
"APIVersion": "4",
"AsyncTimeout": 0,
"AuthorizationEndpoint": "iam.test.cloud.ibm.com",
"ColorEnabled": "",
"ConfigVersion": 3,
"DopplerEndpoint": "",
"Locale": "",
"LogCacheEndPoint": "",
"MinCLIVersion": "",
"MinRecommendedCLIVersion": "",
"OrganizationFields": {
"GUID": "",
"Name": "",
"QuotaDefinition": {
"name": "",
"memory_limit": 0,
"instance_memory_limit": 0,
"total_routes": 0,
"total_services": 0,
"non_basic_services_allowed": false,
"app_instance_limit": 0
}
},
"PluginRepos": null,
"RefreshToken": "",
"RoutingAPIEndpoint": "",
"SpaceFields": {
"GUID": "",
"Name": "",
"AllowSSH": false
},
"SSHOAuthClient": "",
"SSLDisabled": false,
"Target": "",
"Trace": "",
"UaaEndpoint": "",
"LoginAt": "0001-01-01T00:00:00Z",
"UAAGrantType": "",
"UAAOAuthClient": "cf",
"UAAOAuthClientSecret": ""
}`

c := core_config.NewCFConfigData()
assert.NoError(t, c.Unmarshal([]byte(v3JSON)))

assert.Equal(t, 3, c.ConfigVersion)
assert.Equal(t, "4", c.APIVersion)
assert.Equal(t, "bar", c.AccessToken)
assert.Equal(t, "iam.test.cloud.ibm.com", c.AuthorizationEndpoint)
}

func TestUnmarshalError(t *testing.T) {
c := core_config.NewCFConfigData()
assert.Error(t, c.Unmarshal([]byte(`{"db":cf}`)))
}
2 changes: 0 additions & 2 deletions bluemix/configuration/core_config/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ type CFConfig interface {
HasAPIEndpoint() bool
AuthenticationEndpoint() string
UAAEndpoint() string
LoggregatorEndpoint() string
DopplerEndpoint() string
RoutingAPIEndpoint() string
SSHOAuthClient() string
Expand All @@ -129,7 +128,6 @@ type CFConfig interface {
SetAPIVersion(string)
SetAPIEndpoint(string)
SetAuthenticationEndpoint(string)
SetLoggregatorEndpoint(string)
SetDopplerEndpoint(string)
SetUAAEndpoint(string)
SetRoutingAPIEndpoint(string)
Expand Down

0 comments on commit 2d2b94c

Please sign in to comment.