Skip to content

Commit c48fe3c

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #695 from Brian-Flores/issue691
New command: `ibmcloud sl order quote-delete`
2 parents 6692f0d + cf61f50 commit c48fe3c

6 files changed

Lines changed: 226 additions & 0 deletions

File tree

plugin/commands/order/order.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
2626
cobraCmd.AddCommand(NewQuoteDetailCommand(sl).Command)
2727
cobraCmd.AddCommand(NewQuoteSaveCommand(sl).Command)
2828
cobraCmd.AddCommand(NewQuoteCommand(sl).Command)
29+
cobraCmd.AddCommand(NewQuoteDeleteCommand(sl).Command)
2930
cobraCmd.AddCommand(NewLookupCommand(sl).Command)
3031
return cobraCmd
3132
}

plugin/commands/order/order_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var availableCommands = []string{
2727
"place-quote",
2828
"preset-list",
2929
"quote",
30+
"quote-delete",
3031
"quote-detail",
3132
"quote-list",
3233
"quote-save",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package order
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/spf13/cobra"
7+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
8+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
)
12+
13+
type QuoteDeleteCommand struct {
14+
*metadata.SoftlayerCommand
15+
OrderManager managers.OrderManager
16+
Command *cobra.Command
17+
}
18+
19+
func NewQuoteDeleteCommand(sl *metadata.SoftlayerCommand) (cmd *QuoteDeleteCommand) {
20+
thisCmd := &QuoteDeleteCommand{
21+
SoftlayerCommand: sl,
22+
OrderManager: managers.NewOrderManager(sl.Session),
23+
}
24+
25+
cobraCmd := &cobra.Command{
26+
Use: "quote-delete " + T("IDENTIFIER"),
27+
Short: T("Delete the quote of an order."),
28+
Args: metadata.OneArgs,
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
return thisCmd.Run(args)
31+
},
32+
}
33+
34+
thisCmd.Command = cobraCmd
35+
return thisCmd
36+
}
37+
38+
func (cmd *QuoteDeleteCommand) Run(args []string) error {
39+
40+
quoteId, err := strconv.Atoi(args[0])
41+
if err != nil {
42+
return errors.NewInvalidSoftlayerIdInputError("Quote ID")
43+
}
44+
45+
deletedQuote, err := cmd.OrderManager.DeleteQuote(quoteId)
46+
if err != nil {
47+
return errors.NewAPIError(T("Failed to delete Quote"), err.Error(), 2)
48+
}
49+
50+
i18nSub := map[string]interface{}{"quoteID": deletedQuote.Id}
51+
cmd.UI.Ok()
52+
cmd.UI.Print(T("Quote: {{.quoteID}} was deleted.", i18nSub))
53+
return nil
54+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package order_test
2+
3+
import (
4+
"errors"
5+
6+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
"github.com/softlayer/softlayer-go/datatypes"
10+
11+
"github.com/softlayer/softlayer-go/session"
12+
"github.com/softlayer/softlayer-go/sl"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/order"
14+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
15+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
16+
)
17+
18+
var _ = Describe("order quote-delete", func() {
19+
var (
20+
fakeUI *terminal.FakeUI
21+
cliCommand *order.QuoteDeleteCommand
22+
fakeSession *session.Session
23+
slCommand *metadata.SoftlayerCommand
24+
fakeOrderManager *testhelpers.FakeOrderManager
25+
)
26+
BeforeEach(func() {
27+
fakeUI = terminal.NewFakeUI()
28+
fakeOrderManager = new(testhelpers.FakeOrderManager)
29+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
30+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
31+
cliCommand = order.NewQuoteDeleteCommand(slCommand)
32+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
33+
cliCommand.OrderManager = fakeOrderManager
34+
})
35+
36+
Describe("order quote-delete", func() {
37+
38+
Context("Return error", func() {
39+
It("Set command without Id", func() {
40+
err := testhelpers.RunCobraCommand(cliCommand.Command)
41+
Expect(err).To(HaveOccurred())
42+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
43+
})
44+
45+
It("Set command with an invalid Id", func() {
46+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abcde")
47+
Expect(err).To(HaveOccurred())
48+
Expect(err.Error()).To(ContainSubstring("Invalid input for 'Quote ID'. It must be a positive integer."))
49+
})
50+
51+
It("Set invalid output", func() {
52+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456", "--output=xml")
53+
Expect(err).To(HaveOccurred())
54+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: Invalid output format, only JSON is supported now."))
55+
})
56+
})
57+
58+
Context("Return error", func() {
59+
BeforeEach(func() {
60+
fakeOrderManager.DeleteQuoteReturns(datatypes.Billing_Order_Quote{}, errors.New("Failed to delete Quote"))
61+
})
62+
It("Failed delete Quote", func() {
63+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456")
64+
Expect(err).To(HaveOccurred())
65+
Expect(err.Error()).To(ContainSubstring("Failed to delete Quote"))
66+
})
67+
})
68+
69+
Context("Return no error", func() {
70+
BeforeEach(func() {
71+
fakerQuote := datatypes.Billing_Order_Quote{
72+
Id: sl.Int(123456),
73+
}
74+
fakeOrderManager.DeleteQuoteReturns(fakerQuote, nil)
75+
})
76+
It("Delete quote", func() {
77+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456")
78+
Expect(err).NotTo(HaveOccurred())
79+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
80+
Expect(fakeUI.Outputs()).To(ContainSubstring("was deleted"))
81+
})
82+
})
83+
84+
})
85+
})

plugin/managers/order.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type OrderManager interface {
4444
OrderQuote(quoteId int, extra datatypes.Container_Product_Order) (datatypes.Container_Product_Order_Receipt, error)
4545
GetRecalculatedOrderContainer(quoteId int) (datatypes.Container_Product_Order, error)
4646
GetOrderDetail(orderId int, mask string) (datatypes.Billing_Order, error)
47+
DeleteQuote(quoteId int) (datatypes.Billing_Order_Quote, error)
4748
}
4849

4950
type orderManager struct {
@@ -459,3 +460,8 @@ func (i orderManager) GetOrderDetail(orderId int, mask string) (datatypes.Billin
459460
billingOrderService := services.GetBillingOrderService(i.Session)
460461
return billingOrderService.Id(orderId).Mask(mask).GetObject()
461462
}
463+
464+
// Delete the quote of an order.
465+
func (i orderManager) DeleteQuote(quoteId int) (datatypes.Billing_Order_Quote, error) {
466+
return i.BillingOrderQuoteService.Id(quoteId).DeleteQuote()
467+
}

plugin/testhelpers/fake_order_manager.go

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)