Class: InfobloxClass

Inherits:
Object
  • Object
show all
Defined in:
lib/miq_utilities/infoblox.rb

Overview

Wrapper class to interact with Infoblox

Instance Method Summary collapse

Instance Method Details

#call_infoblox(action:, resource:, ibobj:, body_type: :json, body: nil, servername: nil, username: nil, password: nil, baseurl: 'https://[SERVERNAME]/wapi/v2.3.1/') ⇒ Object



54
55
56
57
58
59
60
61
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/miq_utilities/infoblox.rb', line 54

def call_infoblox(action:, resource:, ibobj:, body_type: :json, body: nil, servername: nil, username: nil, password: nil, baseurl: 'https://[SERVERNAME]/wapi/v2.3.1/')
  @logger = LoggingClass.new("#{self.class}:(#{__method__})")

  servername ||= $evm.object['infobloxservername']
  username   ||= $evm.object['infobloxusername']
  password   ||= $evm.object.decrypt('infobloxpassword')
  baseurl    ||= $evm.object['baseURL'].to_s
  url        = baseurl.sub('[SERVERNAME]', servername.to_s) + resource.to_s
  refend     = '&_return_as_object=1'
  argsfound  = false

  unless ibobj['args'].nil?
    args = ibobj['args']
    argsfound = true
  end

  argextattrs = ibobj['argextattrs'] unless ibobj['argextattrs'].nil?
  returnfields = ibobj['returnfields'] unless ibobj['returnfields'].nil?

  url = process_args(args, url) if args
  url = process_returnfields(url, returnfields, argsfound) if returnfields
  url = process_extattrs(argextattrs, url, argsfound) if argextattrs

  url = if argextattrs || returnfields
          "#{url}#{refend}"
        else
          url
        end

  @logger.log(level: 'info', message: "URL: \"#{url}\"")

  params = {
    method: action,
    url: url,
    user: username,
    password: password,
    verify_ssl: false,
    headers: { content_type: body_type, accept: :json }
  }

  if body_type == :json
    params[:payload] = JSON.generate(body) if body
    @logger.log(level: 'info', message: "Calling -> Infoblox:<#{url}> action:<#{action}> payload:<#{params[:payload]}>") if body
  elsif body
    params[:payload] = body
  end

  response = RestClient::Request.new(params).execute
  if response.code == 200 || response.code == 201
    @logger.log(level: 'info', message: "Success <- Infoblox Response:<#{response.code}>")
  else
    err_msg = "Error calling infoblox. <#{response.inspect}>"
    @logger.log(level: 'error', message: err_msg, notify: true)
  end
  response
end