class OpsmanagerClient::HTTPClient

Public Class Methods

new(url, username, password) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 19
def initialize(url, username, password)
  HTTPClient.base_uri(url)
  HTTPClient.basic_auth(username, password)
end

Public Instance Methods

add_product(product) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 50
def add_product(product)
  query_params = {
    'name' => product.name,
    'product_version' => product.version
  }
  ensure_successful(
    HTTPClient.post("/api/installation_settings/products", query(query_params))
  )
end
apply_changes() click to toggle source
# File lib/opsmanager_client/http_client.rb, line 81
def apply_changes
  response = ensure_successful(HTTPClient.post("/api/installation", query))
  task_id = response.fetch("install").fetch("id")
  task_id
end
available_products() click to toggle source
# File lib/opsmanager_client/http_client.rb, line 36
def available_products
  ensure_successful(
    HTTPClient.get("/api/products", query)
  )
end
delete_unused_products() click to toggle source
# File lib/opsmanager_client/http_client.rb, line 30
def delete_unused_products
  ensure_successful(
    HTTPClient.delete("/api/products", query)
  )
end
installation() click to toggle source
# File lib/opsmanager_client/http_client.rb, line 24
def installation
  ensure_successful(
    HTTPClient.get("/api/installation_settings", query)
  )
end
remove_product_with_guid(guid) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 60
def remove_product_with_guid(guid)
  ensure_successful(
    HTTPClient.delete("/api/installation_settings/products/#{guid}", query)
  )
end
uninstall_product_with_guid(guid) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 75
def uninstall_product_with_guid(guid)
  ensure_successful(
    HTTPClient.delete("/api/installation_settings/products/#{guid}", query)
  )
end
upgrade_product(product, guid) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 66
def upgrade_product(product, guid)
  query_params = {
    'to_version' => product.version
  }
  ensure_successful(
    HTTPClient.put("/api/installation_settings/products/#{guid}", query(query_params))
  )
end
upload_product_from_file(product_file) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 42
def upload_product_from_file(product_file)
  retry_on_fail do
    ensure_successful(
      HTTPClient.post("/api/products", query(:product => {:file => product_file}))
    )
  end
end

Private Instance Methods

ensure_successful(http_response, permitted_status_codes=[200]) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 111
def ensure_successful(http_response, permitted_status_codes=[200])
  if permitted_status_codes.include?(http_response.code)
    http_response
  else
    error_messages = http_response.fetch('errors'){ http_response.inspect }
    error_message  = Array(error_messages).join("\n")
    fail(error_message)
  end
end
query(params = {}) click to toggle source
# File lib/opsmanager_client/http_client.rb, line 104
def query(params = {})
  {
    :verify => false, # disable SSL verify
    :query => params
  }
end
retry_on_fail(max_attempts = 6, retryable_exceptions = [Net::ReadTimeout]) { || ... } click to toggle source
# File lib/opsmanager_client/http_client.rb, line 89
def retry_on_fail(max_attempts = 6, retryable_exceptions = [Net::ReadTimeout])
  attempts = 0
  begin
    attempts += 1
    yield
  rescue *retryable_exceptions
    if attempts < max_attempts
      puts "Attempt #{attempts}/#{max_attempts} failed. Retrying"
      retry
    end
    puts "All retries failed."
    raise
  end
end