def add(self, schema, **kwargs): if schema is None: schema = schemaClass(**kwargs) else: if not isinstance(schema, schemaClass): raise SchemaError(schemaClass) payload = self.hbot._create_payload(schema) url = self.hbot.urlfor.__getattribute__(urlfor)(payload[payload_key]) response = self.api.post(url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def add(self, schema: DeviceGroupSchema = None, **kwargs): """ Add device group to HealthBot :param object schema: `DeviceGroupSchema <jnpr.healthbot.swagger.models.html#devicegroupschema>`_ :param object kwargs: key values, which can be used to create DeviceGroupSchema Check `DeviceGroupSchema <jnpr.healthbot.swagger.models.html#devicegroupschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient from jnpr.healthbot import DeviceSchema from jnpr.healthbot import DeviceGroupSchema hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') ds = DeviceSchema(device_id='xyz', host='xx.xxx.xxx.xxx', authentication={"password": {"password": "******", "username": "******"}}) # This will add device in candidate DB hb.device.add(schema=ds) dgs = DeviceGroupSchema(device_group_name="edge", description="All devices on the edge", devices=['xyz']) hb.device_group.add(dgs) # commit changes to master DB hb.commit() :returns: True when OK """ if schema is None: schema = DeviceGroupSchema(**kwargs) else: if not isinstance(schema, DeviceGroupSchema): raise SchemaError(DeviceGroupSchema) payload = self.hbot._create_payload(schema) device_group_url = self.hbot.urlfor.device_group( payload['device-group-name']) # Add Device Group to the Healthbot response = self.api.post(device_group_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def add(self, schema: NetworkGroupSchema = None, **kwargs): """ Create Network Group :param object schema: `NetworkGroupSchema <jnpr.healthbot.swagger.models.html#networkgroupschema>`_ :param object kwargs: key values, which can be used to create NetworkGroupSchema Check `NetworkGroupSchema <jnpr.healthbot.swagger.models.html#networkgroupschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: hb.devices.add_network_group(network_group_name="HbEZ") # or from jnpr.healthbot import HealthBotClient from jnpr.healthbot import NetworkGroupSchema with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: ngs = NetworkGroupSchema(network_group_name="HbEZ") hb.network_group.add(schema = ngs) """ if schema is None: schema = NetworkGroupSchema(**kwargs) else: if not isinstance(schema, NetworkGroupSchema): raise SchemaError(NetworkGroupSchema) payload = self.hbot._create_payload(schema) # check if the group already exists. If it does, then return now # ... this means this function will not do an "ensure / merge" of data # if the group already exists network_group_url = self.hbot.urlfor.network_group( payload['network-group-name']) resp = self.api.get(network_group_url + self.hbot.apiopt_candidate) if resp.ok: return True response = self.api.post(network_group_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def update(self, schema: DeviceSchema = None, **kwargs): """ Update `DeviceSchema <jnpr.healthbot.swagger.models.html#deviceschema>`_ for given device schema Passing Schema invoke `put` and kwargs `post` :param obj schema: `DeviceSchema <jnpr.healthbot.swagger.models.html#deviceschema>`_ :param object kwargs: key values, which can be used to create DeviceSchema Check `DeviceSchema <jnpr.healthbot.swagger.models.html#deviceschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: schemaObj = hb.device.get('xyz') schemaObj.description = 'changed description' hb.device.update(schemaObj) hb.device.update(device_id="xyz", host='xx.xxx.x.xx', system_id="xxxx") :returns: True when OK """ if schema is None: schema = DeviceSchema(**kwargs) call = self.api.post else: if not isinstance(schema, DeviceSchema): raise SchemaError(DeviceSchema) call = self.api.put payload = self.hbot._create_payload(schema) device_id = schema.device_id device_url = self.hbot.urlfor.device(device_id) response = call(device_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def update(self, schema: PlaybookSchema = None, **kwargs): """ Update `PlaybookSchema <jnpr.healthbot.swagger.models.html#playbookschema>`_ for given playbook schema Passing Schema invoke `put` and kwargs `post` :param obj schema: `PlaybookSchema <jnpr.healthbot.swagger.models.html#playbookschema>`_ :param object kwargs: key values, which can be used to create PlaybookSchema Check `PlaybookSchema <jnpr.healthbot.swagger.models.html#playbookschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: schemaObj = hb.playbook.get('xyz') schemaObj.description = 'changed description' hb.playbook.update(schemaObj) :returns: True when OK """ if schema is None: schema = PlaybookSchema(**kwargs) call = self.api.post else: if not isinstance(schema, PlaybookSchema): raise SchemaError(PlaybookSchema) call = self.api.put payload = self.hbot._create_payload(schema) name = schema.playbook_name playbook_url = self.hbot.urlfor.playbook(name) response = call(playbook_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def update(self, schema: Groups = None, **kwargs): """ Update `Groups <jnpr.healthbot.swagger.models.html#Groups>`_ for given Groups schema Only support `post` and not `put` :param obj schema: `Groups <jnpr.healthbot.swagger.models.html#Groups>`_ :param object kwargs: key values, which can be used to create Groups Check `Groups <jnpr.healthbot.swagger.models.html#Groups>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: schema = hb.administration.group.get(group_name="test") schema.users.append(AssociatedUserSchemaInner(user_id= hb.administration.user.get_userid_from_user_name( user_name="mc"), user_name="mc")) print(hb.administration.group.update(schema=schema)) :returns: True when OK """ if schema is None: schema = groups.Groups(**kwargs) else: if not isinstance(schema, Groups): raise SchemaError(Groups) groupid = self.get_groupid_from_group_name(schema.group_name) try: payload = self.hbot._create_payload(schema) self._admin_api.update_group(self.authorization, groupid=groupid, group=payload) except ApiException as ex: raise ex return True
def update(self, schema: SshKeyProfileSchema = None, **kwargs): """ Update `SshKeyProfileSchema <jnpr.healthbot.swagger.models.html#sshkeyprofileschema>`_ for given ssh key profile schema Passing Schema invoke `put` and kwargs `post` :param obj schema: `SshKeyProfileSchema <jnpr.healthbot.swagger.models.html#sshkeyprofileschema>`_ :param object kwargs: key values, which can be used to create SshKeyProfileSchema Check `SshKeyProfileSchema <jnpr.healthbot.swagger.models.html#sshkeyprofileschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: schemaObj = hb.settings.security.ssh_key_profile.get('xyz') schemaObj.certificate_authority_crt = 'pqr.crt' hb.settings.security.ssh_key_profile.update(schemaObj) :returns: True when OK """ if schema is None: schema = SshKeyProfileSchema(**kwargs) call = self.api.post else: if not isinstance(schema, SshKeyProfileSchema): raise SchemaError(SshKeyProfileSchema) call = self.api.put payload = self.hbot._create_payload(schema) name = schema.name ssh_key_profile_url = self.hbot.urlfor.ssh_key_profile(name) response = call(ssh_key_profile_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def update(self, schema: RetentionPolicySchema = None, **kwargs): """ Update `RetentionPolicySchema <jnpr.healthbot.swagger.models.html#retentionpolicyschema>`_ for given RetentionPolicySchema schema Passing Schema invoke `put` and kwargs `post` :param obj schema: `RetentionPolicySchema <jnpr.healthbot.swagger.models.html#retentionpolicyschema>`_ :param object kwargs: key values, which can be used to create RetentionPolicySchema Check `RetentionPolicySchema <jnpr.healthbot.swagger.models.html#retentionpolicyschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') schemaObj = hb.settings.retention_policy.get('xyz') schemaObj.description = 'changed description' hb.settings.retention_policy.update(schemaObj) :returns: True when OK """ if schema is None: schema = RetentionPolicySchema(**kwargs) call = self.api.post else: if not isinstance(schema, RetentionPolicySchema): raise SchemaError(RetentionPolicySchema) call = self.api.put payload = self.hbot._create_payload(schema) name = schema.retention_policy_name retention_policy_url = self.hbot.urlfor.retention_policy(name) response = call(retention_policy_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def update(self, schema: LocalCertificateSchema = None, **kwargs): """ Update `LocalCertificateSchema <jnpr.healthbot.swagger.models.html#localcertificateschema>`_ for given local certificate schema Passing Schema invoke `put` and kwargs `post` :param obj schema: `LocalCertificateSchema <jnpr.healthbot.swagger.models.html#localcertificateschema>`_ :param object kwargs: key values, which can be used to create LocalCertificateSchema Check `LocalCertificateSchema <jnpr.healthbot.swagger.models.html#localcertificateschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') schemaObj = hb.settings.security.local_certificate.get('xyz') schemaObj.client_key = 'xyz.key' hb.settings.security.local_certificate.update(schemaObj) :returns: True when OK """ if schema is None: schema = LocalCertificateSchema(**kwargs) call = self.api.post else: if not isinstance(schema, LocalCertificateSchema): raise SchemaError(LocalCertificateSchema) call = self.api.put payload = self.hbot._create_payload(schema) name = schema.name local_certificate_url = self.hbot.urlfor.local_certificate(name) response = call(local_certificate_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def update(self, schema: UserSchema = None, **kwargs): """ Update `UserSchema <jnpr.healthbot.swagger.models.html#UserSchema>`_ for given UserSchema schema Only support `post` and not `put` :param obj schema: `UserSchema <jnpr.healthbot.swagger.models.html#UserSchema>`_ :param object kwargs: key values, which can be used to create UserSchema Check `UserSchema <jnpr.healthbot.swagger.models.html#UserSchema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: schemaObj = hb.administration.user.get(user_name="test") schemaObj.last_name = 'Kumar' schemaObj = hb.administration.user.update(schemaObj) :returns: True when OK """ if schema is None: schema = user.User(**kwargs) else: if not isinstance(schema, UserSchema): raise SchemaError(UserSchema) userid = self.get_userid_from_user_name(schema.user_name) try: payload = self.hbot._create_payload(schema) self._admin_api.update_user_profile(self.authorization, userid=userid, user=payload) except ApiException as ex: raise ex return True
def update(self, schema: NetworkGroupSchema = None, **kwargs): """ Update `NetworkGroupSchema <jnpr.healthbot.swagger.models.html#networkgroupschema>`_ for given network schema object Passing Schema invoke `put` and kwargs `post` :param obj schema: `NetworkGroupSchema <jnpr.healthbot.swagger.models.html#networkgroupschema>`_ :param object kwargs: key values, which can be used to create NetworkGroupSchema. Example: :: from jnpr.healthbot import HealthBotClient with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb: schemaObj = hb.network_group.get("HbEZ") schemaObj.description = "HbEZ example" hb.network_group.update(schemaObj) :returns: True when OK """ if schema is None: schema = NetworkGroupSchema(**kwargs) call = self.api.post else: if not isinstance(schema, NetworkGroupSchema): raise SchemaError(NetworkGroupSchema) call = self.api.put payload = self.hbot._create_payload(schema) network_group_url = self.hbot.urlfor.network_group( payload['network-group-name']) response = call(network_group_url, json=payload) if response.status_code != 200: logger.error(response.text) return True
def update(self, topic_name: str, schema: RuleSchema = None, **kwargs): """ Update `RuleSchema <jnpr.healthbot.swagger.models.html#ruleschema>`_ for given rule schema object Passing Schema invoke `put` and kwargs `post` :param str topic_name: The name of the topic under which rule need to be updated :param obj schema: `RuleSchema <jnpr.healthbot.swagger.models.html#ruleschema>`_ :param object kwargs: key values, which can be used to create RuleSchema Example: :: from jnpr.healthbot import HealthBotClient hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') schemaObj = hb.rule.get(topic_name='hbez', rule_name="hbez-fpc-heap-utilization") schemaObj.description = "HbEZ example" hb.rule.update(topic_name='hbez', schemaObj) :returns: True when OK """ if schema is None: schema = RuleSchema(**kwargs) call = self.api.post else: if not isinstance(schema, RuleSchema): raise SchemaError(RuleSchema) call = self.api.put payload = self.hbot._create_payload(schema) rule_url = self.hbot.urlfor.rule(topic_name, payload['rule-name']) response = call(rule_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True
def add(self, device_id: str = None, host: str = None, username: str = None, password: str = None, schema: DeviceSchema = None, **kwargs): """ Add device to HealthBot :param str device_id: The name of the device as provided by the User :param str host: The hostname/ip-address of the target device :param str username: The login user-name for the target device :param str password: The login password for the user :param object schema: `DeviceSchema <jnpr.healthbot.swagger.models.html#deviceschema>`_ Example: :: from jnpr.healthbot import HealthBotClient from jnpr.healthbot import DeviceSchema hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') ds = DeviceSchema(device_id='xyz', host='xx.xxx.xxx.xxx', authentication={"password": {"password": "******", "username": "******"}}) # we can also later assign values like this ds.description = "HbEZ testing" # This will add device in candidate DB hb.device.add(schema=ds) # commit changes to master DB hb.commit() """ def _add_device(device_id=None): if kwargs: if schema is not None: raise SystemError( "schema and kwargs are mutually exclusive") device_schema = DeviceSchema(device_id=device_id, host=host, **kwargs) if username is not None and password is not None: device_schema.authentication = { "password": { "password": password, "username": username } } payload = self.hbot._create_payload(device_schema) elif schema is not None: payload = self.hbot._create_payload(schema) device_id = schema.device_id else: payload = { "authentication": { "password": { "password": password, "username": username } }, "device-id": device_id, "host": host, } url = self.hbot.urlfor.device(device_id) response = self.api.post(url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True if schema is not None: if not isinstance(schema, DeviceSchema): raise SchemaError(DeviceSchema) device_id = schema.device_id host = schema.host devices_list_url = self.hbot.urlfor.devices( ) + self.hbot.apiopt_candidate resp = self.api.get(devices_list_url) # The API will return a 404 if there are no devices in the system. We need to check for # this condition if resp.status_code == 404: return _add_device(device_id) # examine the existing devices and see if there is one that already # exists by this name and host values existing_devices = resp.json()['device'] found = first( filter(lambda i: i['device-id'] == device_id and i['host'] == host, existing_devices)) if found: logger.debug( "Device with given device-id '{}' already exists. Updating same." .format(device_id)) # if we are here, then we need to add this new device return _add_device(device_id)
def add(self, topic_name: str, schema: RuleSchema = None, **kwargs): """ Create Rule under given topic :param str topic_name: Rules to be created under this given topic name :param object schema: `RuleSchema <jnpr.healthbot.swagger.models.html#ruleschema>`_ :param object kwargs: key values, which can be used to create RuleSchema Check `RuleSchema <jnpr.healthbot.swagger.models.html#ruleschema>`_ for details about which all keys can be used Example: :: from jnpr.healthbot import HealthBotClient from jnpr.healthbot import RuleSchema hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') rs = RuleSchema(rule_name="hbez-fpc-heap-utilization") rs.description = "HealthBot EZ example" rs.synopsis = "Using python client for demo" rs.sensor = [{'description': 'Monitors FPC buffer, heap and cpu utilization', 'iAgent': {'file': 'fpc-utilization.yml', 'frequency': '30s', 'table': 'FPCCPUHEAPutilizationTable'}, 'sensor_name': 'fpccpuheaputilization'}] rs.field = [{'constant': {'value': '{{fpc-buffer-usage-threshold}}'}, 'description': 'This field is for buffer usage threshold', 'field_name': 'linecard-buffer-usage-threshold'}, {'constant': {'value': '{{fpc-cpu-usage-threshold}}'}, 'description': 'This field is for linecard cpu usage threshold', 'field_name': 'linecard-cpu-usage-threshold'}, {'constant': {'value': '{{fpc-heap-usage-threshold}}'}, 'description': 'This field is for linecard heap usage threshold', 'field_name': 'linecard-heap-usage-threshold'}] rs.keys = ['slot'] rs.variable = [{'description': 'Linecard Buffer Memory usage threshold value', 'name': 'fpc-buffer-usage-threshold', 'type': 'int', 'value': '80'}, {'description': 'Linecard CPU usage threshold value', 'name': 'fpc-cpu-usage-threshold', 'type': 'int', 'value': '80'}, {'description': 'Linecard Heap Memory usage threshold value', 'name': 'fpc-heap-usage-threshold', 'type': 'int', 'value': '80'}] rs.trigger = [{'description': 'Sets health based on linecard buffer memory', 'frequency': '60s', 'synopsis': 'Linecard buffer memory kpi', 'term': [{'term_name': 'is-buffer-memory-utilization-greater-than-threshold', 'then': {'status': {'color': 'red', 'message': 'FPC buffer memory ' 'utilization ' '($memory-buffer-utilization) ' 'is over threshold ' '($linecard-buffer-usage-threshold)'}}, 'when': {'greater_than': [{'left_operand': '$memory-buffer-utilization', 'right_operand': '$linecard-buffer-usage-threshold'}]}}, {'term_name': 'buffer-utilization-less-than-threshold', 'then': {'status': {'color': 'green'}}}], 'trigger_name': 'fpc-buffer-memory-utilization'}, {'description': 'Sets health based on linecard cpu utilization', 'frequency': '60s', 'synopsis': 'Linecard cpu utilization kpi', 'term': [{'term_name': 'is-cpu-utilization-greater-than-80', 'then': {'status': {'color': 'red', 'message': 'FPC CPU utilization ' '($cpu-total) is over ' 'threshold ' '($linecard-cpu-usage-threshold)'}}, 'when': {'greater_than': [{'left_operand': '$cpu-total', 'right_operand': '$linecard-cpu-usage-threshold', 'time_range': '180s'}]}}, {'term_name': 'cpu-utilization-less-than-threshold', 'then': {'status': {'color': 'green'}}}], 'trigger_name': 'fpc-cpu-utilization'}, {'description': 'Sets health based on linecard heap memory ' 'utilization', 'frequency': '60s', 'synopsis': 'Linecard heap memory kpi', 'term': [{'term_name': 'is-heap-memory-utilization-greater-than-threshold', 'then': {'status': {'color': 'red', 'message': 'FPC heap memory ' 'utilization ' '($memory-heap-utilization) ' 'is over threshold ' '($linecard-heap-usage-threshold)'}}, 'when': {'greater_than': [{'left_operand': '$memory-heap-utilization', 'right_operand': '$linecard-heap-usage-threshold'}]}}, {'term_name': 'heap-memory-utilization-less-than-threshold', 'then': {'status': {'color': 'green'}}}], 'trigger_name': 'fpc-heap-memory-utilization'}] # Add rule to HealthBot print(hb.rule.add('external', schema=rs) Returns: True if action successful """ if schema is None: schema = RuleSchema(**kwargs) else: if not isinstance(schema, RuleSchema): raise SchemaError(RuleSchema) # check for topic presence, if not do all that first topic_url = self.hbot.urlfor.topic(topic_name) resp = self.api.get(topic_url + self.hbot.apiopt_candidate) if not resp.ok: logger.debug("Adding topic '{}' first as its not present".format( topic_name)) response = self.api.post(topic_url, json={"topic-name": topic_name}) if response.status_code != 200: logger.error(response.text) response.raise_for_status() payload = self.hbot._create_payload(schema) rule_url = self.hbot.urlfor.rule(topic_name, payload['rule-name']) resp = self.api.get(rule_url + self.hbot.apiopt_candidate) if resp.ok: return True response = self.api.post(rule_url, json=payload) if response.status_code != 200: logger.error(response.text) response.raise_for_status() return True