Class: Azure::BaseManagement::ManagementHttpRequest

Inherits:
Core::Http::HttpRequest
  • Object
show all
Includes:
Core::Utility
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, options_or_body = {}) ⇒ ManagementHttpRequest

Creates the ManagementHttpRequest

Parameters:

  • method (Symbol)

    The HTTP method to use (:get, :post, :put, :del, etc…)

  • path (URI)

    The URI of the HTTP endpoint to query

  • options_or_body (Hash|IO|String) (defaults to: {})

    The request options including :body or raw body only



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

def initialize(method, path, options_or_body = {})
  options ||= unless options_or_body.is_a?(Hash)
              {body: options_or_body}
            end || options_or_body || {}
  options[:client] ||= Azure
  super(method, self.class.request_uri(path, options[:client]), options)
  @warn = options.fetch(:warn, false)
  content_length = body ? body.bytesize.to_s : '0'
  headers.update({
                      'x-ms-version' => '2015-04-01',
                      'Content-Type' => 'application/xml',
                      'Content-Length' => content_length
                  })
end

Instance Attribute Details

#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

#apply_body_headersObject



56
57
58
# File 'lib/azure/base_management/management_http_request.rb', line 56

def apply_body_headers
  super
end

#callObject

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

Returns a Nokogiri::XML instance of HttpResponse body



49
50
51
52
53
54
# File 'lib/azure/base_management/management_http_request.rb', line 49

def call
  conn = http_setup
  res = set_up_response(method.to_sym, uri, conn, headers ,body)
  response = wait_for_completion(Azure::Core::Http::HttpResponse.new(res))
  Nokogiri::XML response.body unless response.nil?
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/azure/ee460783.aspx

Print Error or Success of Operation.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/azure/base_management/management_http_request.rb', line 108

def check_completion(request_id)
  request_path = "/#{client.subscription_id}/operations/#{request_id}"
  conn = http_setup
  headers['Content-Length'] = '0'
  @method = :get
  done = false
  until done
    Azure::Loggerx.info('# ')
    res = set_up_response(method.to_sym, URI(request_path), conn, headers ,body)
    response = Azure::Core::Http::HttpResponse.new(res)
    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
      @uri = self.class.request_uri(response.headers['location'], client)
      conn = http_setup
      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_setupObject



91
92
93
94
95
96
# File 'lib/azure/base_management/management_http_request.rb', line 91

def http_setup
  http = super
  http.ssl[:client_cert] = @client.http_certificate_key if @client.http_certificate_key
  http.ssl[:client_key] = @client.http_private_key if @client.http_private_key
  http
end

#rebuild_request(response) ⇒ Object



144
145
146
147
148
149
# File 'lib/azure/base_management/management_http_request.rb', line 144

def rebuild_request(response)
  host_uri = URI.parse(response.headers['location'])
  conn = http_setup
  res = set_up_response(method.to_sym, host_uri, conn, headers ,body)
  wait_for_completion(HttpResponse.new(res))
end

#redirected?(response) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/azure/base_management/management_http_request.rb', line 151

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

#wait_for_completion(response) ⇒ Object

Public: Wait for HTTP request completion.

Attributes

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

Print Error or Success of HttpRequest



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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
    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?
    response
  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