blob: 34265d4d216de20e7b7fc858087a1dd0fffd5452 [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 url = dict(required=True, type='str'),
32 service_type = dict(default="HyperCache", type="str"),
33 content_provider = dict(required=True, type="str"),
34
35 state = dict(required=True, type='str', choices=["present", "absent"]),
36 force = dict(default=False, type="bool"),
37 username = dict(required=True, type='str'),
38 password = dict(required=True, type='str'),
39 hostname = dict(required=True, type='str'),
40 plc_name = dict(required=True, type='str'),
41 )
42 )
43
44 credentials = {"username": module.params["username"],
45 "password": module.params["password"],
46 "hostname": module.params["hostname"],
47 "plc_name": module.params["plc_name"]}
48
49 state = module.params["state"]
50 origin_url = module.params["url"]
51 force = module.params["force"]
52
53 api = LCDNAPI(credentials, experimental=True)
54
55 content_providers = api.onevapi.ListAll("ContentProvider", {"name": module.params["content_provider"]})
56 if not content_providers:
57 raise Exception("Unable to find %s" % module.params["content_provider"])
58 content_provider = content_providers[0]
59
60 origins = api.onevapi.ListAll("OriginServer", {"url": origin_url})
61
62 if (origins or force) and (state=="absent"):
63 api.Delete("OriginServer", origins[0]["origin_servier_id"])
64 module.exit_json(changed=True, msg="origin server deleted")
65 elif ((not origins) or force) and (state=="present"):
66 if origins:
67 # must have been called with force=True, so delete the node so we can re-create it
68 api.onevapi.Delete("OriginServer", origins[0]["origin_server_id"])
69
70 origin = {"url": origin_url,
71 "service_type": module.params["service_type"],
72 "content_provider_id": content_provider["content_provider_id"]}
73 ret = api.onevapi.Create("OriginServer", origin)
74
75 module.exit_json(changed=True, msg="origin server created")
76 else:
77 module.exit_json(changed=False)
78
79if __name__ == '__main__':
80 main()