Class: Puppet::Util::NetworkDevice::Transport::F5

Inherits:
Base
  • Object
show all
Defined in:
lib/puppet/util/network_device/transport/f5.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, _options = {}) ⇒ F5

Returns a new instance of F5.



8
9
10
11
# File 'lib/puppet/util/network_device/transport/f5.rb', line 8

def initialize(url, _options = {})
  require 'faraday'
  @connection = Faraday.new(url: url, ssl: { verify: false })
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/puppet/util/network_device/transport/f5.rb', line 6

def connection
  @connection
end

Instance Method Details

#call(url, args = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/puppet/util/network_device/transport/f5.rb', line 13

def call(url, args={})
  result = connection.get(url, args)
  JSON.parse(result.body)
rescue JSON::ParserError
  # This should be better at handling errors
  return nil
end

#delete(url) ⇒ Object



69
70
71
72
73
# File 'lib/puppet/util/network_device/transport/f5.rb', line 69

def delete(url)
  result = connection.delete(url)
  failure?(result)
  return result
end

#failure?(result) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/puppet/util/network_device/transport/f5.rb', line 21

def failure?(result)
  unless result.status == 200
    fail("REST failure: HTTP status code #{result.status} detected.  Body of failure is: #{result.body}")
  end
end

#find_availability(string) ⇒ Object

Monitoring: Parse out the availability integer.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/puppet/util/network_device/transport/f5.rb', line 96

def find_availability(string)
  return nil if string.nil?
  if string == "default" or string == "none"
    return nil
  end
  # Look for integers within the string.
  matches = string.match(/min\s(\d+)/)
  if matches
    matches[1]
  else
    "all"
  end
end

#find_monitors(string) ⇒ Object

Given a string containing objects matching /Partition/Object, return an array of all found objects.



84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/util/network_device/transport/f5.rb', line 84

def find_monitors(string)
  return nil if string.nil?
  if string == "default"
    ["default"]
  elsif string =~ %r{/none$}
    ["none"]
  else
    string.scan(/(\/\S+)/).flatten
  end
end

#patch(url, json) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puppet/util/network_device/transport/f5.rb', line 55

def patch(url, json)
  if valid_json?(json)
    result = connection.patch do |req|
      req.url url
      req.headers['Content-Type'] = 'application/json'
      req.body = json
    end
    failure?(result)
    return result
  else
    fail('Invalid JSON detected.')
  end
end

#post(url, json) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet/util/network_device/transport/f5.rb', line 27

def post(url, json)
  if valid_json?(json)
    result = connection.post do |req|
      req.url url
      req.headers['Content-Type'] = 'application/json'
      req.body = json
    end
    failure?(result)
    return result
  else
    fail('Invalid JSON detected.')
  end
end

#put(url, json) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet/util/network_device/transport/f5.rb', line 41

def put(url, json)
  if valid_json?(json)
    result = connection.put do |req|
      req.url url
      req.headers['Content-Type'] = 'application/json'
      req.body = json
    end
    failure?(result)
    return result
  else
    fail('Invalid JSON detected.')
  end
end

#valid_json?(json) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/puppet/util/network_device/transport/f5.rb', line 75

def valid_json?(json)
  JSON.parse(json)
  return true
rescue
  return false
end