Module: Pacemaker::Helpers

Included in:
Puppet::Provider::PacemakerXML, Serverspec::Type::PacemakerXML
Defined in:
lib/pacemaker/xml/helpers.rb

Overview

misc helper methods used in other submodules

Instance Method Summary collapse

Instance Method Details

#attributes_to_hash(element, hash = {}) ⇒ Hash<String => String>

convert elements’s attributes to hash

Parameters:

  • element (REXML::Element)

Returns:

  • (Hash<String => String>)


7
8
9
10
11
12
13
# File 'lib/pacemaker/xml/helpers.rb', line 7

def attributes_to_hash(element, hash = {})
  element.attributes.each do |a, v|
    next if a == '__crm_diff_marker__'
    hash.store a.to_s, v.to_s
  end
  hash
end

#children_elements_to_array(element, tag = nil) ⇒ Array<Hash>

convert element’s children to array of their attributes

Parameters:

  • element (REXML::Element)
  • tag (String) (defaults to: nil)

    get only this type of children

Returns:

  • (Array<Hash>)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pacemaker/xml/helpers.rb', line 39

def children_elements_to_array(element, tag = nil)
  return unless element.is_a? REXML::Element
  elements = []
  children = element.get_elements tag
  return elements unless children
  children.each do |child|
    child_structure = attributes_to_hash child
    next unless child_structure['id']
    elements << child_structure
  end
  elements
end

#children_elements_to_hash(element, key, tag = nil) ⇒ Hash<String => String>

convert element’s children to hash of their attributes using key and hash key

Parameters:

  • element (REXML::Element)
  • key (String)

    use this attribute as hash key

  • tag (String) (defaults to: nil)

    get only this type of children

Returns:

  • (Hash<String => String>)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pacemaker/xml/helpers.rb', line 21

def children_elements_to_hash(element, key, tag = nil)
  return unless element.is_a? REXML::Element
  elements = {}
  children = element.get_elements tag
  return elements unless children
  children.each do |child|
    child_structure = attributes_to_hash child
    name = child_structure[key]
    next unless name
    elements.store name, child_structure
  end
  elements
end

#copy_value(from, from_key, to, to_key = nil) ⇒ Object

copy value from one hash_like structure to another if the value is present

Parameters:

  • from (Hash)
  • from_key (String, Symbol)
  • to (Hash)
  • to_key (String, Symbol, NilClass) (defaults to: nil)


58
59
60
61
62
63
64
# File 'lib/pacemaker/xml/helpers.rb', line 58

def copy_value(from, from_key, to, to_key = nil)
  value = from[from_key]
  return value unless value
  to_key = from_key unless to_key
  to[to_key] = value
  value
end

#export_attributes_structure(hash, attributes_id_tag) ⇒ Hash, NilClass

export the Puppet representation of attributes to the library one

Parameters:

  • hash (Hash)

    attributes (name => value)

  • attributes_id_tag (String)

    attributes name for id naming

Returns:

  • (Hash, NilClass)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pacemaker/xml/helpers.rb', line 124

def export_attributes_structure(hash, attributes_id_tag)
  return unless hash.is_a? Hash
  attributes = {}
  hash.each do |attribute_name, attribute_value|
    id_components = [resource[:name], attributes_id_tag, attribute_name]
    id_components.reject!(&:nil?)
    attribute_structure = {}
    attribute_structure['id'] = id_components.join '-'
    attribute_structure['name'] = attribute_name
    attribute_structure['value'] = attribute_value
    attributes.store attribute_name, attribute_structure
  end
  attributes
end

#get_primitive_puppet_enable(primitive) ⇒ :true, :false

return service enabled status value expected by Puppet puppet wants :true or :false symbols

Parameters:

  • primitive (String)

Returns:

  • (:true, :false)


96
97
98
99
100
101
102
# File 'lib/pacemaker/xml/helpers.rb', line 96

def get_primitive_puppet_enable(primitive)
  if primitive_is_managed? primitive
    :true
  else
    :false
  end
end

#get_primitive_puppet_status(primitive, node = nil) ⇒ :running, :stopped

return service status value expected by Puppet puppet wants :running or :stopped symbol

Parameters:

  • primitive (String)

    primitive id

  • node (String) (defaults to: nil)

    on this node if given

Returns:

  • (:running, :stopped)


84
85
86
87
88
89
90
# File 'lib/pacemaker/xml/helpers.rb', line 84

def get_primitive_puppet_status(primitive, node = nil)
  if primitive_is_running? primitive, node
    :running
  else
    :stopped
  end
end

#import_attributes_structure(attributes) ⇒ Object

import the library representation of the attributes structure to the Puppet one



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pacemaker/xml/helpers.rb', line 106

def import_attributes_structure(attributes)
  return unless attributes.respond_to? :each
  hash = {}
  attributes.each do |attribute|
    if attribute.is_a?(Array) && attribute.length == 2
      attribute = attribute[1]
    end
    next unless attribute['name'] && attribute['value']
    hash.store attribute['name'], attribute['value']
  end
  hash
end

#sort_data(data, key = 'id') ⇒ Array<Hash>

sort hash of hashes into an array of hashes by one of the subhash’s attributes

Parameters:

  • data (Hash<String => Hash>)
  • key (String) (defaults to: 'id')

Returns:

  • (Array<Hash>)


71
72
73
74
75
76
77
# File 'lib/pacemaker/xml/helpers.rb', line 71

def sort_data(data, key = 'id')
  data = data.values if data.is_a? Hash
  data.sort do |x, y|
    break 0 unless x[key] && y[key]
    x[key] <=> y[key]
  end
end