Class: Hpe3parSdk::HTTPJSONRestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/Hpe3parSdk/http.rb

Constant Summary collapse

USER_AGENT =
'ruby-3parclient'.freeze
'X-Hp3Par-Wsapi-Sessionkey'.freeze
CONTENT_TYPE =
'application/json'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url, secure = false, http_log_debug = false, suppress_ssl_warnings = false, timeout = nil) ⇒ HTTPJSONRestClient

Returns a new instance of HTTPJSONRestClient.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/Hpe3parSdk/http.rb', line 28

def initialize(api_url, secure = false, http_log_debug = false,
               suppress_ssl_warnings = false, timeout = nil)
  @api_url = api_url
  @secure = secure
  @http_log_debug = http_log_debug
  @suppress_ssl_warnings = suppress_ssl_warnings
  @timeout = timeout
  @session_key = nil
  HTTParty::Logger.add_formatter('custom', CustomHTTPFormatter)
  @httparty_log_level = :info
  @httparty_log_format = :custom
  set_debug_flag
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def api_url
  @api_url
end

#http_log_debugObject

Returns the value of attribute http_log_debug.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def http_log_debug
  @http_log_debug
end

#log_levelObject

Returns the value of attribute log_level.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def logger
  @logger
end

#secureObject

Returns the value of attribute secure.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def secure
  @secure
end

#session_keyObject

Returns the value of attribute session_key.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def session_key
  @session_key
end

#suppress_ssl_warningsObject

Returns the value of attribute suppress_ssl_warnings.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def suppress_ssl_warnings
  @suppress_ssl_warnings
end

#timeoutObject

Returns the value of attribute timeout.



25
26
27
# File 'lib/Hpe3parSdk/http.rb', line 25

def timeout
  @timeout
end

Instance Method Details

#authenticate(user, password, _optional = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/Hpe3parSdk/http.rb', line 50

def authenticate(user, password, _optional = nil)
  begin
    @session_key = nil

    info = {:user => user, :password => password}

    auth_url = '/credentials'
    headers, body = post(auth_url, body: info)
    @session_key = body['key']
  rescue => ex
    Util.log_exception(ex, caller_locations(1, 1)[0].label)
  end
end

#delete(url, **kwargs) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/Hpe3parSdk/http.rb', line 101

def delete(url, **kwargs)
  headers, payload = get_headers_and_payload(kwargs)
  response = HTTParty.delete(api_url + url,
                             headers: headers,
                             verify: secure, logger: Hpe3parSdk.logger,
                             log_level: @httparty_log_level,
                             log_format: @httparty_log_format)
  process_response(response)
end

#get(url, **kwargs) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/Hpe3parSdk/http.rb', line 69

def get(url, **kwargs)
  headers, payload = get_headers_and_payload(kwargs)
  response = HTTParty.get(api_url + url,
                          headers: headers,
                          verify: secure, logger: Hpe3parSdk.logger,
                          log_level: @httparty_log_level,
                          log_format: @httparty_log_format)
  process_response(response)
end

#get_headers_and_payload(**kwargs) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/Hpe3parSdk/http.rb', line 144

def get_headers_and_payload(**kwargs)
  if session_key
    kwargs['headers'] = kwargs.fetch('headers', {})
    kwargs['headers'][SESSION_COOKIE_NAME] = session_key
  end

  kwargs['headers'] = kwargs.fetch('headers', {})
  kwargs['headers']['User-Agent'] = USER_AGENT
  kwargs['headers']['Accept'] = CONTENT_TYPE
  if kwargs.key?(:body)
    kwargs['headers']['Content-Type'] = CONTENT_TYPE
    kwargs[:body] = kwargs[:body].to_json
    payload = kwargs[:body]
  else
    payload = nil
  end
  [kwargs['headers'], payload]
end

#log_exception(exception, caller_location) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/Hpe3parSdk/http.rb', line 124

def log_exception(exception, caller_location)
  formatted_stack_trace = exception.backtrace
                              .map { |line| "\t\tfrom #{line}" }
                              .join($/)
  err_msg = "(#{caller_location}) #{exception}#{$/}  #{formatted_stack_trace}"
  Hpe3parSdk.logger.error(err_msg)
end

#post(url, **kwargs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/Hpe3parSdk/http.rb', line 79

def post(url, **kwargs)
  headers, payload = get_headers_and_payload(kwargs)
  response = HTTParty.post(api_url + url,
                           headers: headers,
                           body: payload,
                           verify: secure, logger: Hpe3parSdk.logger,
                           log_level: @httparty_log_level,
                           log_format: @httparty_log_format)
  process_response(response)
end

#process_response(response) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/Hpe3parSdk/http.rb', line 111

def process_response(response)
  headers = response.headers
  body = response.parsed_response

  if response.code != 200
    if !body.nil? && body.key?('code') && body.key?('desc')
      exception = Hpe3parSdk.exception_from_response(response, body)
      raise exception
    end
  end
  [headers, body]
end

#put(url, **kwargs) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/Hpe3parSdk/http.rb', line 90

def put(url, **kwargs)
  headers, payload = get_headers_and_payload(kwargs)
  response = HTTParty.put(api_url + url,
                          headers: headers,
                          body: payload,
                          verify: secure, logger: Hpe3parSdk.logger,
                          log_level: @httparty_log_level,
                          log_format: @httparty_log_format)
  process_response(response)
end

#set_debug_flagObject

This turns on/off http request/response debugging output to console



43
44
45
46
47
48
# File 'lib/Hpe3parSdk/http.rb', line 43

def set_debug_flag
  if @http_log_debug
    @httparty_log_level = :debug
    @httparty_log_format = :curl
  end
end

#set_url(api_url) ⇒ Object



64
65
66
67
# File 'lib/Hpe3parSdk/http.rb', line 64

def set_url(api_url)
  # should be http://<Server:Port>/api/v1
  @api_url = api_url.chomp('/')
end

#unauthenticateObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/Hpe3parSdk/http.rb', line 132

def unauthenticate
  # delete the session on the 3Par
  unless @session_key.nil?
    begin
      delete('/credentials/%s' % session_key)
      @session_key = nil
    rescue => ex
      Util.log_exception(ex, caller_locations(1, 1)[0].label)
    end
  end
end