rubix 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,126 @@
1
+ module Rubix
2
+
3
+ # It might be worth using ActiveModel -- but maybe not. The goal is
4
+ # to keep dependencies low while still retaining expressiveness.
5
+ class Model
6
+
7
+ attr_accessor :properties, :id
8
+
9
+ extend Logs
10
+ include Logs
11
+
12
+ def self.model_name
13
+ self.to_s.split('::').last
14
+ end
15
+
16
+ def self.log_name
17
+ model_name
18
+ end
19
+
20
+ def initialize properties={}
21
+ @properties = properties
22
+ @id = properties[:id]
23
+ @log_name = self.class.model_name
24
+ end
25
+
26
+ def new_record?
27
+ @id.nil?
28
+ end
29
+
30
+ def request method, params
31
+ self.class.request(method, params)
32
+ end
33
+
34
+ def self.request method, params
35
+ Rubix.connection && Rubix.connection.request(method, params)
36
+ end
37
+
38
+ def self.find options={}
39
+ response = find_request(options)
40
+ case
41
+ when response.has_data?
42
+ build(response.result.first)
43
+ when response.success?
44
+ # a successful but empty response means it wasn't found
45
+ else
46
+ error("Could not find #{options.inspect}: #{response.error_message}")
47
+ nil
48
+ end
49
+ end
50
+
51
+ def self.find_or_create options={}
52
+ response = find_request(options)
53
+ case
54
+ when response.has_data?
55
+ build(response.result.first)
56
+ when response.success?
57
+ # doesn't exist
58
+ obj = new(options)
59
+ if obj.save
60
+ obj
61
+ else
62
+ false
63
+ end
64
+ else
65
+ error("Could not create #{options.inspect}: #{response.error_message}")
66
+ false
67
+ end
68
+ end
69
+
70
+ def validate
71
+ true
72
+ end
73
+
74
+ def create
75
+ return false unless validate
76
+ response = create_request
77
+ if response.has_data?
78
+ @id = response.result[self.class.id_field + 's'].first.to_i
79
+ info("Created")
80
+ true
81
+ else
82
+ error("Could not create: #{response.error_message}")
83
+ false
84
+ end
85
+ end
86
+
87
+ def update
88
+ return false unless validate
89
+ return create if new_record?
90
+ response = update_request
91
+ if response.has_data?
92
+ info("Updated")
93
+ after_update
94
+ else
95
+ error("Could not update: #{response.error_message}")
96
+ false
97
+ end
98
+ after_update
99
+ end
100
+
101
+ def after_update
102
+ true
103
+ end
104
+
105
+ def save
106
+ new_record? ? create : update
107
+ end
108
+
109
+ def destroy
110
+ return false if new_record?
111
+ response = destroy_request
112
+ case
113
+ when response.has_data? && response.result.values.first.first.to_i == id
114
+ info("Destroyed")
115
+ true
116
+ when response.zabbix_error? && response.error_message =~ /does not exist/i
117
+ # was never there
118
+ true
119
+ else
120
+ error("Could not destroy: #{response.error_message}")
121
+ false
122
+ end
123
+ end
124
+
125
+ end
126
+ end
@@ -2,79 +2,81 @@ module Rubix
2
2
 
3
3
  class Template < Model
4
4
 
5
- attr_accessor :name, :host_ids
5
+ #
6
+ # == Properties & Finding ==
7
+ #
6
8
 
9
+ attr_accessor :name
10
+
7
11
  def initialize properties={}
8
12
  super(properties)
9
- @name = properties[:name]
10
- end
11
-
12
- def log_name
13
- "TEMPLATE #{name || id}"
14
- end
13
+ @name = properties[:name]
14
+
15
+ self.host_ids = properties[:host_ids]
16
+ self.hosts = properties[:hosts]
15
17
 
16
- def register
17
- exists? ? update : create
18
+ self.host_group_ids = properties[:host_group_ids]
19
+ self.host_groups = properties[:host_groups]
18
20
  end
19
21
 
20
- def unregister
21
- destroy if exists?
22
- end
23
-
24
- def load
25
- response = request('template.get', 'filter' => {'templateid' => id, 'name' => name}, 'select_hosts' => 'refer', 'output' => 'extend')
22
+ def self.find_request options={}
23
+ params = {'select_groups' => 'refer', 'select_hosts' => 'refer', 'output' => 'extend'}
26
24
  case
27
- when response.has_data?
28
- @id = response.first['templateid'].to_i
29
- @name = response.first['name']
30
- @host_ids = response.first['hosts'].map { |host_info| host_info['hostid'].to_i }
31
- @loaded = true
32
- @exists = true
33
- when response.success?
34
- @exists = false
35
- @loaded = true
36
- else
37
- error("Could not load: #{response.error_messaage}")
25
+ when options[:id]
26
+ params['templateids'] = [options[:id]]
27
+ when options[:name]
28
+ params['filter'] = { 'host' => options[:name] }
38
29
  end
30
+ request('template.get', params)
39
31
  end
40
32
 
41
- def create
42
- response = request('template.create', [{'name' => name}])
43
- if response.has_data?
44
- @id = response['templateids'].first.to_i
45
- @exists = true
46
- info("Created")
47
- else
48
- error("Could not create: #{response.error_message}.")
49
- end
33
+ def self.build template
34
+ new({
35
+ :id => (template['templateid'] || template['hostid']).to_i,
36
+ :name => template['host'],
37
+ :host_ids => template['hosts'].map { |host_info| host_info['hostid'].to_i },
38
+ :host_group_ids => template['groups'].map { |group| group['groupid'].to_i }
39
+ })
40
+ end
41
+
42
+ def log_name
43
+ "TEMPLATE #{name || id}"
50
44
  end
51
45
 
52
- def update
53
- # noop
54
- info("Updated")
46
+ def self.id_field
47
+ 'templateid'
55
48
  end
56
49
 
57
- def destroy
58
- response = request('template.delete', [{'templateid' => id}])
59
- case
60
- when response.has_data? && response['templateids'].first.to_i == id
61
- info("Deleted")
62
- when response.zabbix_error? && response.error_message =~ /does not exist/i
63
- # was never there...
64
- else
65
- error("Could not delete: #{response.error_message}")
66
- end
50
+ #
51
+ # == Validation ==
52
+ #
53
+
54
+ def validate
55
+ raise ValidationError.new("A template must have at least one host group.") if host_group_ids.nil? || host_group_ids.empty?
56
+ true
67
57
  end
58
+
59
+ #
60
+ # == Associations ==
61
+ #
68
62
 
69
- def contains? host
70
- return unless exists?
71
- host_ids.include?(host.id)
63
+ include Associations::HasManyHosts
64
+ include Associations::HasManyHostGroups
65
+
66
+ #
67
+ # == CRUD ==
68
+ #
69
+
70
+ def create_request
71
+ request('template.create', {'host' => name, 'groups' => host_group_params})
72
+ end
73
+
74
+ def update_request
75
+ request('template.update', [{'host' => name, 'templateid' => id, 'groups' => host_group_params}])
72
76
  end
73
77
 
74
- def self.find_or_create_by_name name
75
- new(:name => name).tap do |group|
76
- group.create unless group.exists?
77
- end
78
+ def destroy_request
79
+ request('template.delete', [{'templateid' => id}])
78
80
  end
79
81
 
80
82
  end
@@ -0,0 +1,84 @@
1
+ module Rubix
2
+
3
+ class UserMacro < Model
4
+
5
+ #
6
+ # == Properties & Finding ==
7
+ #
8
+
9
+ attr_accessor :name, :value
10
+
11
+ def initialize properties={}
12
+ super(properties)
13
+ @name = properties[:name] || self.class.unmacro_name(properties[:macro])
14
+ @value = properties[:value]
15
+
16
+ self.host = properties[:host]
17
+ self.host_id = properties[:host_id]
18
+ end
19
+
20
+ def self.find_request options={}
21
+ request('usermacro.get', 'hostids' => [options[:host_id]], 'filter' => {'macro' => macro_name(options[:name])}, "output" => "extend")
22
+ end
23
+
24
+ def self.build macro
25
+ new({
26
+ :id => macro['hostmacroid'].to_i,
27
+ :name => unmacro_name(macro['macro']),
28
+ :value => macro['value'],
29
+ :host_id => macro['hostid']
30
+ })
31
+ end
32
+
33
+ def log_name
34
+ "MACRO #{macro_name}@#{host.name}"
35
+ end
36
+
37
+ def self.unmacro_name name
38
+ (name || '').gsub(/^\{\$/, '').gsub(/\}$/, '').upcase
39
+ end
40
+
41
+ def self.macro_name name
42
+ "{$#{name.upcase}}"
43
+ end
44
+
45
+ def macro_name
46
+ self.class.macro_name(name)
47
+ end
48
+
49
+ def self.id_field
50
+ 'hostmacroid'
51
+ end
52
+
53
+ #
54
+ # == Associations ==
55
+ #
56
+
57
+ include Associations::BelongsToHost
58
+
59
+ #
60
+ # == Validation ==
61
+ #
62
+ def validate
63
+ raise ValidationError.new("A user macro must have both a 'name' and a 'value'") if name.nil? || name.strip.empty? || value.nil? || value.strip.empty?
64
+ true
65
+ end
66
+
67
+ #
68
+ # == CRUD ==
69
+ #
70
+
71
+ def create_request
72
+ request('usermacro.massAdd', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
73
+ end
74
+
75
+ def update_request
76
+ request('usermacro.massUpdate', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
77
+ end
78
+
79
+ def destroy_request
80
+ request('usermacro.massRemove', 'hostids' => [host_id], 'macros' => [macro_name])
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ module Rubix
2
+ autoload :Monitor, 'rubix/monitors/monitor'
3
+ autoload :ChefMonitor, 'rubix/monitors/chef_monitor'
4
+ autoload :ClusterMonitor, 'rubix/monitors/cluster_monitor'
5
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe "CRUD for hosts" do
4
+
5
+ before do
6
+ @hg = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
+ @hg.save
8
+
9
+ @h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg])
10
+ @h1.save
11
+
12
+ @h2 = Rubix::Host.new(:name => 'rubix_spec_host_2', :host_groups => [@hg])
13
+ @h2.save
14
+ end
15
+
16
+ after do
17
+ @h1.destroy
18
+ @h2.destroy
19
+ @hg.destroy
20
+ end
21
+
22
+ it "should be able to create, update, and destroy a host" do
23
+ integration_test
24
+
25
+ Rubix::Application.find(:name => 'rubix_spec_app_1', :host_id => @h1.id).should be_nil
26
+
27
+ app = Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @h1.id)
28
+ app.save
29
+ new_a = Rubix::Application.find(:name => 'rubix_spec_app_1', :host_id => @h1.id)
30
+ new_a.should_not be_nil
31
+ new_a.id.should == app.id
32
+ new_a.host_id.should == @h1.id
33
+ id = app.id
34
+ id.should_not be_nil
35
+
36
+ app.name = 'rubix_spec_app_2'
37
+ app.update
38
+ new_a = Rubix::Application.find(:id => id, :name => 'rubix_spec_app_2', :host_id => @h1.id)
39
+ new_a.should_not be_nil
40
+ new_a.name.should == 'rubix_spec_app_2'
41
+
42
+ app.destroy
43
+ Rubix::Application.find(:id => id, :host_id => @h1.id).should be_nil
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "CRUD for host groups" do
4
+
5
+ it "should be able to create, update, and destroy a host group" do
6
+ integration_test
7
+
8
+ Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should be_nil
9
+
10
+ hg = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
11
+ hg.save
12
+ Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should_not be_nil
13
+ id = hg.id
14
+ id.should_not be_nil
15
+
16
+ hg.name = 'rubix_spec_host_group_2'
17
+ hg.update
18
+ Rubix::HostGroup.find(:id => id).name.should == 'rubix_spec_host_group_2'
19
+
20
+ hg.destroy
21
+ Rubix::HostGroup.find(:id => id).should be_nil
22
+ end
23
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe "CRUD for hosts" do
4
+
5
+ before do
6
+ @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
+ @hg1.save
8
+
9
+ @hg2 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_2')
10
+ @hg2.save
11
+
12
+ @t1 = Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@hg1])
13
+ @t1.save
14
+
15
+ @t2 = Rubix::Template.new(:name => 'rubix_spec_template_2', :host_groups => [@hg2])
16
+ @t2.save
17
+
18
+ @um1 = Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1')
19
+ @um2 = Rubix::UserMacro.new(:name => 'rubix_spec_macro_2', :value => 'rubix_spec_value_2')
20
+
21
+ end
22
+
23
+ after do
24
+ @t1.destroy
25
+ @t2.destroy
26
+ @hg1.destroy
27
+ @hg2.destroy
28
+ end
29
+
30
+ it "should be able to create, update, and destroy a host" do
31
+ integration_test
32
+
33
+ Rubix::Host.find(:name => 'rubix_spec_host_1').should be_nil
34
+
35
+ h = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1], :templates => [@t1], :user_macros => [@um1])
36
+ h.save
37
+ begin
38
+ new_h = Rubix::Host.find(:name => 'rubix_spec_host_1')
39
+ new_h.should_not be_nil
40
+ new_h.template_ids.should include(@t1.id)
41
+ new_h.host_group_ids.should include(@hg1.id)
42
+ new_h.user_macros.size.should == 1
43
+ new_h.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_1'
44
+ new_h.user_macros.first.value.should == 'rubix_spec_value_1'
45
+
46
+ id = h.id
47
+ id.should_not be_nil
48
+
49
+ h.name = 'rubix_spec_host_2'
50
+ h.host_groups = [@hg2]
51
+ h.templates = [@t2]
52
+ h.user_macros = [@um2]
53
+ h.update
54
+
55
+ new_h = Rubix::Host.find(:name => 'rubix_spec_host_2')
56
+ new_h.should_not be_nil
57
+ new_h.template_ids.should include(@t2.id)
58
+ new_h.host_group_ids.should include(@hg2.id)
59
+ new_h.user_macros.size.should == 1
60
+ new_h.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_2'
61
+ new_h.user_macros.first.value.should == 'rubix_spec_value_2'
62
+ rescue => e
63
+ puts "#{e.class} -- #{e.message}"
64
+ puts e.backtrace
65
+ ensure
66
+ h.destroy
67
+ end
68
+ Rubix::Host.find(:id => id).should be_nil
69
+ end
70
+ end