Adding idempotency fixes for alerting, cloud and folder modules

* Fixing cloud_plugin idempotency

* Sanity check fixes

* fix test

* fix

* reset test

* Add 9dempotency check to test

* Working cloud_plugin with idempotency

* Adding idempotency for contactPoint

* fixes

* Tests update

* sanity fix

* Fixed

* Fixes

* sanity fix

* Fix sanity

* fixes

* fixes

* Fixed idempotency

* fix sanity

* alert_notification update

* fix notification

* fixes

* folder idempotency

* folder test

Co-authored-by: ishanjainn <ishan.jain@gmail.com>
This commit is contained in:
Ishan Jain 2022-11-01 12:52:19 +05:30 committed by GitHub
parent e103856767
commit 8eeaea2aa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 41 deletions

View File

@ -148,29 +148,39 @@ def present_alert_contact_point(module):
if result.status_code == 202:
return False, True, result.json()
elif result.status_code == 500:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points/' + module.params['uid']
sameConfig = False
contactPointInfo = {}
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points'
if result.status_code == 202:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
for contact_points in result.json():
if contact_points['uid'] == module.params['uid']:
if (contact_points['name'] == module.params['name'] and contact_points['type'] == module.params['type'] and contact_points['settings']
and contact_points['settings'] == module.params['settings']
and contact_points['disableResolveMessage'] == module.params['disableResolveMessage']):
contactPointFound = False
contactPointInfo = {}
for contact_points in result.json():
if contact_points['uid'] == module.params['uid']:
contactPointFound = True
sameConfig = True
contactPointInfo = contact_points
if contactPointFound:
return False, True, contactPointInfo
else:
return True, False, "Contact Point not found"
if sameConfig:
return False, False, contactPointInfo
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points/' + \
module.params['uid']
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 202:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
for contact_points in result.json():
if contact_points['uid'] == module.params['uid']:
return False, True, contact_points
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}

View File

@ -179,16 +179,21 @@ def alert_notification_policy(module):
'repeat_interval': module.params['repeatInterval']}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/policies'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 202:
if (result.json()['receiver'] == module.params['rootPolicyReceiver'] and result.json()['routes'] == module.params['routes']
and result.json()['group_wait'] == module.params['groupWait'] and result.json()['group_interval'] == module.params['groupInterval']
and result.json()['repeat_interval'] == module.params['repeatInterval']):
return False, False, result.json()
else:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/policies'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 202:
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
def main():

View File

@ -115,12 +115,18 @@ def present_cloud_plugin(module):
if result.status_code == 200:
return False, True, result.json()
elif result.status_code == 409:
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins/' + module.params[
'name']
result = requests.post(api_url, json={'version': module.params['version']},
headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins/' + module.params['name']
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
return False, True, result.json()
if result.json()['pluginSlug'] == module.params['name'] and result.json()['version'] == module.params['version']:
return False, False, result.json()
else:
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins/' + module.params[
'name']
result = requests.post(api_url, json={'version': module.params['version']},
headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}

View File

@ -176,20 +176,33 @@ def present_folder(module):
if result.status_code == 200:
return False, True, result.json()
elif result.status_code == 412:
body = {
'uid': module.params['uid'],
'title': module.params['title'],
'overwrite': module.params['overwrite']
}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/folders/' + module.params['uid']
sameConfig = False
folderInfo = {}
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/folders'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 200:
return False, True, result.json()
for folder in result.json():
if folder['uid'] == module.params['uid'] and folder['title'] == module.params['title']:
sameConfig = True
folderInfo = folder
if sameConfig:
return False, False, folderInfo
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
body = {
'uid': module.params['uid'],
'title': module.params['title'],
'overwrite': module.params['overwrite']
}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/folders/' + module.params['uid']
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 200:
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}

View File

@ -16,6 +16,24 @@
- add_result.failed == false
- add_result.output.provenance == "api"
- name: Idempotency Check
grafana.grafana.alert_contact_point:
name: ops-email
uid: opsemail
type: email
settings: {
addresses: "ops@mydomain.com,devs@mydomain.com"
}
stack_slug: "{{ stack_name }}"
grafana_api_key: "{{ grafana_api_key }}"
state: present
register: idempotent_result
- assert:
that:
- idempotent_result.changed == false
- idempotent_result.output.provenance == "api"
- name: Update Alerting contact point
grafana.grafana.alert_contact_point:
name: ops-email

View File

@ -12,7 +12,21 @@
- add_result.changed == true
- add_result.pluginName == "GitHub"
- name: update a plugin
- name: Idempotency Check
grafana.grafana.cloud_plugin:
name: grafana-github-datasource
version: 1.0.14
stack_slug: "{{ stack_name }}"
cloud_api_key: "{{ grafana_cloud_api_key }}"
state: present
register: idempotency_result
- assert:
that:
- idempotency_result.changed == false
- idempotency_result.pluginName == "GitHub"
- name: Update a plugin
grafana.grafana.cloud_plugin:
name: grafana-github-datasource
version: 1.0.15

View File

@ -10,8 +10,7 @@
- assert:
that:
- create_result.changed == true
- create_result.output.url == "/dashboards/f/test123/ansible-integration-test"
- create_result.failed == false
- name: Delete a folder
grafana.grafana.folder: