Class: Lbrt::Driver

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper
Defined in:
lib/lbrt/driver.rb

Instance Method Summary collapse

Methods included from Logger::Helper

#log

Constructor Details

#initialize(client, options = {}) ⇒ Driver

Returns a new instance of Driver.



4
5
6
7
# File 'lib/lbrt/driver.rb', line 4

def initialize(client, options = {})
  @client = client
  @options = options
end

Instance Method Details

#create_alert(name, expected) ⇒ Object

Alert



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lbrt/driver.rb', line 104

def create_alert(name, expected)
  updated = false

  log(:info, "Create Alert `#{name}`", :color => :cyan)

  unless @options[:dry_run]
    params = alert_to_params(name, expected)
    response = @client.alerts.post(params)
    expected['id'] = response.fetch('id')
    updated = true
  end

  updated
end

#create_chart(space_name_or_id, space_id, name, expected) ⇒ Object

Chart



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lbrt/driver.rb', line 46

def create_chart(space_name_or_id, space_id, name, expected)
  updated = false

  log(:info, "Create Space `#{space_name_or_id}` > Chart `#{name}`", :color => :cyan)

  unless name.is_a?(String)
    raise TypeError, "wrong argument type #{name.class}: #{name.inspect} (expected String)"
  end

  unless @options[:dry_run]
    response = @client.spaces(space_id).charts.post(expected.merge('name' => name))
    expected['id'] = response.fetch('id')
    updated = true
  end

  updated
end

#create_service(key, expected) ⇒ Object

Service



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/lbrt/driver.rb', line 153

def create_service(key, expected)
  updated = false
  type, title = key

  log(:info, "Create Service `#{type}/#{title}`", :color => :cyan)

  settings = expected.fetch('settings')

  unless @options[:dry_run]
    response = @client.services.post(
      'title'    => title,
      'type'     => type,
      'settings' => settings
    )

    expected['id'] = response.fetch('id')
    updated = true
  end

  updated
end

#create_space(name, expected) ⇒ Object

Space



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lbrt/driver.rb', line 11

def create_space(name, expected)
  updated = false

  log(:info, "Create Space `#{name}`", :color => :cyan)

  unless name.is_a?(String)
    raise TypeError, "wrong argument type #{name.class}: #{name.inspect} (expected String)"
  end

  unless @options[:dry_run]
    response = @client.spaces.post('name' => name)
    expected['id'] = response.fetch('id')
    updated = true
  end

  updated
end

#delete_alert(name, actual) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lbrt/driver.rb', line 119

def delete_alert(name, actual)
  updated = false

  log(:info, "Delete Alert `#{name}`", :color => :red)

  alert_id = actual.fetch('id')

  unless @options[:dry_run]
    @client.alerts(alert_id).delete
    updated = true
  end

  updated
end

#delete_chart(space_name_or_id, space_id, name_or_id, actual) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lbrt/driver.rb', line 64

def delete_chart(space_name_or_id, space_id, name_or_id, actual)
  updated = false

  log(:info, "Delete Space `#{space_name_or_id}` > `#{name_or_id}`", :color => :red)

  chart_id = actual.fetch('id')

  unless @options[:dry_run]
    @client.spaces(space_id).charts(chart_id).delete
    updated = true
  end

  updated
end

#delete_service(key, actual) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/lbrt/driver.rb', line 175

def delete_service(key, actual)
  updated = false
  type, title = key

  log(:info, "Delete Service `#{type}/#{title}`", :color => :red)

  service_id = actual.fetch('id')

  unless @options[:dry_run]
    @client.services(service_id).delete
    updated = true
  end

  updated
end

#delete_space(name_or_id, actual) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lbrt/driver.rb', line 29

def delete_space(name_or_id, actual)
  updated = false

  log(:info, "Delete Space `#{name_or_id}`", :color => :red)

  space_id = actual.fetch('id')

  unless @options[:dry_run]
    @client.spaces(space_id).delete
    updated = true
  end

  updated
end

#update_alert(name, expected, actual) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/lbrt/driver.rb', line 134

def update_alert(name, expected, actual)
  updated = false

  log(:info, "Update Alert `#{name}`", :color => :green)
  log(:info, diff(Lbrt::Alert::DSL::Converter, {name => actual}, {name => expected}))

  alert_id = actual.fetch('id')

  unless @options[:dry_run]
    params = alert_to_params(name, expected)
    @client.alerts(alert_id).put(params)
    updated = true
  end

  updated
end

#update_chart(space_name_or_id, space_id, name_or_id, expected, actual) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lbrt/driver.rb', line 79

def update_chart(space_name_or_id, space_id, name_or_id, expected, actual)
  updated = false

  log(:info, "Update Space `#{space_name_or_id}` > Chart: #{name_or_id}", :color => :green)

  delta = diff(Lbrt::Space::DSL::Converter,
    {space_name_or_id => {'charts' => {name_or_id => actual}}},
    {space_name_or_id => {'charts' => {name_or_id => expected}}}
  )

  log(:info, delta)

  chart_id = actual.fetch('id')

  unless @options[:dry_run]
    params = chart_to_update_params(expected)
    @client.spaces(space_id).charts(chart_id).put(params)
    updated = true
  end

  updated
end

#update_service(key, expected, actual) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/lbrt/driver.rb', line 191

def update_service(key, expected, actual)
  updated = false
  type, title = key

  log(:info, "Update Service `#{type}/#{title}`", :color => :green)
  log(:info, diff(Lbrt::Service::DSL::Converter, {key => actual}, {key => expected}))

  service_id = actual.fetch('id')
  settings = expected.fetch('settings')

  unless @options[:dry_run]
    @client.services(service_id).put(
      'settings' => settings
    )

    updated = true
  end

  updated
end