Class: Puppet::Util::NetworkDevice::Transport::Cudawaf

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize the transport layer object using the SwaggerClient SDK for WAF.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 36

def initialize(url, _options = {})
  @device_url = URI.parse(url)
  Puppet.debug(self.class.to_s.split("::").last + ": Connecting to WAF device - " + url)

  #raise ArgumentError, "Invalid scheme #{url.scheme}. Must be https" unless url.scheme == 'https'
  raise ArgumentError, "no user specified" unless @device_url.user
  raise ArgumentError, "no password specified" unless @device_url.password

  @transport = SwaggerClient::Configuration.new(url)
end

Instance Attribute Details

#transportObject (readonly)

Returns the value of attribute transport.



31
32
33
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 31

def transport
  @transport
end

#urlObject (readonly)

Returns the value of attribute url.



31
32
33
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 31

def url
  @url
end

Instance Method Details

#client_delete(device, api_url, *args) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 290

def client_delete(device, api_url, *args)
  @config = SwaggerClient::Configuration.new(device)
  rest_url = @config.base_url + api_url
  auth_header = get_auth_header(device)

  response = RestClient.delete rest_url, { :Authorization => "#{auth_header}", accept: :json }
  parsed_response = JSON.parse(response)

  if response.code == 200 or response.code == 202
    return parsed_response
  else
    Puppet.debug(self.class.to_s.split("::").last + ": Error in DELETE operation for " + rest_url)
  end
end

#client_get(device, api_url, *args) ⇒ Object

Support the major API methods via the Transport layer using the RestClient library here.

Supported methods - GET, POST, PUT and DELETE.


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 242

def client_get(device, api_url, *args)
  @config = SwaggerClient::Configuration.new(device)
  rest_url = @config.base_url + api_url
  auth_header = get_auth_header(device)

  response = RestClient.get rest_url, { :Authorization => "#{auth_header}", accept: :json }
  parsed_response = JSON.parse(response)

  if response.code == 200
    return parsed_response
  else
    Puppet.debug(self.class.to_s.split("::").last + ": Error in GET operation for " + rest_url) 
  end

  return parsed_response
end

#client_post(device, api_url, postdata) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 275

def client_post(device, api_url, postdata)
  @config = SwaggerClient::Configuration.new(device)
  rest_url = @config.base_url + api_url
  auth_header = get_auth_header(device)

  response = RestClient.post rest_url, postdata, { :Authorization => "#{auth_header}", accept: :json, content_type: :json }
  parsed_response = JSON.parse(response)

  if response.code == 200 or response.code == 201 or response.code == 202
    return parsed_response
  else
    Puppet.debug(self.class.to_s.split("::").last + ": Error in POST operation for " + rest_url)
  end
end

#client_put(device, api_url, postdata) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 259

def client_put(device, api_url, postdata)
  @config = SwaggerClient::Configuration.new(device)
  rest_url = @config.base_url + api_url
  auth_header = get_auth_header(device)

  response = RestClient.put rest_url, postdata, { :Authorization => "#{auth_header}", accept: :json, content_type: :json }
  Puppet.debug(self.class.to_s.split("::").last + "Response - #{response}")
  parsed_response = JSON.parse(response)

  if response.code == 200 or response.code == 202
    return parsed_response
  else
    Puppet.debug(self.class.to_s.split("::").last + ": Error in PUT operation for " + rest_url)
  end
end

#convert_plural(object_name) ⇒ Object

Given an object name, convert it to the plural version since the SDK is written that way.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 213

def convert_plural(object_name)
  object_name = object_name.gsub(/([a-z])([A-Z])/, '\1_\2')
  pluralized_string = object_name

  if object_name =~ /^System$/i
    #  do nothing
  elsif object_name =~ /y$/
    pluralized_string = object_name.sub(/y$/, "ies")
  elsif object_name =~ /s$/
    pluralized_string = object_name + "es"
  else
    pluralized_string = object_name + "s"
  end

  return pluralized_string.downcase
end

#delete(device, instance, *args) ⇒ Object

DELETE method - delete an existing instance of a Puppet object type.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 148

def delete(device, instance, *args)
  instance_plural = convert_plural(instance)
  instance_var_name = get_mapped_object_name(instance)
  delete_method = instance_plural + "_" + instance_var_name + "_delete_with_http_info"

  auth_header = get_auth_header(device)

  #
  #  Form the instance based on the URL passed and invoke the appropriate SwaggerClient SDK.
  #  Each provider will pass the object type as the instance and we would instantiate the appropriate SwaggerClient object.
  #
  object_instance = "SwaggerClient::#{instance}Api"

  instance_method = Object.const_get(object_instance).new(device).method(delete_method)
  response,status_code,headers = instance_method.call(auth_header, *args)
  parsed_response = JSON.parse(response)

  Puppet.debug(self.class.to_s.split("::").last + ": Response received from WAF for DELETE operation:  #{parsed_response}")

  failure?(parsed_response, status_code, "DELETE")
  return parsed_response, status_code, headers
end

#failure?(result, status_code, method) ⇒ Boolean

Util function to check if API method resulted in success or failure.

WAF returns the following status codes -
2xx - success codes (200, 201, 202)
3xx, 4xx, 5xx - error codes (authentication errors, malformed JSON errors, resource not found, server errors etc)

Failure stops further processing and returns the error message to the user.

Returns:

  • (Boolean)


191
192
193
194
195
196
197
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 191

def failure?(result, status_code, method)
  if status_code == 200 or status_code == 201 or status_code == 202 then
    Puppet.debug(self.class.to_s.split("::").last + ": API method " + method + " successfully executed with status code #{status_code}")
  else
    fail(self.class.to_s.split("::").last + ": REST failure for " + method + " method: HTTP status code #{status_code} detected.  Error is: #{result}")
  end
end

#get(device, instance, *args) ⇒ Object

GET method - list objects. Also used for retrieving the device facts.

Example usage:
  transport.get(url, "Service", "service_web_application_name_get", "svcName")


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
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 61

def get(device, instance, *args)
  *get_args, last = *args
  instance_plural = convert_plural(instance)

  if get_args.empty? or last.empty?
      get_method = instance_plural.downcase + "_get_with_http_info"
  else
      instance_var_name = get_mapped_object_name(instance)
      get_method = instance_plural.downcase + "_" + instance_var_name + "_get_with_http_info"
  end

  auth_header = get_auth_header(device)

  #
  #  Form the instance based on the URL passed and invoke the appropriate SwaggerClient SDK.
  #  Each provider will pass the object type as the instance and we would instantiate the appropriate SwaggerClient object.
  #
  object_instance = "SwaggerClient::#{instance}Api"

  instance_method = Object.const_get(object_instance).new(device).method(get_method)
  response,status_code,headers = instance_method.call(auth_header, *args)
  parsed_response = JSON.parse(response)

  Puppet.debug(self.class.to_s.split("::").last + ": Response received from WAF for GET operation:  #{parsed_response}")

  if response.to_s.empty?
    fail(self.class.to_s.split("::").last + ": Not able to process the request. Please check the request parameters.")
  end

  failure?(parsed_response, status_code, "GET")
  return parsed_response, status_code, headers
end

#get_auth_header(device) ⇒ Object

Get the login token for subsequent API calls.

Accepts device url as input - to figure out which WAF to get the token for.


175
176
177
178
179
180
181
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 175

def get_auth_header(device)
   = Login.new
  auth_header = .get_auth_header(device)

  Puppet.debug(self.class.to_s.split("::").last + ": WAF authorization token:  #{auth_header}")
  return auth_header
end

#get_mapped_object_name(object) ⇒ Object

Get the internally mapped object name for an external name.

Example: "Service" ==> "web_application_name"


234
235
236
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 234

def get_mapped_object_name(object)
  return $mapped_object_types[object]
end

#post(device, instance, *postdata) ⇒ Object

POST method - create a new instance of a Puppet object type.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 97

def post(device, instance, *postdata)
  instance_plural = convert_plural(instance)
  post_method = instance_plural + "_post_with_http_info"

  auth_header = get_auth_header(device)

  #
  #  Form the instance based on the URL passed and invoke the appropriate SwaggerClient SDK.
   #  Each provider will pass the object type as the instance and we would instantiate the appropriate SwaggerClient object.
   #
   object_instance = "SwaggerClient::#{instance}Api"

   instance_method = Object.const_get(object_instance).new(device).method(post_method)
   response,status_code,headers = instance_method.call(auth_header, *postdata)
   parsed_response = JSON.parse(response)

   Puppet.debug(self.class.to_s.split("::").last + ": Response received from WAF for POST operation:  #{parsed_response}")

   failure?(parsed_response, status_code, "POST")
   return parsed_response, status_code, headers
end

#put(device, instance, *postdata) ⇒ Object

PUT method - edit an existing instance of a Puppet object type.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 122

def put(device, instance, *postdata)
  instance_plural = convert_plural(instance)
  instance_var_name = get_mapped_object_name(instance)
  put_method = instance_plural + "_" + instance_var_name + "_put_with_http_info"

  auth_header = get_auth_header(device)

   #
   #  Form the instance based on the URL passed and invoke the appropriate SwaggerClient SDK.
   #  Each provider will pass the object type as the instance and we would instantiate the appropriate SwaggerClient object.
   #
   object_instance = "SwaggerClient::#{instance}Api"

   instance_method = Object.const_get(object_instance).new(device).method(put_method)
   response,status_code,headers = instance_method.call(auth_header, *postdata)
   parsed_response = JSON.parse(response)

   Puppet.debug(self.class.to_s.split("::").last + ": Response received from WAF for PUT operation:  #{parsed_response}")

   failure?(parsed_response, status_code, "PUT")
   return parsed_response, status_code, headers
end

#valid_json?(json) ⇒ Boolean

Util function to check if the body of the request is valid well-formed JSON.

Returns a boolean value.

Returns:

  • (Boolean)


203
204
205
206
207
208
# File 'lib/puppet/util/network_device/transport/cudawaf.rb', line 203

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