Esempio n. 1
0
class ModuleExecutor(object):
    def __init__(self, module):
        self.module = module
        self.main_nitro_class = 'rba_policy'
        self.fetcher = NitroAPIFetcher(module)

        # Dictionary containing attribute information
        # for each NITRO object utilized by this module
        self.attribute_config = {
            'rba_policy': {
                'attributes_list': [
                    'tenant_id',
                    'statement',
                    'ui',
                    'name',
                    'id',
                    'description',
                ],
                'transforms': {},
                'get_id_attributes': [
                    'name',
                    'id',
                ],
                'delete_id_attributes': [
                    'name',
                    'id',
                ],
            },
        }

        self.module_result = dict(
            changed=False,
            failed=False,
            loglines=loglines,
        )
        self.calculate_configured_rba_policy()
        self.fetch_rba_policy()

    def calculate_configured_rba_policy(self):
        log('ModuleExecutor.calculate_configured_rba_policy()')
        self.configured_rba_policy = {}
        for attribute in self.attribute_config['rba_policy'][
                'attributes_list']:
            value = self.module.params.get(attribute)
            # Skip null values
            if value is None:
                continue
            transform = self.attribute_config['rba_policy']['transforms'].get(
                attribute)
            if transform is not None:
                value = transform(value)
            self.configured_rba_policy[attribute] = value

        log('calculated configured rba_policy %s' % self.configured_rba_policy)

    def fetch_rba_policy(self):
        log('ModuleExecutor.fetch_rba_policy()')
        self.fetched_rba_policy = {}

        # The following fetch will always succeed
        # The result will be an array of all existing rba_policies
        result = self.fetcher.get('rba_policy')
        log('get result %s' % result)

        for rba_policy in result['data']['rba_policy']:
            match = True
            for get_id_attribute in self.attribute_config['rba_policy'][
                    'get_id_attributes']:
                fetched_value = rba_policy.get(get_id_attribute)
                configured_value = self.configured_rba_policy.get(
                    get_id_attribute)
                # Do not compare if it is not defined
                if configured_value is None:
                    continue
                # Emulate AND between get_id_attributes
                if configured_value != fetched_value:
                    match = False
            if match:
                self.fetched_rba_policy = rba_policy

        log('fetched rba_policy %s' % self.fetched_rba_policy)

    def rba_policy_exists(self):
        log('ModuleExecutor.rba_policy_exists()')

        if self.fetched_rba_policy == {}:
            return False
        else:
            return True

    def element_in_fetched(self, item, fetched):
        # task configured item keys drive the comparison
        log('ModuleExecutor.element_in_fetched()')
        for fetched_item in fetched:
            identical = True
            for attribute in item:
                item_value = item.get(attribute)
                fetched_value = fetched_item.get(attribute)
                if item_value != fetched_value:
                    str_tuple = (attribute, type(item_value), item_value,
                                 type(fetched_value), fetched_value)
                    # Message is too verbose for normal use
                    # Leaving it commented here for debugging when needed
                    # log('fetched item differs %s item: (%s) %s fetched: (%s) %s' % str_tuple)
                    identical = False
                    break
            if identical:
                return True
        # Fallthrough

        return False

    def element_in_configured(self, item, configured):
        # task configured list item keys drive the comparison
        log('ModuleExecutor.element_in_configured()')

        for configured_item in configured:
            identical = True
            for attribute in configured_item:
                item_value = item.get(attribute)
                configured_value = configured_item.get(attribute)
                if item_value != configured_value:
                    str_tuple = (attribute, type(item_value), item_value,
                                 type(configured_value), configured_value)
                    # Message is too verbose for normal use
                    # Leaving it commented here for debugging when needed
                    # log('configured item differs %s item: (%s) %s configured: (%s) %s' % str_tuple)
                    identical = False
                    break
            if identical:
                return True
        # Fallthrough

        return False

    def rba_policy_identical(self):
        log('ModuleExecutor.rba_policy_identical()')
        is_identical = True

        # Compare simple attributes
        for attribute in self.configured_rba_policy:
            if attribute in ['ui', 'statement']:
                continue
            configured_value = self.configured_rba_policy.get(attribute)
            fetched_value = self.fetched_rba_policy.get(attribute)
            if configured_value != fetched_value:
                is_identical = False
                str_tuple = (attribute,
                             type(configured_value), configured_value,
                             type(fetched_value), fetched_value)
                log('Attribute %s differs. configured: (%s) %s  fetched: (%s) %s'
                    % str_tuple)

        # Compare ui and statement elements
        for item in self.configured_rba_policy['ui']:
            if not self.element_in_fetched(item,
                                           self.fetched_rba_policy['ui']):
                log('ui element not found in fetched %s' % item)
                is_identical = False

        for item in self.configured_rba_policy['statement']:
            if not self.element_in_fetched(
                    item, self.fetched_rba_policy['statement']):
                log('statement element not found in fetched %s' % item)
                is_identical = False

        for item in self.fetched_rba_policy['ui']:
            if not self.element_in_configured(
                    item, self.configured_rba_policy['ui']):
                log('ui element not found in configured %s' % item)
                is_identical = False

        for item in self.fetched_rba_policy['statement']:
            if not self.element_in_configured(
                    item, self.configured_rba_policy['statement']):
                log('statement element not found in configured %s' % item)
                is_identical = False

        return is_identical

    def create_rba_policy(self):
        log('ModuleExecutor.create_rba_policy()')

        post_data = {'rba_policy': self.configured_rba_policy}

        log('post data: %s' % post_data)
        result = self.fetcher.post(post_data=post_data, resource='rba_policy')

        log('result of post: %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result[
                'http_response_data']['status']
            self.module.fail_json(msg=msg, **self.module_result)

    def update_rba_policy(self):
        log('ModuleExecutor.update_rba_policy()')

        put_payload = self.configured_rba_policy

        put_data = {'rba_policy': put_payload}

        log('request put data: %s' % put_data)

        id = self.fetched_rba_policy['id']
        result = self.fetcher.put(put_data=put_data,
                                  resource='rba_policy',
                                  id=id)

        log('result of put: %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result[
                'http_response_data']['status']

    def delete_rba_policy(self):
        log('ModuleExecutor.delete_rba_policy()')

        id = self.fetched_rba_policy['id']

        result = self.fetcher.delete(resource='rba_policy', id=id)
        log('delete result %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result[
                'http_response_data']['status']

    def update_or_create(self):
        log('ModuleExecutor.update_or_create()')

        if not self.rba_policy_exists():
            self.module_result['changed'] = True
            if not self.module.check_mode:
                self.create_rba_policy()
        else:
            if not self.rba_policy_identical():
                self.module_result['changed'] = True
                if not self.module.check_mode:
                    self.update_rba_policy()

        # Update with rba_policy key
        self.fetch_rba_policy()
        self.module_result['rba_policy'] = self.fetched_rba_policy

    def delete(self):
        log('ModuleExecutor.delete()')

        if self.rba_policy_exists():
            self.module_result['changed'] = True
            if not self.module.check_mode:
                self.delete_rba_policy()

    def main(self):
        try:

            if self.module.params['state'] == 'present':
                self.update_or_create()

            elif self.module.params['state'] == 'absent':
                self.delete()

            self.module.exit_json(**self.module_result)

        except NitroException as e:
            msg = "nitro exception errorcode=%s, message=%s, severity=%s" % (
                str(e.errorcode), e.message, e.severity)
            self.module.fail_json(msg=msg, **self.module_result)
        except Exception as e:
            msg = 'Exception %s: %s' % (type(e), str(e))
            self.module.fail_json(msg=msg, **self.module_result)
class ModuleExecutor(object):

    def __init__(self, module):
        self.module = module
        self.main_nitro_class = 'managed_device'
        self.fetcher = NitroAPIFetcher(module)

        # Dictionary containing attribute information
        # for each NITRO object utilized by this module
        self.attribute_config = {
            'managed_device': {
                'attributes_list': [
                    'instance_classifier',
                    'hostname',
                    'std_bw_config',
                    'gateway_deployment',
                    'gateway_ipv6',
                    'instance_available',
                    'device_finger_print',
                    'name',
                    'ent_bw_available',
                    'description',
                    'is_autoscale_group',
                    'geo_support',
                    'sslvpn_config',
                    'mastools_version',
                    'sysservices',
                    'ent_bw_total',
                    'vcpu_config',
                    'netmask',
                    'autoprovisioned',
                    'ent_bw_config',
                    'datacenter_id',
                    'instance_config',
                    'is_managed',
                    'discovery_time',
                    'instance_mode',
                    'instance_total',
                    'is_ha_configured',
                    'trust_id',
                    'ipv4_address',
                    'profile_name',
                    'std_bw_available',
                    'servicepackage',
                    'last_updated_time',
                    'plt_bw_total',
                    'id',
                    'mgmt_ip_address',
                    'ipv6_address',
                    'partition_id',
                    'license_edition',
                    'plt_bw_available',
                    'device_family',
                    'template_interval',
                    'type',
                    'gateway',
                    'internal_annotation',
                    'config_type',
                    'node_id',
                    'isolation_policy',
                    'ip_address',
                    'provision_request_id',
                    'httpxforwardedfor',
                    'std_bw_total',
                    'display_name',
                    'plt_bw_config',
                    'partition_name',
                    'agent_id',
                    'sslvpn_total',
                    'peer_device_ip',
                    'profile_password',
                    'file_name',
                    'profile_username',
                    'file_location_path',
                    'peer_host_device_ip',
                    'device_host_ip',
                    'tr_task_id',
                    'entity_tag',
                ],
                'transforms': {
                },
                'get_id_attributes': [
                    'ip_address',
                ],
                'delete_id_attributes': [
                ],
            },

        }

        self.module_result = dict(
            changed=False,
            failed=False,
            loglines=loglines,
        )
        self.calculate_configured_managed_device()
        self.fetch_managed_device()

    def calculate_configured_managed_device(self):
        log('ModuleExecutor.calculate_configured_managed_device()')
        self.configured_managed_device = {}
        for attribute in self.attribute_config['managed_device']['attributes_list']:
            value = self.module.params.get(attribute)
            # Skip null values
            if value is None:
                continue
            transform = self.attribute_config['managed_device']['transforms'].get(attribute)
            if transform is not None:
                value = transform(value)
            self.configured_managed_device[attribute] = value

        log('calculated configured managed_device %s' % self.configured_managed_device)

    def fetch_managed_device(self):
        log('ModuleExecutor.fetch_managed_device()')
        self.fetched_managed_device = {}

        # The following fetch will always succeed
        # The result will be an array of all existing managed devices
        result = self.fetcher.get('managed_device')
        log('get result %s' % result)

        for managed_device in result['data']['managed_device']:
            match = True
            for get_id_attribute in self.attribute_config['managed_device']['get_id_attributes']:
                fetched_value = managed_device.get(get_id_attribute)
                configured_value = self.configured_managed_device.get(get_id_attribute)
                # Do not compare if it is not defined
                if configured_value is None:
                    continue
                # Emulate AND between get_id_attributes
                if configured_value != fetched_value:
                    match = False
            if match:
                self.fetched_managed_device = managed_device

        log('fetched managed device %s' % self.fetched_managed_device)


    def managed_device_exists(self):
        log('ModuleExecutor.managed_device_exists()')

        if self.fetched_managed_device == {}:
            return False
        else:
            return True

    def managed_device_identical(self):
        log('ModuleExecutor.managed_device_identical()')
        is_identical = True

        # Compare simple attributes
        for attribute in self.configured_managed_device:
            configured_value = self.configured_managed_device.get(attribute)
            fetched_value = self.fetched_managed_device.get(attribute)
            if configured_value != fetched_value:
                is_identical = False
                str_tuple = (attribute, type(configured_value), configured_value, type(fetched_value), fetched_value)
                log('Attribute %s differs. configured: (%s) %s  fetched: (%s) %s' % str_tuple)

        return is_identical

    def create_managed_device(self):
        log('ModuleExecutor.create_managed_device()')

        post_data = {
            'managed_device': self.configured_managed_device,
        }

        log('post data: %s' % post_data)
        result = self.fetcher.post(post_data=post_data, resource='managed_device', action='add_device')

        log('result of post: %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result['http_response_data']['status']
            self.module.fail_json(msg=msg, **self.module_result)

    def update_managed_device(self):
        log('ModuleExecutor.update_managed_device()')

        put_payload = self.configured_managed_device

        put_data = {
            'managed_device': put_payload
        }

        log('request put data: %s' % put_data)

        id = self.fetched_managed_device['id']
        result = self.fetcher.put(put_data=put_data, resource='managed_device', id=id)

        log('result of put: %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result['http_response_data']['status']

    def delete_managed_device(self):
        log('ModuleExecutor.delete_managed_device()')

        id = self.fetched_managed_device['id']

        result = self.fetcher.delete(resource='managed_device', id=id)
        log('delete result %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result['http_response_data']['status']

    def update_or_create(self):
        log('ModuleExecutor.update_or_create()')

        if not self.managed_device_exists():
            self.module_result['changed'] = True
            if not self.module.check_mode:
                self.create_managed_device()
        else:
            if not self.managed_device_identical():
                self.module_result['changed'] = True
                if not self.module.check_mode:
                    self.update_managed_device()

        # Update with managed device key
        self.fetch_managed_device()
        self.module_result['managed_device'] = self.fetched_managed_device


    def delete(self):
        log('ModuleExecutor.delete()')

        if self.managed_device_exists():
            self.module_result['changed'] = True
            if not self.module.check_mode:
                self.delete_managed_device()

    def main(self):
        try:

            if self.module.params['state'] == 'present':
                self.update_or_create()

            elif self.module.params['state'] == 'absent':
                self.delete()

            self.module.exit_json(**self.module_result)

        except NitroException as e:
            msg = "nitro exception errorcode=%s, message=%s, severity=%s" % (str(e.errorcode), e.message, e.severity)
            self.module.fail_json(msg=msg, **self.module_result)
        except Exception as e:
            msg = 'Exception %s: %s' % (type(e), str(e))
            self.module.fail_json(msg=msg, **self.module_result)
Esempio n. 3
0
class ModuleExecutor(object):
    def __init__(self, module):
        self.module = module
        self.main_nitro_class = 'ns_device_profile'
        self.fetcher = NitroAPIFetcher(module)

        # Dictionary containing attribute information
        # for each NITRO object utilized by this module
        self.attribute_config = {
            'ns_device_profile': {
                'attributes_list': [
                    'name',
                    'svm_ns_comm',
                    'use_global_setting_for_communication_with_ns',
                    'id',
                    'type',
                    'snmpsecurityname',
                    'snmpauthprotocol',
                    'ssl_private_key',
                    'ssl_cert',
                    'http_port',
                    'ns_profile_name',
                    'ssh_port',
                    'password',
                    'snmpsecuritylevel',
                    'snmpcommunity',
                    'passphrase',
                    'snmpprivprotocol',
                    'https_port',
                    'username',
                    'host_password',
                    'max_wait_time_reboot',
                    'snmpprivpassword',
                    'snmpversion',
                    'cb_profile_name',
                    'snmpauthpassword',
                    'host_username',
                ],
                'transforms': {},
                'get_id_attributes': [
                    'name',
                ],
                'delete_id_attributes': [],
            },
        }

        self.module_result = dict(
            changed=False,
            failed=False,
            loglines=loglines,
        )
        self.calculate_configured_ns_device_profile()
        self.fetch_ns_device_profile()

    def calculate_configured_ns_device_profile(self):
        log('ModuleExecutor.calculate_configured_ns_device_profile()')
        self.configured_ns_device_profile = {}
        for attribute in self.attribute_config['ns_device_profile'][
                'attributes_list']:
            value = self.module.params.get(attribute)
            # Skip null values
            if value is None:
                continue
            transform = self.attribute_config['ns_device_profile'][
                'transforms'].get(attribute)
            if transform is not None:
                value = transform(value)
            self.configured_ns_device_profile[attribute] = value

        log('calculated configured ns_device_profile %s' %
            self.configured_ns_device_profile)

    def fetch_ns_device_profile(self):
        log('ModuleExecutor.fetch_ns_device_profile()')
        self.fetched_ns_device_profile = {}

        # The following fetch will always succeed
        # The result will be an array of all existing profiles
        result = self.fetcher.get('ns_device_profile')
        log('get result %s' % result)

        for ns_device_profile in result['data']['ns_device_profile']:
            match = True
            for get_id_attribute in self.attribute_config['ns_device_profile'][
                    'get_id_attributes']:
                fetched_value = ns_device_profile.get(get_id_attribute)
                configured_value = self.configured_ns_device_profile.get(
                    get_id_attribute)
                # Do not compare if it is not defined
                if configured_value is None:
                    continue
                # Emulate AND between get_id_attributes
                if configured_value != fetched_value:
                    match = False
            if match:
                self.fetched_ns_device_profile = ns_device_profile

        log('fetched ns_device_profile device %s' %
            self.fetched_ns_device_profile)

    def ns_device_profile_exists(self):
        log('ModuleExecutor.ns_device_profile_exists()')

        if self.fetched_ns_device_profile == {}:
            return False
        else:
            return True

    def ns_device_profile_identical(self):
        log('ModuleExecutor.ns_device_profile_identical()')
        is_identical = True

        # Compare simple attributes
        skip_attributes = [
            'password',
            'host_password',
            'passphrase',
            'snmpprivpassword',
            'snmpauthpassword',
        ]
        for attribute in self.configured_ns_device_profile:
            if attribute in skip_attributes:
                continue
            configured_value = self.configured_ns_device_profile.get(attribute)
            fetched_value = self.fetched_ns_device_profile.get(attribute)
            if configured_value != fetched_value:
                is_identical = False
                str_tuple = (attribute,
                             type(configured_value), configured_value,
                             type(fetched_value), fetched_value)
                log('Attribute %s differs. configured: (%s) %s  fetched: (%s) %s'
                    % str_tuple)

        return is_identical

    def create_ns_device_profile(self):
        log('ModuleExecutor.create_ns_device_profile()')

        post_data = {
            'ns_device_profile': self.configured_ns_device_profile,
        }

        log('post data: %s' % post_data)
        result = self.fetcher.post(post_data=post_data,
                                   resource='ns_device_profile')

        log('result of post: %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result[
                'http_response_data']['status']
            self.module.fail_json(msg=msg, **self.module_result)

    def update_ns_device_profile(self):
        log('ModuleExecutor.update_ns_device_profile()')

        put_payload = self.configured_ns_device_profile

        put_data = {'ns_device_profile': put_payload}

        log('request put data: %s' % put_data)

        id = self.fetched_ns_device_profile['id']
        result = self.fetcher.put(put_data=put_data,
                                  resource='ns_device_profile',
                                  id=id)

        log('result of put: %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result[
                'http_response_data']['status']

    def delete_ns_device_profile(self):
        log('ModuleExecutor.delete_ns_device_profile()')

        id = self.fetched_ns_device_profile['id']

        result = self.fetcher.delete(resource='ns_device_profile', id=id)
        log('delete result %s' % result)

        if result['http_response_data']['status'] == 200:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 200 or 4xx (%s)' % result[
                'http_response_data']['status']

    def update_or_create(self):
        log('ModuleExecutor.update_or_create()')

        if not self.ns_device_profile_exists():
            self.module_result['changed'] = True
            if not self.module.check_mode:
                self.create_ns_device_profile()
        else:
            if not self.ns_device_profile_identical():
                self.module_result['changed'] = True
                if not self.module.check_mode:
                    self.update_ns_device_profile()

        # Update with fetched ns device profile key
        self.fetch_ns_device_profile()
        self.module_result[
            'ns_device_profile'] = self.fetched_ns_device_profile

    def delete(self):
        log('ModuleExecutor.delete()')

        if self.ns_device_profile_exists():
            self.module_result['changed'] = True
            if not self.module.check_mode:
                self.delete_ns_device_profile()

    def main(self):
        try:

            if self.module.params['state'] == 'present':
                self.update_or_create()

            elif self.module.params['state'] == 'absent':
                self.delete()

            self.module.exit_json(**self.module_result)

        except NitroException as e:
            msg = "nitro exception errorcode=%s, message=%s, severity=%s" % (
                str(e.errorcode), e.message, e.severity)
            self.module.fail_json(msg=msg, **self.module_result)
        except Exception as e:
            msg = 'Exception %s: %s' % (type(e), str(e))
            self.module.fail_json(msg=msg, **self.module_result)