circonus_api 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3ede201a007dc23e83a498822aaecdd1d088c10
4
+ data.tar.gz: 2acd47ca32aa05a890eaa51b73166cd30bc39044
5
+ SHA512:
6
+ metadata.gz: 117a9a496d9d6c056cddfcc42a6a0185a300d2ab7fb729498f30bc493cf024e773b40109ec16d64531456fe22b29261823461f66872e9396b8b3642e047e2ca8
7
+ data.tar.gz: 041229edd86a31bacb0f557107bef5ac6940c2246eb645460347d71d4e14191cbe22b4c5123f0efaadf223f79940544be133fcc109b6e981821b1eb8d3d20fad
@@ -0,0 +1,99 @@
1
+ #circonus_api
2
+
3
+ ##### The gem implements Active Resource pattern for Circonus API. The library maps Circonus' models to corresponding ruby classes and implements simple CRUD interface.
4
+
5
+ ## Contents
6
+ - [Installation](#installation)
7
+ - [Models](#models)
8
+ - [CRUD](#crud)
9
+ - [Search](#search)
10
+ - [Contributing](#contributing)
11
+
12
+ ## Installation
13
+ In your `Gemfile`:
14
+ ```ruby
15
+ gem 'circonus_api'
16
+ ```
17
+ Require library in the project:
18
+ ```ruby
19
+ require 'circonus_api'
20
+ ```
21
+ Initialize `Circonus` with client:
22
+ ```ruby
23
+ client = Circonus::Client.new(app_name, auth_token)
24
+ Circonus.init(client)
25
+ ```
26
+ Check if it works:
27
+ ```ruby
28
+ > Circonus::Account.current
29
+ => #<Circonus::Account:0x007f99bfac8090 ...
30
+ ```
31
+
32
+ ## Models
33
+ There is a list of the Circonus models with links to API documentation:
34
+ - [Account](https://login.circonus.com/resources/api/calls/account)
35
+ - [Acknowledgement](https://login.circonus.com/resources/api/calls/acknowledgement)
36
+ - [Alert](https://login.circonus.com/resources/api/calls/alert)
37
+ - [Annotation](https://login.circonus.com/resources/api/calls/annotation)
38
+ - [Broker](https://login.circonus.com/resources/api/calls/broker)
39
+ - [Check](https://login.circonus.com/resources/api/calls/check)
40
+ - [Check Bundle](https://login.circonus.com/resources/api/calls/check_bundle)
41
+ - [Check Move](https://login.circonus.com/resources/api/calls/check_move)
42
+ - [Contact Group](https://login.circonus.com/resources/api/calls/contact_group)
43
+ - [Data](https://login.circonus.com/resources/api/calls/data)
44
+ - [Graph](https://login.circonus.com/resources/api/calls/graph)
45
+ - [Metric Cluster](https://login.circonus.com/resources/api/calls/metric_cluster)
46
+ - [Maintenance](https://login.circonus.com/resources/api/calls/maintenance)
47
+ - [Rebuild Broker](https://login.circonus.com/resources/api/calls/rebuild_broker)
48
+ - [Rule Set](https://login.circonus.com/resources/api/calls/rule_set)
49
+ - [Rule Set Group](https://login.circonus.com/resources/api/calls/rule_set_group)
50
+ - [Tag](https://login.circonus.com/resources/api/calls/tag)
51
+ - [Template](https://login.circonus.com/resources/api/calls/template)
52
+ - [User](https://login.circonus.com/resources/api/calls/user)
53
+ - [Worksheet](https://login.circonus.com/resources/api/calls/worksheet)
54
+
55
+ ## CRUD
56
+ There are several basic CRUD methods:
57
+
58
+ Class methods:
59
+
60
+ `::all` - returns all records for corresponding model
61
+ ```ruby
62
+ Circonus::RuleSet.all
63
+ ```
64
+ `::find` - find the record by id
65
+ ```ruby
66
+ Circonus::RuleSet.find(42)
67
+ ```
68
+ `::create` - creates new record
69
+ ```ruby
70
+ Circonus::RuleSet.create(attribute_hash)
71
+ ```
72
+
73
+ Instrance methods:
74
+ `#update` - updates the record on Circonus (performs PUT request) and returns new object.
75
+ ```ruby
76
+ rule_set = Circonus::RuleSet.find(42)
77
+ rule_set.metric_type = 'code'
78
+ rule_set.update
79
+ ```
80
+ `#create` creates a new record on Circonus (performs POST request) and returns new object.
81
+ ```ruby
82
+ rule_set = Circonus::RuleSet.new({metric_type: 'code', ...})
83
+ rule_set.create
84
+ ```
85
+ There is also the `#save` method which calls `#update` if record has `id` or `#create` if `id` is `nil`.
86
+
87
+ Some models don't implement all CRUD functionality. See Circonus documentation and specs.
88
+
89
+ ## Search
90
+ There is the `::search` method which can filter results by attributes:
91
+ ```ruby
92
+ rule_sets = Circonus::RuleSet.search(metric_name: 'code', check: '/check/100500')
93
+ ```
94
+
95
+ ## Contributing
96
+ ##### Contributions are welcome and appreciated!
97
+
98
+
99
+
@@ -0,0 +1,18 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ require 'circonus/version'
5
+ require 'circonus/models'
6
+ require 'circonus/exceptions'
7
+ require 'circonus/client'
8
+
9
+
10
+ module Circonus
11
+ def self.init(client)
12
+ @client = client
13
+ end
14
+
15
+ def self.client
16
+ @client || fail('Client is not set')
17
+ end
18
+ end
@@ -0,0 +1,36 @@
1
+ module Circonus
2
+ class Client
3
+ API_URL = 'https://api.circonus.com/v2'
4
+
5
+ def initialize(app_name, auth_token)
6
+ headers = { x_circonus_app_name: app_name, x_circonus_auth_token: auth_token, content_type: :json, accept: :json }
7
+ @resource = RestClient::Resource.new(API_URL, headers: headers)
8
+ end
9
+
10
+ def get(path, params = {})
11
+ with_rescue { @resource[path].get(params: params) }
12
+ end
13
+
14
+ def post(path, data)
15
+ with_rescue { @resource[path].post(data.to_json) }
16
+ end
17
+
18
+ def put(path, data)
19
+ with_rescue { @resource[path].put(data.to_json) }
20
+ end
21
+
22
+ def delete(path)
23
+ with_rescue { @resource[path].delete }
24
+ end
25
+
26
+ private
27
+
28
+ def with_rescue
29
+ begin
30
+ yield
31
+ rescue RestClient::Exception => e
32
+ raise ClientError.new(e.inspect)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ module Circonus
2
+ class UnsupportedAction < StandardError
3
+ end
4
+
5
+ class ClientError < StandardError
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ require 'circonus/models/actions'
2
+ require 'circonus/models/base_model'
3
+
4
+ require 'circonus/models/account'
5
+ require 'circonus/models/acknowledgement'
6
+ require 'circonus/models/alert'
7
+ require 'circonus/models/annotation'
8
+ require 'circonus/models/broker'
9
+ require 'circonus/models/check'
10
+ require 'circonus/models/check_bundle'
11
+ require 'circonus/models/check_move'
12
+ require 'circonus/models/contact_group'
13
+ require 'circonus/models/data'
14
+ require 'circonus/models/graph'
15
+ require 'circonus/models/metric_cluster'
16
+ require 'circonus/models/maintenance'
17
+ require 'circonus/models/rebuild_broker'
18
+ require 'circonus/models/rule_set'
19
+ require 'circonus/models/rule_set_group'
20
+ require 'circonus/models/tag'
21
+ require 'circonus/models/template'
22
+ require 'circonus/models/user'
23
+ require 'circonus/models/worksheet'
@@ -0,0 +1,20 @@
1
+ module Circonus
2
+ class Account < BaseModel
3
+ fields :_cid, :country_code, :timezone, :name, :_owner, :users, :description, :address2,
4
+ :_contact_groups, :city, :invites, :state_prov, :address1, :_usage
5
+ actions :all, :find
6
+
7
+ class << self
8
+ def current
9
+ data = client.get('account/current')
10
+ new(JSON.parse(data))
11
+ end
12
+ end
13
+
14
+ def update
15
+ client.put('account/current', attributes)
16
+ end
17
+
18
+ alias_method :save, :update
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module Circonus
2
+ class Acknowledgement < BaseModel
3
+ fields :_cid, :alert, :_last_modified_by, :_acknowledged_by,
4
+ :acknowledged_until, :_acknowledged_on, :notes, :_active, :_last_modified
5
+ actions :all, :find, :search, :create, :update
6
+ end
7
+ end
@@ -0,0 +1,82 @@
1
+ module Circonus
2
+ module Actions
3
+ module ClassMethods
4
+ module All
5
+ def all
6
+ data = client.get(URI.escape(path))
7
+ JSON.parse(data).map { |datum| new(datum) }
8
+ end
9
+ end
10
+
11
+ module Find
12
+ def find(id, conditions = {})
13
+ data = client.get(URI.escape("#{path}/#{id}"), conditions)
14
+ new(JSON.parse(data))
15
+ end
16
+ end
17
+
18
+ module Create
19
+ def create(attributes)
20
+ data = client.post(URI.escape(path), attributes)
21
+ new(JSON.parse(data))
22
+ end
23
+ end
24
+
25
+ module Search
26
+ def search(conditions)
27
+ query = build_query(conditions)
28
+ full_path = URI.escape("#{path}?#{query}")
29
+ data = client.get(full_path, {})
30
+ JSON.parse(data).map { |datum| new(datum) }
31
+ end
32
+
33
+ private
34
+
35
+ def build_query(conditions)
36
+ conditions.map do |key, value|
37
+ if value.is_a?(Array)
38
+ value.map { |v| "f_#{key}=#{v}" }.join('&')
39
+ else
40
+ "f_#{key}=#{value}"
41
+ end
42
+ end.join('&')
43
+ end
44
+ end
45
+ end
46
+
47
+ module InstanceMethods
48
+ module Save
49
+ def save
50
+ new_record? ? create : update
51
+ end
52
+
53
+ def new_record?
54
+ id.nil?
55
+ end
56
+ end
57
+
58
+ module Create
59
+ include Circonus::Actions::InstanceMethods::Save
60
+ def create
61
+ data = client.post(URI.escape(path), filtered_attributes)
62
+ self.class.new(JSON.parse(data))
63
+ end
64
+ end
65
+
66
+ module Update
67
+ include Circonus::Actions::InstanceMethods::Save
68
+ def update
69
+ data = client.put(URI.escape("#{path}/#{id}"), filtered_attributes)
70
+ self.class.new(JSON.parse(data))
71
+ end
72
+ end
73
+
74
+ module Destroy
75
+ def destroy
76
+ client.delete(URI.escape("#{path}/#{id}"))
77
+ end
78
+ alias_method :delete, :destroy
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,8 @@
1
+ module Circonus
2
+ class Alert < BaseModel
3
+ fields :_cid, :_metric_notes, :_cleared_value, :_tags, :_occurred_on, :_cleared_on,
4
+ :_check_name, :_broker, :_rule_set, :_maintenance, :_metric_link, :_value,
5
+ :_acknowledgement, :_severity, :_check, :_alert_url, :_metric_name
6
+ actions :all, :find, :search
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Circonus
2
+ class Annotation < BaseModel
3
+ fields :_cid, :_created, :stop, :_last_modified_by, :rel_metrics,
4
+ :description, :title, :category, :_last_modified, :start
5
+ actions :all, :find, :search, :create, :update, :destroy
6
+ end
7
+ end
@@ -0,0 +1,81 @@
1
+ module Circonus
2
+ class BaseModel
3
+ ACTIONS_CLASS = {
4
+ all: Circonus::Actions::ClassMethods::All,
5
+ find: Circonus::Actions::ClassMethods::Find,
6
+ create: Circonus::Actions::ClassMethods::Create,
7
+ search: Circonus::Actions::ClassMethods::Search
8
+ }
9
+
10
+ ACTIONS_INSTANCE = {
11
+ create: Circonus::Actions::InstanceMethods::Create,
12
+ update: Circonus::Actions::InstanceMethods::Update,
13
+ destroy: Circonus::Actions::InstanceMethods::Destroy
14
+ }
15
+
16
+ class << self
17
+ attr_reader :attributes
18
+
19
+ def circonus_path(path)
20
+ @circonus_path = path.to_s
21
+ end
22
+
23
+ def path
24
+ @circonus_path ||= name.split('::').last.gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
25
+ end
26
+
27
+ def fields(*attributes)
28
+ @attributes = attributes.map(&:to_s)
29
+ attr_accessor(*attributes)
30
+ end
31
+
32
+ def actions(*acts)
33
+ acts.each do |action|
34
+ module_class = ACTIONS_CLASS[action]
35
+ module_instance = ACTIONS_INSTANCE[action]
36
+ if module_class || module_instance
37
+ extend(module_class) if module_class
38
+ include(module_instance) if module_instance
39
+ else
40
+ fail 'Wrong action'
41
+ end
42
+ end
43
+ end
44
+
45
+ def client
46
+ ::Circonus.client
47
+ end
48
+ end
49
+
50
+ def initialize(data = {})
51
+ self.class::attributes.each do |attribute|
52
+ value = data[attribute.to_s].nil? ? data[attribute.to_sym] : data[attribute.to_s]
53
+ send("#{attribute}=", value)
54
+ end
55
+ end
56
+
57
+ def id
58
+ _cid ? _cid.split('/').last : nil
59
+ end
60
+
61
+ def attributes
62
+ Hash[self.class::attributes.map { |attr| [attr, send(attr)] }]
63
+ end
64
+
65
+ def filtered_attributes
66
+ attributes.reject { |_k, v| v.nil? }
67
+ end
68
+
69
+ def client
70
+ self.class.client
71
+ end
72
+
73
+ def path
74
+ self.class.path
75
+ end
76
+
77
+ (ACTIONS_CLASS.keys + ACTIONS_INSTANCE.keys).each do |action|
78
+ define_method(action) { raise Circonus::UnsupportedAction }
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class Broker < BaseModel
3
+ fields :_cid, :_tags, :_name, :_type, :_longitude, :_latitude, :_details
4
+ actions :all, :find, :search
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class Check < BaseModel
3
+ fields :_cid, :_broker, :_active, :_check_uuid, :_check_bundle, :_details
4
+ actions :all, :find, :search
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module Circonus
2
+ class CheckBundle < BaseModel
3
+ fields :_created, :status, :_reverse_connection_urls, :target, :_checks, :timeout, :metrics, :_last_modified,
4
+ :brokers, :_last_modified_by, :period, :_cid, :display_name, :tags, :_check_uuids, :notes, :type, :config
5
+ actions :all, :find, :search, :create, :update, :destroy
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class CheckMove < BaseModel
3
+ fields :_cid, :_error, :new_broker, :check_id, :_broker, :_status
4
+ actions :all, :find, :search, :create, :update, :destroy
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module Circonus
2
+ class ContactGroup < BaseModel
3
+ fields :_cid, :contacts, :aggregation_window, :_last_modified_by, :name,
4
+ :escalations, :tags, :reminders, :_last_modified, :alert_formats
5
+ actions :all, :find, :search, :create, :update, :destroy
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ module Circonus
2
+ class Data < BaseModel
3
+ TYPES = %w(text numeric histogram)
4
+ PERIODS = [300, 1800, 10800, 86400]
5
+ REQUIRED_CONDITIONS = [:type, :start, :end, :period]
6
+
7
+ fields :_cid, :data
8
+ actions :find
9
+
10
+ class << self
11
+ def find(check_id, metric_name, conditions)
12
+ check_conditions!(conditions)
13
+ convert_time!(conditions)
14
+ super("#{check_id}_#{metric_name}", conditions)
15
+ end
16
+
17
+ alias_method :search, :find
18
+
19
+ private
20
+
21
+ def check_conditions!(conditions)
22
+ REQUIRED_CONDITIONS.each do |condition|
23
+ fail "'#{condition}' is required" unless conditions.include?(condition)
24
+ end
25
+ fail "wrong 'type'" unless TYPES.include?(conditions[:type])
26
+ fail "wrong 'period'" unless PERIODS.include?(conditions[:period])
27
+ end
28
+
29
+ def convert_time!(conditions)
30
+ conditions[:start] = conditions[:start].to_i
31
+ conditions[:end] = conditions[:end].to_i
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ module Circonus
2
+ class Graph < BaseModel
3
+ fields :_cid, :access_keys, :composites, :style, :description, :line_style,
4
+ :tags, :logarithmic_right_y, :logarithmic_left_y, :datapoints, :max_left_y,
5
+ :notes, :title, :max_right_y, :min_left_y, :guides, :metric_clusters, :min_right_y
6
+ actions :all, :find, :search, :create, :update, :destroy
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class Maintenance < BaseModel
3
+ fields :_cid, :severities, :stop, :item, :tags, :notes, :type, :start
4
+ actions :all, :find, :search, :create, :update, :destroy
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class MetricCluster < BaseModel
3
+ fields :_cid, :name, :queries, :description, :tags
4
+ actions :all, :find, :search, :create, :update, :destroy
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ module Circonus
2
+ class RebuildBroker < BaseModel
3
+ fields :_cid, :_cert, :rebuild, :csr
4
+ actions :all, :find, :search, :update
5
+
6
+ def self.all
7
+ raise UnsupportedAction
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class RuleSet < BaseModel
3
+ fields :_cid, :link, :parent, :metric_type, :rules, :check, :metric_name, :notes, :derive, :contact_groups
4
+ actions :all, :find, :search, :create, :update, :destroy
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class RuleSetGroup < BaseModel
3
+ fields :_cid, :formulas, :name, :contact_groups, :tags, :rule_set_conditions
4
+ actions :all, :find, :search, :create, :update, :destroy
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class Tag < BaseModel
3
+ fields :_cid
4
+ actions :all, :find, :search
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module Circonus
2
+ class Template < BaseModel
3
+ fields :_cid, :hosts, :_last_modified_by, :status, :name, :sync_rules,
4
+ :tags, :master_host, :notes, :check_bundles, :_last_modified
5
+ actions :all, :find, :search, :create, :update, :destroy
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class User < BaseModel
3
+ fields :_cid, :firstname, :lastname, :email, :contact_info
4
+ actions :all, :find, :search, :update
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Circonus
2
+ class Worksheet < BaseModel
3
+ fields :_cid, :description, :tags, :graphs, :notes, :smart_queries, :title, :favorite
4
+ actions :all, :find, :search, :create, :update, :destroy
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Circonus
2
+ VERSION = '0.3.4'
3
+ end
@@ -0,0 +1 @@
1
+ require 'circonus'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: circonus_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.4
5
+ platform: ruby
6
+ authors:
7
+ - Anton Mishchuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Object Oriented mapper for Circonus API
28
+ email: anton.mishchuk@opower.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.markdown
34
+ - lib/circonus.rb
35
+ - lib/circonus/client.rb
36
+ - lib/circonus/exceptions.rb
37
+ - lib/circonus/models.rb
38
+ - lib/circonus/models/account.rb
39
+ - lib/circonus/models/acknowledgement.rb
40
+ - lib/circonus/models/actions.rb
41
+ - lib/circonus/models/alert.rb
42
+ - lib/circonus/models/annotation.rb
43
+ - lib/circonus/models/base_model.rb
44
+ - lib/circonus/models/broker.rb
45
+ - lib/circonus/models/check.rb
46
+ - lib/circonus/models/check_bundle.rb
47
+ - lib/circonus/models/check_move.rb
48
+ - lib/circonus/models/contact_group.rb
49
+ - lib/circonus/models/data.rb
50
+ - lib/circonus/models/graph.rb
51
+ - lib/circonus/models/maintenance.rb
52
+ - lib/circonus/models/metric_cluster.rb
53
+ - lib/circonus/models/rebuild_broker.rb
54
+ - lib/circonus/models/rule_set.rb
55
+ - lib/circonus/models/rule_set_group.rb
56
+ - lib/circonus/models/tag.rb
57
+ - lib/circonus/models/template.rb
58
+ - lib/circonus/models/user.rb
59
+ - lib/circonus/models/worksheet.rb
60
+ - lib/circonus/version.rb
61
+ - lib/circonus_api.rb
62
+ homepage: https://github.com/opower/circonus_api
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.6
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Client for Circonus API
85
+ test_files: []