Skip to content

Commit 30ae70b

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #879 from SoftLayer/issues740
Adds dedicatedhost cancel command
2 parents 5f507d3 + d36bd9a commit 30ae70b

File tree

9 files changed

+244
-27
lines changed

9 files changed

+244
-27
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ Expect(slOptions.Filter).To(ContainSubstring(`"id":{"operation":"orderBy","optio
156156
Check testhelpers/fake_softlayer_session.go for all the fields that get recorded with an API call.
157157

158158

159+
Heres a fancy way to test an API call matches a few different properties at the same time:
160+
161+
```go
162+
// This is where the MatchFields/PointTo come from
163+
. "github.com/onsi/gomega/gstruct"
164+
165+
It("it returns dedicatedhost verify response", func() {
166+
err := dedicatedhostManager.DeleteHost(12345)
167+
Expect(err).NotTo(HaveOccurred())
168+
apiCalls := fakeHandler.ApiCallLogs
169+
Expect(len(apiCalls)).To(Equal(1))
170+
Expect(apiCalls[0]).To(MatchFields(IgnoreExtras, Fields{
171+
"Service": Equal("SoftLayer_Virtual_DedicatedHost"),
172+
"Method": Equal("deleteObject"),
173+
"Options": PointTo(MatchFields(IgnoreExtras, Fields{"Id": PointTo(Equal(12345))})),
174+
}))
175+
})
176+
177+
```
159178

160179
### Test Fakes
161180

bin/buildAndDeploy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def runTests() -> None:
102102
except FileNotFoundError:
103103
gosec_instal = "curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $GOPATH/bin"
104104
print(f"[red]gosec not found. Try running:\n{gosec_instal}")
105+
print('[turquoise2] go sec OK.')
105106

106107
### Section for i18n4go stuff ###
107108
def runI18n4go(path: str) -> None:
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dedicatedhost
2+
3+
import (
4+
"strconv"
5+
"github.com/spf13/cobra"
6+
7+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
8+
slErr "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
)
12+
13+
type CancellHostCommand struct {
14+
*metadata.SoftlayerCommand
15+
DedicatedHostManager managers.DedicatedHostManager
16+
Command *cobra.Command
17+
}
18+
19+
func NewCancelHostCommand(sl *metadata.SoftlayerCommand) *CancellHostCommand {
20+
thisCmd := &CancellHostCommand{
21+
SoftlayerCommand: sl,
22+
DedicatedHostManager: managers.NewDedicatedhostManager(sl.Session),
23+
}
24+
cobraCmd := &cobra.Command{
25+
Use: "cancel " + T("IDENTIFIER"),
26+
Short: T("Cancel a dedicated host server immediately."),
27+
Long: T(`If there are any guests on this Dedicated Host, this command will fail until those guests are deleted.
28+
Use 'sl dedicatedhost cancel-guests [IDENTIFIER]' to remove all guests from a host.
29+
Use 'sl vs delete' to remove a specific guest.`),
30+
Args: metadata.OneArgs,
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return thisCmd.Run(args)
33+
},
34+
}
35+
36+
thisCmd.Command = cobraCmd
37+
return thisCmd
38+
}
39+
40+
func (cmd *CancellHostCommand) Run(args []string) error {
41+
hardwareID, err := strconv.Atoi(args[0])
42+
if err != nil {
43+
return slErr.NewInvalidSoftlayerIdInputError("IDENTIFIER")
44+
}
45+
err = cmd.DedicatedHostManager.DeleteHost(hardwareID)
46+
if err != nil {
47+
return err
48+
}
49+
cmd.UI.Print(T("Dedicated Host {{.ID}} was cancelled", map[string]interface{}{"ID": hardwareID}))
50+
return nil
51+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dedicatedhost_test
2+
3+
import (
4+
"errors"
5+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
"github.com/softlayer/softlayer-go/session"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/dedicatedhost"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
12+
)
13+
14+
var _ = Describe("Dedicated host cancel", func() {
15+
var (
16+
fakeUI *terminal.FakeUI
17+
cliCommand *dedicatedhost.CancellHostCommand
18+
fakeSession *session.Session
19+
slCommand *metadata.SoftlayerCommand
20+
FakeDedicatedhostManager *testhelpers.FakeDedicatedHostManager
21+
)
22+
BeforeEach(func() {
23+
fakeUI = terminal.NewFakeUI()
24+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
25+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
26+
cliCommand = dedicatedhost.NewCancelHostCommand(slCommand)
27+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
28+
FakeDedicatedhostManager = new(testhelpers.FakeDedicatedHostManager)
29+
cliCommand.DedicatedHostManager = FakeDedicatedhostManager
30+
})
31+
32+
Describe("Dedicatedhost cancel usage errors", func() {
33+
Context("Dedicatedhost cancel without ID", func() {
34+
It("return error", func() {
35+
err := testhelpers.RunCobraCommand(cliCommand.Command)
36+
Expect(err).To(HaveOccurred())
37+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
38+
})
39+
})
40+
})
41+
Describe("Dedicatedhost cancel usage", func() {
42+
Context("Dedicatedhost cancel Happy Path", func() {
43+
It("Succss", func() {
44+
err := testhelpers.RunCobraCommand(cliCommand.Command, "12345")
45+
Expect(err).NotTo(HaveOccurred())
46+
Expect(fakeUI.Outputs()).To(ContainSubstring("Dedicated Host 12345 was cancelled"))
47+
})
48+
})
49+
Context("Dedicatedhost cancel API errors", func() {
50+
BeforeEach(func() {
51+
FakeDedicatedhostManager.DeleteHostReturns(errors.New("API ERROR"))
52+
})
53+
It("Handle API error", func() {
54+
err := testhelpers.RunCobraCommand(cliCommand.Command, "12345")
55+
Expect(err).To(HaveOccurred())
56+
Expect(err.Error()).To(ContainSubstring("API ERROR"))
57+
})
58+
})
59+
})
60+
})

plugin/commands/dedicatedhost/detail_test.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"time"
66

7-
. "github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/matchers"
87
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
98
. "github.com/onsi/ginkgo/v2"
109
. "github.com/onsi/gomega"
@@ -26,7 +25,7 @@ var _ = Describe("Dedicated host detail", func() {
2625
)
2726
BeforeEach(func() {
2827
fakeUI = terminal.NewFakeUI()
29-
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
28+
fakeSession = testhelpers.NewFakeSoftlayerSession(nil)
3029
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
3130
cliCommand = dedicatedhost.NewDetailCommand(slCommand)
3231
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
@@ -111,35 +110,18 @@ var _ = Describe("Dedicated host detail", func() {
111110
It("return no error", func() {
112111
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234")
113112
Expect(err).NotTo(HaveOccurred())
114-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"1234"}))
115-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"dedicatedhost"}))
116-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"56"}))
117-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"1200"}))
118-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"242"}))
119-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"2022-02-01T00:00:00Z"}))
120-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"2022-02-01T00:00:00Z"}))
121-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"3"}))
122-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"dal13"}))
123-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"wilmawang"}))
113+
Expect(fakeUI.Outputs()).To(ContainSubstring("1234"))
114+
Expect(fakeUI.Outputs()).To(ContainSubstring("dedicatedhost"))
115+
Expect(fakeUI.Outputs()).To(ContainSubstring("dal13"))
116+
Expect(fakeUI.Outputs()).To(ContainSubstring("wilmawang"))
124117
})
125118
It("return no error", func() {
126119
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "--guests", "--price")
127120
Expect(err).NotTo(HaveOccurred())
128-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"1234"}))
129-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"dedicatedhost"}))
130-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"56"}))
131-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"1200"}))
132-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"242"}))
133-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"2022-02-01T00:00:00Z"}))
134-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"2022-02-01T00:00:00Z"}))
135-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"3"}))
136-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"dal13"}))
137-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"10"}))
138-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"wilmawang"}))
139-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"test.com"}))
140-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"test"}))
141-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"9131111-2222-6a10-3333-992c544444"}))
142-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"1234567"}))
121+
Expect(fakeUI.Outputs()).To(ContainSubstring("1234"))
122+
Expect(fakeUI.Outputs()).To(ContainSubstring("dedicatedhost"))
123+
Expect(fakeUI.Outputs()).To(ContainSubstring("9131111-2222-6a10-3333-992c544444"))
124+
Expect(fakeUI.Outputs()).To(ContainSubstring("1234567"))
143125
})
144126
})
145127
})

plugin/managers/dedicatedhost.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type DedicatedHostManager interface {
4545
GetCreateOptions(productPackage datatypes.Product_Package) map[string]map[string]string
4646
GetVlansOptions(datacenter string, flavor string, productPackage datatypes.Product_Package) ([]datatypes.Network_Vlan, error)
4747
ListDedicatedHost(name, datacenter, owner string, orderId int) ([]datatypes.Virtual_DedicatedHost, error)
48+
DeleteHost(identifier int) error
4849
}
4950

5051
type dedicatedhostManager struct {
@@ -302,3 +303,9 @@ func (d dedicatedhostManager) ListDedicatedHost(name, datacenter, owner string,
302303
}
303304
return resourceList, nil
304305
}
306+
307+
// Calls https://sldn.softlayer.com/reference/services/SoftLayer_Virtual_DedicatedHost/deleteObject/
308+
func (d dedicatedhostManager) DeleteHost(identifier int) error {
309+
_, err := d.VirtualDedicatedHost.Id(identifier).DeleteObject()
310+
return err
311+
}

plugin/managers/dedicatedhost_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package managers_test
33
import (
44
. "github.com/onsi/ginkgo/v2"
55
. "github.com/onsi/gomega"
6+
. "github.com/onsi/gomega/gstruct"
67
"github.com/softlayer/softlayer-go/session"
78
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
89
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
@@ -11,13 +12,19 @@ import (
1112
var _ = Describe("DedicatedhostManager", func() {
1213
var (
1314
fakeSLSession *session.Session
15+
fakeHandler *testhelpers.FakeTransportHandler
1416
dedicatedhostManager managers.DedicatedHostManager
1517
)
1618

1719
BeforeEach(func() {
1820
fakeSLSession = testhelpers.NewFakeSoftlayerSession(nil)
21+
fakeHandler = testhelpers.GetSessionHandler(fakeSLSession)
1922
dedicatedhostManager = managers.NewDedicatedhostManager(fakeSLSession)
2023
})
24+
AfterEach(func() {
25+
fakeHandler.ClearApiCallLogs()
26+
fakeHandler.ClearErrors()
27+
})
2128

2229
Describe("Genereate a dedicatedhost order template", func() {
2330
Context("not found Package", func() {
@@ -80,4 +87,19 @@ var _ = Describe("DedicatedhostManager", func() {
8087
})
8188
})
8289
})
90+
Describe("Dedicated Host Manager Simple functions", func() {
91+
Context("DeleteHost", func() {
92+
It("it returns dedicatedhost verify response", func() {
93+
err := dedicatedhostManager.DeleteHost(12345)
94+
Expect(err).NotTo(HaveOccurred())
95+
apiCalls := fakeHandler.ApiCallLogs
96+
Expect(len(apiCalls)).To(Equal(1))
97+
Expect(apiCalls[0]).To(MatchFields(IgnoreExtras, Fields{
98+
"Service": Equal("SoftLayer_Virtual_DedicatedHost"),
99+
"Method": Equal("deleteObject"),
100+
"Options": PointTo(MatchFields(IgnoreExtras, Fields{"Id": PointTo(Equal(12345))})),
101+
}))
102+
})
103+
})
104+
})
83105
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

plugin/testhelpers/fake_dedicated_host_manager.go

Lines changed: 74 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)