blob: badfe74d7cb42b9107b0c1908e18ddb0f5ba411a [file] [log] [blame]
Matteo Scandolo3896c472017-08-01 13:31:42 -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
Zack Williamsa2763112017-01-03 11:38:38 -070017#!/usr/bin/python
18
19import json
20import os
21import requests
22import sys
23import traceback
24
25from ansible.module_utils.basic import AnsibleModule
26
27def main():
28
29 # args styled after the uri module
30 module = AnsibleModule(
31 argument_spec = dict(
32 url = dict(required=True, type='str'),
33 recipe = dict(required=True, type='str'),
34 user = dict(required=True, type='str'),
35 password = dict(required=True, type='str'),
36 )
37 )
38
39 xos_auth=(module.params['user'], module.params['password'])
40
41 r = requests.post(module.params['url'], data={"recipe": module.params['recipe']}, auth=xos_auth)
42 if (r.status_code != 200):
43 try:
44 error_text=r.json()["error_text"]
45 except:
46 error_text="error while formatting the error: " + traceback.format_exc()
47 module.fail_json(msg=error_text, rc=r.status_code)
48
49 result = r.json()
50 if "log_msgs" in result:
51 module.exit_json(changed=True, msg="\n".join(result["log_msgs"])+"\n")
52 else:
53 module.exit_json(changed=True, msg="success")
54
55if __name__ == '__main__':
56 main()