@@ -14,6 +14,7 @@ import (
1414 "github.com/gophercloud/gophercloud/v2"
1515 "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools"
1616 "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/backups"
17+ "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/manageablevolumes"
1718 "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/qos"
1819 "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/snapshots"
1920 "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes"
@@ -779,3 +780,73 @@ func ReImage(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Vo
779780
780781 return nil
781782}
783+
784+ func Unmanage (t * testing.T , client * gophercloud.ServiceClient , volume * volumes.Volume ) error {
785+ t .Logf ("Attempting to unmanage volume %s" , volume .ID )
786+
787+ err := volumes .Unmanage (context .TODO (), client , volume .ID ).ExtractErr ()
788+ if err != nil {
789+ return err
790+ }
791+
792+ gophercloud .WaitFor (context .TODO (), func (ctx context.Context ) (bool , error ) {
793+ if _ , err := volumes .Get (ctx , client , volume .ID ).Extract (); err != nil {
794+ if _ , ok := err .(gophercloud.ErrResourceNotFound ); ok {
795+ return true , nil
796+ }
797+ return false , err
798+ }
799+ return false , nil
800+ })
801+
802+ t .Logf ("Successfully unmanaged volume %s" , volume .ID )
803+
804+ return nil
805+ }
806+
807+ func ManageExisting (t * testing.T , client * gophercloud.ServiceClient , volume * volumes.Volume ) (* volumes.Volume , error ) {
808+ t .Logf ("Attempting to manage existing volume %s" , volume .Name )
809+
810+ manageOpts := manageablevolumes.ManageExistingOpts {
811+ Host : volume .Host ,
812+ Ref : map [string ]string {
813+ "source-name" : fmt .Sprintf ("volume-%s" , volume .ID ),
814+ },
815+ Name : volume .Name ,
816+ AvailabilityZone : volume .AvailabilityZone ,
817+ Description : volume .Description ,
818+ VolumeType : volume .VolumeType ,
819+ Bootable : volume .Bootable == "true" ,
820+ Metadata : volume .Metadata ,
821+ }
822+
823+ managed , err := manageablevolumes .ManageExisting (context .TODO (), client , manageOpts ).Extract ()
824+ if err != nil {
825+ return managed , err
826+ }
827+
828+ ctx , cancel := context .WithTimeout (context .TODO (), 60 * time .Second )
829+ defer cancel ()
830+
831+ if err := volumes .WaitForStatus (ctx , client , managed .ID , "available" ); err != nil {
832+ return managed , err
833+ }
834+
835+ managed , err = volumes .Get (context .TODO (), client , managed .ID ).Extract ()
836+ if err != nil {
837+ return managed , err
838+ }
839+
840+ tools .PrintResource (t , managed )
841+ th .AssertEquals (t , managed .Host , volume .Host )
842+ th .AssertEquals (t , managed .Name , volume .Name )
843+ th .AssertEquals (t , managed .AvailabilityZone , volume .AvailabilityZone )
844+ th .AssertEquals (t , managed .Description , volume .Description )
845+ th .AssertEquals (t , managed .VolumeType , volume .VolumeType )
846+ th .AssertEquals (t , managed .Bootable , volume .Bootable )
847+ th .AssertDeepEquals (t , managed .Metadata , volume .Metadata )
848+
849+ t .Logf ("Successfully managed existing volume %s" , managed .ID )
850+
851+ return managed , nil
852+ }
0 commit comments