icontrol_rest 2.3.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fad69ef03119e3b1e0d4b14d2d52bbe2068ac82
4
- data.tar.gz: 790fa57a3e23e0681741d22abadb895b41cdb739
3
+ metadata.gz: 99b9915fe47d60aefe4fe54e3a141b7286d01d59
4
+ data.tar.gz: 67dbe13f3596cd49ab55e38419e483c230260183
5
5
  SHA512:
6
- metadata.gz: 498c382ef83d8644cb071dc9bc954f2ef5606ee39b5902ffd8f1ae4dfacff9b1395019b85c4b93836dfafa414dcfea70034f6ba41fdbd815c5be92d7ca4e671c
7
- data.tar.gz: b9b73ddf7e16e5598eb773ef4e4a148605df073f192cfadebabf3f31dbdfb08341c9ea6e4714d276d1e5d1857890c9d4ed588a8ba6843155cf64777595f047ed
6
+ metadata.gz: 750301793adb19dbb71af14bce9129b959ad629338d5b8feb66d586bec70ae6f0dc027804863c850bc6aa9ae05faa07d70ce6242c4857b2232f623a7a0bc2c7f
7
+ data.tar.gz: 89c2e83e29f0392e8ae1d16ee4e409d62a09d4c499192daed864bbc8a834f440259b00bd807cc7bdf49306e446ae48993d16800304f21cc0c7753e9823031cde
data/README.md CHANGED
@@ -47,6 +47,30 @@ api.get('/mgmt/tm/sys/dns')
47
47
  "description"=>"configured-by-dhcp",
48
48
  "nameServers"=>["1.2.3.72", "1.2.3.73"],
49
49
  "search"=>["domain.com"]}
50
+
51
+ # post/put/patch will allow you to specify a body and header with your request
52
+ api.post_transaction(body: {})
53
+ # => {"transId"=>1526674837169915,
54
+ "state"=>"STARTED",
55
+ "timeoutSeconds"=>120,
56
+ "asyncExecution"=>false,
57
+ "validateOnly"=>false,
58
+ "executionTimeout"=>300,
59
+ "executionTime"=>0,
60
+ "failureReason"=>"",
61
+ "kind"=>"tm:transactionstate",
62
+ "selfLink"=>"https://localhost/mgmt/tm/transaction/1526674837169915?ver=12.1.2"}
63
+
64
+ api.put_sys_dns(body: { nameServers: ['1.2.3.69'] }, headers: { 'X-F5-REST-Coordination-Id' => '1526674837169915' })
65
+ # => {"method": "PUT",
66
+ "uri": "https://localhost/mgmt/tm/sys/dns",
67
+ "body": {
68
+ "nameServers": ["1.2.3.69"]
69
+ },
70
+ "evalOrder": 1,
71
+ "commandId": 1,
72
+ "kind": "tm:transaction:commandsstate",
73
+ "selfLink": "https://localhost/mgmt/tm/transaction/1526674837169915/commands/1?ver=12.1.2"
50
74
  ```
51
75
 
52
76
 
@@ -5,7 +5,7 @@ require 'logger'
5
5
  require 'retriable'
6
6
  require 'uri'
7
7
 
8
- require 'icontrol_rest/logger'
8
+ require 'icontrol_rest/utils/logging'
9
9
  require 'icontrol_rest/client'
10
10
  require 'icontrol_rest/version'
11
11
 
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
4
+
3
5
  module IcontrolRest
4
6
  # A iControl REST Client for retrieving data from F5s.
5
7
  class Client
8
+ include Utils::Logging
6
9
  include HTTParty
7
10
  format :json
8
11
 
@@ -44,16 +47,12 @@ module IcontrolRest
44
47
  end
45
48
  # rubocop:enable Metrics/ParameterLists
46
49
 
47
- # Public: Returns a set logger. If none exists, a NullLogger is returned for stability.
48
- def logger
49
- @logger ||= IcontrolRest::Logging::NullLogger.new
50
- end
51
-
52
- # Public - Delete/Get request method.
50
+ # Public - Delete/Get request method.
53
51
  #
54
- # route - URI to send to server as a string.
52
+ # route - URI to send to server as a string.
53
+ # headers - Hash of headers to pass with request.
55
54
  #
56
- # Returns - response from the server as a hash.
55
+ # Returns - response from the server as a hash.
57
56
  #
58
57
  # Examples
59
58
  #
@@ -65,22 +64,21 @@ module IcontrolRest
65
64
  # "search"=>["domain.com"]}
66
65
  #
67
66
  %w[delete get].each do |action|
68
- define_method(action.to_s) do |route, _arg = nil|
69
- send_request { self.class.send(action, route, @options) }
67
+ define_method(action.to_s) do |route, headers: {}|
68
+ send_request(action: action, route: route, headers: headers)
70
69
  end
71
70
  end
72
71
 
73
- # Public - Post/Put request methods.
72
+ # Public - Post/Put/Patch request methods.
74
73
  #
75
- # route - URI to send to server as a string.
76
- # data - Hash of parameters to pass with request.
74
+ # route - URI to send to server as a string.
75
+ # body - Hash representing the request body.
76
+ # headers - Hash of headers to pass with request.
77
77
  #
78
- # Returns - response from the server as a hash.
78
+ # Returns - response from the server as a hash.
79
79
  %w[post put patch].each do |action|
80
- define_method(action.to_s) do |route, data|
81
- body = {}
82
- body[:body] = data.to_json
83
- send_request { self.class.send(action, route, body.merge(@options)) }
80
+ define_method(action.to_s) do |route, body: {}, headers: {}|
81
+ send_request(action: action, route: route, body: body, headers: headers)
84
82
  end
85
83
  end
86
84
 
@@ -88,14 +86,18 @@ module IcontrolRest
88
86
 
89
87
  # Private - Handles response and raises error if the response isn't a 200.
90
88
  #
91
- # block - Takes block to yield.
89
+ # action - The HTTP action being executed.
90
+ # route - The route to send the request to.
91
+ # headers - A Hash representing any headers to be passed with the request.
92
+ # body - A Hash representing the request body to be passed with the request.
92
93
  #
93
94
  # Returns - response from the server as a hash.
94
95
  #
95
96
  # Raises - RuntimeError when the server returns a response that isn't a 200
96
- def send_request
97
+ def send_request(action:, route:, headers: {}, body: nil)
97
98
  Retriable.retriable on_retry: @retry_handler, tries: @tries do
98
- response = yield
99
+ body = body.to_json unless body.nil?
100
+ response = self.class.send(action, route, @options.deep_merge(body: body, headers: headers))
99
101
  sleep(@sleep) if @sleep.positive?
100
102
  return response if response.code == 200
101
103
  raise "#{response['code']}: #{response['message']}"
@@ -108,10 +110,10 @@ module IcontrolRest
108
110
  # *args - An array of arguments passed with the method call.
109
111
  #
110
112
  # Returns - Response from the method called.
111
- def method_missing(method_name, *args)
113
+ def method_missing(method_name, **args)
112
114
  super unless respond_to_missing?(method_name)
113
115
  route_chain = method_name.to_s.split('_')
114
- send(route_chain[0].downcase, route(route_chain), args.first)
116
+ send(route_chain[0].downcase, route(route_chain), args)
115
117
  end
116
118
 
117
119
  # Private - Adds methods prefixed with delete/get/post/put/patch to object.
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module IcontrolRest
6
+ module Utils
7
+ # Module to handle all logging across this gem
8
+ module Logging
9
+ def self.included(klass)
10
+ klass.extend ClassMethods
11
+ klass.include InstanceMethods
12
+ end
13
+
14
+ def self.logger
15
+ @logger ||= NullLogger.new
16
+ @logger.progname = 'IControlRest'
17
+ @logger
18
+ end
19
+
20
+ def self.use_logger(logger)
21
+ @logger = logger
22
+ end
23
+
24
+ # Module for instance methods to be included when IcontrolRest::Utils::Logging is included by a class.
25
+ module InstanceMethods
26
+ def logger
27
+ IcontrolRest::Utils::Logging.logger
28
+ end
29
+ end
30
+
31
+ # Module for class methods to be extended when IcontrolRest::Utils::Logging is included by a class.
32
+ module ClassMethods
33
+ def logger
34
+ IcontrolRest::Utils::Logging.logger
35
+ end
36
+ end
37
+
38
+ # A stand-in Logger that does nothing when it is written to.
39
+ #
40
+ # Examples
41
+ #
42
+ # null_logger = NullLogger.new(STDOUT)
43
+ # null_logger.info { 'This message will never get writen' }
44
+ class NullLogger < Logger
45
+ # Intentionally do nothing
46
+ def initialize(*_args); end
47
+
48
+ # Intentionally do nothing
49
+ def add(*_args, &_block); end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IcontrolRest
4
- VERSION = '2.3.0'
4
+ VERSION = '3.0.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: icontrol_rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jerrod Carpenter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-28 00:00:00.000000000 Z
11
+ date: 2018-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -54,7 +54,7 @@ files:
54
54
  - bin/setup
55
55
  - lib/icontrol_rest.rb
56
56
  - lib/icontrol_rest/client.rb
57
- - lib/icontrol_rest/logger.rb
57
+ - lib/icontrol_rest/utils/logging.rb
58
58
  - lib/icontrol_rest/version.rb
59
59
  homepage: https://github.com/cerner/icontrol_rest.git
60
60
  licenses: []
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module IcontrolRest
4
- # Module to handle all logging across our gem
5
- module Logging
6
- class << self
7
- attr_writer :logger
8
- end
9
-
10
- # Default logger to be used if logging configuration is not supplied by the user.
11
- class NullLogger < Logger
12
- def initialize(*)
13
- # Intentionally do nothing
14
- end
15
-
16
- def add(*)
17
- # Intentionally do nothing
18
- end
19
- end
20
-
21
- def self.logger
22
- @logger ||= NullLogger.new
23
- end
24
-
25
- def self.use_logger(logger)
26
- @logger = logger
27
- end
28
-
29
- def logger
30
- Logging.logger
31
- end
32
- end
33
- end