def create_server(self, request, tenant_id): """ Returns a generic create server response, with status 'ACTIVE'. """ try: content = json.loads(request.content.read()) except ValueError: return json.dumps(bad_request("Invalid JSON request body", request)) try: creation = self._region_collection_for_tenant(tenant_id).request_creation(request, content, self.url) except BadRequestError as e: return json.dumps(bad_request(e.nova_message, request)) except LimitError as e: return json.dumps(forbidden(e.nova_message, request)) return creation
def set_metadata(self, request): """ Set the metadata for the specified server - this replaces whatever metadata was there. The resulting metadata is not a union of the previous metadata and the new metadata - it is *just* the new metadata. The body must look like: ``{"metadata": {...}}`` although ``{"metadata": {...}, "other": "garbage", "keys": "included"}`` is ok too. All the response messages and codes have been verified as of 2015-04-23 against Rackspace Nova. """ try: content = json.loads(request.content.read()) except ValueError: return json.dumps(bad_request("Malformed request body", request)) # more than one key is ok, non-"meta" keys are just ignored if 'metadata' not in content: return json.dumps(bad_request("Malformed request body", request)) # When setting metadata, None is special for some reason if content['metadata'] is None: return json.dumps( bad_request("Malformed request body. metadata must be object", request)) try: Server.validate_metadata(content['metadata']) except BadRequestError as e: return json.dumps(bad_request(e.nova_message, request)) except LimitError as e: return json.dumps(forbidden(e.nova_message, request)) self._server.set_metadata(content['metadata']) return json.dumps({'metadata': content['metadata']})
def set_metadata_item(self, request, key): """ Set a metadata item. The body must look like: ``{"meta": {<key>: value}}`` although ``{"meta": {<key>: value}, "other": "garbage", "keys": "included"}`` is ok too. All the response messages and codes have been verified as of 2015-04-23 against Rackspace Nova. """ try: content = json.loads(request.content.read()) except ValueError: request.setResponseCode(400) return json.dumps(bad_request("Malformed request body", request)) # more than one key is ok, non-"meta" keys are just ignored if 'meta' not in content or not isinstance(content['meta'], dict): return json.dumps(bad_request("Malformed request body", request)) if len(content['meta']) > 1: return json.dumps(bad_request( "Request body contains too many items", request)) if key not in content['meta']: return json.dumps(bad_request( "Request body and URI mismatch", request)) try: self._server.set_metadata_item(key, content['meta'][key]) except BadRequestError as e: return json.dumps(bad_request(e.nova_message, request)) except LimitError as e: return json.dumps(forbidden(e.nova_message, request)) # no matter how many keys were passed in, only the meta key is # returned return json.dumps({'meta': content['meta']})
def create_server(self, request, tenant_id): """ Returns a generic create server response, with status 'ACTIVE'. """ try: content = json.loads(request.content.read()) except ValueError: return json.dumps( bad_request("Invalid JSON request body", request)) try: creation = (self._region_collection_for_tenant(tenant_id) .request_creation(request, content, self.url)) except BadRequestError as e: return json.dumps(bad_request(e.nova_message, request)) except LimitError as e: return json.dumps(forbidden(e.nova_message, request)) return creation
def set_metadata(self, request): """ Set the metadata for the specified server - this replaces whatever metadata was there. The resulting metadata is not a union of the previous metadata and the new metadata - it is *just* the new metadata. The body must look like: ``{"metadata": {...}}`` although ``{"metadata": {...}, "other": "garbage", "keys": "included"}`` is ok too. All the response messages and codes have been verified as of 2015-04-23 against Rackspace Nova. """ try: content = json.loads(request.content.read()) except ValueError: return json.dumps(bad_request("Malformed request body", request)) # more than one key is ok, non-"meta" keys are just ignored if 'metadata' not in content: return json.dumps(bad_request("Malformed request body", request)) # When setting metadata, None is special for some reason if content['metadata'] is None: return json.dumps(bad_request( "Malformed request body. metadata must be object", request)) try: Server.validate_metadata(content['metadata']) except BadRequestError as e: return json.dumps(bad_request(e.nova_message, request)) except LimitError as e: return json.dumps(forbidden(e.nova_message, request)) self._server.set_metadata(content['metadata']) return json.dumps({'metadata': content['metadata']})
def create_key_pair(self, request, tenant_id): """ Returns a newly created key pair with the specified name. http://docs.rackspace.com/servers/api/v2/cs-devguide/content/UploadKeyPair.html """ try: content = json.loads(request.content.read()) keypair = content["keypair"] keypair_from_request = KeyPair(name=keypair["name"], public_key=keypair["public_key"]) except (ValueError or KeyError): request.setResponseCode(400) return json.dumps(bad_request("Malformed request body", request)) keypair_response = self._keypair_collection_for_tenant(tenant_id).create_keypair(keypair=keypair_from_request) return json.dumps(keypair_response)
def create_key_pair(self, request, tenant_id): """ Returns a newly created key pair with the specified name. http://docs.rackspace.com/servers/api/v2/cs-devguide/content/UploadKeyPair.html """ try: content = json.loads(request.content.read()) keypair = content["keypair"] keypair_from_request = KeyPair( name=keypair["name"], public_key=keypair["public_key"]) except (ValueError or KeyError): request.setResponseCode(400) return json.dumps(bad_request("Malformed request body", request)) keypair_response = self._keypair_collection_for_tenant( tenant_id).create_keypair(keypair=keypair_from_request) return json.dumps(keypair_response)