Esempio n. 1
0
    def import_policy_from_file(self,
                                file,
                                update=False,
                                check_mode=False,
                                push=False):

        vmanage_policy_lists = PolicyLists(self.session, self.host, self.port)
        vmanage_policy_definitions = PolicyDefinitions(self.session, self.host,
                                                       self.port)
        vmanage_central_policy = CentralPolicy(self.session, self.host,
                                               self.port)
        vmanage_local_policy = LocalPolicy(self.session, self.host, self.port)

        policy_list_updates = []
        policy_definition_updates = []
        central_policy_updates = []
        local_policy_updates = []

        # Read in the datafile
        if not os.path.exists(file):
            raise Exception('Cannot find file {0}'.format(file))
        with open(file) as f:
            if file.endswith('.yaml') or file.endswith('.yml'):
                policy_data = yaml.safe_load(f)
            else:
                policy_data = json.load(f)

        # Separate the feature template data from the device template data
        if 'vmanage_policy_lists' in policy_data:
            policy_list_data = policy_data['vmanage_policy_lists']
        else:
            policy_list_data = []
        if 'vmanage_policy_definitions' in policy_data:
            policy_definition_data = policy_data['vmanage_policy_definitions']
        else:
            policy_definition_data = []
        if 'vmanage_central_policies' in policy_data:
            central_policy_data = policy_data['vmanage_central_policies']
        else:
            central_policy_data = []
        if 'vmanage_local_policies' in policy_data:
            local_policy_data = policy_data['vmanage_local_policies']
        else:
            local_policy_data = []

        policy_list_updates = vmanage_policy_lists.import_policy_list_list(
            policy_list_data, check_mode=check_mode, update=update, push=push)

        vmanage_policy_lists.clear_policy_list_cache()

        policy_definition_updates = vmanage_policy_definitions.import_policy_definition_list(
            policy_definition_data,
            check_mode=check_mode,
            update=update,
            push=push)
        central_policy_updates = vmanage_central_policy.import_central_policy_list(
            central_policy_data,
            check_mode=check_mode,
            update=update,
            push=push)
        local_policy_updates = vmanage_local_policy.import_local_policy_list(
            local_policy_data, check_mode=check_mode, update=update, push=push)

        return {
            'policy_list_updates': policy_list_updates,
            'policy_definition_updates': policy_definition_updates,
            'central_policy_updates': central_policy_updates,
            'local_policy_updates': local_policy_updates
        }
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'activated', 'deactivated'], default='present'),
                         name=dict(type='str', alias='policyName'),
                         description=dict(type='str', alias='policyDescription'),
                         definition=dict(type='str', alias='policyDefinition'),
                         type=dict(type='list', alias='policyType'),
                         wait=dict(type='bool', default=False),
                         update=dict(type='bool', default=False),
                         push=dict(type='bool', default=False),
                         aggregate=dict(type='list'),
                         )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(
        changed=False,
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True,
                           )
    vmanage = Vmanage(module)
    vmanage_central_policy = CentralPolicy(vmanage.auth, vmanage.host)

    # Always as an aggregate... make a list if just given a single entry
    if vmanage.params['aggregate']:
        policy_list = vmanage.params['aggregate']
    else:
        if vmanage.params['state'] == 'present':
            policy_list = [
                {
                    'policyName': vmanage.params['name'],
                    'policyDescription': vmanage.params['description'],
                    'policyType': vmanage.params['type'],
                    'policyDefinition': vmanage.params['definition'],
                }
            ]
        else:
            policy_list = [
                {
                    'policyName': vmanage.params['name'],
                    'state': 'absent'
                }
            ]

    central_policy_updates = []
    if vmanage.params['state'] == 'present':
        central_policy_updates = vmanage_central_policy.import_central_policy_list(
            policy_list,
            check_mode=module.check_mode,
            update=vmanage.params['update'],
            push=vmanage.params['push']
        )
        if central_policy_updates:
            vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'absent':
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        for policy in policy_list:
            if policy in central_policy_dict:
                if not module.check_mode:
                    vmanage_central_policy.delete_central_policy(central_policy_dict[policy['policyName']]['policyId'])
                vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'activated':
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        if vmanage.params['name'] in central_policy_dict:
            if not central_policy_dict[vmanage.params['name']]['isPolicyActivated']:
                vmanage.result['changed'] = True
                if not module.check_mode:
                    action_id = vmanage_central_policy.activate_central_policy(vmanage.params['name'],
                                                                               central_policy_dict[vmanage.params['name']]['policyId'])
                    if action_id:
                        if vmanage.params['wait']:
                            vmanage_central_policy.waitfor_action_completion(action_id)
                    else:
                        vmanage.fail_json(msg='Did not get action ID after attaching device to template.')

        else:
            # TODO: The reference to 'policy' in the following line is wrong. What should it be?
            vmanage.fail_json(msg="Cannot find central policy {0}".format(vmanage.params['name']))
    if vmanage.params['state'] in ['absent', 'deactivated']:
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        if policy['policyName'] in central_policy_dict:
            if central_policy_dict[policy['policyName']]['isPolicyActivated']:
                vmanage.result['changed'] = True
                if not module.check_mode:
                    action_id = vmanage_central_policy.deactivate_central_policy(vmanage.params['name'])
                    if action_id:
                        if vmanage.params['wait']:
                            vmanage_central_policy.waitfor_action_completion(action_id)
                    else:
                        vmanage.fail_json(msg='Did not get action ID after attaching device to template.')
        else:
            vmanage.fail_json(msg="Cannot find central policy {0}".format(policy['policyName']))

    vmanage.params['updates'] = central_policy_updates
    vmanage.exit_json(**vmanage.result)