began converstion to common class

This commit is contained in:
Valentin Gurmeza 2018-01-24 15:15:44 -08:00
parent 7d8db10565
commit 86e8e6c295
2 changed files with 55 additions and 150 deletions

View file

@ -64,173 +64,75 @@ EXAMPLES = '''
on_event: put "hello" on_event: put "hello"
''' '''
from ansible.module_utils import mt_api from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
from ansible.module_utils.mt_common import clean_params
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
hostname =dict(required=True), hostname=dict(required=True),
username =dict(required=True), username=dict(required=True),
password =dict(required=True, no_log=True), password=dict(required=True, no_log=True),
name =dict(required=True, type='str'), settings=dict(required=True, type='dict'),
on_event =dict(required=False, type='str'), parameter = dict(
comment =dict(required=False, type='str'), required = True,
interval =dict(required=False, type='str'), choices = ['scheduler'],
policy =dict(required=False, type='list'), type = 'str'
start_date=dict(required=False, type='str'),
start_time=dict(required=False, type='str'),
state=dict(
required=True,
choices=['present', 'absent'],
type='str'
), ),
state = dict(
required = False,
choices = ['present', 'absent'],
type = 'str'
)
), ),
supports_check_mode=True supports_check_mode=True
) )
hostname = module.params['hostname'] params = module.params
username = module.params['username'] idempotent_parameter = 'name'
password = module.params['password']
state = module.params['state']
check_mode = module.check_mode
ansible_scheduler_name = module.params['name']
changed = False
changed_message = []
msg = ""
mk = mt_api.Mikrotik(hostname, username, password) mt_obj = MikrotikIdempotent(
try: hostname = params['hostname'],
mk.login() username = params['username'],
except: password = params['password'],
state = params['state'],
desired_params = params['settings'],
idempotent_param = idempotent_parameter,
api_path = '/system/' + str(params['parameter']),
check_mode = module.check_mode
)
# exit if login failed
if not mt_obj.login_success:
module.fail_json( module.fail_json(
msg="Could not log into Mikrotik device." + msg = mt_obj.failed_msg
" Check the username and password.",
) )
api_path = '/system/scheduler' # add, remove or edit things
mt_obj.sync_state()
response = mk.api_print(base_path=api_path) if mt_obj.failed:
ansible_scheduler_params = module.params module.fail_json(
mikrotik_scheduler_task = {} msg = mt_obj.failed_msg
for item in response: )
if 'name' in item[1]: elif mt_obj.changed:
if ansible_scheduler_name == item[1]['name']:
mikrotik_scheduler_task = item[1]
#######################################
# remove unneeded parameters
######################################
remove_params = ['hostname', 'username', 'password', 'state']
for i in remove_params:
del ansible_scheduler_params[i]
##########################################
# modify params in place
############################################
clean_params(ansible_scheduler_params)
if '.id' in mikrotik_scheduler_task:
client_id = mikrotik_scheduler_task['.id']
else:
client_id = False
if state == "present":
#################################################
# Convert policy list to comma separated string
#################################################
if mikrotik_scheduler_task == {}:
if 'policy' in ansible_scheduler_params:
list_to_string = ""
list_to_string = ','.join(map(str, ansible_scheduler_params['policy']))
ansible_scheduler_params['policy'] = list_to_string
if not check_mode:
mk.api_add(
base_path=api_path,
params=ansible_scheduler_params
)
changed_message.append(ansible_scheduler_name + " added to bridge")
changed = True,
else:
scheduler_diff_keys = {}
########################################################################
# policy parameter is a comma separated list of values in a string that
# we receive from mikrotik
# we need to convert it to a list and then do a comparison against
# ansible policy list to get the difference
# if there is a difference between the two we need to convert the
# ansible_scheduler_params['policy'] to a string with comma separated values
#########################################################################
if 'policy' in ansible_scheduler_params:
dif_list = []
if 'policy' in mikrotik_scheduler_task:
policy = mikrotik_scheduler_task['policy'].split(',')
dif_list = set(ansible_scheduler_params['policy']) & set(policy)
if dif_list == []:
list_to_string = ""
list_to_string = ','.join(map(str, ansible_scheduler_params['policy']))
scheduler_diff_keys['policy'] = list_to_string
for key in ansible_scheduler_params:
if key != 'policy':
if key in mikrotik_scheduler_task:
if ansible_scheduler_params[key] != mikrotik_scheduler_task[key]:
scheduler_diff_keys[key] = ansible_scheduler_params[key]
else:
scheduler_diff_keys[key] = ansible_scheduler_params[key]
if scheduler_diff_keys != {}:
scheduler_diff_keys['numbers'] = client_id
if not check_mode:
mk.api_edit(base_path=api_path, params=scheduler_diff_keys)
changed = True
changed_message.append(
"Changed scheduler task : " + ansible_scheduler_params['name']
)
else:
####################
# Already up date
###################
if not changed:
changed = False
elif state == "absent":
if client_id:
if not check_mode:
mk.api_remove(base_path=api_path, remove_id=client_id)
changed_message.append(ansible_scheduler_params['name'] + " removed")
changed = True
#####################################################
# if client_id is not set there is nothing to remove
#####################################################
else:
if not changed:
changed = False
else:
module.exit_json( module.exit_json(
failed=True, failed=False,
changed=False, changed=True,
msg="state is invalid" msg=mt_obj.changed_msg,
) diff={ "prepared": {
"old": mt_obj.old_params,
if changed: "new": mt_obj.new_params,
module.exit_json( }},
failed=False,
changed=True,
msg=changed_message
) )
else: else:
module.exit_json( module.exit_json(
failed=False, failed=False,
changed=False, changed=False,
msg=params['settings'],
) )
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -5,9 +5,12 @@
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: present state: present
name: ansible_test parameter: scheduler
on_event: 'put "test"' settings:
interval: 1s name: ansible_test
on_event: 'put "test"'
interval: 1s
tags: test1
- name: ALWAYS_CHANGES modify existing scheduler - name: ALWAYS_CHANGES modify existing scheduler
mt_system_scheduler: mt_system_scheduler: