-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasb_dashboard_url.py
executable file
·95 lines (77 loc) · 2.59 KB
/
asb_dashboard_url.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
#!/usr/bin/python
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: asb_dashboard_url
short_description: Adds a annotation to the pod running the apb with the dashboard URL
dashboard_url:
- Takes a string containing the dashboard URL. This URL should point to the provisioned application.
This URL is then added as an annotation to the pod executing the apb and read by the broker.
notes: []
requirements: []
author:
- "Red Hat, Inc."
options:
dashboard_url:
dashboard_url:
- 'string containing URL to provisioned application'
required: true
default: ""
env:
- Set via the downward API on the APB Pod
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
'''
EXAMPLES = '''
- name: update last operation
asb_dashboard_url:
dashboard_url: automationbroker.io
'''
RETURN = '''
'''
import json
import base64
import os
from ansible.module_utils.basic import AnsibleModule
try:
from kubernetes import client, config
config.load_kube_config()
api = client.CoreV1Api()
except Exception as error:
ansible_module.fail_json(msg="Error attempting to load kubernetes client: {}".format(error))
DASHBOARD_URL_ANNOTATION = "apb_dashboard_url"
ENV_NAME = "POD_NAME"
ENV_NAMESPACE = "POD_NAMESPACE"
def main():
argument_spec = dict(
dashboard_url=dict(required=True, type='str')
)
ansible_module = AnsibleModule(argument_spec=argument_spec)
dashUrl = ansible_module.params['dashboard_url']
try:
name = os.environ[ENV_NAME]
namespace = os.environ[ENV_NAMESPACE]
except KeyError as error:
ansible_module.fail_json(msg="Error attempting to update pod with dashboard_url annotation. Missing key from environment: {}".format(error))
try:
pod = api.read_namespaced_pod(
name=name,
namespace=namespace
)
if not pod.metadata.annotations:
pod.metadata.annotations = {}
pod.metadata.annotations[DASHBOARD_URL_ANNOTATION] = dashUrl
api.replace_namespaced_pod(name=name,namespace=namespace,body=pod,pretty='true')
except Exception as error:
ansible_module.fail_json(msg="Error attempting to update pod with dashboard_url annotation: {}".format(error))
ansible_module.exit_json(changed=True, dashboard_url=dashUrl)
if __name__ == '__main__':
main()