Skip to content

Commit

Permalink
Revert "fix(generate-config): update success message and default path…
Browse files Browse the repository at this point in the history
… for env (#1676)"

This reverts commit 79ea694.

Signed-off-by: Ramakrishna Pattnaik <[email protected]>
  • Loading branch information
rkpattnaik780 committed Jul 26, 2022
1 parent 8d28b63 commit 4fe2bff
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 33 deletions.
6 changes: 3 additions & 3 deletions docs/commands/rhoas_generate-config.md

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

57 changes: 54 additions & 3 deletions pkg/cmd/generate/build-configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,51 @@ import (
"fmt"
"time"

"github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"

kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
)

type configValues struct {
KafkaHost string
RegistryURL string
KafkaHost string
RegistryURL string
ClientID string
ClientSecret string
TokenURL string

// Optional
Name string
}

func createServiceAccount(opts *options, shortDescription string) (*kafkamgmtclient.ServiceAccount, error) {
conn, err := opts.Connection()
if err != nil {
return nil, err
}
serviceAccountPayload := kafkamgmtclient.ServiceAccountRequest{Name: shortDescription}

serviceacct, httpRes, err := conn.API().
ServiceAccountMgmt().
CreateServiceAccount(opts.Context).
ServiceAccountRequest(serviceAccountPayload).
Execute()

if httpRes != nil {
defer httpRes.Body.Close()
}

if err != nil {
return nil, err
}

return &serviceacct, nil
}

// BuildConfiguration builds the configs for the service context
func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options) error {

Expand All @@ -34,7 +64,6 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options)
configurations := &configValues{}

var serviceAvailable bool
var err error

if svcConfig.KafkaID != "" {
kafkaInstance, newErr := contextutil.GetCurrentKafkaInstance(factory)
Expand All @@ -60,7 +89,29 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options)
return opts.localizer.MustLocalizeError("generate.log.info.noServices")
}
configInstanceName := fmt.Sprintf("%s-%v", opts.name, time.Now().Unix())
serviceAccount, err := createServiceAccount(opts, configInstanceName)
if err != nil {
return err
}

opts.Logger.Info(
icon.SuccessPrefix(),
opts.localizer.MustLocalize("serviceAccount.create.log.info.createdSuccessfully", localize.NewEntry("ID", serviceAccount.GetId())),
)

conn, err := opts.Connection()
if err != nil {
return err
}

providerUrls, err := svcaccountcmdutil.GetProvidersDetails(conn, opts.Context)
if err != nil {
return err
}

configurations.ClientID = serviceAccount.GetClientId()
configurations.ClientSecret = serviceAccount.GetClientSecret()
configurations.TokenURL = providerUrls.GetTokenUrl()
configurations.Name = configInstanceName

var fileName string
Expand Down
20 changes: 10 additions & 10 deletions pkg/cmd/generate/configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const (
envFormat = "env"
jsonFormat = "json"
propertiesFormat = "properties"
configmapFormat = "configmap"
secretFormat = "secret"
)

var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, configmapFormat}
var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, secretFormat}

var (
envConfig = template.Must(template.New(envFormat).Parse(templateEnv))
jsonConfig = template.Must(template.New(jsonFormat).Parse(templateJSON))
propertiesConfig = template.Must(template.New(propertiesFormat).Parse(templateProperties))
configMapTemplateConfig = template.Must(template.New(configmapFormat).Parse(templateConfigMap))
envConfig = template.Must(template.New(envFormat).Parse(templateEnv))
jsonConfig = template.Must(template.New(jsonFormat).Parse(templateJSON))
propertiesConfig = template.Must(template.New(propertiesFormat).Parse(templateProperties))
secretTemplateConfig = template.Must(template.New(secretFormat).Parse(templateSecret))
)

// WriteConfig saves the configurations to a file
Expand Down Expand Up @@ -62,8 +62,8 @@ func getDefaultPath(configType string) (filePath string) {
filePath = "rhoas.properties"
case jsonFormat:
filePath = "rhoas.json"
case configmapFormat:
filePath = "rhoas-services.yaml"
case secretFormat:
filePath = "rhoas-services-secret.yaml"
}

pwd, err := os.Getwd()
Expand All @@ -84,8 +84,8 @@ func getFileFormat(configType string) (template *template.Template) {
template = propertiesConfig
case jsonFormat:
template = jsonConfig
case configmapFormat:
template = configMapTemplateConfig
case secretFormat:
template = secretTemplateConfig
}

return template
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/generate/generate-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func NewGenerateCommand(f *factory.Factory) *cobra.Command {
flags.AddContextName(&opts.name)
flags.StringVar(&opts.configType, "type", "", opts.localizer.MustLocalize("generate.flag.type"))
cmd.Flags().BoolVar(&opts.overwrite, "overwrite", false, opts.localizer.MustLocalize("generate.flag.overwrite.description"))
flags.StringVar(&opts.fileName, "output-file", "", opts.localizer.MustLocalize("generate.common.flag.fileLocation.description"))

cmd.Flags().StringVar(&opts.fileName, "output-file", "", opts.localizer.MustLocalize("generate.common.flag.fileLocation.description"))
_ = cmd.MarkFlagRequired("type")

flagutil.EnableStaticFlagCompletion(cmd, "type", configurationTypes)
Expand Down
32 changes: 25 additions & 7 deletions pkg/cmd/generate/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,57 @@ var (
SERVICE_REGISTRY_CORE_PATH=` + registrycmdutil.REGISTRY_CORE_PATH + `
SERVICE_REGISTRY_COMPAT_PATH=` + registrycmdutil.REGISTRY_COMPAT_PATH + `
{{end}}
## Authentication Configuration
RHOAS_CLIENT_ID={{.ClientID}}
RHOAS_CLIENT_SECRET={{.ClientSecret}}
RHOAS_OAUTH_TOKEN_URL={{.TokenURL}}
`)

templateJSON = heredoc.Doc(`
{
{{if .KafkaHost}}"kafkaHost":"{{.KafkaHost}}",
{{end}}{{if .RegistryURL}}"serviceRegistryUrl":"{{.RegistryURL}}",
"serviceRegistryCorePath":"` + registrycmdutil.REGISTRY_CORE_PATH + `",
"serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `"{{end}}
"serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `",
{{end}}"rhoasClientID":"{{.ClientID}}",
"rhoasClientSecret":"{{.ClientSecret}}",
"rhoasOauthTokenUrl":"{{.TokenURL}}"
}
`)

templateProperties = heredoc.Doc(`
## Generated by rhoas cli
{{if .KafkaHost}}## Kafka Configuration
kafkaHost={{.KafkaHost}}
{{end}}{{if .RegistryURL}}## Service Registry Configuration
{{end}}{{if .RegistryURL}} ## Service Registry Configuration
serviceRegistryUrl={{.RegistryURL}}
serviceRegistryCorePath=` + registrycmdutil.REGISTRY_CORE_PATH + `
serviceRegistryCompatPath=` + registrycmdutil.REGISTRY_COMPAT_PATH + `{{end}}
serviceRegistryCompatPath=` + registrycmdutil.REGISTRY_COMPAT_PATH + `
{{end}}
## Authentication Configuration
rhoasClientID={{.ClientID}}
rhoasClientSecret={{.ClientSecret}}
rhoasOauthTokenUrl={{.TokenURL}}
`)

templateConfigMap = heredoc.Doc(`
templateSecret = heredoc.Doc(`
apiVersion: v1
kind: ConfigMap
kind: Secret
metadata:
name: {{.Name}}
data:
type: Opaque
stringData:
{{if .KafkaHost}}## Kafka Configuration
KAFKA_HOST: {{.KafkaHost}}
{{end}}
{{if .RegistryURL}}## Service Registry Configuration
SERVICE_REGISTRY_URL: {{.RegistryURL}}
SERVICE_REGISTRY_COMPAT_PATH: ` + registrycmdutil.REGISTRY_COMPAT_PATH + `
SERVICE_REGISTRY_CORE_PATH: ` + registrycmdutil.REGISTRY_CORE_PATH + `{{end}}
SERVICE_REGISTRY_CORE_PATH: ` + registrycmdutil.REGISTRY_CORE_PATH + `
{{end}}
## Authentication Configuration
RHOAS_CLIENT_ID: {{.ClientID}}
RHOAS_CLIENT_SECRET: {{.ClientSecret}}
RHOAS_OAUTH_TOKEN_URL: {{.TokenURL}}
`)
)
12 changes: 4 additions & 8 deletions pkg/core/localize/locales/en/cmd/generate_config.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You must specify an output format into which the credentials will be stored:
- env (default): Store configurations in an env file as environment variables
- json: Store configurations in a JSON file
- properties: Store configurations in a properties file, which is typically used in Java-related technologies
- configmap: Store configurations in a Kubernetes ConfigMap file
- secret: Store configurations in a Kubernetes secret file
'''

[generate.cmd.example]
Expand All @@ -21,8 +21,8 @@ $ rhoas generate-config --type json
## Generate configurations for the current service context in env format and save it in specified path
$ rhoas generate-config --type env --output-file ./configs/.env
## Generate configurations for a specified context as Kubernetes ConfigMap
$ rhoas generate-config --name qaprod --type configmap
## Generate configurations for a specified context as Kubernetes secret
$ rhoas generate-config --name qaprod --type secret
'''

[generate.flag.type]
Expand All @@ -43,8 +43,4 @@ one = 'file {{.FilePath}} already exists. Use --overwrite to overwrite the file,
one='No services available to generate configurations'

[generate.log.info.credentialsSaved]
one='''
Configurations successfully saved to "{{.FilePath}}"
You can now create new service accounts or use existing ones to connect to the service(s)
'''
one='Configurations successfully saved to "{{.FilePath}}"'

0 comments on commit 4fe2bff

Please sign in to comment.