Skip to content

Commit

Permalink
OCM-10221 | fix: forbid to create hcp cluster without billing account
Browse files Browse the repository at this point in the history
  • Loading branch information
Mischulee committed Aug 14, 2024
1 parent b037752 commit 903dabe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
27 changes: 19 additions & 8 deletions cmd/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,14 +1120,10 @@ func run(cmd *cobra.Command, _ []string) {

if billingAccount == "" && !interactive.Enabled() {
// if a billing account is not provided we will try to use the infrastructure account as default
if helper.ContainsPrefix(billingAccounts, awsCreator.AccountID) {
billingAccount = awsCreator.AccountID
r.Reporter.Infof("Using '%s' as billing account", billingAccount)
r.Reporter.Infof(
"To use a different billing account, add --billing-account xxxxxxxxxx to previous command",
)
} else {
r.Reporter.Errorf("A billing account is required for Hosted Control Plane clusters. %s", listBillingAccountMessage)
billingAccount, err = provideBillingAccount(billingAccounts, awsCreator.AccountID, r)
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
}
}

Expand Down Expand Up @@ -3350,6 +3346,21 @@ func validateBillingAccount(billingAccount string) error {
return nil
}

func provideBillingAccount(billingAccounts []string, accountID string, r *rosa.Runtime) (string, error) {
if !helper.ContainsPrefix(billingAccounts, accountID) {
return "", fmt.Errorf("A billing account is required for Hosted Control Plane clusters. %s",
listBillingAccountMessage)
}

billingAccount := accountID

r.Reporter.Infof("Using '%s' as billing account", billingAccount)
r.Reporter.Infof(
"To use a different billing account, add --billing-account xxxxxxxxxx to previous command",
)
return billingAccount, nil
}

// validateNetworkType ensure user passes a valid network type parameter at creation
func validateNetworkType(networkType string) error {
if networkType == "" {
Expand Down
28 changes: 28 additions & 0 deletions cmd/create/cluster/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,34 @@ var _ = Describe("validateBillingAccount()", func() {

})

var _ = Describe("provideBillingAccount()", func() {

var (
r *rosa.Runtime
billingAccounts = []string{"123456789012", "987654321098"}
)

BeforeEach(func() {
r = rosa.NewRuntime()
})

It("OK: returns the the billing account if accountID is found in billingAccounts", func() {
accountID := "123456789012"
billingAccount, err := provideBillingAccount(billingAccounts, accountID, r)
Expect(billingAccount).To(Equal(billingAccount))
Expect(err).NotTo(HaveOccurred())
})

It("KO: fails with an error message if accountID is not found in billingAccounts", func() {
accountID := "000"
billingAccount, err := provideBillingAccount(billingAccounts, accountID, r)
Expect(billingAccount).To(Equal(""))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("A billing account is required for Hosted Control Plane clusters." +
" To see the list of billing account options, you can use interactive mode by passing '-i'."))
})
})

var _ = Describe("getInitialValidSubnets()", func() {

var (
Expand Down

0 comments on commit 903dabe

Please sign in to comment.