def get_urls(self, sot): sf = service_filter.ServiceFilter(service_type='compute') exp = [ "http://compute.region0.public/v1.1", "http://compute.region2.public/v1", "http://compute.region1.public/v2.0" ] self.assertEqual(exp, sot.get_urls(sf)) sf = service_filter.ServiceFilter(service_type='image') self.assertEqual(["http://image.region1.public/v2"], sot.get_urls(sf)) sf = service_filter.ServiceFilter(service_type='identity') self.assertEqual(["http://identity.region1.public/v1.1/123123"], sot.get_urls(sf)) sf = service_filter.ServiceFilter(service_type='object-store') self.assertEqual(["http://object-store.region1.public/"], sot.get_urls(sf)) sf = service_filter.ServiceFilter(service_type='object-store', version='v9') self.assertEqual(["http://object-store.region1.public/v9"], sot.get_urls(sf)) sf = service_filter.ServiceFilter(service_type='object-store') osf = object_store_service.ObjectStoreService() sf = sf.join(osf) self.assertEqual(["http://object-store.region1.public/v1"], sot.get_urls(sf))
def __init__(self, plugins=None): """User preference for each service. :param list plugins: List of entry point namespaces to load. Create a new :class:`~openstack.profile.Profile` object with no preferences defined, but knowledge of the services. Services are identified by their service type, e.g.: 'identity', 'compute', etc. """ self._preferences = {} self._services = {} self._add_service(cluster_service.ClusterService()) self._add_service(compute_service.ComputeService()) self._add_service(database_service.DatabaseService()) self._add_service(identity_service.IdentityService()) self._add_service(image_service.ImageService()) self._add_service(metric_service.MetricService()) self._add_service(network_service.NetworkService()) self._add_service(object_store_service.ObjectStoreService()) self._add_service(orchestration_service.OrchestrationService()) self._add_service(key_management_service.KeyManagementService()) self._add_service(telemetry_service.TelemetryService()) self._add_service(block_store_service.BlockStoreService()) self._add_service(message_service.MessageService()) if plugins: for plugin in plugins: self._load_plugin(plugin) self.service_names = sorted(self._services.keys())
class BulkDelete(resource.Resource): base_path = "/?bulk-delete" service = object_store_service.ObjectStoreService() allow_delete = True #: The number of items that weren't found not_found = resource.prop("Number Not Found") #: The response code returned by the bulk delete operation response_code = resource.prop("Response Status") #: A list of errors that occurred errors = resource.prop("Errors") #: The number of objects deleted deleted = resource.prop("Number Deleted") #: Any additional response body response_body = resource.prop("Response Body") @classmethod def delete(cls, session, body): """Issue a delete call on the session :param str body: The body of the bulk delete request. The API works on a text/plain list of lines in the format /container_name (to delete empty containers) or /container_name/object_name (to delete objects in a container) :rtype:`~rackspace.object_store.v1.bulk_delete.BulkDelete` """ resp = session.delete(cls.base_path, service=cls.service, data=body).body return cls.existing(**resp)
def __init__(self, plugins=None): """User preference for each service. :param plugins: List of entry point namespaces to load. Create a new :class:`~openstack.profile.Profile` object with no preferences defined, but knowledge of the services. Services are identified by their service type, e.g.: 'identity', 'compute', etc. """ self._services = {} self._add_service(baremetal_service.BaremetalService(version="v1")) self._add_service( block_storage_service.BlockStorageService(version="v2")) self._add_service(clustering_service.ClusteringService(version="v1")) self._add_service(compute_service.ComputeService(version="v2")) self._add_service(database_service.DatabaseService(version="v1")) self._add_service(identity_service.IdentityService(version="v3")) self._add_service(image_service.ImageService(version="v2")) self._add_service(key_manager_service.KeyManagerService(version="v1")) self._add_service(lb_service.LoadBalancerService(version="v2")) self._add_service(message_service.MessageService(version="v1")) self._add_service(network_service.NetworkService(version="v2")) self._add_service( object_store_service.ObjectStoreService(version="v1")) self._add_service( orchestration_service.OrchestrationService(version="v1")) self._add_service(workflow_service.WorkflowService(version="v2")) self.service_keys = sorted(self._services.keys())
class BaseResource(resource.Resource): service = object_store_service.ObjectStoreService() commit_method = 'POST' create_method = 'PUT' #: Metadata stored for this resource. *Type: dict* metadata = dict() _custom_metadata_prefix = None _system_metadata = dict() def _calculate_headers(self, metadata): headers = {} for key in metadata: if key in self._system_metadata.keys(): header = self._system_metadata[key] elif key in self._system_metadata.values(): header = key else: if key.startswith(self._custom_metadata_prefix): header = key else: header = self._custom_metadata_prefix + key headers[header] = metadata[key] return headers def set_metadata(self, session, metadata): request = self._prepare_request() response = session.post(request.url, headers=self._calculate_headers(metadata)) self._translate_response(response, has_body=False) response = session.head(request.url) self._translate_response(response, has_body=False) return self def delete_metadata(self, session, keys): request = self._prepare_request() headers = {key: '' for key in keys} response = session.post(request.url, headers=self._calculate_headers(headers)) exceptions.raise_from_response( response, error_message="Error deleting metadata keys") return self def _set_metadata(self, headers): self.metadata = dict() for header in headers: if header.startswith(self._custom_metadata_prefix): key = header[len(self._custom_metadata_prefix):].lower() self.metadata[key] = headers[header] def _translate_response(self, response, has_body=None, error_message=None): super(BaseResource, self)._translate_response(response, has_body=has_body, error_message=error_message) self._set_metadata(response.headers)
def test_service(self): sot = object_store_service.ObjectStoreService() self.assertEqual('object-store', sot.service_type) self.assertEqual('public', sot.interface) self.assertIsNone(sot.region) self.assertIsNone(sot.service_name) self.assertEqual(1, len(sot.valid_versions)) self.assertEqual('v1', sot.valid_versions[0].module) self.assertEqual('v1', sot.valid_versions[0].path)
def __init__(self): """Preferences for each service. Create a new :class:`~openstack.profile.Profile` object with no preferences defined, but knowledge of the services. Services are identified by their service type, e.g.: 'identity', 'compute', etc. """ self._preferences = {} self._services = {} """ NOTE(thowe): We should probably do something more clever here rather than brute force create all the services. Maybe use entry points or something, but I'd like to leave that work for another commit. """ serv = cluster_service.ClusterService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = compute_service.ComputeService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = database_service.DatabaseService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = identity_service.IdentityService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = image_service.ImageService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = metric_service.MetricService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = network_service.NetworkService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = object_store_service.ObjectStoreService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = orchestration_service.OrchestrationService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = keystore_service.KeystoreService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = telemetry_service.TelemetryService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = block_store_service.BlockStoreService() serv.set_visibility(None) self._services[serv.service_type] = serv serv = message_service.MessageService() serv.set_visibility(None) self._services[serv.service_type] = serv self.service_names = sorted(self._services.keys())
def test_get_versions(self): sot = catalog.ServiceCatalog(common.TEST_SERVICE_CATALOG_V3) service = compute_service.ComputeService() self.assertEqual(['v1.1', 'v1', 'v2.0'], sot.get_versions(service)) service = identity_service.IdentityService() self.assertEqual(['v1.1'], sot.get_versions(service)) service = image_service.ImageService() self.assertEqual(['v2'], sot.get_versions(service)) service = network_service.NetworkService() self.assertEqual(None, sot.get_versions(service)) service = object_store_service.ObjectStoreService() self.assertEqual([], sot.get_versions(service))
def __init__(self, plugins=None): """User preference for each service. :param plugins: List of entry point namespaces to load. Create a new :class:`~openstack.profile.Profile` object with no preferences defined, but knowledge of the services. Services are identified by their service type, e.g.: 'identity', 'compute', etc. """ self._services = {} self._add_service(anti_ddos_service.AntiDDosService(version="v1")) self._add_service(alarm_service.AlarmService(version="v2")) self._add_service(bare_metal_service.BareMetalService(version="v1")) self._add_service(block_store_service.BlockStoreService(version="v2")) self._add_service(cluster_service.ClusterService(version="v1")) self._add_service(compute_service.ComputeService(version="v2")) self._add_service(cts_service.CTSService(version="v1")) self._add_service(database_service.DatabaseService(version="v1")) self._add_service(dms_service.DMSService(version="v1")) self._add_service(identity_service.IdentityService(version="v3")) self._add_service(image_service.ImageService(version="v2")) self._add_service(key_manager_service.KeyManagerService(version="v1")) self._add_service(kms_service.KMSService(version="v1")) self._add_service(lb_service.LoadBalancerService(version="v1")) self._add_service(maas_service.MaaSService(version="v1")) self._add_service(message_service.MessageService(version="v1")) self._add_service(network_service.NetworkService(version="v2")) self._add_service( object_store_service.ObjectStoreService(version="v1")) self._add_service( orchestration_service.OrchestrationService(version="v1")) self._add_service(rds_service.RDSService(version="v1")) self._add_service(rds_os_service.RDSService(version="v1")) self._add_service(smn_service.SMNService(version="v2")) self._add_service(telemetry_service.TelemetryService(version="v2")) self._add_service(workflow_service.WorkflowService(version="v2")) # QianBiao.NG HuaWei Services self._add_service(dns_service.DNSService(version="v2")) self._add_service(cloud_eye_service.CloudEyeService(version="v1")) ass = auto_scaling_service.AutoScalingService(version="v1") self._add_service(ass) vbs_v2 = volume_backup_service.VolumeBackupService(version="v2") self._add_service(vbs_v2) self._add_service(map_reduce_service.MapReduceService(version="v1")) if plugins: for plugin in plugins: self._load_plugin(plugin) self.service_keys = sorted(self._services.keys())
class BaseResource(resource.Resource): service = object_store_service.ObjectStoreService() @classmethod def update_by_id(cls, session, resource_id, attrs, path_args=None): """Update a Resource with the given attributes. :param session: The session to use for making this request. :type session: :class:`~openstack.session.Session` :param resource_id: This resource's identifier, if needed by the request. The default is ``None``. :param dict attrs: The attributes to be sent in the body of the request. :param dict path_args: This parameter is sent by the base class but is ignored for this method. :return: A ``dict`` representing the response headers. """ url = cls._get_url(None, resource_id) headers = attrs.get(resource.HEADERS, dict()) headers['Accept'] = '' return session.post(url, endpoint_filter=cls.service, headers=headers).headers
class BaseResource(resource.Resource): service = object_store_service.ObjectStoreService() #: Metadata stored for this resource. *Type: dict* metadata = dict() _custom_metadata_prefix = None _system_metadata = dict() def _calculate_headers(self, metadata): headers = dict() for key in metadata: if key in self._system_metadata: header = self._system_metadata[key] else: header = self._custom_metadata_prefix + key headers[header] = metadata[key] return headers def set_metadata(self, session, metadata): url = self._get_url(self, self.id) session.post(url, endpoint_filter=self.service, headers=self._calculate_headers(metadata)) def delete_metadata(self, session, keys): url = self._get_url(self, self.id) headers = {key: '' for key in keys} session.post(url, endpoint_filter=self.service, headers=self._calculate_headers(headers)) def _set_metadata(self): self.metadata = dict() headers = self.get_headers() for header in headers: if header.startswith(self._custom_metadata_prefix): key = header[len(self._custom_metadata_prefix):].lower() self.metadata[key] = headers[header] def get(self, session, include_headers=False, args=None): super(BaseResource, self).get(session, include_headers, args) self._set_metadata() return self def head(self, session): super(BaseResource, self).head(session) self._set_metadata() return self @classmethod def update_by_id(cls, session, resource_id, attrs, path_args=None): """Update a Resource with the given attributes. :param session: The session to use for making this request. :type session: :class:`~openstack.session.Session` :param resource_id: This resource's identifier, if needed by the request. The default is ``None``. :param dict attrs: The attributes to be sent in the body of the request. :param dict path_args: This parameter is sent by the base class but is ignored for this method. :return: A ``dict`` representing the response headers. """ url = cls._get_url(None, resource_id) headers = attrs.get(resource.HEADERS, dict()) headers['Accept'] = '' return session.post(url, endpoint_filter=cls.service, headers=headers).headers
class Object(_base.BaseResource): _custom_metadata_prefix = "X-Object-Meta-" _system_metadata = { "content_disposition": "content-disposition", "content_encoding": "content-encoding", "content_type": "content-type", "delete_after": "x-delete-after", "delete_at": "x-delete-at", "is_content_type_detected": "x-detect-content-type", } base_path = "/%(container)s" service = object_store_service.ObjectStoreService() id_attribute = "name" allow_create = True allow_retrieve = True allow_update = True allow_delete = True allow_list = True allow_head = True # Data to be passed during a POST call to create an object on the server. data = None # URL parameters #: The unique name for the container. container = resource.prop("container") #: The unique name for the object. name = resource.prop("name") # Object details hash = resource.prop("hash") bytes = resource.prop("bytes") # Headers for HEAD and GET requests #: If set to True, Object Storage queries all replicas to return #: the most recent one. If you omit this header, Object Storage #: responds faster after it finds one valid replica. Because #: setting this header to True is more expensive for the back end, #: use it only when it is absolutely needed. *Type: bool* is_newest = resource.header("x-newest", type=bool) #: TODO(briancurtin) there's a lot of content here... range = resource.header("range", type=dict) #: See http://www.ietf.org/rfc/rfc2616.txt. if_match = resource.header("if-match", type=dict) #: In combination with Expect: 100-Continue, specify an #: "If-None-Match: \*" header to query whether the server already #: has a copy of the object before any data is sent. if_none_match = resource.header("if-none-match", type=dict) #: See http://www.ietf.org/rfc/rfc2616.txt. if_modified_since = resource.header("if-modified-since", type=dict) #: See http://www.ietf.org/rfc/rfc2616.txt. if_unmodified_since = resource.header("if-unmodified-since", type=dict) # Query parameters #: Used with temporary URLs to sign the request. For more #: information about temporary URLs, see OpenStack Object Storage #: API v1 Reference. signature = resource.header("signature") #: Used with temporary URLs to specify the expiry time of the #: signature. For more information about temporary URLs, see #: OpenStack Object Storage API v1 Reference. expires_at = resource.header("expires") #: If you include the multipart-manifest=get query parameter and #: the object is a large object, the object contents are not #: returned. Instead, the manifest is returned in the #: X-Object-Manifest response header for dynamic large objects #: or in the response body for static large objects. multipart_manifest = resource.header("multipart-manifest") # Response headers from HEAD and GET #: HEAD operations do not return content. However, in this #: operation the value in the Content-Length header is not the #: size of the response body. Instead it contains the size of #: the object, in bytes. content_length = resource.header("content-length") #: The MIME type of the object. content_type = resource.header("content-type") #: The type of ranges that the object accepts. accept_ranges = resource.header("accept-ranges") #: For objects smaller than 5 GB, this value is the MD5 checksum #: of the object content. The value is not quoted. #: For manifest objects, this value is the MD5 checksum of the #: concatenated string of MD5 checksums and ETags for each of #: the segments in the manifest, and not the MD5 checksum of #: the content that was downloaded. Also the value is enclosed #: in double-quote characters. #: You are strongly recommended to compute the MD5 checksum of #: the response body as it is received and compare this value #: with the one in the ETag header. If they differ, the content #: was corrupted, so retry the operation. etag = resource.header("etag") #: Set to True if this object is a static large object manifest object. #: *Type: bool* is_static_large_object = resource.header("x-static-large-object", type=bool) #: If set, the value of the Content-Encoding metadata. #: If not set, this header is not returned by this operation. content_encoding = resource.header("content-encoding") #: If set, specifies the override behavior for the browser. #: For example, this header might specify that the browser use #: a download program to save this file rather than show the file, #: which is the default. #: If not set, this header is not returned by this operation. content_disposition = resource.header("content-disposition") #: Specifies the number of seconds after which the object is #: removed. Internally, the Object Storage system stores this #: value in the X-Delete-At metadata item. delete_after = resource.header("x-delete-after", type=int) #: If set, the time when the object will be deleted by the system #: in the format of a UNIX Epoch timestamp. #: If not set, this header is not returned by this operation. delete_at = resource.header("x-delete-at") #: If set, to this is a dynamic large object manifest object. #: The value is the container and object name prefix of the #: segment objects in the form container/prefix. object_manifest = resource.header("x-object-manifest") #: The timestamp of the transaction. timestamp = resource.header("x-timestamp") #: The date and time that the object was created or the last #: time that the metadata was changed. last_modified_at = resource.header("last_modified", alias="last-modified") # Headers for PUT and POST requests #: Set to chunked to enable chunked transfer encoding. If used, #: do not set the Content-Length header to a non-zero value. transfer_encoding = resource.header("transfer-encoding") #: If set to true, Object Storage guesses the content type based #: on the file extension and ignores the value sent in the #: Content-Type header, if present. *Type: bool* is_content_type_detected = resource.header("x-detect-content-type", type=bool) #: If set, this is the name of an object used to create the new #: object by copying the X-Copy-From object. The value is in form #: {container}/{object}. You must UTF-8-encode and then URL-encode #: the names of the container and object before you include them #: in the header. #: Using PUT with X-Copy-From has the same effect as using the #: COPY operation to copy an object. copy_from = resource.header("x-copy-from") # The Object Store treats the metadata for its resources inconsistently so # Object.set_metadata must override the BaseResource.set_metadata to # account for it. def set_metadata(self, session, metadata): # Filter out items with empty values so the create metadata behaviour # is the same as account and container filtered_metadata = \ {key: value for key, value in metadata.items() if value} # Get a copy of the original metadata so it doesn't get erased on POST # and update it with the new metadata values. obj = self.head(session) metadata2 = copy.deepcopy(obj.metadata) metadata2.update(filtered_metadata) # Include any original system metadata so it doesn't get erased on POST for key in self._system_metadata: value = getattr(obj, key) if value and key not in metadata2: metadata2[key] = value super(Object, self).set_metadata(session, metadata2) # The Object Store treats the metadata for its resources inconsistently so # Object.delete_metadata must override the BaseResource.delete_metadata to # account for it. def delete_metadata(self, session, keys): # Get a copy of the original metadata so it doesn't get erased on POST # and update it with the new metadata values. obj = self.head(session) metadata = copy.deepcopy(obj.metadata) # Include any original system metadata so it doesn't get erased on POST for key in self._system_metadata: value = getattr(obj, key) if value: metadata[key] = value # Remove the metadata for key in keys: if key == 'delete_after': del (metadata['delete_at']) else: del (metadata[key]) url = self._get_url(self, self.id) session.post(url, headers=self._calculate_headers(metadata)) def get(self, session, include_headers=False, args=None, error_message=None): url = self._get_url(self, self.id) headers = {'Accept': 'bytes'} resp = session.get(url, headers=headers, error_message=error_message) resp = resp.content self._set_metadata() return resp def create(self, session): url = self._get_url(self, self.id) headers = self.get_headers() headers['Accept'] = '' if self.data is not None: resp = session.put(url, data=self.data, headers=headers).headers else: resp = session.post(url, data=None, headers=headers).headers self.set_headers(resp) return self
def test_service(self): sot = object_store_service.ObjectStoreService() self.assertEqual('object-store', sot.service_type) self.assertEqual('public', sot.visibility) self.assertIsNone(sot.region) self.assertIsNone(sot.service_name)
class Object(resource.Resource): base_path = "/%(container)s" service = object_store_service.ObjectStoreService() id_attribute = "name" allow_create = True allow_retrieve = True allow_update = True allow_delete = True allow_list = True allow_head = True # Data to be passed during a POST call to create an object on the server. data = None # URL parameters #: The unique name for the container. container = resource.prop("container") #: The unique name for the object. name = resource.prop("name") # Object details hash = resource.prop("hash") bytes = resource.prop("bytes") # Headers for HEAD and GET requests #: If set to True, Object Storage queries all replicas to return #: the most recent one. If you omit this header, Object Storage #: responds faster after it finds one valid replica. Because #: setting this header to True is more expensive for the back end, #: use it only when it is absolutely needed. newest = resource.header("x-newest", type=bool) #: TODO(briancurtin) there's a lot of content here... range = resource.header("range", type=dict) #: See http://www.ietf.org/rfc/rfc2616.txt. if_match = resource.header("if-match", type=dict) #: In combination with Expect: 100-Continue, specify an #: "If-None-Match: \*" header to query whether the server already #: has a copy of the object before any data is sent. if_none_match = resource.header("if-none-match", type=dict) #: See http://www.ietf.org/rfc/rfc2616.txt. if_modified_since = resource.header("if-modified-since", type=dict) #: See http://www.ietf.org/rfc/rfc2616.txt. if_unmodified_since = resource.header("if-unmodified-since", type=dict) # Query parameters #: Used with temporary URLs to sign the request. For more #: information about temporary URLs, see OpenStack Object Storage #: API v1 Reference. signature = resource.header("signature") #: Used with temporary URLs to specify the expiry time of the #: signature. For more information about temporary URLs, see #: OpenStack Object Storage API v1 Reference. expires = resource.header("expires") #: If you include the multipart-manifest=get query parameter and #: the object is a large object, the object contents are not #: returned. Instead, the manifest is returned in the #: X-Object-Manifest response header for dynamic large objects #: or in the response body for static large objects. multipart_manifest = resource.header("multipart-manifest") # Response headers from HEAD and GET #: HEAD operations do not return content. However, in this #: operation the value in the Content-Length header is not the #: size of the response body. Instead it contains the size of #: the object, in bytes. content_length = resource.header("content-length") #: The MIME type of the object. content_type = resource.header("content_type", alias="content-type") #: The type of ranges that the object accepts. accept_ranges = resource.header("accept-ranges") #: The date and time that the object was created or the last #: time that the metadata was changed. last_modified = resource.header("last_modified", alias="last-modified") #: For objects smaller than 5 GB, this value is the MD5 checksum #: of the object content. The value is not quoted. #: For manifest objects, this value is the MD5 checksum of the #: concatenated string of MD5 checksums and ETags for each of #: the segments in the manifest, and not the MD5 checksum of #: the content that was downloaded. Also the value is enclosed #: in double-quote characters. #: You are strongly recommended to compute the MD5 checksum of #: the response body as it is received and compare this value #: with the one in the ETag header. If they differ, the content #: was corrupted, so retry the operation. etag = resource.header("etag") #: Set to True if this object is a static large object manifest object. is_static_large_object = resource.header("x-static-large-object") #: The transaction date and time. date = resource.header("date") #: If set, the value of the Content-Encoding metadata. #: If not set, this header is not returned by this operation. content_encoding = resource.header("content-encoding") #: If set, specifies the override behavior for the browser. #: For example, this header might specify that the browser use #: a download program to save this file rather than show the file, #: which is the default. #: If not set, this header is not returned by this operation. content_disposition = resource.header("content-disposition") #: If set, the time when the object will be deleted by the system #: in the format of a UNIX Epoch timestamp. #: If not set, this header is not returned by this operation. delete_at = resource.header("x-delete-at", type=int) #: If set, to this is a dynamic large object manifest object. #: The value is the container and object name prefix of the #: segment objects in the form container/prefix. object_manifest = resource.header("x-object-manifest") #: The UNIX timestamp of the transaction. timestamp = resource.header("x-timestamp") # Headers for PUT and POST requests #: Set to chunked to enable chunked transfer encoding. If used, #: do not set the Content-Length header to a non-zero value. transfer_encoding = resource.header("transfer-encoding") #: If set to true, Object Storage guesses the content type based #: on the file extension and ignores the value sent in the #: Content-Type header, if present. detect_content_type = resource.header("x-detect-content-type", type=bool) #: If set, this is the name of an object used to create the new #: object by copying the X-Copy-From object. The value is in form #: {container}/{object}. You must UTF-8-encode and then URL-encode #: the names of the container and object before you include them #: in the header. #: Using PUT with X-Copy-From has the same effect as using the #: COPY operation to copy an object. copy_from = resource.header("x-copy-from") #: Specifies the number of seconds after which the object is #: removed. Internally, the Object Storage system stores this #: value in the X-Delete-At metadata item. delete_after = resource.header("x-delete-after", type=int) def get(self, session): url = self._get_url(self, self.id) # TODO(thowe): Add filter header support bug #1488269 resp = session.get(url, service=self.service, accept="bytes").content return resp def create(self, session): """Create a remote resource from this instance.""" url = self._get_url(self, self.id) headers = self.get_headers() if self.data is not None: resp = session.put(url, service=self.service, data=self.data, accept="bytes", headers=headers).headers else: resp = session.post(url, service=self.service, data=None, accept=None, headers=headers).headers self.set_headers(resp) return self
class Object(resource.Resource): base_path = "/%(container)s" service = object_store_service.ObjectStoreService() id_attribute = "name" allow_create = True allow_retrieve = True allow_update = True allow_delete = True allow_list = True allow_head = True # URL parameters container = resource.prop("container") name = resource.prop("name") # Object details hash = resource.prop("hash") bytes = resource.prop("bytes") # Headers for HEAD and GET requests auth_token = resource.prop("x-auth-token") newest = resource.prop("x-newest", type=bool) range = resource.prop("range", type=dict) if_match = resource.prop("if-match", type=dict) if_none_match = resource.prop("if-none-match", type=dict) if_modified_since = resource.prop("if-modified-since", type=dict) if_unmodified_since = resource.prop("if-unmodified-since", type=dict) # Query parameters signature = resource.prop("signature") expires = resource.prop("expires") multipart_manifest = resource.prop("multipart-manifest") # Response headers from HEAD and GET content_length = resource.prop("content-length") content_type = resource.prop("content-type") accept_ranges = resource.prop("accept-ranges") last_modified = resource.prop("last-modified") etag = resource.prop("etag") is_static_large_object = resource.prop("x-static-large-object") date = resource.prop("date") content_encoding = resource.prop("content-encoding") content_disposition = resource.prop("content-disposition") delete_at = resource.prop("x-delete-at", type=int) object_manifest = resource.prop("x-object-manifest") timestamp = resource.prop("x-timestamp") # Headers for PUT and POST requests transfer_encoding = resource.prop("transfer-encoding") detect_content_type = resource.prop("x-detect-content-type", type=bool) copy_from = resource.prop("x-copy-from") delete_after = resource.prop("x-delete-after", type=int) def get(self, session): if not self.allow_retrieve: raise exceptions.MethodNotSupported('retrieve') # When joining the base_path part and the id part, base_path's # leading slash gets dropped off here. Putting an empty leading value # in front of it causes it to get joined and replaced. url = utils.urljoin("", self.base_path % self, self.id) # Only send actual headers, not potentially set body values and # query parameters. headers = self._attrs.copy() for val in ("container", "name", "hash", "bytes", "signature", "expires", "multipart_manifest"): headers.pop(val, None) resp = session.get(url, service=self.service, accept="bytes", headers=headers).content return resp
class ServicesMixin(object): identity = identity_service.IdentityService(service_type='identity') compute = compute_service.ComputeService(service_type='compute') image = image_service.ImageService(service_type='image') load_balancer = load_balancer_service.LoadBalancerService( service_type='load-balancer') object_store = object_store_service.ObjectStoreService( service_type='object-store') clustering = clustering_service.ClusteringService( service_type='clustering') resource_cluster = clustering cluster = clustering data_processing = service_description.ServiceDescription( service_type='data-processing') baremetal = baremetal_service.BaremetalService(service_type='baremetal') bare_metal = baremetal baremetal_introspection = baremetal_introspection_service.BaremetalIntrospectionService( service_type='baremetal-introspection') key_manager = key_manager_service.KeyManagerService( service_type='key-manager') resource_optimization = service_description.ServiceDescription( service_type='resource-optimization') infra_optim = resource_optimization message = message_service.MessageService(service_type='message') messaging = message application_catalog = service_description.ServiceDescription( service_type='application-catalog') container_infrastructure_management = service_description.ServiceDescription( service_type='container-infrastructure-management') container_infrastructure = container_infrastructure_management container_infra = container_infrastructure_management search = service_description.ServiceDescription(service_type='search') dns = dns_service.DnsService(service_type='dns') workflow = workflow_service.WorkflowService(service_type='workflow') rating = service_description.ServiceDescription(service_type='rating') operator_policy = service_description.ServiceDescription( service_type='operator-policy') policy = operator_policy shared_file_system = service_description.ServiceDescription( service_type='shared-file-system') share = shared_file_system data_protection_orchestration = service_description.ServiceDescription( service_type='data-protection-orchestration') orchestration = orchestration_service.OrchestrationService( service_type='orchestration') block_storage = block_storage_service.BlockStorageService( service_type='block-storage') block_store = block_storage volume = block_storage alarm = service_description.ServiceDescription(service_type='alarm') alarming = alarm meter = service_description.ServiceDescription(service_type='meter') metering = meter telemetry = meter event = service_description.ServiceDescription(service_type='event') events = event application_deployment = service_description.ServiceDescription( service_type='application-deployment') application_deployment = application_deployment multi_region_network_automation = service_description.ServiceDescription( service_type='multi-region-network-automation') tricircle = multi_region_network_automation database = database_service.DatabaseService(service_type='database') application_container = service_description.ServiceDescription( service_type='application-container') container = application_container root_cause_analysis = service_description.ServiceDescription( service_type='root-cause-analysis') rca = root_cause_analysis nfv_orchestration = service_description.ServiceDescription( service_type='nfv-orchestration') network = network_service.NetworkService(service_type='network') backup = service_description.ServiceDescription(service_type='backup') monitoring_logging = service_description.ServiceDescription( service_type='monitoring-logging') monitoring_log_api = monitoring_logging monitoring = service_description.ServiceDescription( service_type='monitoring') monitoring_events = service_description.ServiceDescription( service_type='monitoring-events') placement = service_description.ServiceDescription( service_type='placement') instance_ha = instance_ha_service.InstanceHaService( service_type='instance-ha') ha = instance_ha reservation = service_description.ServiceDescription( service_type='reservation') function_engine = service_description.ServiceDescription( service_type='function-engine') accelerator = accelerator_service.AcceleratorService( service_type='accelerator') admin_logic = service_description.ServiceDescription( service_type='admin-logic') registration = admin_logic
class Container(resource.Resource): base_path = "/" service = object_store_service.ObjectStoreService() id_attribute = "name" allow_create = True allow_retrieve = True allow_update = True allow_delete = True allow_list = True allow_head = True # Account data (when id=None) #: The total number of bytes that are stored in Object Storage for #: the account. account_bytes_used = resource.header("x-account-bytes-used", type=int) #: The number of containers. account_container_count = resource.header("x-account-container-count", type=int) #: The number of objects in the account. account_object_count = resource.header("x-account-object-count", type=int) #: The secret key value for temporary URLs. If not set, #: this header is not returned by this operation. meta_temp_url_key = resource.header("x-account-meta-temp-url-key") #: A second secret key value for temporary URLs. If not set, #: this header is not returned by this operation. meta_temp_url_key_2 = resource.header("x-account-meta-temp-url-key-2") # Container body data (when id=None) #: The name of the container. name = resource.prop("name") #: The number of objects in the container. count = resource.prop("count") #: The total number of bytes that are stored in Object Storage #: for the container. bytes = resource.prop("bytes") # Container metadata (when id=name) #: The number of objects. object_count = resource.header("x-container-object-count", type=int) #: The count of bytes used in total. bytes_used = resource.header("x-container-bytes-used", type=int) # Request headers (when id=None) #: If set to True, Object Storage queries all replicas to return the #: most recent one. If you omit this header, Object Storage responds #: faster after it finds one valid replica. Because setting this #: header to True is more expensive for the back end, use it only #: when it is absolutely needed. newest = resource.header("x-newest", type=bool) # Request headers (when id=name) #: The ACL that grants read access. If not set, this header is not #: returned by this operation. read_ACL = resource.header("x-container-read") #: The ACL that grants write access. If not set, this header is not #: returned by this operation. write_ACL = resource.header("x-container-write") #: The destination for container synchronization. If not set, #: this header is not returned by this operation. sync_to = resource.header("x-container-sync-to") #: The secret key for container synchronization. If not set, #: this header is not returned by this operation. sync_key = resource.header("x-container-sync-key") #: Enables versioning on this container. The value is the name #: of another container. You must UTF-8-encode and then URL-encode #: the name before you include it in the header. To disable #: versioning, set the header to an empty string. versions_location = resource.header("x-versions-location") #: Set to any value to disable versioning. remove_versions_location = resource.header("x-remove-versions-location") #: Changes the MIME type for the object. content_type = resource.header("content-type") #: If set to true, Object Storage guesses the content type based #: on the file extension and ignores the value sent in the #: Content-Type header, if present. detect_content_type = resource.header("x-detect-content-type", type=bool) #: In combination with Expect: 100-Continue, specify an #: "If-None-Match: \*" header to query whether the server already #: has a copy of the object before any data is sent. if_none_match = resource.header("if-none-match") @classmethod def update_by_id(cls, session, resource_id, attrs, path_args=None): """Update a Container with the given attributes. :param session: The session to use for making this request. :type session: :class:`~openstack.session.Session` :param resource_id: This resource's identifier, if needed by the request. The default is ``None``. :param dict attrs: The attributes to be sent in the body of the request. :param dict path_args: This parameter is sent by the base class but is ignored for this method. :return: A ``dict`` representing the response headers. """ url = cls._get_url(None, resource_id) headers = attrs.get(resource.HEADERS, dict()) return session.post(url, service=cls.service, accept=None, headers=headers).headers @classmethod def create_by_id(cls, session, attrs, resource_id=None): """Create a Container from its attributes. :param session: The session to use for making this request. :type session: :class:`~openstack.session.Session` :param dict attrs: The attributes to be sent in the body of the request. :param resource_id: This resource's identifier, if needed by the request. The default is ``None``. :return: A ``dict`` representing the response headers. """ url = cls._get_url(None, resource_id) headers = attrs.get(resource.HEADERS, dict()) return session.put(url, service=cls.service, accept=None, headers=headers).headers def create(self, session): """Create a Container from this instance. :param session: The session to use for making this request. :type session: :class:`~openstack.session.Session` :return: This :class:`~openstack.object_store.v1.container.Container` instance. """ resp = self.create_by_id(session, self._attrs, self.id) self.set_headers(resp) self._reset_dirty() return self
class Object(_base.BaseResource): _custom_metadata_prefix = "X-Object-Meta-" _system_metadata = { "content_disposition": "content-disposition", "content_encoding": "content-encoding", "content_type": "content-type", "delete_after": "x-delete-after", "delete_at": "x-delete-at", "is_content_type_detected": "x-detect-content-type", } base_path = "/%(container)s" pagination_key = 'X-Container-Object-Count' service = object_store_service.ObjectStoreService() allow_create = True allow_fetch = True allow_commit = True allow_delete = True allow_list = True allow_head = True # Data to be passed during a POST call to create an object on the server. # TODO(mordred) Make a base class BaseDataResource that can be used here # and with glance images that has standard overrides for dealing with # binary data. data = None # URL parameters #: The unique name for the container. container = resource.URI("container") #: The unique name for the object. name = resource.Body("name", alternate_id=True) # Object details # Make these private because they should only matter in the case where # we have a Body with no headers (like if someone programmatically is # creating an Object) _hash = resource.Body("hash") _bytes = resource.Body("bytes", type=int) _last_modified = resource.Body("last_modified") _content_type = resource.Body("content_type") # Headers for HEAD and GET requests #: If set to True, Object Storage queries all replicas to return #: the most recent one. If you omit this header, Object Storage #: responds faster after it finds one valid replica. Because #: setting this header to True is more expensive for the back end, #: use it only when it is absolutely needed. *Type: bool* is_newest = resource.Header("x-newest", type=bool) #: TODO(briancurtin) there's a lot of content here... range = resource.Header("range", type=dict) #: See http://www.ietf.org/rfc/rfc2616.txt. if_match = resource.Header("if-match", type=list) #: In combination with Expect: 100-Continue, specify an #: "If-None-Match: \*" header to query whether the server already #: has a copy of the object before any data is sent. if_none_match = resource.Header("if-none-match", type=list) #: See http://www.ietf.org/rfc/rfc2616.txt. if_modified_since = resource.Header("if-modified-since", type=str) #: See http://www.ietf.org/rfc/rfc2616.txt. if_unmodified_since = resource.Header("if-unmodified-since", type=str) # Query parameters #: Used with temporary URLs to sign the request. For more #: information about temporary URLs, see OpenStack Object Storage #: API v1 Reference. signature = resource.Header("signature") #: Used with temporary URLs to specify the expiry time of the #: signature. For more information about temporary URLs, see #: OpenStack Object Storage API v1 Reference. expires_at = resource.Header("expires") #: If you include the multipart-manifest=get query parameter and #: the object is a large object, the object contents are not #: returned. Instead, the manifest is returned in the #: X-Object-Manifest response header for dynamic large objects #: or in the response body for static large objects. multipart_manifest = resource.Header("multipart-manifest") # Response headers from HEAD and GET #: HEAD operations do not return content. However, in this #: operation the value in the Content-Length header is not the #: size of the response body. Instead it contains the size of #: the object, in bytes. content_length = resource.Header( "content-length", type=int, alias='_bytes') #: The MIME type of the object. content_type = resource.Header("content-type", alias="_content_type") #: The type of ranges that the object accepts. accept_ranges = resource.Header("accept-ranges") #: For objects smaller than 5 GB, this value is the MD5 checksum #: of the object content. The value is not quoted. #: For manifest objects, this value is the MD5 checksum of the #: concatenated string of MD5 checksums and ETags for each of #: the segments in the manifest, and not the MD5 checksum of #: the content that was downloaded. Also the value is enclosed #: in double-quote characters. #: You are strongly recommended to compute the MD5 checksum of #: the response body as it is received and compare this value #: with the one in the ETag header. If they differ, the content #: was corrupted, so retry the operation. etag = resource.Header("etag", alias='_hash') #: Set to True if this object is a static large object manifest object. #: *Type: bool* is_static_large_object = resource.Header("x-static-large-object", type=bool) #: If set, the value of the Content-Encoding metadata. #: If not set, this header is not returned by this operation. content_encoding = resource.Header("content-encoding") #: If set, specifies the override behavior for the browser. #: For example, this header might specify that the browser use #: a download program to save this file rather than show the file, #: which is the default. #: If not set, this header is not returned by this operation. content_disposition = resource.Header("content-disposition") #: Specifies the number of seconds after which the object is #: removed. Internally, the Object Storage system stores this #: value in the X-Delete-At metadata item. delete_after = resource.Header("x-delete-after", type=int) #: If set, the time when the object will be deleted by the system #: in the format of a UNIX Epoch timestamp. #: If not set, this header is not returned by this operation. delete_at = resource.Header("x-delete-at") #: If set, to this is a dynamic large object manifest object. #: The value is the container and object name prefix of the #: segment objects in the form container/prefix. object_manifest = resource.Header("x-object-manifest") #: The timestamp of the transaction. timestamp = resource.Header("x-timestamp") #: The date and time that the object was created or the last #: time that the metadata was changed. last_modified_at = resource.Header("last-modified", alias='_last_modified') # Headers for PUT and POST requests #: Set to chunked to enable chunked transfer encoding. If used, #: do not set the Content-Length header to a non-zero value. transfer_encoding = resource.Header("transfer-encoding") #: If set to true, Object Storage guesses the content type based #: on the file extension and ignores the value sent in the #: Content-Type header, if present. *Type: bool* is_content_type_detected = resource.Header("x-detect-content-type", type=bool) #: If set, this is the name of an object used to create the new #: object by copying the X-Copy-From object. The value is in form #: {container}/{object}. You must UTF-8-encode and then URL-encode #: the names of the container and object before you include them #: in the header. #: Using PUT with X-Copy-From has the same effect as using the #: COPY operation to copy an object. copy_from = resource.Header("x-copy-from") has_body = False def __init__(self, data=None, **attrs): super(_base.BaseResource, self).__init__(**attrs) self.data = data # The Object Store treats the metadata for its resources inconsistently so # Object.set_metadata must override the BaseResource.set_metadata to # account for it. def set_metadata(self, session, metadata): # Filter out items with empty values so the create metadata behaviour # is the same as account and container filtered_metadata = \ {key: value for key, value in metadata.items() if value} # Update from remote if we only have locally created information if not self.last_modified_at: self.head(session) # Get a copy of the original metadata so it doesn't get erased on POST # and update it with the new metadata values. metadata = copy.deepcopy(self.metadata) metadata.update(filtered_metadata) # Include any original system metadata so it doesn't get erased on POST for key in self._system_metadata: value = getattr(self, key) if value and key not in metadata: metadata[key] = value request = self._prepare_request() headers = self._calculate_headers(metadata) response = session.post(request.url, headers=headers) self._translate_response(response, has_body=False) self.metadata.update(metadata) return self # The Object Store treats the metadata for its resources inconsistently so # Object.delete_metadata must override the BaseResource.delete_metadata to # account for it. def delete_metadata(self, session, keys): if not keys: return # If we have an empty object, update it from the remote side so that # we have a copy of the original metadata. Deleting metadata requires # POSTing and overwriting all of the metadata. If we already have # metadata locally, assume this is an existing object. if not self.metadata: self.head(session) metadata = copy.deepcopy(self.metadata) # Include any original system metadata so it doesn't get erased on POST for key in self._system_metadata: value = getattr(self, key) if value: metadata[key] = value # Remove the requested metadata keys # TODO(mordred) Why don't we just look at self._header_mapping() # instead of having system_metadata? deleted = False attr_keys_to_delete = set() for key in keys: if key == 'delete_after': del(metadata['delete_at']) else: if key in metadata: del(metadata[key]) # Delete the attribute from the local copy of the object. # Metadata that doesn't have Component attributes is # handled by self.metadata being reset when we run # self.head if hasattr(self, key): attr_keys_to_delete.add(key) deleted = True # Nothing to delete, skip the POST if not deleted: return self request = self._prepare_request() response = session.post( request.url, headers=self._calculate_headers(metadata)) exceptions.raise_from_response( response, error_message="Error deleting metadata keys") # Only delete from local object if the remote delete was successful for key in attr_keys_to_delete: delattr(self, key) # Just update ourselves from remote again. return self.head(session) def _download(self, session, error_message=None, stream=False): request = self._prepare_request() request.headers['Accept'] = 'bytes' response = session.get( request.url, headers=request.headers, stream=stream) exceptions.raise_from_response(response, error_message=error_message) return response def download(self, session, error_message=None): response = self._download(session, error_message=error_message) return response.content def stream(self, session, error_message=None, chunk_size=1024): response = self._download( session, error_message=error_message, stream=True) return response.iter_content(chunk_size, decode_unicode=False) def create(self, session): request = self._prepare_request() request.headers['Accept'] = '' response = session.put( request.url, data=self.data, headers=request.headers) self._translate_response(response, has_body=False) return self
class Container(resource.Resource): base_path = "/" service = object_store_service.ObjectStoreService() id_attribute = "name" allow_create = True allow_retrieve = True allow_update = True allow_delete = True allow_list = True allow_head = True # Account data (when id=None) timestamp = resource.prop("x-timestamp") account_bytes_used = resource.prop("x-account-bytes-used") account_container_count = resource.prop("x-account-container-count") account_object_count = resource.prop("x-account-object-count") meta_temp_url_key = resource.prop("x-account-meta-temp-url-key") meta_temp_url_key_2 = resource.prop("x-account-meta-temp-url-key-2") # Container body data (when id=None) name = resource.prop("name") count = resource.prop("count") bytes = resource.prop("bytes") # Container metadata (when id=name) object_count = resource.prop("x-container-object-count") bytes_used = resource.prop("x-container-bytes-used") # Request headers (when id=None) newest = resource.prop("x-newest", type=bool) # Request headers (when id=name) read_ACL = resource.prop("x-container-read") write_ACL = resource.prop("x-container-write") sync_to = resource.prop("x-container-sync-to") sync_key = resource.prop("x-container-sync-key") versions_location = resource.prop("x-versions-location") remove_versions_location = resource.prop("x-remove-versions-location") content_type = resource.prop("content-type") detect_content_type = resource.prop("x-detect-content-type", type=bool) if_none_match = resource.prop("if-none-match") def _do_create_update(self, session, method): url = utils.urljoin(self.base_path, self.id) # Only send actual headers, not potentially set body values. headers = self._attrs.copy() for val in ("name", "count", "bytes"): headers.pop(val, None) data = method(url, service=self.service, accept=None, headers=headers).headers self._reset_dirty() return data def create(self, session): if not self.allow_create: raise exceptions.MethodNotSupported('create') return self._do_create_update(session, session.put) def update(self, session): if not self.allow_update: raise exceptions.MethodNotSupported('update') return self._do_create_update(session, session.post)