Skip to content

Commit ba8b7d3

Browse files
#730 updated snapshotorder documentation
1 parent ab42435 commit ba8b7d3

11 files changed

Lines changed: 145 additions & 378 deletions

File tree

plugin/commands/block/snapshot_list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func NewSnapshotListCommand(sl *metadata.SoftlayerStorageCommand) *SnapshotListC
2828
cobraCmd := &cobra.Command{
2929
Use: "snapshot-list " + T("IDENTIFIER"),
3030
Short: T("List {{.storageType}} storage snapshots", sl.StorageI18n),
31-
Long: T(`${COMMAND_NAME} sl {{.storageType}} snapshot-list VOLUME_ID [OPTIONS]
32-
31+
Long: T(`
3332
EXAMPLE:
3433
${COMMAND_NAME} sl {{.storageType}} snapshot-list 12345678 --sortby id
3534
This command lists all snapshots of volume with ID 12345678 and sorts them by ID.`, sl.StorageI18n),

plugin/commands/block/snapshot_list_test.go

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,19 @@ var _ = Describe("Snapshot list", func() {
5656
cliCommand.StorageManager = FakeStorageManager
5757
})
5858

59-
Describe("Snapshot list", func() {
60-
Context("Snapshot list without volume id", func() {
61-
It("return error", func() {
59+
Describe("Snapshot list tests", func() {
60+
Context("Usage Errors", func() {
61+
It("No volumeid", func() {
6262
err := testhelpers.RunCobraCommand(cliCommand.Command)
6363
Expect(err).To(HaveOccurred())
6464
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
6565
})
66-
})
67-
Context("Snapshot list with wrong volume id", func() {
68-
It("return error", func() {
66+
It("Bad volume id", func() {
6967
err := testhelpers.RunCobraCommand(cliCommand.Command, "abc")
7068
Expect(err).To(HaveOccurred())
7169
Expect(err.Error()).To(ContainSubstring("Invalid input for 'Volume ID'. It must be a positive integer."))
7270
})
73-
})
74-
75-
Context("Snapshot list with wrong --sortby", func() {
76-
BeforeEach(func() {
77-
FakeStorageManager.GetVolumeSnapshotListReturns(nil, nil)
78-
})
79-
It("return error", func() {
71+
It("Bad --sortby", func() {
8072
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "--sortby", "bcd")
8173
Expect(err).To(HaveOccurred())
8274
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: --sortby bcd is not supported."))
@@ -87,7 +79,7 @@ var _ = Describe("Snapshot list", func() {
8779
BeforeEach(func() {
8880
FakeStorageManager.GetVolumeSnapshotListReturns(nil, errors.New("Internal Server Error"))
8981
})
90-
It("return error", func() {
82+
It("SL API ERROR", func() {
9183
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234")
9284
Expect(err).To(HaveOccurred())
9385
Expect(err.Error()).To(ContainSubstring("Failed to get snapshot list on your account."))
@@ -99,7 +91,7 @@ var _ = Describe("Snapshot list", func() {
9991
BeforeEach(func() {
10092
FakeStorageManager.GetVolumeSnapshotListReturns(fakeReturn, nil)
10193
})
102-
It("return no error", func() {
94+
It("Happy Path", func() {
10395
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234")
10496
Expect(err).NotTo(HaveOccurred())
10597
// I don't like ContainSubstrings, but its useful for checking for multiple strings in a single line
@@ -109,41 +101,23 @@ var _ = Describe("Snapshot list", func() {
109101
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"3", "sp-0003", "2016-12-28T00:12:00", "100"}))
110102

111103
})
112-
})
113-
114-
Context("Snapshot list with correct volume id and --sortby=size_bytes", func() {
115-
BeforeEach(func() {
116-
FakeStorageManager.GetVolumeSnapshotListReturns(fakeReturn, nil)
117-
})
118-
It("return no error", func() {
104+
It("Sorted by size_bytes", func() {
119105
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "--sortby", "size_bytes")
120106
Expect(err).NotTo(HaveOccurred())
121107
rows := strings.Split(fakeUI.Outputs(), "\n")
122108
Expect(rows[1]).To(ContainSubstring("100"))
123109
Expect(rows[2]).To(ContainSubstring("500"))
124110
Expect(rows[3]).To(ContainSubstring("540"))
125111
})
126-
})
127-
128-
Context("Snapshot list with correct volume id and --sortby=created", func() {
129-
BeforeEach(func() {
130-
FakeStorageManager.GetVolumeSnapshotListReturns(fakeReturn, nil)
131-
})
132-
It("return no error", func() {
112+
It("Sorted by created", func() {
133113
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "--sortby", "created")
134114
Expect(err).NotTo(HaveOccurred())
135115
rows := strings.Split(fakeUI.Outputs(), "\n")
136116
Expect(rows[1]).To(ContainSubstring("2016-12-25T00:12:00"))
137117
Expect(rows[2]).To(ContainSubstring("2016-12-26T00:12:00"))
138118
Expect(rows[3]).To(ContainSubstring("2016-12-28T00:12:00"))
139119
})
140-
})
141-
142-
Context("Snapshot list with correct volume id and --sortby=created", func() {
143-
BeforeEach(func() {
144-
FakeStorageManager.GetVolumeSnapshotListReturns(fakeReturn, nil)
145-
})
146-
It("return no error", func() {
120+
It("Sorted by name", func() {
147121
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "--sortby", "name")
148122
Expect(err).NotTo(HaveOccurred())
149123
rows := strings.Split(fakeUI.Outputs(), "\n")

plugin/commands/block/snapshot_order.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package block
22

33
import (
44
"fmt"
5+
"slices"
56
"strconv"
67

78
"github.com/spf13/cobra"
@@ -24,6 +25,8 @@ type SnapshotOrderCommand struct {
2425
Force bool
2526
}
2627

28+
var TIERS = []float64{0.25, 2, 4, 10}
29+
2730
func NewSnapshotOrderCommand(sl *metadata.SoftlayerStorageCommand) *SnapshotOrderCommand {
2831
thisCmd := &SnapshotOrderCommand{
2932
SoftlayerStorageCommand: sl,
@@ -32,7 +35,8 @@ func NewSnapshotOrderCommand(sl *metadata.SoftlayerStorageCommand) *SnapshotOrde
3235
cobraCmd := &cobra.Command{
3336
Use: "snapshot-order " + T("IDENTIFIER"),
3437
Short: T("Order snapshot space for a block storage volume"),
35-
Long: T(`${COMMAND_NAME} sl {{.storageType}} snapshot-order VOLUME_ID [OPTIONS]
38+
Long: T(`See https://cloud.ibm.com/docs/BlockStorage?topic=BlockStorage-getting-started for sizing options.
39+
${COMMAND_NAME} sl block volume-options' to get available options.
3640
3741
EXAMPLE:
3842
${COMMAND_NAME} sl {{.storageType}} snapshot-order 12345678 -s 1000 -t 4
@@ -42,11 +46,15 @@ EXAMPLE:
4246
return thisCmd.Run(args)
4347
},
4448
}
45-
cobraCmd.Flags().IntVarP(&thisCmd.Size, "size", "s", 0, T("Size of snapshot space to create in GB [required]"))
46-
cobraCmd.Flags().Float64VarP(&thisCmd.Tier, "tier", "t", 0, T("Endurance Storage Tier (IOPS per GB) of the block volume for which space is ordered [optional], options are: 0.25,2,4,10"))
47-
cobraCmd.Flags().IntVarP(&thisCmd.Iops, "iops", "i", 0, T("Performance Storage IOPs, between 100 and 6000 in multiples of 100"))
48-
cobraCmd.Flags().BoolVarP(&thisCmd.Upgrade, "upgrade", "u", false, T("Flag to indicate that the order is an upgrade"))
49+
cobraCmd.Flags().IntVarP(&thisCmd.Size, "size", "s", 0, T("Size of snapshot space to create in GB"))
50+
cobraCmd.Flags().Float64VarP(&thisCmd.Tier, "tier", "t", 0,
51+
T("Endurance Storage Tier (IOPS per GB) of the block volume for which space is ordered [optional], options are: 0.25,2,4,10"))
52+
cobraCmd.Flags().IntVarP(&thisCmd.Iops, "iops", "i", 0,
53+
T("Performance Storage IOPs, between 100 and 6000 in multiples of 100"))
54+
cobraCmd.Flags().BoolVarP(&thisCmd.Upgrade, "upgrade", "u", false,
55+
T("Flag to indicate that the order is an upgrade"))
4956
cobraCmd.Flags().BoolVarP(&thisCmd.Force, "force", "f", false, T("Force operation without confirmation"))
57+
cobraCmd.MarkFlagRequired("size") // #nosec G104 -- Doesn't matter if this errors
5058
thisCmd.Command = cobraCmd
5159
return thisCmd
5260
}
@@ -57,27 +65,16 @@ func (cmd *SnapshotOrderCommand) Run(args []string) error {
5765
if err != nil {
5866
return slErr.NewInvalidSoftlayerIdInputError("Volume ID")
5967
}
60-
subs := map[string]interface{}{"CommandName": "ibmcloud"}
61-
if cmd.Size == 0 {
62-
return slErr.NewInvalidUsageError(T("[-s|--size] is required.\nRun '{{.CommandName}} sl block volume-options' to get available options.", subs))
63-
}
64-
size := cmd.Size
6568

66-
tier := cmd.Tier
67-
if tier > 0 {
68-
if tier != 0 && tier != 0.25 && tier != 2 && tier != 4 && tier != 10 {
69+
if cmd.Tier > 0 {
70+
if !slices.Contains(TIERS, cmd.Tier) {
6971
return slErr.NewInvalidUsageError(T("[-t|--tier] is optional, options are: 0.25,2,4,10."))
7072
}
7173
}
72-
iops := cmd.Iops
73-
if iops > 0 {
74-
if iops < 100 || iops > 6000 {
75-
return slErr.NewInvalidUsageError(T("-i|--iops must be between 100 and 6000, inclusive.\nRun '{{.CommandName}} sl block volume-options' to check available options.", subs))
76-
77-
}
78-
if iops%100 != 0 {
79-
return slErr.NewInvalidUsageError(T("-i|--iops must be a multiple of 100.\nRun '{{.CommandName}} sl block volume-options' to check available options.", subs))
8074

75+
if cmd.Iops > 0 {
76+
if cmd.Iops%100 != 0 {
77+
return slErr.NewInvalidUsageError(T("-i|--iops must be a multiple of 100."))
8178
}
8279
}
8380

@@ -93,10 +90,12 @@ func (cmd *SnapshotOrderCommand) Run(args []string) error {
9390
return nil
9491
}
9592
}
96-
orderReceipt, err := cmd.StorageManager.OrderSnapshotSpace("block", volumeID, size, tier, iops, cmd.Upgrade)
93+
orderReceipt, err := cmd.StorageManager.OrderSnapshotSpace(
94+
cmd.GetStorageType(), volumeID, cmd.Size, cmd.Tier, cmd.Iops, cmd.Upgrade)
9795
if err != nil {
98-
return slErr.NewAPIError(T("Failed to order snapshot space for volume {{.VolumeID}}.Please verify your options and try again.\n",
99-
map[string]interface{}{"VolumeID": volumeID}), err.Error(), 2)
96+
return slErr.NewAPIError(
97+
T("Failed to order snapshot space for volume {{.VolumeID}}.Please verify your options and try again.\n",
98+
map[string]interface{}{"VolumeID": volumeID}), err.Error(), 2)
10099
}
101100

102101
if outputFormat == "JSON" {

plugin/commands/block/snapshot_order_test.go

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
1616
)
1717

18-
var _ = Describe("Snapshot order", func() {
18+
var _ = Describe("Block Snapshot order", func() {
1919
var (
2020
fakeUI *terminal.FakeUI
2121
cliCommand *block.SnapshotOrderCommand
@@ -34,40 +34,28 @@ var _ = Describe("Snapshot order", func() {
3434
})
3535

3636
Describe("Snapshot order", func() {
37-
Context("Snapshot order without volume id", func() {
38-
It("return error", func() {
37+
Context("Bad Usage", func() {
38+
It("No Volume ID", func() {
3939
err := testhelpers.RunCobraCommand(cliCommand.Command)
4040
Expect(err).To(HaveOccurred())
4141
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
4242
})
43-
})
44-
Context("Snapshot order with wrong volume id", func() {
45-
It("return error", func() {
46-
err := testhelpers.RunCobraCommand(cliCommand.Command, "abc")
43+
It("Bad Volume ID", func() {
44+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abc", "-s=100")
4745
Expect(err).To(HaveOccurred())
4846
Expect(err.Error()).To(ContainSubstring("Invalid input for 'Volume ID'. It must be a positive integer."))
4947
})
50-
})
51-
52-
Context("Snapshot order without -s", func() {
53-
It("return error", func() {
48+
It("No --size", func() {
5449
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234")
5550
Expect(err).To(HaveOccurred())
56-
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: [-s|--size] is required."))
57-
Expect(err.Error()).To(ContainSubstring("sl block volume-options' to get available options."))
51+
Expect(err.Error()).To(ContainSubstring(`required flag(s) "size" not set`))
5852
})
59-
})
60-
61-
Context("Snapshot order with wrong tier", func() {
62-
It("return error", func() {
53+
It("Bad Tier", func() {
6354
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "100", "-t", "0.3")
6455
Expect(err).To(HaveOccurred())
6556
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: [-t|--tier] is optional, options are: 0.25,2,4,10."))
6657
})
67-
})
68-
69-
Context("Snapshot order with -f and not continue", func() {
70-
It("return no error", func() {
58+
It("No confirmation", func() {
7159
fakeUI.Inputs("No")
7260
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "100", "-t", "0.25")
7361
Expect(err).NotTo(HaveOccurred())
@@ -91,14 +79,59 @@ var _ = Describe("Snapshot order", func() {
9179
},
9280
}, nil)
9381
})
94-
It("return no error", func() {
82+
It("Normal Order Happy Path", func() {
9583
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "100", "-t", "0.25", "-f")
9684
Expect(err).NotTo(HaveOccurred())
9785
Expect(fakeUI.Outputs()).To(ContainSubstring("Order 123456 was placed."))
9886
})
87+
It("Upgrade Order Happy Path", func() {
88+
err := testhelpers.RunCobraCommand(cliCommand.Command, "4567", "-s", "1000", "-t", "10", "-u", "-f")
89+
Expect(err).NotTo(HaveOccurred())
90+
Expect(fakeUI.Outputs()).To(ContainSubstring("Order 123456 was placed."))
91+
storage_type, volumeId, size, tier, iops, upgrade := FakeStorageManager.OrderSnapshotSpaceArgsForCall(0)
92+
Expect(storage_type).To(Equal("block"))
93+
Expect(volumeId).To(Equal(4567))
94+
Expect(size).To(Equal(1000))
95+
Expect(tier).To(Equal(10.0))
96+
Expect(iops).To(Equal(0))
97+
Expect(upgrade).To(BeTrue())
98+
})
9999
})
100100

101-
Context("Snapshot order with -f and continue and upgrade", func() {
101+
Context("Snapshot order with correct parameters but server API call fails", func() {
102+
BeforeEach(func() {
103+
FakeStorageManager.OrderSnapshotSpaceReturns(
104+
datatypes.Container_Product_Order_Receipt{}, errors.New("Internal Server Error"))
105+
})
106+
It("return no error", func() {
107+
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "100", "-t", "0.25", "-f")
108+
Expect(err).To(HaveOccurred())
109+
Expect(err.Error()).To(ContainSubstring("Failed to order snapshot space for volume 1234.Please verify your options and try again."))
110+
})
111+
})
112+
})
113+
})
114+
115+
var _ = Describe("File Snapshot order", func() {
116+
var (
117+
fakeUI *terminal.FakeUI
118+
cliCommand *block.SnapshotOrderCommand
119+
fakeSession *session.Session
120+
slCommand *metadata.SoftlayerStorageCommand
121+
FakeStorageManager *testhelpers.FakeStorageManager
122+
)
123+
BeforeEach(func() {
124+
fakeUI = terminal.NewFakeUI()
125+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
126+
FakeStorageManager = new(testhelpers.FakeStorageManager)
127+
slCommand = metadata.NewSoftlayerStorageCommand(fakeUI, fakeSession, "file")
128+
cliCommand = block.NewSnapshotOrderCommand(slCommand)
129+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
130+
cliCommand.StorageManager = FakeStorageManager
131+
})
132+
133+
Describe("Snapshot order", func() {
134+
Context("Snapshot order with -f and continue", func() {
102135
BeforeEach(func() {
103136
FakeStorageManager.OrderSnapshotSpaceReturns(datatypes.Container_Product_Order_Receipt{
104137
OrderId: sl.Int(123456),
@@ -114,16 +147,36 @@ var _ = Describe("Snapshot order", func() {
114147
},
115148
}, nil)
116149
})
117-
It("return no error", func() {
118-
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "100", "-t", "0.25", "-u", "-f")
150+
It("Normal Order Happy Path", func() {
151+
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "100", "-t", "0.25", "-f")
152+
Expect(err).NotTo(HaveOccurred())
153+
Expect(fakeUI.Outputs()).To(ContainSubstring("Order 123456 was placed."))
154+
storage_type, volumeId, size, tier, iops, upgrade := FakeStorageManager.OrderSnapshotSpaceArgsForCall(0)
155+
Expect(storage_type).To(Equal("file"))
156+
Expect(volumeId).To(Equal(1234))
157+
Expect(size).To(Equal(100))
158+
Expect(tier).To(Equal(0.25))
159+
Expect(iops).To(Equal(0))
160+
Expect(upgrade).To(BeFalse())
161+
})
162+
It("Upgrade Order Happy Path", func() {
163+
err := testhelpers.RunCobraCommand(cliCommand.Command, "4567", "-s", "1000", "-t", "10", "-u", "-f")
119164
Expect(err).NotTo(HaveOccurred())
120165
Expect(fakeUI.Outputs()).To(ContainSubstring("Order 123456 was placed."))
166+
storage_type, volumeId, size, tier, iops, upgrade := FakeStorageManager.OrderSnapshotSpaceArgsForCall(0)
167+
Expect(storage_type).To(Equal("file"))
168+
Expect(volumeId).To(Equal(4567))
169+
Expect(size).To(Equal(1000))
170+
Expect(tier).To(Equal(10.0))
171+
Expect(iops).To(Equal(0))
172+
Expect(upgrade).To(BeTrue())
121173
})
122174
})
123175

124176
Context("Snapshot order with correct parameters but server API call fails", func() {
125177
BeforeEach(func() {
126-
FakeStorageManager.OrderSnapshotSpaceReturns(datatypes.Container_Product_Order_Receipt{}, errors.New("Internal Server Error"))
178+
FakeStorageManager.OrderSnapshotSpaceReturns(
179+
datatypes.Container_Product_Order_Receipt{}, errors.New("Internal Server Error"))
127180
})
128181
It("return no error", func() {
129182
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "100", "-t", "0.25", "-f")

plugin/commands/block/snapshot_restore.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ func NewSnapshotRestoreCommand(sl *metadata.SoftlayerStorageCommand) *SnapshotRe
2525
cobraCmd := &cobra.Command{
2626
Use: "snapshot-restore " + T("IDENTIFIER") + " " + T("SNAPSHOT_ID"),
2727
Short: T("Restore {{.storageType}} volume using a given snapshot", sl.StorageI18n),
28-
Long: T(`${COMMAND_NAME} sl {{.storageType}} snapshot-restore VOLUME_ID SNAPSHOT_ID
29-
28+
Long: T(`
3029
EXAMPLE:
3130
${COMMAND_NAME} sl {{.storageType}} snapshot-restore 12345678 87654321
3231
This command restores volume with ID 12345678 from snapshot with ID 87654321.`, sl.StorageI18n),

0 commit comments

Comments
 (0)