Skip to content

Commit

Permalink
AEROGEAR-2371 [APB] Parse config element of Config Map as Json in mob…
Browse files Browse the repository at this point in the history
…ile cli
  • Loading branch information
Vitalii Chepeliuk committed Mar 22, 2018
1 parent b8e65ee commit 9ca9891
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 3 additions & 2 deletions pkg/cmd/clientConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,16 @@ kubectl plugin mobile get clientconfig`,
}
return errors.Wrap(err, "unable to create config. Failed to get service "+svc.Name+" configmap")
}
configType := configMap.ObjectMeta.Annotations["configType"]
if _, ok := convertors[svc.Name]; !ok {
convertor := defaultSecretConvertor{}
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data); err != nil {
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data, configType); err != nil {
return err
}
} else {
// we can only convert what is available
convertor := convertors[svc.Name]
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data); err != nil {
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data, configType); err != nil {
return err
}
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const (

// SecretConvertor converts a kubernetes secret into a mobile.ServiceConfig
type SecretConvertor interface {
Convert(id string, params map[string]string) (*ServiceConfig, error)
Convert(id string, params map[string]string, configType string) (*ServiceConfig, error)
}

//ServiceConfigs are collection of configurations for services in a specific namespace
Expand Down Expand Up @@ -110,17 +110,26 @@ func (i ignoredFields) Contains(field string) bool {
var defaultIgnored = ignoredFields{"password", "token", "url", "uri", "name", "type", "id"}

//Convert a kubernetes secret to a mobile.ServiceConfig
func (dsc defaultSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
func (dsc defaultSecretConvertor) Convert(id string, params map[string]string, configType string) (*ServiceConfig, error) {
config := map[string]interface{}{}
headers := map[string]string{}
for k, v := range params {
if !defaultIgnored.Contains(k) {
config[k] = string(v)
if configType == "json" && k == "config" {
jsCfg := map[string]interface{}{}
if err := json.Unmarshal([]byte(v), &jsCfg); err != nil {
return nil, errors.Wrap(err, "failed to unmarshall service configuration ")
}
config[k] = jsCfg
} else {
config[k] = string(v)
}
}
}
if len(headers) > 0 {
config["headers"] = headers
}

return &ServiceConfig{
ID: id,
Name: params["name"],
Expand All @@ -133,7 +142,7 @@ func (dsc defaultSecretConvertor) Convert(id string, params map[string]string) (
type keycloakSecretConvertor struct{}

//Convert a kubernetes keycloak secret into a keycloak mobile.ServiceConfig
func (ksc keycloakSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
func (ksc keycloakSecretConvertor) Convert(id string, params map[string]string, configType string) (*ServiceConfig, error) {
config := map[string]interface{}{}
headers := map[string]string{}
err := json.Unmarshal([]byte(params["public_installation"]), &config)
Expand All @@ -155,7 +164,7 @@ func (ksc keycloakSecretConvertor) Convert(id string, params map[string]string)
type syncSecretConvertor struct{}

//Convert a kubernetes Sync Server secret into a keycloak mobile.ServiceConfig
func (scc syncSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
func (scc syncSecretConvertor) Convert(id string, params map[string]string, configType string) (*ServiceConfig, error) {
config := map[string]interface{}{
"url": params["host"],
}
Expand Down

0 comments on commit 9ca9891

Please sign in to comment.