def get(self, artifact_id, type_name=None, type_version=None, show_level=None): """Get information about an artifact. :param artifact_id: ID of the artifact to get. :param show_level: value of datalization. Possible values: "none", "basic", "direct", "transitive" """ type_name, type_version = self._check_type_params( type_name, type_version) url = glare_urls['update_get_delete'].format(version=self.version, type_name=type_name, type_version=type_version, artifact_id=artifact_id) if show_level: if show_level not in ArtifactType.supported_show_levels: msg = "Invalid show level: %s" % show_level raise exc.HTTPBadRequest(msg) url += '?show_level=%s' % show_level resp, body = self.http_client.get(url) return ArtifactType(**body)
def update(self, artifact_id, type_name=None, type_version=None, remove_props=None, **kwargs): """Update attributes of an artifact. :param artifact_id: ID of the artifact to modify. :param remove_props: List of property names to remove :param \*\*kwargs: Artifact attribute names and their new values. """ type_name, type_version = self._check_type_params( type_name, type_version) url = glare_urls['update_get_delete'].format(version=self.version, type_name=type_name, type_version=type_version, artifact_id=artifact_id) hdrs = {'Content-Type': 'application/openstack-images-v2.1-json-patch'} artifact_obj = self.get(artifact_id, type_name, type_version) changes = [] if remove_props: for prop in remove_props: if prop in ArtifactType.generic_properties: msg = "Generic properties cannot be removed" raise exc.HTTPBadRequest(msg) if prop not in kwargs: changes.append({'op': 'remove', 'path': '/' + prop}) for prop in kwargs: if prop in artifact_obj.generic_properties: op = 'add' if getattr(artifact_obj, prop) is None else 'replace' elif prop in artifact_obj.type_specific_properties: if artifact_obj.type_specific_properties[prop] is None: op = 'add' else: op = 'replace' else: msg = ("Property '%s' doesn't exist in type '%s' with version" " '%s'" % (prop, type_name, type_version)) raise exc.HTTPBadRequest(msg) changes.append({ 'op': op, 'path': '/' + prop, 'value': kwargs[prop] }) resp, body = self.http_client.patch(url, headers=hdrs, data=changes) return ArtifactType(**body)
def active(self, artifact_id, type_name=None, type_version=None): """Set artifact status to 'active'. :param artifact_id: ID of the artifact to get. """ type_name, type_version = self._check_type_params( type_name, type_version) url = '/v0.1/artifacts/%s/v%s/%s/publish' % (type_name, type_version, artifact_id) resp, body = self.http_client.post(url) return ArtifactType(**body)
def active(self, artifact_id, type_name=None, type_version=None): """Set artifact status to 'active'. :param artifact_id: ID of the artifact to get. """ type_name, type_version = self._check_type_params( type_name, type_version) url = glare_urls['publish'].format(version=self.version, type_name=type_name, type_version=type_version, artifact_id=artifact_id) resp, body = self.http_client.post(url) return ArtifactType(**body)
def create(self, name, version, type_name=None, type_version=None, **kwargs): """Create an artifact of given type and version. :param name: name of creating artifact. :param version: semver string describing an artifact version """ type_name, type_version = self._check_type_params( type_name, type_version) kwargs.update({'name': name, 'version': version}) url = '/v0.1/artifacts/%s/v%s/drafts' % (type_name, type_version) resp, body = self.http_client.post(url, data=kwargs) return ArtifactType(**body)
def paginate(url, page_size, limit=None): next_url = url while True: if limit and page_size > limit: next_url = next_url.replace("limit=%s" % page_size, "limit=%s" % limit) resp, body = self.http_client.get(next_url) for artifact in body['artifacts']: yield ArtifactType(**artifact) if limit: limit -= 1 if limit <= 0: raise StopIteration try: next_url = body['next'] except KeyError: return