def central_policy(ctx, name): """ Activate Central Policy """ vmanage_central_policy = CentralPolicy(ctx.auth, ctx.host, ctx.port) central_policy_dict = vmanage_central_policy.get_central_policy_dict( remove_key=True) if name in central_policy_dict: click.echo(f'Activating Central Policy {name}') action_id = vmanage_central_policy.activate_central_policy( name, central_policy_dict[name]['policyId']) vmanage_central_policy.utilities.waitfor_action_completion(action_id) else: click.secho(f'Cannot find Central Policy {name}', fg="red")
def policy_activation(): try: log_level = logging.DEBUG logger = get_logger("log/sdwan-te-integration.txt", log_level) if logger is not None: logger.info("Loading vManage login details from YAML\n") data = json.loads(request.data) with open("config_details.yaml") as f: config = yaml.safe_load(f.read()) vmanage_host = config["vmanage_host"] vmanage_port = config["vmanage_port"] username = config["vmanage_username"] password = config["vmanage_password"] session = Authentication(host=vmanage_host, port=vmanage_port, user=username, password=password).login() name = config["central_policy_name"] vmanage_central_policy = CentralPolicy(session, vmanage_host, vmanage_port) central_policy_dict = vmanage_central_policy.get_central_policy_dict( remove_key=True) if name in central_policy_dict: print(f'Activating Central Policy {name}') action_id = vmanage_central_policy.activate_central_policy( name, central_policy_dict[name]['policyId']) utils = Utilities(session, vmanage_host, vmanage_port) utils.waitfor_action_completion(action_id) else: return jsonify(f'Cannot find Central Policy {name}'), 200 except Exception as exc: print('Exception line number: {}'.format(sys.exc_info()[-1].tb_lineno), type(exc).__name__, exc) return jsonify(str(exc)), 500 return jsonify(f'Activated Policy {name}'), 200
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)
if central_policy['isPolicyActivated']: state = "activated" print( f"Policy Name: {central_policy['policyName']} is activated. Policy ID: {central_policy['policyId']}" ) else: state = "inactive" print( f"Policy Name: {central_policy['policyName']} is inactive.Policy ID: {central_policy['policyId']} " ) # Find the current activated centralized policy def activated_central_policy(): central_policy_list = display_central_policy() for central_policy in central_policy_list: if central_policy['isPolicyActivated']: return central_policy['policyId'] if __name__ == "__main__": auth, vmanage_host = login_vmanage() vmanage_central_policy = CentralPolicy(auth, vmanage_host) vmanage_central_policy.activate_central_policy( 'all-vpn20-omp-tag-pol', 'a85d2ced-d3d5-45f9-aea0-9d538e4dae06') #print_central_policy() print('Central Policy activation done!') # vnendc lab: a85d2ced-d3d5-45f9-aea0-9d538e4dae06 # vnendc lab pol name: all-vpn20-omp-tag-pol
return central_policy['policyId'] def deactivate_central_pol(polId): auth, vmanage_host = login_vmanage() de_central_policy = CentralPolicy(auth, vmanage_host) de_central_policy.deactivate_central_policy(polId) return None if __name__ == "__main__": auth, vmanage_host = login_vmanage() vmanage_central_policy = CentralPolicy(auth, vmanage_host) print("Devices in SDWAN Lab:") devices = display_devices() print("Cetralized Policy:") central_policy = display_central_policy() activated_policy_id = activated_central_policy() ''' d = input("What would you want to do with current activated policy? Type 'D' for Deactive, other key for exit:") if d.upper() != 'D': exit else: vmanage_central_policy.deactivate_central_policy(activated_policy_id) ''' vmanage_central_policy.activate_central_policy('all-vpn20-omp-tag-pol', activated_policy_id)