Class: Azure::BaseManagement::ManagementHttpRequest

Inherits:
HttpRequest
  • Object
show all
Defined in:
lib/azure/base_management/management_http_request.rb

Direct Known Subclasses

SqlManagementHttpRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, body = nil) ⇒ ManagementHttpRequest

Public: Creates the ManagementHttpRequest

method - Symbol. The HTTP method to use (:get, :post, :put, :del, etc…) path - URI. The URI of the HTTP endpoint to query body - IO or String. The request body (optional) key - String. The request key cert - String. The request certificate



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/azure/base_management/management_http_request.rb', line 33

def initialize(method, path, body = nil)
  super
  @warn = false
  content_length = body ? body.bytesize.to_s : '0'
  @headers = {
    'x-ms-version' => '2014-04-01',
    'Content-Type' => 'application/xml',
    'Content-Length' => content_length
  }
  @uri = URI.parse(Azure.config.management_endpoint + Azure.config.subscription_id + path)
  @key = Azure.config.http_private_key
  @cert = Azure.config.http_certificate_key
end

Instance Attribute Details

#certObject

Returns the value of attribute cert.



24
25
26
# File 'lib/azure/base_management/management_http_request.rb', line 24

def cert
  @cert
end

#keyObject

Returns the value of attribute key.



24
25
26
# File 'lib/azure/base_management/management_http_request.rb', line 24

def key
  @key
end

#uriObject

Returns the value of attribute uri.



24
25
26
# File 'lib/azure/base_management/management_http_request.rb', line 24

def uri
  @uri
end

#warnObject

Returns the value of attribute warn.



24
25
26
# File 'lib/azure/base_management/management_http_request.rb', line 24

def warn
  @warn
end

Instance Method Details

#call(options = {}) ⇒ Object

Public: Sends a request to HTTP server and returns a HttpResponse

  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • :fire_and_forget - Boolean(optional). Default is false. If true, the client

    does not wait until the request is completed
    
  • :no_exit_on_failure - Boolean(optional). Default is false.

Returns a Nokogiri::XML instance of HttpResponse body



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/azure/base_management/management_http_request.rb', line 60

def call(options={})
  fire_and_forget = options[:fire_and_forget].nil? ? false : options[:fire_and_forget]
  request = http_request_class.new(uri.request_uri, headers)
  request.body = body if body
  http = http_setup
  # http.set_debug_output($stdout)
  if fire_and_forget
    response = validate_response(HttpResponse.new(http.request(request)), options)
    Nokogiri::XML response.body unless response.nil?
  else
    response = wait_for_completion(HttpResponse.new(http.request(request)), options)
    Nokogiri::XML response.body unless response.nil?
  end
end

#check_completion(request_id) ⇒ Object

Public: Gets the status of the specified operation and determines whether the operation has succeeded, failed, or is still in progress.

Attributes

  • request_id - String. x-ms-request-id response header of request

See: msdn.microsoft.com/en-us/library/windowsazure/ee460783.aspx

Print Error or Success of Operation.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/azure/base_management/management_http_request.rb', line 162

def check_completion(request_id)
  request_path = "/#{Azure.config.subscription_id}/operations/#{request_id}"
  http = http_setup
  headers['Content-Length'] = '0'
  @method = :get
  done = false
  while not done
    print '# '
    request = http_request_class.new(request_path, headers)
    response = HttpResponse.new(http.request(request))
    ret_val = Nokogiri::XML response.body
    status = xml_content(ret_val, 'Operation Status')
    status_code = response.status_code.to_i
    if status != 'InProgress'
      done = true
    end
    if redirected? response
      host_uri = URI.parse(response.headers['location'])
      http = http_setup(host_uri)
      done = false
    end
    if done
      if status.downcase != 'succeeded'
        error_code = xml_content(ret_val, 'Operation Error Code')
        error_msg = xml_content(ret_val, 'Operation Error Message')
        Loggerx.exception_message "#{error_code}: #{error_msg}"
      else
        Loggerx.success "#{status.downcase} (#{status_code})"
      end
      return
    else
      sleep(5)
    end
  end
end

#http_setup(host_uri = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/azure/base_management/management_http_request.rb', line 210

def http_setup(host_uri = nil)
  @uri = host_uri if host_uri
  http = nil
  if ENV['HTTP_PROXY'] || ENV['HTTPS_PROXY']
    if ENV['HTTP_PROXY']
      proxy_uri = URI.parse(ENV['HTTP_PROXY'])
    else
      proxy_uri = URI.parse(ENV['HTTPS_PROXY'])
    end
    http = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new(uri.host, uri.port)
  else
    http = Net::HTTP.new(uri.host, uri.port)
  end

  if uri.scheme.downcase == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.cert = cert
    http.key = key
  end
  http
end

#rebuild_request(response) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/azure/base_management/management_http_request.rb', line 198

def rebuild_request(response)
  host_uri = URI.parse(response.headers['location'])
  request = http_request_class.new(host_uri.request_uri, headers)
  request.body = body if body
  http = http_setup(host_uri)
  wait_for_completion(HttpResponse.new(http.request(request)))
end

#redirected?(response) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/azure/base_management/management_http_request.rb', line 206

def redirected?(response)
  (response.status_code.to_i == 307)
end

#validate_response(response, options = {}) ⇒ Object

Public: Validate the Http response

Attributes

  • response - Azure::Core::Http::HttpResponse. HttpResponse Response

  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • :no_exit_on_failure - Boolean(optional). Default is false.

Print Error or Success of HttpRequest



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/azure/base_management/management_http_request.rb', line 130

def validate_response(response, options={})
  no_exit_on_failure = options[:no_exit_on_failure].nil? ? false : options[:no_exit_on_failure]
  ret_val = Nokogiri::XML response.body
  if ret_val.at_css('Error Code') && ret_val.at_css('Error Code').content == 'AuthenticationFailed'
    error_msg = ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content
    return log_warn_or_exit(response, error_msg, no_exit_on_failure)
  end
  status_code = response.status_code.to_i
  if [200, 201, 202].include? status_code
    return response
  elsif response.body
    if ret_val.at_css('Error Code') && ret_val.at_css('Error Message')
      error_msg = ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content
      return log_warn_or_exit(response, error_msg, no_exit_on_failure)
    else
      Loggerx.exception_message "http error: #{response.status_code}"
    end
  else
    Loggerx.exception_message "http error: #{response.status_code}"
  end
end

#wait_for_completion(response, options = {}) ⇒ Object

Public: Wait for HTTP request completion.

Attributes

  • response - Azure::Core::Http::HttpResponse. HttpResponse Response

  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • :no_exit_on_failure - Boolean(optional). Default is false.

Print Error or Success of HttpRequest



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/azure/base_management/management_http_request.rb', line 89

def wait_for_completion(response, options={})
  no_exit_on_failure = options[:no_exit_on_failure].nil? ? false : options[:no_exit_on_failure]
  ret_val = Nokogiri::XML response.body
  if ret_val.at_css('Error Code') && ret_val.at_css('Error Code').content == 'AuthenticationFailed'
    error_msg = ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content
    return log_warn_or_exit(response, error_msg, no_exit_on_failure)
  end
  if response.status_code.to_i == 200 || response.status_code.to_i == 201
    return response
  elsif redirected? response
    rebuild_request response
  elsif response.status_code.to_i > 201 && response.status_code.to_i <= 299
    check_completion(response.headers['x-ms-request-id'])
  elsif warn && !response.success?
    # Loggerx.warn ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content
  elsif response.body
    if ret_val.at_css('Error Code') && ret_val.at_css('Error Message')
      error_msg = ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content
      return log_warn_or_exit(response, error_msg, no_exit_on_failure)
    else
      Loggerx.exception_message "http error: #{response.status_code}"
    end
  else
    Loggerx.exception_message "http error: #{response.status_code}"
  end
end