def autodisable_cloud(cloud): """Disable cloud after multiple failures and notify user""" log.warning("Autodisabling %s", cloud) cloud.ctl.disable() title = "Cloud %s has been automatically disabled" % cloud.title message = "%s after multiple failures to connect to it." % title notify_user(cloud.owner, title=cloud, message=message, email_notify=True)
def run(self, resource, *args, **kwargs): from mist.api.config import CORE_URI resource_type = getattr(self._instance, 'resource_model_name', 'org') if resource_type == 'org': resource_url = CORE_URI else: resource_url = '%s/%ss/%s' % (CORE_URI, resource_type, resource.id) if hasattr(resource, "name"): resource_name = resource.name elif hasattr(resource, "title"): resource_name = resource.title else: resource_name = 'unknown' if self.json: json_body = self.json.replace("{resource_id}", getattr(resource, 'id', '')).replace( "{resource_url}", resource_url).replace( "{resource_name}", resource_name) json_body = json.loads(json_body) else: json_body = None if self.data: data = self.data.replace("{resource_id}", getattr(resource, 'id', '')).replace( "{resource_url}", resource_url).replace( "{resource_name}", resource.name) else: data = None headers = json.loads(self.headers) if self.headers else None response = requests.request(self.method, self.url, params=self.params, data=data, json=json_body, headers=headers) # Notify user & admin if response indicates an error if not response.ok: title = "Webhook for rule `%s` responded with http code `%d`" % ( self._instance.title, response.status_code) try: body = "URL: %s\n Response body: %s\n" % (self.url, str(response.json())) except json.JSONDecodeError: body = "URL: %s\n Response body: %s\n" % (self.url, response.text) log.info("%s - %s - %s", title, self._instance.id, body) from mist.api.methods import notify_user, notify_admin notify_user(self._instance.owner, title, message=body) notify_admin(title + ' ' + self._instance.id, body) return {'status_code': response.status_code}