This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk5_key_container_list.py
155 lines (114 loc) · 4 KB
/
k5_key_container_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'version': '1.0'}
DOCUMENTATION = '''
---
module: k5_key_container_list
short_description: List key metadata containers
version_added: "1.0"
description:
- returns a dict of containers
options:
k5_auth:
description:
- dict of k5_auth module output.
required: true
default: None
requirements:
- "python >= 2.6"
'''
EXAMPLES = '''
- k5_key_container_list:
k5_auth: "{{ k5_auth_facts }}"
'''
RETURN = '''
k5_key_container_list
description: Dictionary describing the novnc details.
returned: On success when the server is found
type: dictionary
contains:
list:
name: container_ref
description:
type:
sample: https://keymanagement.uk-1.cloud.global.fujitsu.com/v1/dbbd47230bfd4e699099462cd8f51b53/containers/0a561620-3e4d-47e7-9b2d-77fc59b7395f
#TODO
created: '2017-04-13T08:55:13.241314'
name: Rail_VPN_1_container
secret_refs:
- name: ca
secret_id: 8c1b3826-b653-4f07-9543-3db7f0be6b6a
- name: server_certificate
secret_id: e05a9e43-d07d-4835-a635-9a976e2f88b0
- name: server_key
secret_id: eb17318f-bd3d-41a2-a0fc-d0a75a4c522a
- name: dh
secret_id: ad556fcd-a506-4ceb-8d81-42da2589be45
status: ACTIVE
type: generic
updated: '2017-04-13T08:55:13.241325'
'''
import requests
import os
import json
from ansible.module_utils.basic import *
############## Common debug ###############
k5_debug = False
k5_debug_out = []
def k5_debug_get():
"""Return our debug list"""
return k5_debug_out
def k5_debug_clear():
"""Clear our debug list"""
k5_debug_out = []
def k5_debug_add(s):
"""Add string to debug list if env K5_DEBUG is defined"""
if k5_debug:
k5_debug_out.append(s)
############## functions #############
def k5_get_endpoint(e,name):
"""Pull particular endpoint name from dict"""
return e['endpoints'][name]
def k5_key_container_list(module):
"""list vpn servies"""
global k5_debug
k5_debug_clear()
if 'K5_DEBUG' in os.environ:
k5_debug = True
if 'auth_spec' in module.params['k5_auth']:
k5_facts = module.params['k5_auth']
else:
module.fail_json(msg="k5_auth_facts not found, have you run k5_auth?")
endpoint = k5_facts['endpoints']['keymanagement']
auth_token = k5_facts['auth_token']
k5_debug_add('auth_token: {0}'.format(auth_token))
# actually the project_id, but stated as tenant_id in the API
tenant_id = k5_facts['auth_spec']['os_project_id']
session = requests.Session()
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/' + tenant_id + '/containers?limit=1000' # TODO limit + offset
k5_debug_add('endpoint: {0}'.format(endpoint))
k5_debug_add('REQ: {0}'.format(url))
k5_debug_add('headers: {0}'.format(headers))
#k5_debug_add('json: {0}'.format(query_json))
try:
response = session.request('GET', url, headers=headers)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to make a change
if response.status_code not in (200,):
module.fail_json(msg="RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
if k5_debug:
module.exit_json(changed=True, msg="List VPN Credentials Successful", k5_container_facts=response.json(), debug=k5_debug_out )
else:
module.exit_json(changed=True, msg="List VPN Credentials Successful", k5_container_facts=response.json() )
######################################################################################
def main():
module = AnsibleModule( argument_spec=dict(
k5_auth = dict(required=True, default=None, type='dict')
) )
k5_key_container_list(module)
######################################################################################
if __name__ == '__main__':
main()