Esempio n. 1
0
 def get(self, name: str = None, uncommitted: bool = uncommitted):
     if name is not None:
         url = self.hbot.urlfor.__getattribute__(urlfor)(name)
         if uncommitted:
             url += self.hbot.apiopt_candidate
         response = self.api.get(url)
         if response.status_code != 200:
             logger.error(response.text)
         response.raise_for_status()
         return self.hbot._create_schema(response, schemaClass)
     else:
         url = self.hbot.urlfor.__getattribute__(urlfor)()
         if uncommitted:
             url += self.hbot.apiopt_candidate
         response = self.api.get(url)
         if response.status_code == 404:
             return []
         response_json = response.json()
         if response.status_code != 200:
             logger.error(response.text)
             raise NotFoundError(response_json)
         response.raise_for_status()
         return_list = []
         if return_response_json:
             return response_json
         items = response_json[payload_key]
         for item in items:
             obj = self.hbot._create_schema(item, schemaClass)
             return_list.append(obj)
         return return_list
Esempio n. 2
0
 def get(self, uncommitted: bool = True):
     deployment_url = self.hbot.urlfor.deployment()
     if uncommitted:
         deployment_url += self.hbot.apiopt_candidate
     response = self.api.get(deployment_url)
     response_json = response.json()
     if response.status_code != 200:
         logger.error(response.text)
         raise NotFoundError(response_json)
     return response_json
Esempio n. 3
0
 def get(self, uncommitted: bool = True):
     snmp_notifications_url = self.hbot.urlfor.snmp_notifications()
     if uncommitted:
         snmp_notifications_url += self.hbot.apiopt_candidate
     response = self.api.get(snmp_notifications_url)
     response_json = response.json()
     if response.status_code != 200:
         logger.error(response.text)
         raise NotFoundError(response_json)
     return response_json
Esempio n. 4
0
        def _get_response(url):
            response = self.api.get(url)

            if response.status_code == 404:
                return []

            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            return response_json
Esempio n. 5
0
    def get(self, device_group_name: str = None, uncommitted: bool = True):
        """
        Get `DeviceGroupSchema <jnpr.healthbot.swagger.models.html#devicegroupschema>`_ for
        given device group name or list for all device groups

        :param str device_group_name: Name of the device-group
        :param bool uncommitted: True includes fetches uncommitted changes,
                False restricts data set to only committed changes

        Example:
        ::
            device_group_schema = hb.device_group.get('edge')

            groups = hb.device_group.get()
            for group in groups:
                print(group)

        :return: `DeviceGroupSchema(s) <jnpr.healthbot.swagger.models.html#devicegroupschema>`_
        """
        if device_group_name is not None:
            device_group_url = self.hbot.urlfor.device_group(device_group_name)
            if uncommitted:
                device_group_url += self.hbot.apiopt_candidate

            res = self.api.get(device_group_url)

            # if 404 is returned, it means there is not a device group by that name
            # we need to explicitly check for this given the API behavior

            if res.status_code == 404:
                return None

            return self.hbot._create_schema(res, DeviceGroupSchema)
        else:
            device_groups_url = self.hbot.urlfor.device_groups()
            if uncommitted:
                device_groups_url += self.hbot.apiopt_candidate

            response = self.api.get(device_groups_url)

            if response.status_code == 404:
                return []

            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            device_group_list = []
            existing_device_groups = response_json['device-group']
            for device_group in existing_device_groups:
                obj = self.hbot._create_schema(device_group, DeviceGroupSchema)
                device_group_list.append(obj)
            return device_group_list
Esempio n. 6
0
    def get(
            self,
            retention_policy_name: str = None,
            uncommitted=True):
        """
        Get `RetentionPolicySchema(s) <jnpr.healthbot.swagger.models.html#retentionpolicyschema>`_ for
        given retention policy name or list for all

        :param retention_policy_name: ID of retention-policy-name
        :param bool uncommitted: True includes fetches uncommitted changes,
            False restricts data set to only committed changes

        Example:
            ::
                from jnpr.healthbot import HealthBotClient

                hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx')
                print(hb.settings.retention_policy.get('xyz')

                # for all
                print(hb.settings.retention_policy.get()

        :return: `RetentionPolicySchema(s) <jnpr.healthbot.swagger.models.html#retentionpolicyschema>`_
        """
        if retention_policy_name is not None:
            retention_policy_url = self.hbot.urlfor.retention_policy(
                retention_policy_name)
            if uncommitted:
                retention_policy_url += self.hbot.apiopt_candidate
            response = self.api.get(retention_policy_url)
            if response.status_code != 200:
                logger.error(response.text)
            response.raise_for_status()
            return self.hbot._create_schema(response, RetentionPolicySchema)
        else:
            notifications_url = self.hbot.urlfor.retention_policies()
            if uncommitted:
                notifications_url += self.hbot.apiopt_candidate
            response = self.api.get(notifications_url)
            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            existing_retention_policies = response_json['retention-policy']
            existing_retention_policies_list = []
            for retention_policy in existing_retention_policies:
                obj = self.hbot._create_schema(
                    retention_policy, RetentionPolicySchema)
                existing_retention_policies_list.append(obj)

            return existing_retention_policies_list
Esempio n. 7
0
    def get(self, name: str = None, uncommitted: bool = True):
        """
        Get `ReportSchema(s) <jnpr.healthbot.swagger.models.html#reportschema>`_ for
        given report name or list for all

        :param name: report ID
        :param bool uncommitted: True includes fetches uncommitted changes,
            False restricts data set to only committed changes

        Example:
            ::
                from jnpr.healthbot import HealthBotClient

                hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx')
                print(hb.settings.report.get('xyz')

                # for all
                print(hb.settings.report.get()

        :return: `ReportSchema(s) <jnpr.healthbot.swagger.models.html#reportschema>`_
        """
        if name is not None:
            report_url = self.hbot.urlfor.report(name)
            if uncommitted:
                report_url += self.hbot.apiopt_candidate
            response = self.api.get(report_url)
            if response.status_code != 200:
                logger.error(response.text)
            response.raise_for_status()
            return self.hbot._create_schema(
                response, ReportSchema)
        else:
            reports_url = self.hbot.urlfor.reports()
            if uncommitted:
                reports_url += self.hbot.apiopt_candidate
            response = self.api.get(reports_url)
            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            existing_reports = response_json['report']
            reports_list = []
            for report in existing_reports:
                obj = self.hbot._create_schema(report, ReportSchema)
                reports_list.append(obj)

            return reports_list
Esempio n. 8
0
    def get(self, name: str = None, uncommitted: bool = True):
        """
        Get `SshKeyProfileSchema(s) <jnpr.healthbot.swagger.models.html#sshkeyprofileschema>`_ for
        given ssh key profile name or list for all

        :param name: ID of name
        :param bool uncommitted: True includes fetches uncommitted changes,
            False restricts data set to only committed changes

        Example:
            ::
                from jnpr.healthbot import HealthBotClient

                hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx')
                print(hb.settings.security.ssh_key_profile.get('xyz')

                # for all
                print(hb.settings.security.ssh_key_profile.get()

        :return: `SshKeyProfileSchema(s) <jnpr.healthbot.swagger.models.html#sshkeyprofileschema>`_
        """
        if name is not None:
            ssh_key_profile_url = self.hbot.urlfor.ssh_key_profile(name)
            if uncommitted:
                ssh_key_profile_url += self.hbot.apiopt_candidate
            response = self.api.get(ssh_key_profile_url)
            if response.status_code != 200:
                logger.error(response.text)
            response.raise_for_status()
            return self.hbot._create_schema(response, SshKeyProfileSchema)
        else:
            ssh_key_profiles_url = self.hbot.urlfor.ssh_key_profiles()
            if uncommitted:
                ssh_key_profiles_url += self.hbot.apiopt_candidate
            response = self.api.get(ssh_key_profiles_url)
            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            existing_ssh_key_profiles = response_json['ssh-key-profile']
            ssh_key_profiles_list = []
            for ssh_key_profile in existing_ssh_key_profiles:
                obj = self.hbot._create_schema(ssh_key_profile,
                                               SshKeyProfileSchema)
                ssh_key_profiles_list.append(obj)

            return ssh_key_profiles_list
Esempio n. 9
0
    def get(self, name: str = None, uncommitted: bool = True):
        """
        Get `LocalCertificateSchema(s) <jnpr.healthbot.swagger.models.html#localcertificateschema>`_ for
        given local certificate name or list for all

        :param name: ID of name
        :param bool uncommitted: True includes fetches uncommitted changes,
            False restricts data set to only committed changes

        Example:
            ::
                from jnpr.healthbot import HealthBotClient

                hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx')
                print(hb.settings.security.local_certificate.get('xyz')

                # for all
                print(hb.settings.security.local_certificate.get()

        :return: `LocalCertificateSchema(s) <jnpr.healthbot.swagger.models.html#localcertificateschema>`_
        """
        if name is not None:
            local_cert_url = self.hbot.urlfor.local_certificate(name)
            if uncommitted:
                local_cert_url += self.hbot.apiopt_candidate
            response = self.api.get(local_cert_url)
            if response.status_code != 200:
                logger.error(response.text)
            response.raise_for_status()
            return self.hbot._create_schema(response, LocalCertificateSchema)
        else:
            local_certs_url = self.hbot.urlfor.local_certificates()
            if uncommitted:
                local_certs_url += self.hbot.apiopt_candidate
            response = self.api.get(local_certs_url)
            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            existing_local_certificates = response_json['local-certificate']
            local_certificates_list = []
            for local_certificate in existing_local_certificates:
                obj = self.hbot._create_schema(local_certificate,
                                               LocalCertificateSchema)
                local_certificates_list.append(obj)

            return local_certificates_list
Esempio n. 10
0
    def get(self, name: str = None, uncommitted: bool = True):
        """
        Get `DestinationSchema(s) <jnpr.healthbot.swagger.models.html#destinationschema>`_ for
        given destination name or list for all

        :param name: destination ID
        :param bool uncommitted: True includes fetches uncommitted changes,
            False restricts data set to only committed changes

        Example:
        ::

                from jnpr.healthbot import HealthBotClient

                with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb:
                    print(hb.settings.destination.get('xyz')

                    # for all
                    print(hb.settings.destination.get()

        :return: `DestinationSchema(s) <jnpr.healthbot.swagger.models.html#destinationschema>`_
        """
        if name is not None:
            destination_url = self.hbot.urlfor.destination(name)
            if uncommitted:
                destination_url += self.hbot.apiopt_candidate
            response = self.api.get(destination_url)
            if response.status_code != 200:
                logger.error(response.text)
            response.raise_for_status()
            return self.hbot._create_schema(response, DestinationSchema)
        else:
            destination_url = self.hbot.urlfor.destinations()
            if uncommitted:
                destination_url += self.hbot.apiopt_candidate
            response = self.api.get(destination_url)
            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            existing_destinations = response_json['destination']
            destinations_list = []
            for destination in existing_destinations:
                obj = self.hbot._create_schema(destination, DestinationSchema)
                destinations_list.append(obj)

            return destinations_list
Esempio n. 11
0
    def get(self, network_group_name: str = None, uncommitted: bool = True):
        """
        get Network Group(s) details

        :param str network_group_name: The name of the network group to be fetched
        :param bool uncommitted: True includes fetches uncommitted changes,
                False restricts data set to only committed changes

        Example:
        ::
            from jnpr.healthbot import HealthBotClient

            with HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx') as hb:
                print(hb.network_group.get(network_group_name="HbEZ"))
                # for all network groups
                print(hb.network_group.get())

        """
        if network_group_name is not None:
            network_group_url = self.hbot.urlfor.network_group(
                network_group_name)
            if uncommitted:
                network_group_url += self.hbot.apiopt_candidate
            response = self.api.get(network_group_url)
            if response.status_code != 200:
                logger.error(response.text)
            response.raise_for_status()
            return self.hbot._create_schema(response, NetworkGroupSchema)
        else:
            network_groups_url = self.hbot.urlfor.network_groups()
            if uncommitted:
                network_groups_url += self.hbot.apiopt_candidate
            response = self.api.get(network_groups_url)
            if response.status_code == 404:
                return []
            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            response.raise_for_status()
            network_group_list = []
            existing_network_groups = response_json['network-group']
            for network_group in existing_network_groups:
                obj = self.hbot._create_schema(network_group,
                                               NetworkGroupSchema)
                network_group_list.append(obj)
            return network_group_list
Esempio n. 12
0
    def get(self, notification_name: str = None, uncommitted: bool = True):
        """
        Get `NotificationSchema(s) <jnpr.healthbot.swagger.models.html#notificationschema>`_ for
        given notification name or list for all

        :param notification_name: ID of notification-name
        :param bool uncommitted: True includes fetches uncommitted changes,
            False restricts data set to only committed changes

        Example:
            ::
                from jnpr.healthbot import HealthBotClient

                hb = HealthBotClient('xx.xxx.x.xx', 'xxxx', 'xxxx')
                print(hb.settings.notification.get('xyz')

        :return: `NotificationSchema(s) <jnpr.healthbot.swagger.models.html#notificationschema>`_
        """
        if notification_name is not None:
            notification_url = self.hbot.urlfor.notification(notification_name)
            if uncommitted:
                notification_url += self.hbot.apiopt_candidate
            response = self.api.get(notification_url)
            if response.status_code != 200:
                logger.error(response.text)
            response.raise_for_status()
            return self.hbot._create_schema(response, NotificationSchema)
        else:
            notifications_url = self.hbot.urlfor.notifications()
            if uncommitted:
                notifications_url += self.hbot.apiopt_candidate
            response = self.api.get(notifications_url)
            response_json = response.json()
            if response.status_code != 200:
                logger.error(response.text)
                raise NotFoundError(response_json)
            existing_notifications = response_json['notification']
            notification_list = []
            for notification in existing_notifications:
                obj = self.hbot._create_schema(
                    notification, NotificationSchema)
                notification_list.append(obj)

            return notification_list
Esempio n. 13
0
    def get_groupid_from_group_name(self, group_name: str):
        """
        Get Group ID from given group name

        :param group_name: group name

        """
        try:
            responses = self._admin_api.retrieve_groups(
                authorization=self.authorization)
        except ApiException as ex:
            raise ex
        for response in responses:
            if group_name == response.group_name:
                return response.group_id
        raise NotFoundError({
            'detail':
            'Not able to find groupid for given group_name: "{}"'.format(
                group_name),
            'status':
            404
        })
Esempio n. 14
0
    def get_userid_from_user_name(self, user_name: str):
        """
        Get user ID from given user name

        :param user_name: user name

        """
        try:
            responses = self._admin_api.retrieve_users(
                authorization=self.authorization)
        except ApiException as ex:
            raise ex
        for response in responses:
            if user_name == response.user_name:
                return response.user_id
        raise NotFoundError({
            'detail':
            'Not able to find userid for given '
            'user_name: "{}"'.format(user_name),
            'status':
            404
        })
Esempio n. 15
0
    def delete(self):
        """
        Delete playbook instance

        Example:
        ::

            from jnpr.healthbot import PlayBookInstanceBuilder
            pbb = PlayBookInstanceBuilder(hb, 'forwarding-table-summary', 'HbEZ-instance', 'Core')
            pbb.delete()

        :return: True if success
        """
        delete_playbook_from_group = True
        if self.device_group_name is None:
            raise RuntimeError(
                "PlayBookInstanceBuilder object device group attribute not provided"
            )
        device_instance = Device(self.hbot)
        device_group_instance = DeviceGroup(self.hbot)
        device_group = device_group_instance.get(self.device_group_name)
        if device_group is None:
            raise NotFoundError({
                'detail':
                'No details for device group: {}'.format(
                    self.device_group_name),
                'status':
                404
            })
        for device_id in device_group.devices:
            device = device_instance.get(device_id)
            existing_variables = device.variable or []
            if len(existing_variables) > 0:
                changed = False
                for variable in existing_variables:
                    if self.playbook == variable.get('playbook'):
                        if self.instance_id == variable.get('instance-id'):
                            device.variable.remove(variable)
                            changed = True
                        else:
                            delete_playbook_from_group = False
                if changed and not device_instance.update(device):
                    logger.error(
                        "Not able to update '{}' device id".format(device_id))
                    return False

        existing_variables = copy.copy(device_group.variable) or []
        if len(existing_variables) > 0:
            changed = False
            for variable in existing_variables:
                if self.playbook == variable.get('playbook'):
                    if self.instance_id == variable.get('instance-id'):
                        device_group.variable.remove(variable)
                        changed = True
                    else:
                        delete_playbook_from_group = False
            if changed and not device_group_instance.update(device_group):
                logger.error(
                    "Not able to update '{}' device id".format(device_id))
                return False
        if delete_playbook_from_group:
            update_playbooks = device_group.playbooks or []
            if self.playbook in update_playbooks:
                update_playbooks.remove(self.playbook)
                device_group.playbook = update_playbooks
                if not device_group_instance.update(device_group):
                    logger.error("Not able to update '{}' device group".format(
                        device_group))
                    return False
        return True
Esempio n. 16
0
    def apply(self, device_ids: list = None, commit: bool = False):
        """
        Apply the playbook instance

        :param device_ids: if the rule variables need to be associated for
            given device id(s). Default to device group
        :param commit: Pass true if need to commit the changes

        Example:
        ::

            from jnpr.healthbot import PlayBookInstanceBuilder
            pbb = PlayBookInstanceBuilder(hb, 'forwarding-table-summary', 'HbEZ-instance', 'Core')
            pbb.apply()

        :return: True if all OK
        """
        if self.device_group_name is None:
            raise RuntimeError(
                "PlayBookInstanceBuilder object device group attribute not provided"
            )
        device_instance = Device(self.hbot)
        device_group_instance = DeviceGroup(self.hbot)
        device_group = device_group_instance.get(self.device_group_name)
        if device_group is None:
            raise NotFoundError({
                'detail':
                'No details for device group: {}'.format(
                    self.device_group_name),
                'status':
                404
            })
        if device_ids is not None:
            for device_id in device_ids:
                if device_id not in device_group.devices:
                    raise RuntimeError(
                        "Given device id '{}' is not present".format(
                            device_id))
                device = device_instance.get(device_id)
                device.variable = self.device_variable
                if not device_instance.update(device):
                    logger.error(
                        "Not able to update '{}' device id".format(device_id))
            device_group_variable = []

            for item in self.device_variable:
                tmp = copy.copy(item)
                tmp.pop('variable-value', None)
                device_group_variable.append(tmp)
            existing_variable = device_group.variable
            if existing_variable is None:
                device_group.variable = device_group_variable
            else:
                existing_variable.extend(device_group_variable)
                device_group.variable = existing_variable
            if not device_group_instance.update(device_group):
                logger.error("Not able to update '{}' device group".format(
                    device_group))
                return False
        else:
            existing_variable = device_group.variable
            if existing_variable is None:
                device_group.variable = self.device_variable
            else:
                existing_variable.extend(self.device_variable)
                device_group.variable = existing_variable
            existing_playbooks = device_group.playbooks or []
            existing_playbooks.append(self.playbook)
            device_group.playbooks = existing_playbooks
            if not device_group_instance.update(device_group):
                logger.error("Not able to update '{}' device group".format(
                    device_group))
                return False
        if commit:
            self.hbot.commit()
            return True
        return True