Class: Puppet::Provider::PowerstorePolicy::PowerstorePolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/powerstore_policy/powerstore_policy.rb

Overview

class Puppet::Provider::PowerstorePolicy::PowerstorePolicy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_keys_to_request(request, hash) ⇒ Object



366
367
368
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 366

def self.add_keys_to_request(request, hash)
  hash.each { |x, v| request[x] = v } if hash
end

.authenticate(_path_params, _query_params, _header_params, _body_params) ⇒ Object



355
356
357
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 355

def self.authenticate(_path_params, _query_params, _header_params, _body_params)
  true
end

.build_key_valuesObject



123
124
125
126
127
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 123

def self.build_key_values
  key_values = {}
  key_values['api-version'] = 'assets'
  key_values
end

.deep_delete(hash_item, tokens) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 333

def self.deep_delete(hash_item, tokens)
  if tokens.size == 1
    if hash_item.is_a?(Array)
      hash_item.map! { |item| deep_delete(item, tokens) }
    else
      hash_item.delete(tokens[0]) unless hash_item.nil? || hash_item[tokens[0]].nil?
    end
  elsif hash_item.is_a?(Array)
    hash_item.map! { |item| deep_delete(item, tokens[1..-1]) }
  else
    hash_item[tokens.first] = deep_delete(hash_item[tokens.first], tokens[1..-1]) unless hash_item.nil? || hash_item[tokens[0]].nil?
  end
  hash_item
end

.fetch_all(context) ⇒ Object



348
349
350
351
352
353
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 348

def self.fetch_all(context)
  response = invoke_list_all(context)
  return unless response.is_a? Net::HTTPSuccess
  body = JSON.parse(response.body)
  body # ["value"] if body.is_a? Array # and body.key? "value"
end

.fetch_all_as_hash(context) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 303

def self.fetch_all_as_hash(context)
  items = fetch_all(context)
  if items
    items.map { |item|
      hash = {
        add_replication_rule_ids: item['add_replication_rule_ids'],
        add_snapshot_rule_ids: item['add_snapshot_rule_ids'],
        description: item['description'],
        id: item['id'],
        is_replica: item['is_replica'],
        name: item['name'],
        remove_replication_rule_ids: item['remove_replication_rule_ids'],
        remove_snapshot_rule_ids: item['remove_snapshot_rule_ids'],
        replication_rule_ids: item['replication_rule_ids'],
        snapshot_rule_ids: item['snapshot_rule_ids'],
        type: item['type'],
        type_l10n: item['type_l10n'],
        ensure: 'present',
      }
      Puppet.debug("Adding to collection: #{item}")
      hash
    }.compact
  else
    []
  end
rescue StandardError => ex
  Puppet.alert("ex is #{ex} and backtrace is #{ex.backtrace}")
  raise
end

.invoke_create(context, resource = nil, body_params = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 170

def self.invoke_create(context, resource = nil, body_params = nil)
  key_values = build_key_values
  Puppet.info('Calling operation policy_create')
  path_params = {}
  query_params = {}
  header_params = {}
  header_params['User-Agent'] = ''

  op_params = [
    op_param('description', 'body', 'description', 'description'),
    op_param('name', 'body', 'name', 'name'),
    op_param('replication_rule_ids', 'body', 'replication_rule_ids', 'replication_rule_ids'),
    op_param('snapshot_rule_ids', 'body', 'snapshot_rule_ids', 'snapshot_rule_ids'),
  ]
  op_params.each do |i|
    inquery = i[:inquery]
    name    = i[:name]
    paramalias = i[:paramalias]
    name_snake = i[:namesnake]
    if inquery == 'query'
      query_params[name] = key_values[name] unless key_values[name].nil?
      query_params[name] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      query_params[name] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    else
      path_params[name_snake.to_sym] = key_values[name] unless key_values[name].nil?
      path_params[name_snake.to_sym] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      path_params[name_snake.to_sym] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    end
  end
  context.transport.call_op(path_params, query_params, header_params, body_params, '/policy', 'Post', 'application/json')
end

.invoke_delete(context, resource = nil, body_params = nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 241

def self.invoke_delete(context, resource = nil, body_params = nil)
  key_values = build_key_values
  Puppet.info('Calling operation policy_delete')
  path_params = {}
  query_params = {}
  header_params = {}
  header_params['User-Agent'] = ''

  op_params = [
    op_param('id', 'path', 'id', 'id'),
  ]
  op_params.each do |i|
    inquery = i[:inquery]
    name    = i[:name]
    paramalias = i[:paramalias]
    name_snake = i[:namesnake]
    if inquery == 'query'
      query_params[name] = key_values[name] unless key_values[name].nil?
      query_params[name] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      query_params[name] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    else
      path_params[name_snake.to_sym] = key_values[name] unless key_values[name].nil?
      path_params[name_snake.to_sym] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      path_params[name_snake.to_sym] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    end
  end
  context.transport.call_op(path_params, query_params, header_params, body_params, '/policy/%{id}', 'Delete', 'application/json')
end

.invoke_get_one(context, resource = nil, body_params = nil) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 273

def self.invoke_get_one(context, resource = nil, body_params = nil)
  key_values = build_key_values
  Puppet.info('Calling operation policy_instance_query')
  path_params = {}
  query_params = {}
  header_params = {}
  header_params['User-Agent'] = ''

  op_params = [
    op_param('id', 'path', 'id', 'id'),
  ]
  op_params.each do |i|
    inquery = i[:inquery]
    name    = i[:name]
    paramalias = i[:paramalias]
    name_snake = i[:namesnake]
    if inquery == 'query'
      query_params[name] = key_values[name] unless key_values[name].nil?
      query_params[name] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      query_params[name] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    else
      path_params[name_snake.to_sym] = key_values[name] unless key_values[name].nil?
      path_params[name_snake.to_sym] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      path_params[name_snake.to_sym] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    end
  end
  context.transport.call_op(path_params, query_params, header_params, body_params, '/policy/%{id}', 'Get', 'application/json')
end

.invoke_list_all(context, resource = nil, body_params = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 141

def self.invoke_list_all(context, resource = nil, body_params = nil)
  key_values = build_key_values
  Puppet.info('Calling operation policy_collection_query')
  path_params = {}
  query_params = {}
  header_params = {}
  header_params['User-Agent'] = ''

  op_params = [
  ]
  op_params.each do |i|
    inquery = i[:inquery]
    name    = i[:name]
    paramalias = i[:paramalias]
    name_snake = i[:namesnake]
    if inquery == 'query'
      query_params[name] = key_values[name] unless key_values[name].nil?
      query_params[name] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      query_params[name] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    else
      path_params[name_snake.to_sym] = key_values[name] unless key_values[name].nil?
      path_params[name_snake.to_sym] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      path_params[name_snake.to_sym] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    end
  end
  context.transport.call_op(path_params, query_params, header_params, body_params, '/policy', 'Get', 'application/json')
end

.invoke_update(context, resource = nil, body_params = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 203

def self.invoke_update(context, resource = nil, body_params = nil)
  key_values = build_key_values
  Puppet.info('Calling operation policy_modify')
  path_params = {}
  query_params = {}
  header_params = {}
  header_params['User-Agent'] = ''

  op_params = [
    op_param('add_replication_rule_ids', 'body', 'add_replication_rule_ids', 'add_replication_rule_ids'),
    op_param('add_snapshot_rule_ids', 'body', 'add_snapshot_rule_ids', 'add_snapshot_rule_ids'),
    op_param('description', 'body', 'description', 'description'),
    op_param('id', 'path', 'id', 'id'),
    op_param('name', 'body', 'name', 'name'),
    op_param('remove_replication_rule_ids', 'body', 'remove_replication_rule_ids', 'remove_replication_rule_ids'),
    op_param('remove_snapshot_rule_ids', 'body', 'remove_snapshot_rule_ids', 'remove_snapshot_rule_ids'),
    op_param('replication_rule_ids', 'body', 'replication_rule_ids', 'replication_rule_ids'),
    op_param('snapshot_rule_ids', 'body', 'snapshot_rule_ids', 'snapshot_rule_ids'),
  ]
  op_params.each do |i|
    inquery = i[:inquery]
    name    = i[:name]
    paramalias = i[:paramalias]
    name_snake = i[:namesnake]
    if inquery == 'query'
      query_params[name] = key_values[name] unless key_values[name].nil?
      query_params[name] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      query_params[name] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    else
      path_params[name_snake.to_sym] = key_values[name] unless key_values[name].nil?
      path_params[name_snake.to_sym] = ENV["powerstore_#{name_snake}"] unless ENV["powerstore_#{name_snake}"].nil?
      path_params[name_snake.to_sym] = resource[paramalias.to_sym] unless resource.nil? || resource[paramalias.to_sym].nil?
    end
  end
  context.transport.call_op(path_params, query_params, header_params, body_params, '/policy/%{id}', 'Patch', 'application/json')
end

.op_param(name, inquery, paramalias, namesnake) ⇒ Object



378
379
380
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 378

def self.op_param(name, inquery, paramalias, namesnake)
  { name: name, inquery: inquery, paramalias: paramalias, namesnake: namesnake }
end

.to_query(hash) ⇒ Object



370
371
372
373
374
375
376
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 370

def self.to_query(hash)
  if hash
    return_value = hash.map { |x, v| "#{x}=#{v}" }.reduce { |x, v| "#{x}&#{v}" }
    return return_value unless return_value.nil?
  end
  ''
end

Instance Method Details

#build_create_hash(resource) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 79

def build_create_hash(resource)
  policy = {}
  policy['description'] = resource[:description] unless resource[:description].nil?
  policy['name'] = resource[:name] unless resource[:name].nil?
  policy['replication_rule_ids'] = resource[:replication_rule_ids] unless resource[:replication_rule_ids].nil?
  policy['snapshot_rule_ids'] = resource[:snapshot_rule_ids] unless resource[:snapshot_rule_ids].nil?
  policy
end

#build_delete_hash(resource) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



100
101
102
103
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 100

def build_delete_hash(resource)
  policy = {}
  policy
end

#build_hash(resource) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 106

def build_hash(resource)
  policy = {}
  policy['add_replication_rule_ids'] = resource[:add_replication_rule_ids] unless resource[:add_replication_rule_ids].nil?
  policy['add_snapshot_rule_ids'] = resource[:add_snapshot_rule_ids] unless resource[:add_snapshot_rule_ids].nil?
  policy['description'] = resource[:description] unless resource[:description].nil?
  policy['id'] = resource[:id] unless resource[:id].nil?
  policy['is_replica'] = resource[:is_replica] unless resource[:is_replica].nil?
  policy['name'] = resource[:name] unless resource[:name].nil?
  policy['remove_replication_rule_ids'] = resource[:remove_replication_rule_ids] unless resource[:remove_replication_rule_ids].nil?
  policy['remove_snapshot_rule_ids'] = resource[:remove_snapshot_rule_ids] unless resource[:remove_snapshot_rule_ids].nil?
  policy['replication_rule_ids'] = resource[:replication_rule_ids] unless resource[:replication_rule_ids].nil?
  policy['snapshot_rule_ids'] = resource[:snapshot_rule_ids] unless resource[:snapshot_rule_ids].nil?
  policy['type'] = resource[:type] unless resource[:type].nil?
  policy['type_l10n'] = resource[:type_l10n] unless resource[:type_l10n].nil?
  policy
end

#build_update_hash(resource) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 87

def build_update_hash(resource)
  policy = {}
  policy['add_replication_rule_ids'] = resource[:add_replication_rule_ids] unless resource[:add_replication_rule_ids].nil?
  policy['add_snapshot_rule_ids'] = resource[:add_snapshot_rule_ids] unless resource[:add_snapshot_rule_ids].nil?
  policy['description'] = resource[:description] unless resource[:description].nil?
  policy['name'] = resource[:name] unless resource[:name].nil?
  policy['remove_replication_rule_ids'] = resource[:remove_replication_rule_ids] unless resource[:remove_replication_rule_ids].nil?
  policy['remove_snapshot_rule_ids'] = resource[:remove_snapshot_rule_ids] unless resource[:remove_snapshot_rule_ids].nil?
  policy['replication_rule_ids'] = resource[:replication_rule_ids] unless resource[:replication_rule_ids].nil?
  policy['snapshot_rule_ids'] = resource[:snapshot_rule_ids] unless resource[:snapshot_rule_ids].nil?
  policy
end

#canonicalize(_context, resources) ⇒ Object



7
8
9
10
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 7

def canonicalize(_context, resources)
  # nout to do here but seems we need to implement it
  resources
end

#create(context, name, should) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 49

def create(context, name, should)
  context.creating(name) do
    new_hash = build_create_hash(should)
    new_hash.delete('id')
    response = self.class.invoke_create(context, should, new_hash)

    raise("Create failed.  Response is #{response} and body is #{response.body}") unless response.is_a? Net::HTTPSuccess
    should[:ensure] = 'present'
    Puppet.info('Added :ensure to property hash')
  end
rescue StandardError => ex
  Puppet.alert("Exception during create. The state of the resource is unknown.  ex is #{ex} and backtrace is #{ex.backtrace}")
  raise
end

#delete(context, should) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 129

def delete(context, should)
  new_hash = build_delete_hash(should)
  response = self.class.invoke_delete(context, should, new_hash)
  raise("Delete failed.  The state of the resource is unknown.  Response is #{response} and body is #{response.body}") unless response.is_a? Net::HTTPSuccess
  should[:ensure] = 'absent'
  Puppet.info "Added 'absent' to property_hash"
rescue StandardError => ex
  Puppet.alert("Exception during destroy. ex is #{ex} and backtrace is #{ex.backtrace}")
  raise
end

#exists?Boolean

Returns:

  • (Boolean)


360
361
362
363
364
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 360

def exists?
  return_value = @property_hash[:ensure] && @property_hash[:ensure] != 'absent'
  Puppet.info("Checking if resource #{name} of type <no value> exists, returning #{return_value}")
  return_value
end

#get(context) ⇒ Object



12
13
14
15
16
17
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 12

def get(context)
  context.debug('Entered get')
  hash = self.class.fetch_all_as_hash(context)
  context.debug("Completed get, returning hash #{hash}")
  hash
end

#set(context, changes, noop: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet/provider/powerstore_policy/powerstore_policy.rb', line 19

def set(context, changes, noop: false)
  context.debug('Entered set')

  changes.each do |name, change|
    context.debug("set change with #{name} and #{change}")
    # FIXME: key[:name] below hardwires the unique key of the resource to be :name
    is = change.key?(:is) ? change[:is] : get(context).find { |key| key[:name] == name }
    should = change[:should]

    is = { name: name, ensure: 'absent' } if is.nil?
    should = { name: name, ensure: 'absent' } if should.nil?

    if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
      create(context, name, should) unless noop
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
      context.deleting(name) do
        # FIXME: hardwired
        should[:id] = is[:id]
        delete(context, should) unless noop
      end
    elsif is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'absent'
      context.failed(name, message: 'Unexpected absent to absent change')
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
      # FIXME: hardwired
      should[:id] = is[:id]
      update(context, name, should)
    end
  end
end

#update(context, name, should) ⇒ Object



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

def update(context, name, should)
  context.updating(name) do
    new_hash = build_update_hash(should)
    new_hash.delete('id')
    response = self.class.invoke_update(context, should, new_hash)

    raise("Update failed. The state of the resource is unknown.  Response is #{response} and body is #{response.body}") unless response.is_a? Net::HTTPSuccess
    should[:ensure] = 'present'
    Puppet.info('Added :ensure to property hash')
  end
rescue StandardError => ex
  Puppet.alert("Exception during update. ex is #{ex} and backtrace is #{ex.backtrace}")
  raise
end