blob: 5f4156fcc434a57661456b984b12b1522dc1d400 [file] [log] [blame]
Matteo Scandoloede125b2017-08-08 13:05:25 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Bakerc41914d2017-06-26 14:00:52 -070017#!/usr/bin/python
18
19import json
20import os
21import requests
22import sys
23import traceback
24
25from ansible.module_utils.basic import AnsibleModule
26from auraclienttools import LCDNAPI, LCDNFault
27
28def main():
29 module = AnsibleModule(
30 argument_spec = dict(
31 name = dict(required=True, type='str'),
32 site = dict(required=True, type='str'),
33 dns = dict(required=True, type='list'),
34 interfaces= dict(required=True, type='list'),
35 license = dict(required=True, type='str'),
36
37 state = dict(required=True, type='str', choices=["present", "absent"]),
38 force = dict(default=False, type="bool"),
39 username = dict(required=True, type='str'),
40 password = dict(required=True, type='str'),
41 hostname = dict(required=True, type='str'),
42 plc_name = dict(required=True, type='str'),
43
44 remote_hostname = dict(default=None, type="str"),
45 )
46 )
47
48 credentials = {"username": module.params["username"],
49 "password": module.params["password"],
50 "hostname": module.params["hostname"],
51 "plc_name": module.params["plc_name"]}
52
53 state = module.params["state"]
54 node_hostname = module.params["name"]
55 force = module.params["force"]
56
57 api = LCDNAPI(credentials)
58
59 nodes = api.ListAll("Node", {"hostname": node_hostname})
60
61 if (nodes or force) and (state=="absent"):
62 api.deleteCache(node_hostname)
63 module.exit_json(changed=True, msg="cachenode deleted")
64 elif ((not nodes) or force) and (state=="present"):
65 if nodes:
66 # must have been called with force=True, so delete the node so we can re-create it
67 api.deleteCache(node_hostname)
68
69 hpc = {"hostname": node_hostname,
70 "site": module.params["site"],
71 "dns": module.params["dns"],
72 "Interfaces": module.params["interfaces"],
73 "license": module.params["license"]}
74 ret = api.createCache(**hpc)
75 setupscript=ret["setupscript"]
76
77 if module.params["remote_hostname"]:
78 setupscript = setupscript.replace(module.params["hostname"], module.params["remote_hostname"])
79
80 module.exit_json(changed=True, msg="cachenode created", setupscript=setupscript)
81 else:
82 module.exit_json(changed=False)
83
84if __name__ == '__main__':
85 main()