diff --git a/commands/nfs.go b/commands/nfs.go index 3ba253e44..d7b19e7aa 100644 --- a/commands/nfs.go +++ b/commands/nfs.go @@ -90,6 +90,14 @@ doctl nfs list --region 'atl1' --format ID,Name,Size,Status` cmdNfsDetach.Example = `doctl nfs detach --region 'atl1' --id b050990d-4337-4a9d-9c8d-9f759a83936a --vpc_id example-vpc-id` + cmdNfsReassign := CmdBuilder(cmd, nfsReassign, "reassign [flags]", "Reassign an NFS share between VPCs", "Reassigns an NFS share from one VPC to another with the given ID.", Writer) + AddStringFlag(cmdNfsReassign, "id", "", "", "the ID of the NFS share", requiredOpt()) + AddStringFlag(cmdNfsReassign, "old-vpc-id", "", "", "the id of the VPC we want to detach NFS share from", requiredOpt()) + AddStringFlag(cmdNfsReassign, "new-vpc-id", "", "", "the id of the VPC we want to attach NFS share to", requiredOpt()) + AddBoolFlag(cmdNfsReassign, doctl.ArgCommandWait, "", false, "Wait for action to complete") + cmdNfsReassign.Example = + `doctl nfs reassign --id b050990d-4337-4a9d-9c8d-9f759a83936a --old-vpc-id old-vpc-id --new-vpc-id new-vpc-id` + cmdNfsSwitchPerformanceTier := CmdBuilder(cmd, nfsSwitchPerformanceTier, "switch-performance-tier [flags]", "Switch the performance tier of an NFS share", "Switch the performance tier of an NFS share with the given ID and tier.", Writer) AddStringFlag(cmdNfsSwitchPerformanceTier, "id", "", "", "the ID of the NFS share", requiredOpt()) AddStringFlag(cmdNfsSwitchPerformanceTier, "performance-tier", "", "", "the performance tier of the NFS share", requiredOpt()) @@ -410,6 +418,41 @@ func nfsDetach(c *CmdConfig) error { return c.Display(item) } +func nfsReassign(c *CmdConfig) error { + id, err := c.Doit.GetString(c.NS, "id") + if err != nil { + return err + } + oldVpcID, err := c.Doit.GetString(c.NS, "old-vpc-id") + if err != nil { + return err + } + newVpcID, err := c.Doit.GetString(c.NS, "new-vpc-id") + if err != nil { + return err + } + + action, err := c.NfsActions().Reassign(id, oldVpcID, newVpcID) + if err != nil { + return err + } + + wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait) + if err != nil { + return err + } + + if wait { + _, err := actionWait(c, action.ID, 5) + if err != nil { + return err + } + } + + item := &displayers.NfsAction{NfsActions: []do.NfsAction{*action}} + return c.Display(item) +} + func nfsSwitchPerformanceTier(c *CmdConfig) error { id, err := c.Doit.GetString(c.NS, "id") if err != nil { diff --git a/commands/nfs_test.go b/commands/nfs_test.go index 175a1586a..21d7eb887 100644 --- a/commands/nfs_test.go +++ b/commands/nfs_test.go @@ -59,7 +59,7 @@ var ( func TestNfsCommand(t *testing.T) { cmd := Nfs() assert.NotNil(t, cmd) - assertCommandNames(t, cmd, "create", "list", "get", "delete", "snapshot", "resize", "attach", "detach", "switch-performance-tier") + assertCommandNames(t, cmd, "create", "list", "get", "delete", "snapshot", "resize", "attach", "detach", "reassign", "switch-performance-tier") } func TestRunNfsCreate(t *testing.T) { @@ -495,6 +495,59 @@ func TestRunNfsDetach(t *testing.T) { } } +func TestRunNfsReassign(t *testing.T) { + testCases := []struct { + name string + id string + oldVpcID string + newVpcID string + wait bool + expectErr bool + }{ + { + name: "success without wait", + id: testId, + oldVpcID: "vpc-old", + newVpcID: "vpc-new", + wait: false, + expectErr: false, + }, + { + name: "success with wait", + id: testId, + oldVpcID: "vpc-old", + newVpcID: "vpc-new", + wait: true, + expectErr: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + withTestClient(t, func(config *CmdConfig, tm *tcMocks) { + if !tc.expectErr { + tm.nfsActions.EXPECT().Reassign(tc.id, tc.oldVpcID, tc.newVpcID).Return(&testNfsAction, nil) + if tc.wait { + tm.actions.EXPECT().Get(testNfsAction.ID).Return(&testAction, nil) + } + } + + config.Doit.Set(config.NS, "id", tc.id) + config.Doit.Set(config.NS, "old-vpc-id", tc.oldVpcID) + config.Doit.Set(config.NS, "new-vpc-id", tc.newVpcID) + config.Doit.Set(config.NS, "wait", tc.wait) + + err := nfsReassign(config) + if tc.expectErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + }) + } +} + func TestRunNfsSwitchPerformanceTier(t *testing.T) { testCases := []struct { name string diff --git a/do/mocks/NfsActionsService.go b/do/mocks/NfsActionsService.go index 68207c962..2a7da8159 100644 --- a/do/mocks/NfsActionsService.go +++ b/do/mocks/NfsActionsService.go @@ -70,6 +70,21 @@ func (mr *MockNfsActionsServiceMockRecorder) Detach(id, vpcID, region any) *gomo return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Detach", reflect.TypeOf((*MockNfsActionsService)(nil).Detach), id, vpcID, region) } +// Reassign mocks base method. +func (m *MockNfsActionsService) Reassign(id, oldVpcID, newVpcID string) (*do.NfsAction, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Reassign", id, oldVpcID, newVpcID) + ret0, _ := ret[0].(*do.NfsAction) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Reassign indicates an expected call of Reassign. +func (mr *MockNfsActionsServiceMockRecorder) Reassign(id, oldVpcID, newVpcID any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Reassign", reflect.TypeOf((*MockNfsActionsService)(nil).Reassign), id, oldVpcID, newVpcID) +} + // Resize mocks base method. func (m *MockNfsActionsService) Resize(id string, size uint64, region string) (*do.NfsAction, error) { m.ctrl.T.Helper() diff --git a/do/nfs_actions.go b/do/nfs_actions.go index 78093c99e..e89fcfe86 100644 --- a/do/nfs_actions.go +++ b/do/nfs_actions.go @@ -33,6 +33,7 @@ type NfsActionsService interface { Snapshot(id, name, region string) (*NfsAction, error) Attach(id, vpcID, region string) (*NfsAction, error) Detach(id, vpcID, region string) (*NfsAction, error) + Reassign(id, oldVpcID, newVpcID string) (*NfsAction, error) SwitchPerformanceTier(id, tier string) (*NfsAction, error) } @@ -81,6 +82,14 @@ func (s *nfsActionsService) Detach(id, vpcID, region string) (*NfsAction, error) return &NfsAction{NfsAction: action}, nil } +func (s *nfsActionsService) Reassign(id, oldVpcID, newVpcID string) (*NfsAction, error) { + action, _, err := s.client.NfsActions.Reassign(context.TODO(), id, oldVpcID, newVpcID) + if err != nil { + return nil, err + } + return &NfsAction{NfsAction: action}, nil +} + func (s *nfsActionsService) SwitchPerformanceTier(id, tier string) (*NfsAction, error) { action, _, err := s.client.NfsActions.SwitchPerformanceTier(context.TODO(), id, tier) if err != nil {