blob: 8c050d0b6764b52eaf7df33a3a18cea90dadb276 [file] [log] [blame]
Scott Bakerc41914d2017-06-26 14:00:52 -07001#!/usr/bin/python
2
3import json
4import os
5import requests
6import sys
7import traceback
8
9from ansible.module_utils.basic import AnsibleModule
10from auraclienttools import LCDNAPI, LCDNFault
11
12def main():
13 module = AnsibleModule(
14 argument_spec = dict(
15 cdn_prefix= dict(required=True, type='str'),
16 enabled = dict(required=True, type="bool"),
17 service = dict(default="HyperCache", type="str"),
18 content_provider=dict(required=True, type="str"),
19 default_origin_server = dict(required=True, type="str"),
20
21 state = dict(required=True, type='str', choices=["present", "absent"]),
22 force = dict(default=False, type="bool"),
23 username = dict(required=True, type='str'),
24 password = dict(required=True, type='str'),
25 hostname = dict(required=True, type='str'),
26 plc_name = dict(required=True, type='str'),
27 )
28 )
29
30 credentials = {"username": module.params["username"],
31 "password": module.params["password"],
32 "hostname": module.params["hostname"],
33 "plc_name": module.params["plc_name"]}
34
35 state = module.params["state"]
36 cdn_prefix = module.params["cdn_prefix"]
37 force = module.params["force"]
38
39 api = LCDNAPI(credentials, experimental=True)
40
41 content_providers = api.onevapi.ListAll("ContentProvider", {"name": module.params["content_provider"]})
42 if not content_providers:
43 raise Exception("Unable to find %s" % module.params["content_provider"])
44 content_provider = content_providers[0]
45
46 prefixes = api.onevapi.ListAll("CDNPrefix", {"cdn_prefix": cdn_prefix})
47
48 if (prefixes or force) and (state=="absent"):
49 api.Delete("CDNPrefix", prefixes[0]["cdn_prefix_id"])
50 module.exit_json(changed=True, msg="cdn prefix deleted")
51 elif ((not prefixes) or force) and (state=="present"):
52 if prefixes:
53 # must have been called with force=True, so delete the node so we can re-create it
54 api.onevapi.Delete("CDNPrefix", prefixes[0]["cdn_prefix_id"])
55
56 cdn_prefix = {"cdn_prefix": cdn_prefix,
57 "enabled": module.params["enabled"],
58 "service": module.params["service"],
59 "content_provider_id": content_provider["content_provider_id"],
60 "default_origin_server": module.params["default_origin_server"]}
61 ret = api.onevapi.Create("CDNPrefix", cdn_prefix)
62
63 module.exit_json(changed=True, msg="sp created")
64 else:
65 module.exit_json(changed=False)
66
67if __name__ == '__main__':
68 main()