Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cmd/attach_dynamic_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts" //nolint:staticcheck
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
)

type AttachDynamicDiskCmd struct {
ui boshui.UI
director boshdir.Director
}

func NewAttachDynamicDiskCmd(ui boshui.UI, director boshdir.Director) AttachDynamicDiskCmd {
return AttachDynamicDiskCmd{ui: ui, director: director}
}

func (c AttachDynamicDiskCmd) Run(opts AttachDynamicDiskOpts) error {
instanceID := opts.Args.InstanceID.String()
diskName := opts.Args.DiskName

return c.director.AttachDynamicDisk(diskName, instanceID)
}
62 changes: 62 additions & 0 deletions cmd/attach_dynamic_disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd_test

import (
"errors"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/cloudfoundry/bosh-cli/v7/cmd"
"github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
fakedir "github.com/cloudfoundry/bosh-cli/v7/director/directorfakes"
fakeui "github.com/cloudfoundry/bosh-cli/v7/ui/fakes"
)

var _ = Describe("AttachDynamicDiskCmd", func() {
var (
ui *fakeui.FakeUI
director *fakedir.FakeDirector
command cmd.AttachDynamicDiskCmd
)

BeforeEach(func() {
ui = &fakeui.FakeUI{}
director = &fakedir.FakeDirector{}
command = cmd.NewAttachDynamicDiskCmd(ui, director)
})

Describe("Run", func() {
var attachOpts opts.AttachDynamicDiskOpts

BeforeEach(func() {
slug := boshdir.NewInstanceSlug("worker", "xyz789")

Check failure on line 33 in cmd/attach_dynamic_disk_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not properly formatted (goimports)

Check failure on line 33 in cmd/attach_dynamic_disk_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

File is not properly formatted (goimports)

attachOpts = opts.AttachDynamicDiskOpts{
Args: opts.AttachDynamicDiskArgs{
DiskName: "my-disk",
InstanceID: slug,
},
}
Comment thread
julian-hj marked this conversation as resolved.
Outdated
})
Comment thread
julian-hj marked this conversation as resolved.
Outdated

act := func() error { return command.Run(attachOpts) }

It("attaches the dynamic disk to the instance", func() {
err := act()
Expect(err).ToNot(HaveOccurred())

diskName, instanceID := director.AttachDynamicDiskArgsForCall(0)
Expect(diskName).To(Equal("my-disk"))
Expect(instanceID).To(Equal("worker/xyz789"))
})

It("returns error if attaching fails", func() {
director.AttachDynamicDiskReturns(errors.New("fake-err"))

err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})
})
})
15 changes: 15 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ func (c Cmd) Execute() (cmdErr error) {
case *OrphanDiskOpts:
return NewOrphanDiskCmd(deps.UI, c.director()).Run(*opts)

case *ProvideDiskOpts:
return NewProvideDiskCmd(deps.UI, c.director()).Run(*opts)

case *DetachDynamicDiskOpts:
return NewDetachDynamicDiskCmd(deps.UI, c.director()).Run(*opts)

case *DeleteDynamicDiskOpts:
return NewDeleteDynamicDiskCmd(deps.UI, c.director()).Run(*opts)

case *CreateDynamicDiskOpts:
return NewCreateDynamicDiskCmd(deps.UI, c.director()).Run(*opts)

case *AttachDynamicDiskOpts:
return NewAttachDynamicDiskCmd(deps.UI, c.director()).Run(*opts)

case *NetworksOpts:
return NewNetworksCmd(deps.UI, c.director()).Run(*opts)

Expand Down
28 changes: 28 additions & 0 deletions cmd/create_dynamic_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts" //nolint:staticcheck
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
)

type CreateDynamicDiskCmd struct {
ui boshui.UI
director boshdir.Director
}

func NewCreateDynamicDiskCmd(ui boshui.UI, director boshdir.Director) CreateDynamicDiskCmd {
return CreateDynamicDiskCmd{ui: ui, director: director}
}

func (c CreateDynamicDiskCmd) Run(opts CreateDynamicDiskOpts) error {
diskName := opts.Args.DiskName

diskCID, err := c.director.CreateDynamicDisk(diskName, opts.DiskPool, opts.Size, nil)
if err != nil {
return err
}

c.ui.PrintLinef("Dynamic disk '%s' created (CID: %s)", diskName, diskCID)
return nil
}
64 changes: 64 additions & 0 deletions cmd/create_dynamic_disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd_test

import (
"errors"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/cloudfoundry/bosh-cli/v7/cmd"
"github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
fakedir "github.com/cloudfoundry/bosh-cli/v7/director/directorfakes"
fakeui "github.com/cloudfoundry/bosh-cli/v7/ui/fakes"
)

var _ = Describe("CreateDynamicDiskCmd", func() {
var (
ui *fakeui.FakeUI
director *fakedir.FakeDirector
command cmd.CreateDynamicDiskCmd
)

BeforeEach(func() {
ui = &fakeui.FakeUI{}
director = &fakedir.FakeDirector{}
command = cmd.NewCreateDynamicDiskCmd(ui, director)
})

Describe("Run", func() {
var createOpts opts.CreateDynamicDiskOpts

BeforeEach(func() {
createOpts = opts.CreateDynamicDiskOpts{
Args: opts.CreateDynamicDiskArgs{DiskName: "my-disk"},
DiskPool: "large",
Size: 102400,
}
})

act := func() error { return command.Run(createOpts) }

It("creates a dynamic disk without attaching", func() {
director.CreateDynamicDiskReturns("disk-cid-456", nil)

err := act()
Expect(err).ToNot(HaveOccurred())

diskName, diskPool, sizeInMB, metadata := director.CreateDynamicDiskArgsForCall(0)
Expect(diskName).To(Equal("my-disk"))
Expect(diskPool).To(Equal("large"))
Expect(sizeInMB).To(Equal(102400))
Expect(metadata).To(BeNil())

Expect(ui.Said).To(ContainElement(ContainSubstring("my-disk")))
})

It("returns error if creation fails", func() {
director.CreateDynamicDiskReturns("", errors.New("fake-err"))

err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})
})
})
25 changes: 25 additions & 0 deletions cmd/delete_dynamic_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts" //nolint:staticcheck
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
)

type DeleteDynamicDiskCmd struct {
ui boshui.UI
director boshdir.Director
}

func NewDeleteDynamicDiskCmd(ui boshui.UI, director boshdir.Director) DeleteDynamicDiskCmd {
return DeleteDynamicDiskCmd{ui: ui, director: director}
}

func (c DeleteDynamicDiskCmd) Run(opts DeleteDynamicDiskOpts) error {
err := c.ui.AskForConfirmation()
if err != nil {
return err
}

return c.director.DeleteDynamicDisk(opts.Args.DiskName)
}
64 changes: 64 additions & 0 deletions cmd/delete_dynamic_disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd_test

import (
"errors"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/cloudfoundry/bosh-cli/v7/cmd"
"github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
fakedir "github.com/cloudfoundry/bosh-cli/v7/director/directorfakes"
fakeui "github.com/cloudfoundry/bosh-cli/v7/ui/fakes"
)

var _ = Describe("DeleteDynamicDiskCmd", func() {
var (
ui *fakeui.FakeUI
director *fakedir.FakeDirector
command cmd.DeleteDynamicDiskCmd
)

BeforeEach(func() {
ui = &fakeui.FakeUI{}
director = &fakedir.FakeDirector{}
command = cmd.NewDeleteDynamicDiskCmd(ui, director)
})

Describe("Run", func() {
var deleteOpts opts.DeleteDynamicDiskOpts

BeforeEach(func() {
deleteOpts = opts.DeleteDynamicDiskOpts{
Args: opts.DeleteDynamicDiskArgs{DiskName: "my-disk"},
}
})

act := func() error { return command.Run(deleteOpts) }

It("deletes the dynamic disk", func() {
err := act()
Expect(err).ToNot(HaveOccurred())

Expect(director.DeleteDynamicDiskArgsForCall(0)).To(Equal("my-disk"))
})

It("returns error if deletion fails", func() {
director.DeleteDynamicDiskReturns(errors.New("fake-err"))

err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})

It("does not delete if confirmation is rejected", func() {
ui.AskedConfirmationErr = errors.New("stop")

err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stop"))

Expect(director.DeleteDynamicDiskCallCount()).To(Equal(0))
})
})
})
25 changes: 25 additions & 0 deletions cmd/detach_dynamic_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts" //nolint:staticcheck
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
)

type DetachDynamicDiskCmd struct {
ui boshui.UI
director boshdir.Director
}

func NewDetachDynamicDiskCmd(ui boshui.UI, director boshdir.Director) DetachDynamicDiskCmd {
return DetachDynamicDiskCmd{ui: ui, director: director}
}

func (c DetachDynamicDiskCmd) Run(opts DetachDynamicDiskOpts) error {
err := c.ui.AskForConfirmation()
if err != nil {
return err
}

return c.director.DetachDynamicDisk(opts.Args.DiskName)
}
64 changes: 64 additions & 0 deletions cmd/detach_dynamic_disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd_test

import (
"errors"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/cloudfoundry/bosh-cli/v7/cmd"
"github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
fakedir "github.com/cloudfoundry/bosh-cli/v7/director/directorfakes"
fakeui "github.com/cloudfoundry/bosh-cli/v7/ui/fakes"
)

var _ = Describe("DetachDynamicDiskCmd", func() {
var (
ui *fakeui.FakeUI
director *fakedir.FakeDirector
command cmd.DetachDynamicDiskCmd
)

BeforeEach(func() {
ui = &fakeui.FakeUI{}
director = &fakedir.FakeDirector{}
command = cmd.NewDetachDynamicDiskCmd(ui, director)
})

Describe("Run", func() {
var detachOpts opts.DetachDynamicDiskOpts

BeforeEach(func() {
detachOpts = opts.DetachDynamicDiskOpts{
Args: opts.DetachDynamicDiskArgs{DiskName: "my-disk"},
}
})

act := func() error { return command.Run(detachOpts) }

It("detaches the dynamic disk", func() {
err := act()
Expect(err).ToNot(HaveOccurred())

Expect(director.DetachDynamicDiskArgsForCall(0)).To(Equal("my-disk"))
})

It("returns error if detaching fails", func() {
director.DetachDynamicDiskReturns(errors.New("fake-err"))

err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})

It("does not detach if confirmation is rejected", func() {
ui.AskedConfirmationErr = errors.New("stop")

err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stop"))

Expect(director.DetachDynamicDiskCallCount()).To(Equal(0))
})
})
})
Loading
Loading