forked from bfraser/puppet-grafana
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bfraser#86 from simp/SIMP-4206
(SIMP-4206) Added organization provider and updated datasource provider
- Loading branch information
Showing
7 changed files
with
491 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
require 'json' | ||
|
||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) | ||
|
||
Puppet::Type.type(:grafana_organization).provide(:grafana, parent: Puppet::Provider::Grafana) do | ||
desc 'Support for Grafana organizations' | ||
|
||
defaultfor kernel: 'Linux' | ||
|
||
def organizations | ||
response = send_request('GET', '/api/orgs') | ||
if response.code != '200' | ||
raise format('Failed to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) | ||
end | ||
|
||
begin | ||
organizations = JSON.parse(response.body) | ||
|
||
organizations.map { |x| x['id'] }.map do |id| | ||
response = send_request 'GET', format('/api/orgs/%s', id) | ||
if response.code != '200' | ||
raise format('Failed to retrieve organization %d (HTTP response: %s/%s)', id, response.code, response.body) | ||
end | ||
|
||
organization = JSON.parse(response.body) | ||
|
||
{ | ||
id: organization['id'], | ||
name: organization['name'], | ||
address: organization['address'] | ||
} | ||
end | ||
rescue JSON::ParserError | ||
raise format('Failed to parse response: %s', response.body) | ||
end | ||
end | ||
|
||
def organization | ||
unless @organization | ||
@organization = organizations.find { |x| x[:name] == resource[:name] } | ||
end | ||
@organization | ||
end | ||
|
||
attr_writer :organization | ||
|
||
# rubocop:enable Style/PredicateName | ||
|
||
def id | ||
organization[:id] | ||
end | ||
|
||
def id=(value) | ||
resource[:id] = value | ||
save_organization | ||
end | ||
|
||
def address | ||
organization[:json_data] | ||
end | ||
|
||
def address=(value) | ||
resource[:address] = value | ||
save_organization | ||
end | ||
|
||
def save_organization | ||
data = { | ||
id: resource[:id], | ||
name: resource[:name], | ||
address: resource[:address] | ||
} | ||
|
||
response = send_request('POST', '/api/orgs', data) if organization.nil? | ||
|
||
if response.code != '200' | ||
raise format('Failed to create save %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) | ||
end | ||
self.organization = nil | ||
end | ||
|
||
def delete_organization | ||
response = send_request 'DELETE', format('/api/orgs/%s', organization[:id]) | ||
|
||
if response.code != '200' | ||
raise format('Failed to delete organization %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) | ||
end | ||
self.organization = nil | ||
end | ||
|
||
def create | ||
save_organization | ||
end | ||
|
||
def destroy | ||
delete_organization | ||
end | ||
|
||
def exists? | ||
organization | ||
end | ||
end |
Oops, something went wrong.