Matteo Scandolo | ede125b | 2017-08-08 13:05:25 -0700 | [diff] [blame^] | 1 | |
| 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 Baker | c41914d | 2017-06-26 14:00:52 -0700 | [diff] [blame] | 17 | #!/usr/bin/python |
| 18 | |
| 19 | import json |
| 20 | import os |
| 21 | import requests |
| 22 | import sys |
| 23 | import traceback |
| 24 | |
| 25 | from ansible.module_utils.basic import AnsibleModule |
| 26 | from auraclienttools import LCDNAPI, LCDNFault |
| 27 | |
| 28 | def main(): |
| 29 | module = AnsibleModule( |
| 30 | argument_spec = dict( |
| 31 | cdn_prefix= dict(required=True, type='str'), |
| 32 | enabled = dict(required=True, type="bool"), |
| 33 | service = dict(default="HyperCache", type="str"), |
| 34 | content_provider=dict(required=True, type="str"), |
| 35 | default_origin_server = 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 | ) |
| 45 | |
| 46 | credentials = {"username": module.params["username"], |
| 47 | "password": module.params["password"], |
| 48 | "hostname": module.params["hostname"], |
| 49 | "plc_name": module.params["plc_name"]} |
| 50 | |
| 51 | state = module.params["state"] |
| 52 | cdn_prefix = module.params["cdn_prefix"] |
| 53 | force = module.params["force"] |
| 54 | |
| 55 | api = LCDNAPI(credentials, experimental=True) |
| 56 | |
| 57 | content_providers = api.onevapi.ListAll("ContentProvider", {"name": module.params["content_provider"]}) |
| 58 | if not content_providers: |
| 59 | raise Exception("Unable to find %s" % module.params["content_provider"]) |
| 60 | content_provider = content_providers[0] |
| 61 | |
| 62 | prefixes = api.onevapi.ListAll("CDNPrefix", {"cdn_prefix": cdn_prefix}) |
| 63 | |
| 64 | if (prefixes or force) and (state=="absent"): |
| 65 | api.Delete("CDNPrefix", prefixes[0]["cdn_prefix_id"]) |
| 66 | module.exit_json(changed=True, msg="cdn prefix deleted") |
| 67 | elif ((not prefixes) or force) and (state=="present"): |
| 68 | if prefixes: |
| 69 | # must have been called with force=True, so delete the node so we can re-create it |
| 70 | api.onevapi.Delete("CDNPrefix", prefixes[0]["cdn_prefix_id"]) |
| 71 | |
| 72 | cdn_prefix = {"cdn_prefix": cdn_prefix, |
| 73 | "enabled": module.params["enabled"], |
| 74 | "service": module.params["service"], |
| 75 | "content_provider_id": content_provider["content_provider_id"], |
| 76 | "default_origin_server": module.params["default_origin_server"]} |
| 77 | ret = api.onevapi.Create("CDNPrefix", cdn_prefix) |
| 78 | |
| 79 | module.exit_json(changed=True, msg="sp created") |
| 80 | else: |
| 81 | module.exit_json(changed=False) |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | main() |