Add support for dry run (test=True) of grafana states

Fixes #43675
This commit is contained in:
Bruno Binet 2018-07-20 11:52:10 +02:00
parent 7e7ae8ace7
commit 3c5083f550
3 changed files with 47 additions and 2 deletions

View file

@ -128,7 +128,7 @@ def present(name,
if isinstance(profile, string_types):
profile = __salt__['config.option'](profile)
ret = {'name': name, 'result': None, 'comment': None, 'changes': None}
ret = {'name': name, 'result': None, 'comment': None, 'changes': {}}
datasource = __salt__['grafana4.get_datasource'](name, orgname, profile)
data = _get_json_data(
name=name,
@ -149,6 +149,9 @@ def present(name,
defaults=datasource)
if not datasource:
if __opts__['test']:
ret['comment'] = 'Datasource {0} will be created'.format(name)
return ret
__salt__['grafana4.create_datasource'](profile=profile, **data)
datasource = __salt__['grafana4.get_datasource'](name, profile=profile)
ret['result'] = True
@ -163,10 +166,12 @@ def present(name,
datasource[key] = None
if data == datasource:
ret['changes'] = {}
ret['comment'] = 'Data source {0} already up-to-date'.format(name)
return ret
if __opts__['test']:
ret['comment'] = 'Datasource {0} will be updated'.format(name)
return ret
__salt__['grafana4.update_datasource'](
datasource['id'], profile=profile, **data)
ret['result'] = True
@ -200,6 +205,9 @@ def absent(name, orgname=None, profile='grafana'):
ret['comment'] = 'Data source {0} already absent'.format(name)
return ret
if __opts__['test']:
ret['comment'] = 'Datasource {0} will be deleted'.format(name)
return ret
__salt__['grafana4.delete_datasource'](datasource['id'], profile=profile)
ret['result'] = True

View file

@ -129,6 +129,9 @@ def present(name,
raise
if create:
if __opts__['test']:
ret['comment'] = 'Org {0} will be created'.format(name)
return ret
__salt__['grafana4.create_org'](profile=profile, name=name)
org = __salt__['grafana4.get_org'](name, profile)
ret['changes'] = org
@ -138,6 +141,9 @@ def present(name,
city=city, zipCode=zip_code, state=address_state, country=country,
defaults=org['address'])
if data != org['address']:
if __opts__['test']:
ret['comment'] = 'Org {0} address will be updated'.format(name)
return ret
__salt__['grafana4.update_org_address'](name, profile=profile, **data)
if create:
dictupdate.update(ret['changes']['address'], data)
@ -148,6 +154,9 @@ def present(name,
data = _get_json_data(theme=theme, homeDashboardId=home_dashboard_id,
timezone=timezone, defaults=prefs)
if data != prefs:
if __opts__['test']:
ret['comment'] = 'Org {0} prefs will be updated'.format(name)
return ret
__salt__['grafana4.update_org_prefs'](name, profile=profile, **data)
if create:
dictupdate.update(ret['changes'], data)
@ -164,13 +173,25 @@ def present(name,
for username, role in users.items():
if username in db_users:
if role is False:
if __opts__['test']:
ret['comment'] = 'Org {0} user {1} will be ' \
'deleted'.format(name, username)
return ret
__salt__['grafana4.delete_org_user'](
db_users[username]['userId'], profile=profile)
elif role != db_users[username]['role']:
if __opts__['test']:
ret['comment'] = 'Org {0} user {1} role will be ' \
'updated'.format(name, username)
return ret
__salt__['grafana4.update_org_user'](
db_users[username]['userId'], loginOrEmail=username,
role=role, profile=profile)
elif role:
if __opts__['test']:
ret['comment'] = 'Org {0} user {1} will be created'.format(
name, username)
return ret
__salt__['grafana4.create_org_user'](
loginOrEmail=username, role=role, profile=profile)
@ -218,6 +239,9 @@ def absent(name, profile='grafana'):
ret['comment'] = 'Org {0} already absent'.format(name)
return ret
if __opts__['test']:
ret['comment'] = 'Org {0} will be deleted'.format(name)
return ret
__salt__['grafana4.delete_org'](org['id'], profile=profile)
ret['result'] = True

View file

@ -91,6 +91,9 @@ def present(name,
create = not user
if create:
if __opts__['test']:
ret['comment'] = 'User {0} will be created'.format(name)
return ret
__salt__['grafana4.create_user'](
login=name,
password=password,
@ -105,12 +108,19 @@ def present(name,
defaults=user_data)
if data != _get_json_data(login=None, email=None, name=None, theme=None,
defaults=user_data):
if __opts__['test']:
ret['comment'] = 'User {0} will be updated'.format(name)
return ret
__salt__['grafana4.update_user'](user['id'], profile=profile, **data)
dictupdate.update(
ret['changes'], deep_diff(
user_data, __salt__['grafana4.get_user_data'](user['id'])))
if user['isAdmin'] != is_admin:
if __opts__['test']:
ret['comment'] = 'User {0} isAdmin status will be updated'.format(
name)
return ret
__salt__['grafana4.update_user_permissions'](
user['id'], isGrafanaAdmin=is_admin, profile=profile)
dictupdate.update(ret['changes'], deep_diff(
@ -148,6 +158,9 @@ def absent(name, profile='grafana'):
user = __salt__['grafana4.get_user'](name, profile)
if user:
if __opts__['test']:
ret['comment'] = 'User {0} will be deleted'.format(name)
return ret
orgs = __salt__['grafana4.get_user_orgs'](user['id'], profile=profile)
__salt__['grafana4.delete_user'](user['id'], profile=profile)
for org in orgs: