Class: PuppetX::Wildfly::CLICommand

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/wildfly/cli_command.rb

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ CLICommand

Returns a new instance of CLICommand



7
8
9
10
11
12
# File 'lib/puppet_x/wildfly/cli_command.rb', line 7

def initialize(command)
  parser = JBossCLIParser.new
  @syntax_node = parser.parse(command)

  raise "Invalid command syntax. Could not parse: #{command}\nFailure reason: #{parser.failure_reason}" if @syntax_node.nil?
end

Instance Method Details

#addressObject



14
15
16
# File 'lib/puppet_x/wildfly/cli_command.rb', line 14

def address
  address_to_key_value_list(@syntax_node.elements[0])
end

#address_to_key_value_list(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet_x/wildfly/cli_command.rb', line 31

def address_to_key_value_list(node)
  list = []

  unless node.empty?
    nodes = node.elements

    nodes.size.times do |index|
      element = nodes[index]
      list << { element.elements[1].text_value => element.elements[3].text_value.delete('"') }
    end
  end

  list
end

#extract_value(element) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet_x/wildfly/cli_command.rb', line 62

def extract_value(element)
  case element.elements[0].text_value
  when '['
    values = []

    values << element.elements[1].text_value

    list = element.elements[2].elements

    list.size.times do |index|
      values << list[index].elements[1].text_value
    end

    values
  when '{'
    values = {}
    key_value = element.elements[1]
    pairs = element.elements[2].elements

    values[key_value.elements[0].text_value] = key_value.elements[2].text_value

    pairs.size.times do |index|
      pair = pairs[index].elements[1]

      values[pair.elements[0].text_value] = pair.elements[2].text_value
    end

    values
  else
    element.text_value
  end
end

#headersObject



27
28
29
# File 'lib/puppet_x/wildfly/cli_command.rb', line 27

def headers
  params_to_key_value_list(@syntax_node.elements[4])
end

#operationObject



18
19
20
21
# File 'lib/puppet_x/wildfly/cli_command.rb', line 18

def operation
  operation = @syntax_node.elements[2]
  operation.empty? ? '' : operation.text_value
end

#paramsObject



23
24
25
# File 'lib/puppet_x/wildfly/cli_command.rb', line 23

def params
  params_to_key_value_list(@syntax_node.elements[3])
end

#params_to_key_value_list(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet_x/wildfly/cli_command.rb', line 46

def params_to_key_value_list(node)
  list = []

  unless node.empty?
    nodes = node.elements[1].elements

    nodes.size.times do |index|
      element = nodes[index].elements[0]

      list << { element.elements[0].text_value => extract_value(element.elements[2]) }
    end
  end

  list
end

#to_detyped_requestObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/puppet_x/wildfly/cli_command.rb', line 95

def to_detyped_request
  request = {}
  request[:operation] = operation
  request[:address] = address

  params.each do |param|
    param.each do |key, value|
      request[key] = value
    end
  end

  unless headers.empty?
    request['operation-headers'] = {}
    headers.each do |header|
      header.each do |key, value|
        request['operation-headers'].store(key, value)
      end
    end
  end

  request
end