def test_exception_to_str(self): class FakeException(Exception): def __str__(self): raise UnicodeError() ret = utils.exception_to_str(Exception("error message")) self.assertEqual(ret, "error message") ret = utils.exception_to_str(Exception("\xa5 error message")) self.assertEqual(ret, " error message") ret = utils.exception_to_str(FakeException("\xa5 error message")) self.assertEqual(ret, "Caught '%(exception)s' exception." % {"exception": "FakeException"})
def test_exception_to_str(self): class FakeException(Exception): def __str__(self): raise UnicodeError() ret = utils.exception_to_str(Exception('error message')) self.assertEqual(ret, 'error message') ret = utils.exception_to_str(Exception('\xa5 error message')) self.assertEqual(ret, ' error message') ret = utils.exception_to_str(FakeException('\xa5 error message')) self.assertEqual(ret, "Caught '%(exception)s' exception." % {'exception': 'FakeException'})
def test_exception_to_str(self): class FakeException(Exception): def __str__(self): raise UnicodeError() ret = utils.exception_to_str(Exception('error message')) self.assertEqual('error message', ret) ret = utils.exception_to_str(Exception('\xa5 error message')) self.assertEqual(' error message', ret) ret = utils.exception_to_str(FakeException('\xa5 error message')) self.assertEqual("Caught '%(exception)s' exception." % {'exception': 'FakeException'}, ret)
def main(): try: OpenStackImagesShell().main(map(encodeutils.safe_decode, sys.argv[1:])) except KeyboardInterrupt: utils.exit('... terminating glance client', exit_code=130) except Exception as e: utils.exit(utils.exception_to_str(e))
def update(self, image_id, remove_props=None, **kwargs): """ Update attributes of an image. :param image_id: ID of the image to modify. :param remove_props: List of property names to remove :param \*\*kwargs: Image attribute names and their new values. """ image = self.get(image_id) for (key, value) in kwargs.items(): try: setattr(image, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) if remove_props is not None: cur_props = image.keys() new_props = kwargs.keys() #NOTE(esheffield): Only remove props that currently exist on the # image and are NOT in the properties being updated / added props_to_remove = set(cur_props).intersection( set(remove_props).difference(new_props)) for key in props_to_remove: delattr(image, key) url = '/v2/images/%s' % image_id hdrs = {'Content-Type': 'application/openstack-images-v2.1-json-patch'} self.http_client.patch(url, headers=hdrs, data=image.patch) #NOTE(bcwaldon): calling image.patch doesn't clear the changes, so # we need to fetch the image again to get a clean history. This is # an obvious optimization for warlock return self.get(image_id)
def update(self, image_id, remove_props=None, **kwargs): """ Update attributes of an image. :param image_id: ID of the image to modify. :param remove_props: List of property names to remove :param **kwargs: Image attribute names and their new values. """ image = self.get(image_id) for (key, value) in kwargs.items(): try: setattr(image, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) if remove_props is not None: cur_props = image.keys() new_props = kwargs.keys() #NOTE(esheffield): Only remove props that currently exist on the # image and are NOT in the properties being updated / added props_to_remove = set(cur_props).intersection( set(remove_props).difference(new_props)) for key in props_to_remove: delattr(image, key) url = '/v2/images/%s' % image_id hdrs = {'Content-Type': 'application/openstack-images-v2.1-json-patch'} self.http_client.patch(url, headers=hdrs, data=image.patch) #NOTE(bcwaldon): calling image.patch doesn't clear the changes, so # we need to fetch the image again to get a clean history. This is # an obvious optimization for warlock return self.get(image_id)
def update(self, namespace, object_name, **kwargs): """Update an object. :param namespace: Name of a namespace the object belongs. :param prop_name: Name of an object (old one). :param kwargs: Unpacked object. """ obj = self.get(namespace, object_name) for (key, value) in kwargs.items(): try: setattr(obj, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) # Remove read-only parameters. read_only = ['schema', 'updated_at', 'created_at'] for elem in read_only: if elem in namespace: del namespace[elem] url = '/v2/metadefs/namespaces/{0}/objects/{1}'.format(namespace, object_name) self.http_client.put(url, data=obj) return self.get(namespace, obj.name)
def update(self, namespace, object_name, **kwargs): """Update an object. :param namespace: Name of a namespace the object belongs. :param prop_name: Name of an object (old one). :param kwargs: Unpacked object. """ obj = self.get(namespace, object_name) for (key, value) in kwargs.items(): try: setattr(obj, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) # Remove read-only parameters. read_only = ['schema', 'updated_at', 'created_at'] for elem in read_only: if elem in obj: del obj[elem] url = '/v2/metadefs/namespaces/{0}/objects/{1}'.format(namespace, object_name) self.http_client.put(url, data=obj) return self.get(namespace, obj.name)
def update(self, namespace, tag_name, **kwargs): """Update a tag. :param namespace: Name of a namespace the Tag belongs. :param prop_name: Name of the Tag (old one). :param kwargs: Unpacked tag. """ tag = self.get(namespace, tag_name) for (key, value) in kwargs.items(): try: setattr(tag, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) # Remove read-only parameters. read_only = ['updated_at', 'created_at'] for elem in read_only: if elem in tag: del tag[elem] url = '/v2/metadefs/namespaces/{0}/tags/{1}'.format(namespace, tag_name) self.http_client.put(url, data=tag) return self.get(namespace, tag.name)
def main(): try: OpenStackImagesShell().main(map(strutils.safe_decode, sys.argv[1:])) except KeyboardInterrupt: print('... terminating glance client', file=sys.stderr) sys.exit(1) except Exception as e: print(utils.exception_to_str(e), file=sys.stderr) sys.exit(1)
def create(self, **kwargs): """Create an image.""" url = '/v2/images' image = self.model() for (key, value) in kwargs.items(): try: setattr(image, key, value) except warlock.InvalidOperation, e: raise TypeError(utils.exception_to_str(e))
def associate(self, namespace, **kwargs): """Associate a resource type with a namespace.""" try: res_type = self.model(kwargs) except (warlock.InvalidOperation, ValueError) as e: raise TypeError(utils.exception_to_str(e)) url = '/v2/metadefs/namespaces/{0}/resource_types'.format(namespace, res_type) resp, body = self.http_client.post(url, data=res_type) body.pop('self', None) return self.model(**body)
def create(self, **kwargs): """Create a namespace. :param kwargs: Unpacked namespace object. """ url = '/v2/metadefs/namespaces' try: namespace = self.model(kwargs) except (warlock.InvalidOperation, ValueError) as e: raise TypeError(utils.exception_to_str(e)) resp, body = self.http_client.post(url, data=namespace) body.pop('self', None) return self.model(**body)
def create(self, namespace, **kwargs): """Create an object. :param namespace: Name of a namespace the object belongs. :param kwargs: Unpacked object. """ try: obj = self.model(kwargs) except (warlock.InvalidOperation, ValueError) as e: raise TypeError(utils.exception_to_str(e)) url = '/v2/metadefs/namespaces/{0}/objects'.format(namespace) resp, body = self.http_client.post(url, data=obj) body.pop('self', None) return self.model(**body)
def create(self, **kwargs): """Create an image.""" url = '/v2/images' image = self.model() for (key, value) in kwargs.items(): try: setattr(image, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) resp, body = self.http_client.json_request('POST', url, body=image) #NOTE(esheffield): remove 'self' for now until we have an elegant # way to pass it into the model constructor without conflict body.pop('self', None) return self.model(**body)
def create(self, **kwargs): """Create an image.""" url = '/v2/images' image = self.model() for (key, value) in kwargs.items(): try: setattr(image, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) resp, body = self.http_client.post(url, data=image) #NOTE(esheffield): remove 'self' for now until we have an elegant # way to pass it into the model constructor without conflict body.pop('self', None) return self.model(**body)
def create(self, **kwargs): """Create a new task.""" url = '/v2/tasks' task = self.model() for (key, value) in kwargs.items(): try: setattr(task, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) resp, body = self.http_client.post(url, data=task) #NOTE(flwang): remove 'self' for now until we have an elegant # way to pass it into the model constructor without conflict body.pop('self', None) return self.model(**body)
def update(self, namespace, prop_name, **kwargs): """Update a property. :param namespace: Name of a namespace the property belongs. :param prop_name: Name of a property (old one). :param kwargs: Unpacked property object. """ prop = self.get(namespace, prop_name) for (key, value) in kwargs.items(): try: setattr(prop, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) url = '/v2/metadefs/namespaces/{0}/properties/{1}'.format(namespace, prop_name) self.http_client.put(url, data=prop) return self.get(namespace, prop.name)
def update(self, namespace_name, **kwargs): """Update a namespace. :param namespace_name: Name of a namespace (old one). :param kwargs: Unpacked namespace object. """ namespace = self.get(namespace_name) for (key, value) in six.iteritems(kwargs): try: setattr(namespace, key, value) except warlock.InvalidOperation as e: raise TypeError(utils.exception_to_str(e)) # Remove read-only parameters. read_only = ['schema', 'updated_at', 'created_at'] for elem in read_only: if elem in namespace: del namespace[elem] url = '/v2/metadefs/namespaces/{0}'.format(namespace_name) self.http_client.put(url, data=namespace) return self.get(namespace.namespace)
def create_multiple(self, namespace, **kwargs): """Create the list of tags. :param namespace: Name of a namespace to which the Tags belong. :param kwargs: list of tags. """ tag_names = kwargs.pop('tags', []) md_tag_list = [] for tag_name in tag_names: try: md_tag_list.append(self.model(name=tag_name)) except (warlock.InvalidOperation) as e: raise TypeError(utils.exception_to_str(e)) tags = {'tags': md_tag_list} url = '/v2/metadefs/namespaces/{0}/tags'.format(namespace) resp, body = self.http_client.post(url, data=tags) body.pop('self', None) for tag in body['tags']: yield self.model(tag)