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_wait(_BlockStorageInit, _VolumeWait): """Wait for volume to finish (default: --while creating)""" arguments = dict( timeout=argument.IntArgument('Wait limit in seconds (default: 60)', '--timeout', default=60), status_w=argument.StatusArgument('Wait while in status (%s)' % ','.join(volume_states), '--while', valid_states=volume_states), status_u=argument.StatusArgument('Wait until status is reached (%s)' % ','.join(volume_states), '--until', valid_states=volume_states), ) @errors.Generic.all def _run(self, volume_id): r = self.client.get_volume_details(volume_id) if self['status_u']: if r['status'] == self['status_u'].lower(): self.error('Volume %s: already in %s' % (volume_id, r['status'])) else: self.wait_until(volume_id, self['status_u'].lower(), timeout=self['timeout']) else: status_w = (self['status_w'] or '').lower() or 'creating' if r['status'] == status_w.lower(): self.wait_while(volume_id, status_w, timeout=self['timeout']) else: self.error('Volume %s status: %s' % (volume_id, r['status'])) def main(self, volume_id): super(self.__class__, self)._run() if all([self['status_w'], self['status_u']]): raise CLIInvalidArgument( 'Invalid argument combination', importance=2, details=[ 'Arguments %s and %s are mutually exclusive' % (self.arguments['status_w'].lvalue, self.arguments['status_u'].lvalue) ]) self._run(volume_id=volume_id)
def test_value(self): ia = argument.IntArgument(parsed_name='--ia') self.assertEqual(ia.value, None) for v in (1, 0, -1): ia.value = v self.assertEqual(ia.value, v) for v in ('1', '-1'): ia.value = v self.assertEqual(ia.value, int(v)) for v, err in (('invalid', errors.CLIError), (2.8, errors.CLIError), (923455555555555555555555555555555, errors.CLIError), (None, TypeError), (False, TypeError), ([1, 2, 3], TypeError)): try: ia.value = v except Exception as e: self.assertTrue(isinstance(e, err))
class volume_create(_BlockStorageInit, OptionalOutput): """Create a new volume""" arguments = dict( size=argument.IntArgument('Volume size in GB', '--size'), server_id=argument.ValueArgument('The server for the new volume', '--server-id'), name=argument.ValueArgument('Display name', '--name'), # src_volume_id=argument.ValueArgument( # 'Associate another volume to the new volume', # '--source-volume-id'), 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('Volume type', '--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'), ) required = ('size', 'server_id', 'name') @errors.Generic.all def _run(self, size, server_id, name): self.print_( self.client.create_volume( size, server_id, name, # source_volid=self['src_volume_id'], display_description=self['description'], snapshot_id=self['snapshot_id'], imageRef=self['image_id'], volume_type=self['volume_type'], metadata=self['metadata'], project=self['project_id']), self.print_dict) def main(self): super(self.__class__, self)._run() self._run(size=self['size'], server_id=self['server_id'], name=self['name'])