-
Notifications
You must be signed in to change notification settings - Fork 13
/
handler.py
131 lines (102 loc) · 5.59 KB
/
handler.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
import asyncio
import json
import kopf
from grafana import api, dashboard, datasource, organization, user
ORG_SWITCH_LOCK: asyncio.Lock
@kopf.on.startup()
async def startup_fn(logger, **kwargs):
global ORG_SWITCH_LOCK
ORG_SWITCH_LOCK = asyncio.Lock() # uses the running asyncio loop by default
@kopf.on.create('grafana.k8spin.cloud', 'v1', 'organizations')
async def create_organization(spec, meta, **kwargs):
name = meta.get('name')
datasources = spec.get('datasources', list())
dashboards = spec.get('dashboards', list())
return await organization.create(api, name, datasources, dashboards, ORG_SWITCH_LOCK)
@kopf.on.update('grafana.k8spin.cloud', 'v1', 'organizations')
async def update_organization(old, new, status, **kwargs):
orgId = status['create_organization']['orgId']
oldDataSources = old.get('spec').get('datasources', list())
newDataSources = new.get('spec').get('datasources', list())
oldDashboards = old.get('spec').get('dashboards', list())
newDashboards = new.get('spec').get('dashboards', list())
await organization.update(api, orgId, oldDataSources,
newDataSources, oldDashboards, newDashboards, ORG_SWITCH_LOCK)
@kopf.on.delete('grafana.k8spin.cloud', 'v1', 'organizations')
def delete_organization(status, **kwargs):
return organization.delete(api, status['create_organization']['orgId'])
@kopf.on.create('grafana.k8spin.cloud', 'v1', 'users')
def create_user(spec, meta, **kwargs):
name = meta.get('name')
email = spec.get('email')
organizationNames = spec.get('organizations', list())
return user.create(api, name, email, organizationNames)
@kopf.on.update('grafana.k8spin.cloud', 'v1', 'users')
def update_user(status, spec, meta, old, new, **kwargs):
oldOrganizationNames = old.get('spec').get('organizations', list())
newOrganizationNames = new.get('spec').get('organizations', list())
userId = status['create_user']['id']
name = meta.get('name')
email = spec.get('email')
return user.update(api, userId, name, email, oldOrganizationNames, newOrganizationNames)
@kopf.on.delete('grafana.k8spin.cloud', 'v1', 'users')
def delete_user(status, **kwargs):
return user.delete(api, status['create_user']['id'])
@kopf.on.create('grafana.k8spin.cloud', 'v1', 'dashboards')
async def create_dashboard(spec, logger, **kwargs):
name = spec.get('dashboard').get('name')
title = spec.get('dashboard').get('title')
dash = json.loads(spec.get('dashboard').get('data'))
organizationNames = spec.get('organizations', list())
responses = await dashboard.create(api, name, title, dash, organizationNames, ORG_SWITCH_LOCK, logger)
if not responses:
raise kopf.TemporaryError("Could not create dashboard yet.", delay=10)
return responses
@kopf.on.update('grafana.k8spin.cloud', 'v1', 'dashboards')
async def update_dashboard(status, old, new, logger, **kwargs):
oldName = old.get('spec').get('dashboard').get('name')
newName = new.get('spec').get('dashboard').get('name')
newTitle = new.get('spec').get('dashboard').get('title')
oldOrganizationNames = old.get('spec').get('organizations', list())
newOrganizationNames = new.get('spec').get('organizations', list())
newDashboard = json.loads(new.get('spec').get('dashboard').get('data'))
responses = await dashboard.update(api, oldName, newName, newTitle, oldOrganizationNames, newOrganizationNames, newDashboard, ORG_SWITCH_LOCK, logger)
if not responses:
raise kopf.TemporaryError("Could not update dashboard yet.", delay=10)
return responses
@kopf.on.delete('grafana.k8spin.cloud', 'v1', 'dashboards')
async def delete_dashboard(spec, logger, **kwargs):
name = spec.get('dashboard').get('name')
organizationNames = spec.get('organizations', list())
responses = await dashboard.delete(api, name, organizationNames, ORG_SWITCH_LOCK, logger)
if not responses:
raise kopf.TemporaryError("Could not delete dashboard yet.", delay=10)
return responses
@kopf.on.create('grafana.k8spin.cloud', 'v1', 'datasources')
async def create_datasource(spec, logger, **kwargs):
name = spec.get('datasource').get('name')
ds = json.loads(spec.get('datasource').get('data'))
organizationNames = spec.get('organizations', list())
responses = await datasource.create(api, name, ds, organizationNames, ORG_SWITCH_LOCK, logger)
if not responses:
raise kopf.TemporaryError("Could not create datasource yet.", delay=10)
return responses
@kopf.on.update('grafana.k8spin.cloud', 'v1', 'datasources')
async def update_datasource(status, old, new, logger, **kwargs):
oldName = old.get('spec').get('datasource').get('name')
newName = new.get('spec').get('datasource').get('name')
newDs = json.loads(new.get('spec').get('datasource').get('data'))
oldOrganizationNames = old.get('spec').get('organizations', list())
newOrganizationNames = new.get('spec').get('organizations', list())
responses = await datasource.update(api, oldName, newName, newDs, oldOrganizationNames, newOrganizationNames, ORG_SWITCH_LOCK, logger)
if not responses:
raise kopf.TemporaryError("Could not update datasource yet.", delay=10)
return responses
@kopf.on.delete('grafana.k8spin.cloud', 'v1', 'datasources')
async def delete_datasource(spec, logger, **kwargs):
name = spec.get('datasource').get('name')
organizationNames = spec.get('organizations', list())
responses = await datasource.delete(api, name, organizationNames, ORG_SWITCH_LOCK, logger)
if not responses:
raise kopf.TemporaryError("Could not delete datasource yet.", delay=10)
return responses