-
Notifications
You must be signed in to change notification settings - Fork 2
[CLOUDGA-28778] Billing estimate cli #309
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package billing | ||
|
|
||
| import ( | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| ybmAuthClient "github.com/yugabyte/ybm-cli/internal/client" | ||
| "github.com/yugabyte/ybm-cli/internal/formatter" | ||
| ) | ||
|
|
||
| var BillingCmd = &cobra.Command{ | ||
| Use: "billing", | ||
| Short: "Billing operations for YugabyteDB Aeon", | ||
| Long: "Billing operations for YugabyteDB Aeon", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| cmd.Help() | ||
| }, | ||
| } | ||
|
|
||
| var billingEstimateCmd = &cobra.Command{ | ||
| Use: "estimate", | ||
| Short: "Get billing estimate for accounts", | ||
| Long: "Get billing estimate for one or more accounts within a specified date range. Results may not reflect real-time usage, please verify against your end-of-month invoice for accurate billing.", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| authApi, err := ybmAuthClient.NewAuthApiClient() | ||
| if err != nil { | ||
| logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
| } | ||
| authApi.GetInfo("", "") | ||
|
|
||
| startDate, _ := cmd.Flags().GetString("start-date") | ||
| endDate, _ := cmd.Flags().GetString("end-date") | ||
| accountNames, _ := cmd.Flags().GetStringSlice("account-names") | ||
|
|
||
| resp, r, err := authApi.GetBillingEstimate(startDate, endDate, accountNames).Execute() | ||
| if err != nil { | ||
| logrus.Debugf("Full HTTP response: %v", r) | ||
| logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
| } | ||
|
|
||
| billingEstimateData := resp.GetData() | ||
| formatter.BillingEstimateWriteFull(billingEstimateData) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| BillingCmd.AddCommand(billingEstimateCmd) | ||
|
|
||
| billingEstimateCmd.Flags().StringSlice("account-names", []string{}, "[OPTIONAL] Comma-separated list of account names to fetch billing information for. Defaults to all accounts of user.") | ||
| billingEstimateCmd.Flags().String("start-date", "", "[OPTIONAL] Start date(format yyyy-MM-dd) for billing estimate (inclusive). Defaults to 1st day of current month if not provided.") | ||
| billingEstimateCmd.Flags().String("end-date", "", "[OPTIONAL] End date(format yyyy-MM-dd) for billing estimate (inclusive). Defaults to current date if not provided.") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package cmd_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/onsi/gomega/gbytes" | ||
| "github.com/onsi/gomega/gexec" | ||
| "github.com/onsi/gomega/ghttp" | ||
| openapi "github.com/yugabyte/yugabytedb-managed-go-client-internal" | ||
| ) | ||
|
|
||
| var _ = Describe("Billing", func() { | ||
|
|
||
| var ( | ||
| server *ghttp.Server | ||
| statusCode int | ||
| args []string | ||
| responseAccount openapi.AccountResponse | ||
| responseProject openapi.AccountResponse | ||
| billingEstimateResponse openapi.BillingEstimateResponse | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| args = os.Args | ||
| os.Args = []string{} | ||
| var err error | ||
| server, err = newGhttpServer(responseAccount, responseProject) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| os.Setenv("YBM_HOST", fmt.Sprintf("http://%s", server.Addr())) | ||
| os.Setenv("YBM_APIKEY", "test-token") | ||
| statusCode = 200 | ||
| }) | ||
|
|
||
| AfterEach(func() { | ||
| os.Args = args | ||
| server.Close() | ||
| os.Unsetenv("YBM_FF_BILLING") | ||
| }) | ||
|
|
||
| Context("When BILLING feature flag is disabled", func() { | ||
| It("should not recognize billing command", func() { | ||
| cmd := exec.Command(compiledCLIPath, "billing") | ||
| session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| session.Wait(2) | ||
| Expect(session.Err).Should(gbytes.Say("unknown command \"billing\"")) | ||
| session.Kill() | ||
| }) | ||
|
|
||
| It("should not recognize billing estimate command", func() { | ||
| cmd := exec.Command(compiledCLIPath, "billing", "estimate") | ||
| session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| session.Wait(2) | ||
| Expect(session.Err).Should(gbytes.Say("unknown command \"billing\"")) | ||
| session.Kill() | ||
| }) | ||
| }) | ||
|
|
||
| Context("When BILLING feature flag is enabled", func() { | ||
| BeforeEach(func() { | ||
| os.Setenv("YBM_FF_BILLING", "true") | ||
| }) | ||
|
|
||
| Describe("When running billing help", func() { | ||
| It("should show billing help", func() { | ||
| cmd := exec.Command(compiledCLIPath, "billing") | ||
| session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| session.Wait(2) | ||
| Expect(session.Out).Should(gbytes.Say("Billing operations for YugabyteDB Aeon")) | ||
| Expect(session.Out).Should(gbytes.Say("estimate")) | ||
| session.Kill() | ||
| }) | ||
| }) | ||
|
|
||
| Describe("When running billing estimate", func() { | ||
| BeforeEach(func() { | ||
| err := loadJson("./test/fixtures/billing-estimate-response.json", &billingEstimateResponse) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| server.AppendHandlers( | ||
| ghttp.CombineHandlers( | ||
| ghttp.VerifyRequest(http.MethodGet, "/api/public/v1/billing-estimate"), | ||
| ghttp.RespondWithJSONEncodedPtr(&statusCode, billingEstimateResponse), | ||
| ), | ||
| ) | ||
| }) | ||
|
|
||
| It("with no params should get billing estimate", func() { | ||
| cmd := exec.Command(compiledCLIPath, "billing", "estimate") | ||
| session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| session.Wait(2) | ||
| Expect(session.Out).Should(gbytes.Say(`Start Date End Date Total Amount | ||
| 2025-01-01 2025-01-31 \$245.67 | ||
|
|
||
| Account Name Amount | ||
| account-1 \$123.45 | ||
| account-2 \$122.22`)) | ||
| session.Kill() | ||
| }) | ||
|
|
||
| It("should get billing estimate with all parameters", func() { | ||
| cmd := exec.Command(compiledCLIPath, "billing", "estimate", | ||
| "--start-date", "2025-01-01", | ||
| "--end-date", "2025-01-31", | ||
| "--account-names", "account-1,account-2") | ||
| session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| session.Wait(2) | ||
| Expect(session.Out).Should(gbytes.Say(`Start Date End Date Total Amount | ||
| 2025-01-01 2025-01-31 \$245.67 | ||
|
|
||
| Account Name Amount | ||
| account-1 \$123.45 | ||
| account-2 \$122.22`)) | ||
| session.Kill() | ||
| }) | ||
| }) | ||
|
|
||
| Describe("When running billing estimate with no billing account", func() { | ||
| BeforeEach(func() { | ||
| err := loadJson("./test/fixtures/billing-estimate-empty-accounts.json", &billingEstimateResponse) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| server.AppendHandlers( | ||
| ghttp.CombineHandlers( | ||
| ghttp.VerifyRequest(http.MethodGet, "/api/public/v1/billing-estimate"), | ||
| ghttp.RespondWithJSONEncodedPtr(&statusCode, billingEstimateResponse), | ||
| ), | ||
| ) | ||
| }) | ||
|
|
||
| It("should show no account data message when accounts are empty", func() { | ||
| cmd := exec.Command(compiledCLIPath, "billing", "estimate") | ||
| session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| session.Wait(2) | ||
| Expect(session.Out).Should(gbytes.Say(`Start Date End Date Total Amount | ||
| 2025-08-01 2025-08-11 \$0.00 | ||
|
|
||
| No account data available.`)) | ||
| session.Kill() | ||
| }) | ||
| }) | ||
|
|
||
| Describe("When running billing estimate with account not belonging to the user", func() { | ||
| BeforeEach(func() { | ||
| errorResponse := map[string]interface{}{ | ||
| "error": map[string]interface{}{ | ||
| "detail": "Cannot find account with name 'account-10'", | ||
| "status": 400, | ||
| }, | ||
| } | ||
|
|
||
| statusCode = 404 | ||
| server.AppendHandlers( | ||
| ghttp.CombineHandlers( | ||
| ghttp.VerifyRequest(http.MethodGet, "/api/public/v1/billing-estimate"), | ||
| ghttp.RespondWithJSONEncodedPtr(&statusCode, errorResponse), | ||
| ), | ||
| ) | ||
| }) | ||
|
|
||
| It("should show error message when account not found", func() { | ||
| cmd := exec.Command(compiledCLIPath, "billing", "estimate", "--account-names", "account-10") | ||
| session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| session.Wait(2) | ||
| Expect(session.Err).Should(gbytes.Say("Cannot find account with name 'account-10'")) | ||
| session.Kill() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "data": { | ||
| "start_date": "2025-08-01", | ||
| "end_date": "2025-08-11", | ||
| "total_amount": 0.0, | ||
| "accounts": [] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "data": { | ||
| "start_date": "2025-01-01", | ||
| "end_date": "2025-01-31", | ||
| "total_amount": 245.67, | ||
| "accounts": [ | ||
| { | ||
| "account_id": "3b98d883-1b63-40ce-a4d7-8dc2d6e0ba53", | ||
| "account_name": "account-1", | ||
| "total_amount": 123.45 | ||
| }, | ||
| { | ||
| "account_id": "cad5c492-f477-42e8-b597-e74376fa2257", | ||
| "account_name": "account-2", | ||
| "total_amount": 122.22 | ||
| } | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.