Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kafka create): reduce number of calls made to AMS API #1596

Merged
merged 6 commits into from
Jun 28, 2022

Conversation

rkpattnaik780
Copy link
Contributor

Re-use QuotaCostList wherever possible.

Follow up for #1593

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Enhancement

@wtrocki wtrocki requested review from wtrocki and jackdelahunt June 14, 2022 09:27
Comment on lines 49 to 54
quotaCostList, err := accountmgmtutil.FetchOrgQuotaCost(f.Context, conn)
if err != nil {
return nil, directive
}

userInstanceType, _ := accountmgmtutil.GetUserSupportedInstanceType(quotaCostList, &constants.Kafka.Ams)
Copy link
Collaborator

@wtrocki wtrocki Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two ways to look at this:

  1. We can make single call and return structure that contains all high level objects
    our create command needs
  2. Have one call that returns some raw api object that contains a lot of non useful data

Here we are using approach nr2. While it is ok this has some problems:

  • 2 calls needed - result of the first call exposes too many fields that are low level.
    There is no enough encapsulation
  • Verbose usage - 2 calls needed - error handling is not well defined and will return
    low level info to user
  • Not clear what method to use to get high level info for the CLI

While this PR is acceptable I personally feel that we should be using encapsulation more.
This means that we should avoid using:

GetValidMarketplaces(quotaCostList *amsclient.QuotaCostList) 

And look to create our own CLi structures that represent values that we really want to operate in CLI

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was already existing structure that we were returning from the AMS calls that contained type of quota and remaining amount. I think we should extend that structure:

https://github.com/redhat-developer/app-services-cli/blob/main/pkg/shared/accountmgmtutil/ams.go#L38

@jackdelahunt
Copy link
Contributor

@rkpattnaik780 I was requested to review this but are you still making changes that Wojciech suggested?

@wtrocki
Copy link
Collaborator

wtrocki commented Jun 15, 2022

Let's ignore reviewing this for now

@@ -225,7 +234,7 @@ func runCreate(opts *options) error {
return f.Localizer.MustLocalizeError("kafka.create.error.noInteractiveMode")
}

payload, err = promptKafkaPayload(opts, *userInstanceType)
payload, err = promptKafkaPayload(opts, userInstanceType)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interactive mode needs more changes, currently the user will need to specify BillingModel as flag before starting the interactive mode.

if err != nil || userInstanceType == nil {
return f.Localizer.MustLocalizeError("kafka.create.error.userInstanceType.notFound")
}

if opts.billingModel == "marketplace" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would see this logic happening outside create command


billingModelPrompt := &survey.Select{
Message: "Billing model:",
Options: kafkacmdutil.ValidBillingModels,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will outdate fast as new billing models should be introduced.
I think the best tradeoffs would be to return error to user from ams package containing billing models he can create when invalid is provided or billing model is missing.

}

func GetUserSupportedInstanceType(ctx context.Context, spec *remote.AmsConfig, conn connection.Connection) (quota *QuotaSpec, err error) {
userInstanceTypes, err := GetUserSupportedInstanceTypes(ctx, spec, conn)
func GetUserSupportedInstanceType(ctx context.Context, spec *remote.AmsConfig, conn connection.Connection, billingModel string) (quota *QuotaSpec, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should probably include marketplaceId and marketplaceType. Just billing model would not be enough for us to return single quota (there will be situations when multiple quotas are returned

func BattleOfInstanceBillingModels(quotas []QuotaSpec) []QuotaSpec {
var betterQuotasMap map[string]*QuotaSpec = make(map[string]*QuotaSpec)
alwaysWinsBillingModel := "standard"
func BattleOfInstanceBillingModels(quotas []QuotaSpec, alwaysWinsBillingModel string) []QuotaSpec {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be removed.

@wtrocki
Copy link
Collaborator

wtrocki commented Jun 27, 2022

I think PR trying to do too much. I would suggest to:

  • Not change too much code ouside accountmgmt package.
  • Remove logic for picking instance types etc.
  • Have new method for determining quota type:
func getMeQuotaFor(billingType *string, 
       marketplaceProvider *string, 
      cloudAccountId *string) (*Quota, err) {
     // match all arguments to the 
     // quota objects and return single one
     // return error if: cannot find quota object 
     // and size of quota object is larger than 1.
}
  • create.go changes should only contain adding 3 new flags and pass them to AMS. None of the existing logic should change.
  • Additionally create.go should also pass all those values to backend.
  • I think we are fine (for now) to keep billing model outside interactive mode/do not support bash suggestions for 3 of those flags ( I see you commented them)

@wtrocki
Copy link
Collaborator

wtrocki commented Jun 27, 2022

@rkpattnaik780 hope that helps

@rkpattnaik780 rkpattnaik780 changed the title fix(kafka create): reduce number of calls made to AMS API WIP: fix(kafka create): reduce number of calls made to AMS API Jun 28, 2022
return nil, errors.New("accountID cant be provided with standard billing model")
}

if (cloudAccountID != "") != (marketplace != "") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever pattern!

Copy link
Collaborator

@wtrocki wtrocki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how simple and elegant this PR is
How it handles all business cases - Now and Future!
How clean and future proof this code looks like
Reusability of this code across services

Reviewed!
Verified!
Amazed!

image

@wtrocki wtrocki changed the title WIP: fix(kafka create): reduce number of calls made to AMS API fix(kafka create): reduce number of calls made to AMS API Jun 28, 2022
@wtrocki wtrocki merged commit 5346fc1 into main Jun 28, 2022
@wtrocki wtrocki deleted the reduce_ams_calls branch June 28, 2022 16:41
@wtrocki
Copy link
Collaborator

wtrocki commented Jun 28, 2022

I have noticed that this does not work with Trial instances. I can fix that on friday without worries

wtrocki added a commit that referenced this pull request Jun 28, 2022
wtrocki added a commit that referenced this pull request Jun 28, 2022
wtrocki added a commit that referenced this pull request Jun 29, 2022
wtrocki added a commit that referenced this pull request Jun 29, 2022
wtrocki added a commit that referenced this pull request Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants