Skip to content

Commit 17ea3c8

Browse files
author
Brian Flores
committed
added command and unit tests
1 parent ce5719f commit 17ea3c8

6 files changed

Lines changed: 221 additions & 0 deletions

File tree

plugin/commands/account/account.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
2929
cobraCmd.AddCommand(NewSummaryCommand(sl).Command)
3030
cobraCmd.AddCommand(NewHooksCommand(sl).Command)
3131
cobraCmd.AddCommand(NewHookCreateCommand(sl).Command)
32+
cobraCmd.AddCommand(NewHookDeleteCommand(sl).Command)
3233
return cobraCmd
3334
}
3435

plugin/commands/account/account_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var availableCommands = []string{
2525
"event-detail",
2626
"events",
2727
"hook-create",
28+
"hook-delete",
2829
"hooks",
2930
"invoice-detail",
3031
"invoices",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package account
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
9+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
12+
)
13+
14+
type HookDeleteCommand struct {
15+
*metadata.SoftlayerCommand
16+
AccountManager managers.AccountManager
17+
Command *cobra.Command
18+
}
19+
20+
func NewHookDeleteCommand(sl *metadata.SoftlayerCommand) *HookDeleteCommand {
21+
thisCmd := &HookDeleteCommand{
22+
SoftlayerCommand: sl,
23+
AccountManager: managers.NewAccountManager(sl.Session),
24+
}
25+
26+
cobraCmd := &cobra.Command{
27+
Use: "hook-delete " + T("IDENTIFIER"),
28+
Short: T("Delete a provisioning scriptt."),
29+
Args: metadata.OneArgs,
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
return thisCmd.Run(args)
32+
},
33+
}
34+
35+
thisCmd.Command = cobraCmd
36+
return thisCmd
37+
}
38+
39+
func (cmd *HookDeleteCommand) Run(args []string) error {
40+
41+
hookID, err := strconv.Atoi(args[0])
42+
if err != nil {
43+
return errors.NewInvalidSoftlayerIdInputError("Hook ID")
44+
}
45+
46+
success, err := cmd.AccountManager.DeleteProvisioningScript(hookID)
47+
if err != nil {
48+
return errors.NewAPIError(T("Failed to delete Provisioning Hook."), err.Error(), 2)
49+
}
50+
51+
if success {
52+
cmd.UI.Ok()
53+
cmd.UI.Print(T("Successfully removed Provisioning Hook."))
54+
}
55+
return nil
56+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package account_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+
10+
"github.com/softlayer/softlayer-go/session"
11+
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/account"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
14+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
15+
)
16+
17+
var _ = Describe("account hook-delete", func() {
18+
var (
19+
fakeUI *terminal.FakeUI
20+
cliCommand *account.HookDeleteCommand
21+
fakeSession *session.Session
22+
slCommand *metadata.SoftlayerCommand
23+
fakeAccountManager *testhelpers.FakeAccountManager
24+
)
25+
BeforeEach(func() {
26+
fakeUI = terminal.NewFakeUI()
27+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
28+
fakeAccountManager = new(testhelpers.FakeAccountManager)
29+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
30+
cliCommand = account.NewHookDeleteCommand(slCommand)
31+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
32+
cliCommand.AccountManager = fakeAccountManager
33+
})
34+
35+
Describe("account hook-delete", func() {
36+
37+
Context("Return error", func() {
38+
It("return error", func() {
39+
err := testhelpers.RunCobraCommand(cliCommand.Command)
40+
Expect(err).To(HaveOccurred())
41+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
42+
})
43+
44+
It("return error", func() {
45+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abc")
46+
Expect(err).To(HaveOccurred())
47+
Expect(err.Error()).To(ContainSubstring("Invalid input for 'Hook ID'. It must be a positive integer."))
48+
})
49+
})
50+
51+
Context("Return error", func() {
52+
BeforeEach(func() {
53+
fakeAccountManager.DeleteProvisioningScriptReturns(false, errors.New("Failed to delete Provisioning Hook"))
54+
})
55+
It("Failed to delete Provisioning Hook", func() {
56+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456")
57+
Expect(err).To(HaveOccurred())
58+
Expect(err.Error()).To(ContainSubstring("Failed to delete Provisioning Hook"))
59+
})
60+
})
61+
62+
Context("Return no error", func() {
63+
BeforeEach(func() {
64+
fakeAccountManager.DeleteProvisioningScriptReturns(true, nil)
65+
})
66+
It("Return no error", func() {
67+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456")
68+
Expect(err).NotTo(HaveOccurred())
69+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
70+
Expect(fakeUI.Outputs()).To(ContainSubstring("Successfully removed Provisioning Hook."))
71+
})
72+
})
73+
})
74+
})

plugin/managers/account.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type AccountManager interface {
3030
GetBandwidthPoolDetail(bandwidthPoolId int, mask string) (datatypes.Network_Bandwidth_Version1_Allotment, error)
3131
GetPostProvisioningHooks(mask string) ([]datatypes.Provisioning_Hook, error)
3232
CreateProvisioningScript(template datatypes.Provisioning_Hook) (datatypes.Provisioning_Hook, error)
33+
DeleteProvisioningScript(idProvisioningScript int) (resp bool, err error)
3334
}
3435

3536
type accountManager struct {
@@ -373,3 +374,12 @@ func (a accountManager) CreateProvisioningScript(template datatypes.Provisioning
373374
provisioningHook := services.GetProvisioningHookService(a.Session)
374375
return provisioningHook.CreateObject(&template)
375376
}
377+
378+
/*
379+
Delete a provisioning script.
380+
https://sldn.softlayer.com/reference/services/SoftLayer_Provisioning_Hook/deleteObject/
381+
*/
382+
func (a accountManager) DeleteProvisioningScript(idProvisioningScript int) (resp bool, err error) {
383+
provisioningHook := services.GetProvisioningHookService(a.Session)
384+
return provisioningHook.Id(idProvisioningScript).DeleteObject()
385+
}

plugin/testhelpers/fake_account_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)