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