Skip to content

Commit

Permalink
Remove gcp and cloud region from kafka create (#1851)
Browse files Browse the repository at this point in the history
* refactor: remove gcp and cloud region from kafka create

* refactor: better error message when the user has enterprise quota but doesn't have any registered clusters

* refactor: using localizer for error
  • Loading branch information
dimakis authored Apr 17, 2023
1 parent 7776062 commit 530f590
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 57 deletions.
102 changes: 48 additions & 54 deletions pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
// FlagMarketPlace is a flag representing billing model of the instance
FlagBillingModel = "billing-model"
TrialInstanceType = "Trial"
CloudProvider = "aws"
)

type options struct {
Expand Down Expand Up @@ -572,19 +573,27 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
f.Logger.Info(opts.f.Localizer.MustLocalize("kafka.create.info.enterpriseQuota"))
}

if opts.useTrialFlow {
opts.f.Logger.Info(opts.f.Localizer.MustLocalize("kafka.create.usingTrialInstance"))
}

answers, err = promptForKafkaName(f, answers)
if err != nil {
return nil, err
}

// Get the list of enterprise clusters in the users organization if there are any, creates a map of cluster ids
// to names to include names in the prompt
kfmClusterList, clusterMap, err := setEnterpriseClusterList(opts)
if err != nil {
return nil, err
opts.kfmClusterList = &kafkamgmtclient.EnterpriseClusterList{}

if opts.useEnterpriseFlow {
// Get the list of enterprise clusters in the users organization if there are any, creates a map of cluster ids
// to names to include names in the prompt
kfmClusterList, clusterMap, err2 := setEnterpriseClusterList(opts)
if err2 != nil {
return nil, err2
}
opts.kfmClusterList = kfmClusterList
opts.clusterMap = clusterMap
}
opts.kfmClusterList = kfmClusterList
opts.clusterMap = clusterMap

// If there are enterprise clusters in the user's organization, prompt them to select a flow (enterprise or legacy)
// using the interactive prompt. If there are no enterprise clusters, the user must use the legacy flow.
Expand All @@ -594,10 +603,6 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
return nil, err
}

if opts.useTrialFlow {
opts.f.Logger.Info(opts.f.Localizer.MustLocalize("kafka.create.usingTrialInstance"))
}

if opts.useEnterpriseFlow {
if len(orgQuota.EnterpriseQuotas) < 1 {
return nil, opts.f.Localizer.MustLocalizeError("kafka.create.error.noStandardQuota")
Expand Down Expand Up @@ -638,12 +643,10 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
}
}

// If the user is not using an enterprise cluster, prompt them to select a cloud provider
// If the user is not using an enterprise cluster, we set the cloud provider to aws as this is currently the only
// cloud provider that we support
if opts.useLegacyFlow {
answers, err = cloudProviderPrompt(f, answers)
if err != nil {
return nil, err
}
answers.CloudProvider = CloudProvider
}

// gets the billing model for the kafka, if the user has more than one billing model, a prompt is shown to select one
Expand Down Expand Up @@ -741,21 +744,18 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
return nil, err
}

if len(regionIDs) == 0 {
// selecting the region, if there is only one region available, we skip the prompt
switch {
case len(regionIDs) == 0:
return nil, f.Localizer.MustLocalizeError("kafka.create.error.noRegionSupported")
case len(regionIDs) == 1:
answers.Region = regionIDs[0]
default:
err = promptForCloudRegion(f, regionIDs, answers)
if err != nil {
return nil, err
}
}

regionPrompt := &survey.Select{
Message: f.Localizer.MustLocalize("kafka.create.input.cloudRegion.message"),
Options: regionIDs,
Help: f.Localizer.MustLocalize("kafka.create.input.cloudRegion.help"),
}

err = survey.AskOne(regionPrompt, &answers.Region)
if err != nil {
return nil, err
}

sizes, err = FetchValidKafkaSizes(opts.f, answers.CloudProvider, answers.Region, *userQuota)
if err != nil {
return nil, err
Expand Down Expand Up @@ -798,6 +798,20 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
return payload, nil
}

func promptForCloudRegion(f *factory.Factory, regionIDs []string, answers *promptAnswers) error {
regionPrompt := &survey.Select{
Message: f.Localizer.MustLocalize("kafka.create.input.cloudRegion.message"),
Options: regionIDs,
Help: f.Localizer.MustLocalize("kafka.create.input.cloudRegion.help"),
}

err := survey.AskOne(regionPrompt, &answers.Region)
if err != nil {
return err
}
return nil
}

func getBillingModel(opts *options, orgQuota *accountmgmtutil.OrgQuotas, answers *promptAnswers) (*promptAnswers, error) {
availableBillingModels := FetchSupportedBillingModels(orgQuota, answers.CloudProvider)

Expand Down Expand Up @@ -937,29 +951,6 @@ func marketplaceQuotaPrompt(orgQuota *accountmgmtutil.OrgQuotas, answers *prompt
return answers, nil
}

func cloudProviderPrompt(f *factory.Factory, answers *promptAnswers) (*promptAnswers, error) {
cloudProviderNames, err := GetEnabledCloudProviderNames(f)
if err != nil {
return nil, err
}

if len(cloudProviderNames) == 1 {
answers.CloudProvider = cloudProviderNames[0]
return answers, nil
}

cloudProviderPrompt := &survey.Select{
Message: f.Localizer.MustLocalize("kafka.create.input.cloudProvider.message"),
Options: cloudProviderNames,
}

err = survey.AskOne(cloudProviderPrompt, &answers.CloudProvider)
if err != nil {
return nil, err
}
return answers, nil
}

// This may need to altered as the `name` are mutable on ocm side
func getClusterNameMap(opts *options, clusterList *kafkamgmtclient.EnterpriseClusterList) (*map[string]v1.Cluster, error) {
// for each cluster in the list, get the name from ocm and add it to the cluster list
Expand All @@ -977,14 +968,17 @@ func getClusterNameMap(opts *options, clusterList *kafkamgmtclient.EnterpriseClu
}

func selectClusterPrompt(opts *options) (int, error) {
promptOptions := openshiftclustercmdutil.CreatePromptOptionsFromClusters(opts.kfmClusterList, opts.clusterMap)
promptOptions, err := openshiftclustercmdutil.CreatePromptOptionsFromClusters(opts.f, opts.kfmClusterList, opts.clusterMap)
if err != nil {
return 0, err
}
promptForCluster := &survey.Select{
Message: opts.f.Localizer.MustLocalize("kafka.create.input.cluster.selectClusterMessage"),
Options: *promptOptions,
}

var index int
err := survey.AskOne(promptForCluster, &index)
err = survey.AskOne(promptForCluster, &index)
if err != nil {
return 0, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ func (v *Validator) ValidatorForMachinePoolNodes(val interface{}) error {
return nil
}

func CreatePromptOptionsFromClusters(clusterList *kafkamgmtclient.EnterpriseClusterList, clusterMap *map[string]v1.Cluster) *[]string {
func CreatePromptOptionsFromClusters(f *factory.Factory, clusterList *kafkamgmtclient.EnterpriseClusterList, clusterMap *map[string]v1.Cluster) (*[]string, error) {
promptOptions := []string{}
validatedClusters := ValidateClusters(clusterList)
if len(validatedClusters.Items) == 0 {
return &promptOptions, f.Localizer.MustLocalizeError("kafka.cluster.common.error.noClustersAvailable")
}
for _, cluster := range validatedClusters.Items {
ocmCluster := (*clusterMap)[cluster.GetId()]
display := ocmCluster.Name() + " (" + cluster.GetId() + ")"
promptOptions = append(promptOptions, display)
}
return &promptOptions
return &promptOptions, nil
}

func ValidateClusters(clusterList *kafkamgmtclient.EnterpriseClusterList) *kafkamgmtclient.EnterpriseClusterList {
Expand Down
4 changes: 3 additions & 1 deletion pkg/core/localize/locales/en/cmd/kafka.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1228,4 +1228,6 @@ one = '''
You do not have enough quota to create a Kafka instance on your own OpenShift Cluster.
Please visit https://www.redhat.com/en/technologies/cloud-computing/openshift/openshift-streams-for-apache-kafka/pricing to request more quota.
'''
'''
[kafka.cluster.common.error.noClustersAvailable]
one = 'no registered OpenShift clusters found, an OpenShit cluster must be registered to use the service'

0 comments on commit 530f590

Please sign in to comment.