class Azure::BaseManagement::ManagementHttpRequest

Attributes

cert[RW]
key[RW]
uri[RW]
warn[RW]

Public Class Methods

new(method, path, body = nil) click to toggle source

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

Calls superclass method
# File lib/azure/base_management/management_http_request.rb, line 34
def initialize(method, path, body = nil)
  super
  @warn = false
  content_length = body ? body.bytesize.to_s : '0'
  @headers.update({
                      'x-ms-version' => '2014-06-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

Public Instance Methods

call() click to toggle source

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

Returns a Nokogiri::XML instance of HttpResponse body

# File lib/azure/base_management/management_http_request.rb, line 51
def call
  request = http_request_class.new(uri.request_uri, headers)
  request.body = body if body
  http = http_setup
  # http.set_debug_output($stdout)
  response = wait_for_completion(HttpResponse.new(http.request(request)))
  Nokogiri::XML response.body unless response.nil?
end
check_completion(request_id) click to toggle source

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/azure/ee460783.aspx

Print Error or Success of Operation.

# File lib/azure/base_management/management_http_request.rb, line 103
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')
        Azure::Loggerx.exception_message "#{error_code}: #{error_msg}"
      else
        Azure::Loggerx.success "#{status.downcase} (#{status_code})"
      end
      return
    else
      sleep(5)
    end
  end
end
http_setup(host_uri = nil) click to toggle source
# File lib/azure/base_management/management_http_request.rb, line 151
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) click to toggle source
# File lib/azure/base_management/management_http_request.rb, line 139
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) click to toggle source
# File lib/azure/base_management/management_http_request.rb, line 147
def redirected?(response)
  (response.status_code.to_i == 307)
end
wait_for_completion(response) click to toggle source

Public: Wait for HTTP request completion.

Attributes

Print Error or Success of HttpRequest

# File lib/azure/base_management/management_http_request.rb, line 67
def wait_for_completion(response)
  ret_val = Nokogiri::XML response.body
  if ret_val.at_css('Error Code') && ret_val.at_css('Error Code').content == 'AuthenticationFailed'
    Azure::Loggerx.error_with_exit ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content
  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 response.status_code.to_i == 307
    @uri = URI::parse (response.headers['location'])
    call
  elsif warn && !response.success?
  elsif response.body
    if ret_val.at_css('Error Code') && ret_val.at_css('Error Message')
      Azure::Loggerx.error_with_exit ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content
    else
      Azure::Loggerx.exception_message "http error: #{response.status_code}"
    end
  else
    Azure::Loggerx.exception_message "http error: #{response.status_code}"
  end
end