class Pxgrid::Client

Attributes

nodename[RW]
username[RW]

Public Class Methods

new(pxgrid_ip, nodename, credentials = {}) click to toggle source
# File lib/pxgrid.rb, line 12
def initialize(pxgrid_ip, nodename, credentials = {})
  @nodename = nodename
  @client = Faraday.new("https://#{pxgrid_ip}:8910/pxgrid/") do |conn|
    conn.adapter Faraday.default_adapter
    conn.ssl[:verify] = false
    conn.headers["Accept"] = "application/json"
    conn.headers["Content-Type"] = "application/json"
  end
  if credentials && credentials[:username] && credentials[:password]
    # Looks like we have an account. Validate the account.
    @username = credentials[:username]
    @password = credentials[:password]
  else
    # Don't have credentials. Create the account in pxGrid.
    # Create an account and get the username and password
    params = {"nodeName": nodename}
    response = @client.post("control/AccountCreate", params.to_json)
    if response.success?
      response = JSON.parse(response.body)
      @username = response["userName"]
      @password = response["password"]
    else
      raise "AccountCreationError"
    end
  end

  # Save the credentials as part of the connection
  @client.basic_auth(@username, @password)
  return @client
end

Public Instance Methods

accessSecret(peerNodeName) click to toggle source
# File lib/pxgrid.rb, line 63
def accessSecret(peerNodeName)
    params = {"peerNodeName": peerNodeName}
    return JSON.parse(@client.post("control/AccessSecret", params.to_json).body)["secret"]
end
activate() click to toggle source
# File lib/pxgrid.rb, line 43
def activate
  params = {"nodeName": @nodename}
  # Validate the credentials and activate it.
  response = JSON.parse(@client.post("control/AccountActivate", params.to_json).body)
  if response["accountState"] == "PENDING"
    # Approve the account in pxGrid
    return {"status": "PENDING", "description": "Account is pending approval. Approve it within pxGrid"}
  elsif response["accountState"] == "ENABLED"
    # Account is approved.
    return {"status": "ENABLED", "description": "Account is ready to be used"}
  else
    return {"status": "DISABLED", "description": "Account is disabled in pxGrid. Please enable account and try again."}
  end
end
serviceLookup(service) click to toggle source
# File lib/pxgrid.rb, line 58
def serviceLookup(service)
  params = {"name": service}
  services = JSON.parse(@client.post("control/ServiceLookup", params.to_json).body)
end