Skip to content

Commit 71ad0cc

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #667 from Brian-Flores/issue665
New Command: `ibmcloud sl account hook-create`
2 parents 5d84e1e + ce5719f commit 71ad0cc

6 files changed

Lines changed: 244 additions & 0 deletions

File tree

plugin/commands/account/account.go

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

plugin/commands/account/account_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var availableCommands = []string{
2424
"cancel-item",
2525
"event-detail",
2626
"events",
27+
"hook-create",
2728
"hooks",
2829
"invoice-detail",
2930
"invoices",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package account
2+
3+
import (
4+
"github.com/softlayer/softlayer-go/datatypes"
5+
"github.com/softlayer/softlayer-go/sl"
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+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
13+
)
14+
15+
type HookCreateCommand struct {
16+
*metadata.SoftlayerCommand
17+
AccountManager managers.AccountManager
18+
Command *cobra.Command
19+
Name string
20+
Uri string
21+
}
22+
23+
func NewHookCreateCommand(sl *metadata.SoftlayerCommand) *HookCreateCommand {
24+
thisCmd := &HookCreateCommand{
25+
SoftlayerCommand: sl,
26+
AccountManager: managers.NewAccountManager(sl.Session),
27+
}
28+
29+
cobraCmd := &cobra.Command{
30+
Use: "hook-create",
31+
Short: T("Order/create a provisioning script."),
32+
Args: metadata.NoArgs,
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
return thisCmd.Run(args)
35+
},
36+
}
37+
38+
cobraCmd.Flags().StringVarP(&thisCmd.Name, "name", "N", "", T("The name of the hook."))
39+
cobraCmd.Flags().StringVarP(&thisCmd.Uri, "uri", "U", "", T("The endpoint that the script will be downloaded."))
40+
41+
//#nosec G104 -- This is a false positive
42+
cobraCmd.MarkFlagRequired("name")
43+
//#nosec G104 -- This is a false positive
44+
cobraCmd.MarkFlagRequired("uri")
45+
46+
thisCmd.Command = cobraCmd
47+
return thisCmd
48+
}
49+
50+
func (cmd *HookCreateCommand) Run(args []string) error {
51+
52+
outputFormat := cmd.GetOutputFlag()
53+
54+
hookTemplate := datatypes.Provisioning_Hook{
55+
Name: sl.String(cmd.Name),
56+
Uri: sl.String(cmd.Uri),
57+
}
58+
59+
provisioningHook, err := cmd.AccountManager.CreateProvisioningScript(hookTemplate)
60+
if err != nil {
61+
return errors.NewAPIError(T("Failed to create Provisioning Hook."), err.Error(), 2)
62+
}
63+
64+
table := cmd.UI.Table([]string{T("Name"), T("Value")})
65+
table.Add(T("Id"), utils.FormatIntPointer(provisioningHook.Id))
66+
table.Add(T("Name"), utils.FormatStringPointer(provisioningHook.Name))
67+
table.Add(T("Created"), utils.FormatSLTimePointer(provisioningHook.CreateDate))
68+
table.Add(T("Uri"), utils.FormatStringPointer(provisioningHook.Uri))
69+
70+
utils.PrintTable(cmd.UI, table, outputFormat)
71+
return nil
72+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package account_test
2+
3+
import (
4+
"errors"
5+
"time"
6+
7+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
8+
. "github.com/onsi/ginkgo"
9+
. "github.com/onsi/gomega"
10+
11+
"github.com/softlayer/softlayer-go/datatypes"
12+
"github.com/softlayer/softlayer-go/session"
13+
"github.com/softlayer/softlayer-go/sl"
14+
15+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/account"
16+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
17+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
18+
)
19+
20+
var _ = Describe("account hook-create", func() {
21+
var (
22+
fakeUI *terminal.FakeUI
23+
cliCommand *account.HookCreateCommand
24+
fakeSession *session.Session
25+
slCommand *metadata.SoftlayerCommand
26+
fakeAccountManager *testhelpers.FakeAccountManager
27+
)
28+
BeforeEach(func() {
29+
fakeUI = terminal.NewFakeUI()
30+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
31+
fakeAccountManager = new(testhelpers.FakeAccountManager)
32+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
33+
cliCommand = account.NewHookCreateCommand(slCommand)
34+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
35+
cliCommand.AccountManager = fakeAccountManager
36+
})
37+
38+
Describe("account hook-create", func() {
39+
40+
Context("Return error", func() {
41+
42+
It("Set invalid output", func() {
43+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--name=myhook", "--uri=http://myuritest.com", "--output=xml")
44+
Expect(err).To(HaveOccurred())
45+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: Invalid output format, only JSON is supported now."))
46+
})
47+
})
48+
49+
Context("Return error", func() {
50+
BeforeEach(func() {
51+
fakeAccountManager.CreateProvisioningScriptReturns(datatypes.Provisioning_Hook{}, errors.New("Failed to create Provisioning Hook"))
52+
})
53+
It("Failed to create Provisioning Hook", func() {
54+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--name=myhook", "--uri=http://myuritest.com")
55+
Expect(err).To(HaveOccurred())
56+
Expect(err.Error()).To(ContainSubstring("Failed to create Provisioning Hook"))
57+
})
58+
})
59+
60+
Context("Return no error", func() {
61+
BeforeEach(func() {
62+
created, _ := time.Parse(time.RFC3339, "2017-11-08T00:00:00Z")
63+
fakerHook := datatypes.Provisioning_Hook{
64+
Id: sl.Int(123456),
65+
Name: sl.String("My Hook"),
66+
CreateDate: sl.Time(created),
67+
Uri: sl.String("http://myuritest.com"),
68+
}
69+
fakeAccountManager.CreateProvisioningScriptReturns(fakerHook, nil)
70+
})
71+
It("Return no error", func() {
72+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--name=myhook", "--uri=http://myuritest.com")
73+
Expect(err).NotTo(HaveOccurred())
74+
Expect(fakeUI.Outputs()).To(ContainSubstring("123456"))
75+
Expect(fakeUI.Outputs()).To(ContainSubstring("My Hook"))
76+
Expect(fakeUI.Outputs()).To(ContainSubstring("2017-11-08T00:00:00Z"))
77+
Expect(fakeUI.Outputs()).To(ContainSubstring("http://myuritest.com"))
78+
})
79+
})
80+
})
81+
})

plugin/managers/account.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type AccountManager interface {
2929
GetSummary(mask string) (datatypes.Account, error)
3030
GetBandwidthPoolDetail(bandwidthPoolId int, mask string) (datatypes.Network_Bandwidth_Version1_Allotment, error)
3131
GetPostProvisioningHooks(mask string) ([]datatypes.Provisioning_Hook, error)
32+
CreateProvisioningScript(template datatypes.Provisioning_Hook) (datatypes.Provisioning_Hook, error)
3233
}
3334

3435
type accountManager struct {
@@ -363,3 +364,12 @@ func (a accountManager) GetPostProvisioningHooks(mask string) ([]datatypes.Provi
363364
}
364365
return a.AccountService.Mask(mask).GetPostProvisioningHooks()
365366
}
367+
368+
/*
369+
Create a provisioning script.
370+
https://sldn.softlayer.com/reference/services/SoftLayer_Provisioning_Hook/createObject/
371+
*/
372+
func (a accountManager) CreateProvisioningScript(template datatypes.Provisioning_Hook) (datatypes.Provisioning_Hook, error) {
373+
provisioningHook := services.GetProvisioningHookService(a.Session)
374+
return provisioningHook.CreateObject(&template)
375+
}

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)