class volume_create(_BlockStorageInit, OptionalOutput, _VolumeWait): """Create a new volume""" arguments = dict( size=argument.IntArgument('Volume size in GB', '--size'), server_id=argument.ValueArgument('The server to attach the volume to', '--server-id'), name=argument.ValueArgument('Display name', '--name'), description=argument.ValueArgument('Volume description', '--description'), snapshot_id=argument.ValueArgument( 'Associate a snapshot to the new volume', '--snapshot-id'), image_id=argument.ValueArgument('Associate an image to the new volume', '--image-id'), volume_type=argument.ValueArgument( 'default: if combined with --server-id, the default is ' 'automatically configured to match the server, otherwise it is ' 'ext_archipelago', '--volume-type'), metadata=argument.KeyValueArgument( 'Metadata of key=value form (can be repeated)', '--metadata'), project_id=argument.ValueArgument('Assign volume to a project', '--project-id'), wait=argument.FlagArgument( 'Wait volume to be created and ready for use', ('-w', '--wait')), ) required = ('size', 'name') @errors.Generic.all def _run(self, size, name): # Figure out volume type volume_type = self['volume_type'] if not (self['server_id'] or volume_type): for vtype in self.client.list_volume_types(): if vtype['name'] in ('ext_archipelago'): volume_type = vtype['id'] break r = self.client.create_volume(size, name, server_id=self['server_id'], display_description=self['description'], snapshot_id=self['snapshot_id'], imageRef=self['image_id'], volume_type=volume_type, metadata=self['metadata'], project=self['project_id']) self.print_dict(r) r = self.client.get_volume_details(r['id']) if self['wait'] and r['status'] != 'in_use': self.wait_while(r['id'], r['status']) r = self.client.get_volume_details(r['id']) if r['status'] != 'in_use': exit(1) def main(self): super(self.__class__, self)._run() self._run(size=self['size'], name=self['name'])
class volume_list(_BlockStorageInit, OptionalOutput): """List volumes""" arguments = dict(detail=argument.FlagArgument('show detailed output', ('-l', '--details')), ) @errors.Generic.all def _run(self): self.print_(self.client.list_volumes(detail=self['detail'])) def main(self): super(self.__class__, self)._run() self._run()
class volume_delete(_BlockStorageInit, _VolumeWait): """Delete a volume""" arguments = dict(wait=argument.FlagArgument('Wait until deleted', ('-w', '--wait')), ) @errors.Generic.all def _run(self, volume_id): r = self.client.get_volume_details(volume_id) self.client.delete_volume(volume_id) if self['wait']: try: self.wait_while(volume_id, r['status']) r = self.client.get_volume_details(volume_id) if r['status'] != 'deleted': exit(1) except ClientError as ce: if ce.status not in (404, ): raise def main(self, volume_id): super(self.__class__, self)._run() self._run(volume_id=volume_id)
def test___init__(self, arg): help, pname, default = 'help', 'pname', 'default' fa = argument.FlagArgument(help, pname, default) self.assertTrue(isinstance(fa, argument.FlagArgument)) arg.assert_called_once_with(0, help, pname, default)