def delete_check(self, entity, check): """ Deletes the specified check from the entity. """ uri = "/%s/%s/checks/%s" % (self.uri_base, utils.get_id(entity), utils.get_id(check)) resp, resp_body = self.api.method_delete(uri)
def create_alarm(self, entity, check, notification_plan, criteria=None, disabled=False, label=None, name=None, metadata=None): """ Creates an alarm that binds the check on the given entity with a notification plan. Note that the 'criteria' parameter, if supplied, should be a string representing the DSL for describing alerting conditions and their output states. Pyrax does not do any validation of these criteria statements; it is up to you as the developer to understand the language and correctly form the statement. This alarm language is documented online in the Cloud Monitoring section of http://docs.rackspace.com. """ uri = "/%s/%s/alarms" % (self.uri_base, utils.get_id(entity)) body = {"check_id": utils.get_id(check), "notification_plan_id": utils.get_id(notification_plan), } if criteria: body["criteria"] = criteria if disabled is not None: body["disabled"] = disabled label_name = label or name if label_name: body["label"] = label_name if metadata: body["metadata"] = metadata resp, resp_body = self.api.method_post(uri, body=body) if resp.status_code == 201: alarm_id = resp.headers["x-object-id"] return self.get_alarm(entity, alarm_id)
def update_check(self, check, label=None, name=None, disabled=None, metadata=None, monitoring_zones_poll=None, timeout=None, period=None, target_alias=None, target_hostname=None, target_receiver=None): if monitoring_zones_poll: monitoring_zones_poll = utils.coerce_string_to_list( monitoring_zones_poll) monitoring_zones_poll = [utils.get_id(mzp) for mzp in monitoring_zones_poll] body = {} local_dict = locals() label = label or name params = ("label", "disabled", "metadata", "monitoring_zones_poll", "timeout", "period", "target_alias", "target_hostname", "target_receiver") body = _params_to_dict(params, body, locals()) entity = check.entity uri = "/%s/%s/checks/%s" % (self.uri_base, utils.get_id(entity), utils.get_id(check)) try: resp, resp_body = self.api.method_put(uri, body=body) except exc.BadRequest as e: msg = e.message dtls = e.details if msg.startswith("Validation error"): raise exc.InvalidMonitoringCheckUpdate("The update failed " "validation: %s: %s" % (msg, dtls)) else: # Some other issue. raise return resp_body
def delete_alarm(self, entity, alarm): """ Deletes the specified alarm. """ uri = "/%s/%s/alarms/%s" % (self.uri_base, utils.get_id(entity), utils.get_id(alarm)) resp, resp_body = self.api.method_delete(uri)
def get_check(self, entity, check): """ Returns the current version of the check for the entity. """ uri = "/%s/%s/checks/%s" % (self.uri_base, utils.get_id(entity), utils.get_id(check)) resp, resp_body = self.api.method_get(uri) return CloudMonitorCheck(self, resp_body, entity)
def list_metrics(self, entity, check): """ Returns a list of all the metrics associated with the specified check. """ uri = "/%s/%s/checks/%s/metrics" % (self.uri_base, utils.get_id(entity), utils.get_id(check)) resp, resp_body = self.api.method_get(uri) metrics = [val["name"] for val in resp_body["values"]] return metrics
def get_alarm(self, entity, alarm): """ Returns the alarm with the specified ID for this entity. If a CloudMonitorAlarm instance is passed, returns a new CloudMonitorAlarm object with the current state from the API. """ uri = "/%s/%s/alarms/%s" % (self.uri_base, utils.get_id(entity), utils.get_id(alarm)) resp, resp_body = self.api.method_get(uri) return CloudMonitorAlarm(self, resp_body, entity)
def test_get_id(self): target = utils.random_unicode() class ObjWithID(object): id = target obj = ObjWithID() self.assertEqual(utils.get_id(obj), target) self.assertEqual(utils.get_id(obj.id), target) plain = object() self.assertEqual(utils.get_id(plain), plain)
def test_delete_record(self): clt = self.client mgr = clt._manager dom = self.domain rec = CloudDNSRecord(mgr, {"id": utils.random_unicode()}) mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s/records/%s" % (utils.get_id(dom), utils.get_id(rec)) clt.delete_record(dom, rec) mgr._async_call.assert_called_once_with(uri, method="DELETE", error_class=exc.DomainRecordDeletionFailed, has_response=False)
def update_entity(self, entity, agent=None, metadata=None): """ Updates the specified entity's values with the supplied parameters. """ body = {} if agent: body["agent_id"] = utils.get_id(agent) if metadata: body["metadata"] = metadata if body: uri = "/%s/%s" % (self.uri_base, utils.get_id(entity)) resp, body = self.api.method_put(uri, body=body)
def test_delete_record(self): clt = self.client mgr = clt._manager dom = self.domain rec = CloudDNSRecord(mgr, {"id": utils.random_unicode()}) mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s/records/%s" % (utils.get_id(dom), utils.get_id(rec)) clt.delete_record(dom, rec) mgr._async_call.assert_called_once_with( uri, method="DELETE", error_class=exc.DomainRecordDeletionFailed, has_response=False)
def get_metadata(self, queue): """ Returns the metadata for the specified queue. """ uri = "/%s/%s/metadata" % (self.uri_base, utils.get_id(queue)) resp, resp_body = self.api.method_get(uri) return resp_body
def test_find_record_not_unique(self): clt = self.client mgr = clt._manager dom = self.domain typ = "A" nm = utils.random_unicode() data = "0.0.0.0" ret_body = { "records": [{ "accountId": "728829", "created": "2012-09-21T21:32:27.000+0000", "emailAddress": "*****@*****.**", "id": "3448214", "name": "example.com", "updated": "2012-09-21T21:35:45.000+0000" }, { "accountId": "728829", "created": "2012-09-21T21:32:27.000+0000", "emailAddress": "*****@*****.**", "id": "3448214", "name": "example.com", "updated": "2012-09-21T21:35:45.000+0000" }] } clt.method_get = Mock(return_value=({}, ret_body)) uri = "/domains/%s/records?type=%s&name=%s&data=%s" % ( utils.get_id(dom), typ, nm, data) self.assertRaises(exc.DomainRecordNotUnique, clt.find_record, dom, typ, name=nm, data=data)
def get_stats(self, queue): """ Returns the message stats for the specified queue. """ uri = "/%s/%s/stats" % (self.uri_base, utils.get_id(queue)) resp, resp_body = self.api.method_get(uri) return resp_body.get("messages")
def test_find_record_not_unique(self): clt = self.client mgr = clt._manager dom = self.domain typ = "A" nm = utils.random_unicode() data = "0.0.0.0" ret_body = {"records": [{ "accountId": "728829", "created": "2012-09-21T21:32:27.000+0000", "emailAddress": "*****@*****.**", "id": "3448214", "name": "example.com", "updated": "2012-09-21T21:35:45.000+0000" }, {"accountId": "728829", "created": "2012-09-21T21:32:27.000+0000", "emailAddress": "*****@*****.**", "id": "3448214", "name": "example.com", "updated": "2012-09-21T21:35:45.000+0000" }]} clt.method_get = Mock(return_value=({}, ret_body)) uri = "/domains/%s/records?type=%s&name=%s&data=%s" % ( utils.get_id(dom), typ, nm, data) self.assertRaises(exc.DomainRecordNotUnique, clt.find_record, dom, typ, name=nm, data=data)
def test_update_record(self): clt = self.client mgr = clt._manager dom = self.domain nm = utils.random_unicode() rec = fakes.FakeDNSRecord(mgr, {"id": utils.random_unicode(), "name": nm}) ttl = 9999 data = "0.0.0.0" mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s/records/%s" % (utils.get_id(dom), utils.get_id(rec)) req_body = {"name": nm, "data": data, "ttl": ttl} clt.update_record(dom, rec, data=data, ttl=ttl) mgr._async_call.assert_called_once_with(uri, method="PUT", body=req_body, error_class=exc.DomainRecordUpdateFailed, has_response=False)
def get_type(self, notification_type_id): """ Returns a CloudMonitorNotificationType object for the given ID. """ uri = "/notification_types/%s" % utils.get_id(notification_type_id) resp, resp_body = self.api.method_get(uri) return CloudMonitorNotificationType(self, resp_body)
def list_alarms(self, entity): """ Returns a list of all the alarms created on the specified entity. """ uri = "/%s/%s/alarms" % (self.uri_base, utils.get_id(entity)) resp, resp_body = self.api.method_get(uri) return [CloudMonitorAlarm(self, dct, entity) for dct in resp_body["values"]]
def list_checks(self, entity): """ Returns a list of all CloudMonitorChecks defined for this entity. """ uri = "/%s/%s/checks" % (self.uri_base, utils.get_id(entity)) resp, resp_body = self.api.method_get(uri) return [CloudMonitorCheck(self, val, entity) for val in resp_body["values"]]
def _get_server_networks(network, public=False, private=False): net_id = utils.get_id(network) ret = [{"net-id": net_id}] if public: ret.append({"net-id": PUBLIC_NET_ID}) if private: ret.append({"net-id": SERVICE_NET_ID}) return ret
def action(self, item, action_type, body={}): """ Several API calls are lumped under the 'action' API. This is the generic handler for such calls. """ uri = "/%s/%s/action" % (self.uri_base, utils.get_id(item)) action_body = {action_type: body} return self.api.method_post(uri, body=action_body)
def test_list_records(self): clt = self.client mgr = clt._manager dom = self.domain resp_body = {'Something': 'here'} clt.method_get = Mock(return_value=({}, resp_body)) uri = "/domains/%s/records" % utils.get_id(dom) clt.list_records(dom) clt.method_get.assert_called_once_with(uri)
def list(self, entity=None): """ Returns overview information, optionally filtered for a given entity. """ uri = "/%s" % self.uri_base if entity: uri = "%s?entityId=%s" % (uri, utils.get_id(entity)) resp, resp_body = self._list(uri, return_raw=True) return resp_body
def test_delete_subdomains(self): clt = self.client mgr = clt._manager dom = self.domain mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s?deleteSubdomains=true" % utils.get_id(dom) clt.delete(dom, delete_subdomains=True) mgr._async_call.assert_called_once_with(uri, method="DELETE", error_class=exc.DomainDeletionFailed, has_response=False)
def update_alarm(self, entity, alarm, criteria=None, disabled=False, label=None, name=None, metadata=None): """ Updates an existing alarm on the given entity. See the comments on the 'create_alarm()' regarding the criteria parameter. """ uri = "/%s/%s/alarms/%s" % (self.uri_base, utils.get_id(entity), utils.get_id(alarm)) body = {} if criteria: body["criteria"] = criteria if disabled is not None: body["disabled"] = disabled label_name = label or name if label_name: body["label"] = label_name if metadata: body["metadata"] = metadata resp, resp_body = self.api.method_put(uri, body=body)
def _create_body(self, name, instance, description=None): body = { "backup": { "instance": utils.get_id(instance), "name": name, } } if description is not None: body["backup"]["description"] = description return body
def list(self, entity=None): """ Returns a dictionary of changelog data, optionally filtered for a given entity. """ uri = "/%s" % self.uri_base if entity: uri = "%s?entityId=%s" % (uri, utils.get_id(entity)) resp, resp_body = self._list(uri, return_raw=True) return resp_body
def create(self, notification_type, label=None, name=None, details=None): """ Defines a notification for handling an alarm. """ uri = "/%s" % self.uri_base body = {"label": label or name, "type": utils.get_id(notification_type), "details": details, } resp, resp_body = self.api.method_post(uri, body=body) return self.get(resp.headers["x-object-id"])
def test_update_record(self): clt = self.client mgr = clt._manager dom = self.domain nm = utils.random_unicode() rec = fakes.FakeDNSRecord(mgr, { "id": utils.random_unicode(), "name": nm }) ttl = 9999 data = "0.0.0.0" mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s/records/%s" % (utils.get_id(dom), utils.get_id(rec)) req_body = {"name": nm, "data": data, "ttl": ttl} clt.update_record(dom, rec, data=data, ttl=ttl) mgr._async_call.assert_called_once_with( uri, method="PUT", body=req_body, error_class=exc.DomainRecordUpdateFailed, has_response=False)
def test_add_records(self): clt = self.client mgr = clt._manager dom = self.domain rec = {"type": "A", "name": "example.com", "data": "0.0.0.0"} mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s/records" % utils.get_id(dom) clt.add_records(dom, rec) mgr._async_call.assert_called_once_with(uri, method="POST", body={"records": [rec]}, error_class=exc.DomainRecordAdditionFailed, has_response=False)
def delete(self, msg, claim_id=None): """ Deletes the specified message from its queue. If the message has been claimed, the ID of that claim must be passed as the 'claim_id' parameter. """ msg_id = utils.get_id(msg) if claim_id: uri = "/%s/%s?claim_id=%s" % (self.uri_base, msg_id, claim_id) else: uri = "/%s/%s" % (self.uri_base, msg_id) return self._delete(uri)
def test_delete_subdomains(self): clt = self.client mgr = clt._manager dom = self.domain mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s?deleteSubdomains=true" % utils.get_id(dom) clt.delete(dom, delete_subdomains=True) mgr._async_call.assert_called_once_with( uri, method="DELETE", error_class=exc.DomainDeletionFailed, has_response=False)
def _list_backups_for_instance(self, instance): """ Instance-specific backups are handled through the instance manager, not the backup manager. """ uri = "/%s/%s/backups" % (self.uri_base, utils.get_id(instance)) resp, resp_body = self.api.method_get(uri) mgr = self.api._backup_manager return [ CloudDatabaseBackup(mgr, backup) for backup in resp_body.get("backups") ]
def test_find_record_not_found(self): clt = self.client mgr = clt._manager dom = self.domain typ = "A" nm = utils.random_unicode() data = "0.0.0.0" ret_body = {"records": []} clt.method_get = Mock(return_value=({}, ret_body)) uri = "/domains/%s/records?type=%s&name=%s&data=%s" % ( utils.get_id(dom), typ, nm, data) self.assertRaises(exc.DomainRecordNotFound, clt.find_record, dom, typ, name=nm, data=data)
def test_search_records_params(self): clt = self.client mgr = clt._manager dom = self.domain typ = "A" nm = utils.random_unicode() data = "0.0.0.0" resp_body = {"Something": "here"} clt.method_get = Mock(return_value=({}, resp_body)) uri = "/domains/%s/records?type=%s&name=%s&data=%s" % ( utils.get_id(dom), typ, nm, data) clt.search_records(dom, typ, name=nm, data=data) clt.method_get.assert_called_once_with(uri)
def set_metadata(self, queue, metadata, clear=False): """ Accepts a dictionary and adds that to the specified queue's metadata. If the 'clear' argument is passed as True, any existing metadata is replaced with the new metadata. """ uri = "/%s/%s/metadata" % (self.uri_base, utils.get_id(queue)) if clear: curr = {} else: curr = self.get_metadata(queue) curr.update(metadata) resp, resp_body = self.api.method_put(uri, body=curr)
def test_add_records(self): clt = self.client mgr = clt._manager dom = self.domain rec = {"type": "A", "name": "example.com", "data": "0.0.0.0"} mgr._async_call = Mock(return_value=({}, {})) uri = "/domains/%s/records" % utils.get_id(dom) clt.add_records(dom, rec) mgr._async_call.assert_called_once_with( uri, method="POST", body={"records": [rec]}, error_class=exc.DomainRecordAdditionFailed, has_response=False)
def update(self, claim, ttl=None, grace=None): """ Updates the specified claim with either a new TTL or grace period, or both. """ body = {} if ttl is not None: body["ttl"] = ttl if grace is not None: body["grace"] = grace if not body: raise exc.MissingClaimParameters( "You must supply a value for " "'ttl' or 'grace' when calling 'update()'") uri = "/%s/%s" % (self.uri_base, utils.get_id(claim)) resp, resp_body = self.api.method_patch(uri, body=body)
def update(self, snapshot, display_name=None, display_description=None): """ Update the specified values on the specified snapshot. You may specify one or more values to update. """ uri = "/%s/%s" % (self.uri_base, utils.get_id(snapshot)) param_dict = {} if display_name: param_dict["display_name"] = display_name if display_description: param_dict["display_description"] = display_description if not param_dict: # Nothing to do! return body = {"snapshot": param_dict} resp, resp_body = self.api.method_put(uri, body=body)
def create_backup(self, instance, name, description=None): """ Creates a backup of the specified instance, giving it the specified name along with an optional description. """ body = { "backup": { "instance": utils.get_id(instance), "name": name, } } if description is not None: body["backup"]["description"] = description uri = "/backups" resp, resp_body = self.api.method_post(uri, body=body) mgr = self.api._backup_manager return CloudDatabaseBackup(mgr, body.get("backup"))
def test_update_domain(self): clt = self.client dom = self.domain mgr = clt._manager emailAddress = None comment = utils.random_unicode() ttl = 666 mgr._async_call = Mock(return_value=({}, "fake")) uri = "/domains/%s" % utils.get_id(dom) req_body = { "comment": comment, "ttl": ttl, } ret = clt.update_domain(dom, emailAddress, ttl, comment) mgr._async_call.assert_called_once_with( uri, method="PUT", body=req_body, error_class=exc.DomainUpdateFailed, has_response=False)
def _create_body(self, name, img=None, cont=None, img_format=None, img_name=None): """ Used to create a new task. Since tasks don't have names, the required 'name' parameter is used for the type of task: 'import' or 'export'. """ img = utils.get_id(img) cont = utils.get_name(cont) body = {"type": name} if name == "export": body["input"] = { "image_uuid": img, "receiving_swift_container": cont} else: nm = "%s/%s" % (cont, utils.get_name(img)) body["input"] = { "image_properties": {"name": img_name or img}, "import_from": nm, "import_from_format": img_format or DEFAULT_FORMAT} return body
def test_search_records(self): clt = self.client mgr = clt._manager dom = self.domain typ = "A" uri = "/domains/%s/records?type=%s" % (utils.get_id(dom), typ) ret_body = {"records": [{"type": typ}]} mgr.count = 0 def mock_get(uri): if mgr.count: return ({}, ret_body) mgr.count += 1 ret = {"totalEntries": 2, "links": [{"href": uri, "rel": "next"}]} ret.update(ret_body) return ({}, ret) clt.method_get = Mock(wraps=mock_get) clt.search_records(dom, typ) calls = [call(uri), call(uri)] clt.method_get.assert_has_calls(calls)
def restore_backup(self, backup, name, flavor, volume): """ Restores a backup to a new database instance. You must supply a backup (either the ID or a CloudDatabaseBackup object), a name for the new instance, as well as a flavor and volume size (in GB) for the instance. """ flavor_ref = self.api._get_flavor_ref(flavor) body = { "instance": { "name": name, "flavorRef": flavor_ref, "volume": { "size": volume }, "restorePoint": { "backupRef": utils.get_id(backup) }, } } uri = "/%s" % self.uri_base resp, resp_body = self.api.method_post(uri, body=body) return CloudDatabaseInstance(self, resp_body.get("instance", {}))
def update(self, img, value_dict): """ Accepts an image reference (object or ID) and dictionary of key/value pairs, where the key is an attribute of the image, and the value is the desired new value for that image. NOTE: There is a bug in Glance where the 'add' operation returns a 409 if the property already exists, which conflicts with the spec. So to get around this a fresh copy of the image must be retrieved, and the value of 'op' must be determined based on whether this attribute exists or not. """ img = self.get(img) uri = "/%s/%s" % (self.uri_base, utils.get_id(img)) body = [] for key, val in value_dict.items(): op = "replace" if key in img.__dict__ else "add" body.append({"op": op, "path": "/%s" % key, "value": val}) headers = {"Content-Type": "application/openstack-images-v2.1-json-patch"} resp, resp_body = self.api.method_patch(uri, body=body, headers=headers)
def delete(self, item): """Deletes the specified item.""" uri = "/%s/%s" % (self.uri_base, utils.get_id(item)) return self._delete(uri)
def head(self, item): """Makes a HEAD request on a specific item.""" uri = "/%s/%s" % (self.uri_base, utils.get_id(item)) return self._head(uri)
def get(self, item): """Gets a specific item.""" uri = "/%s/%s" % (self.uri_base, utils.get_id(item)) return self._get(uri)