class ChefWebui::Session

Attributes

account_url[R]
manage_url[R]

Public Class Methods

new(manage_url, account_url) click to toggle source
# File lib/chef_webui/session.rb, line 9
def initialize(manage_url, account_url)
  @manage_url = manage_url
  @account_url = account_url
  @agent = Mechanize.new
  @agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  get('login')
end

Public Instance Methods

create_org(orgname) click to toggle source
# File lib/chef_webui/session.rb, line 34
def create_org(orgname)
  post('organizations',
    {
      'id' => orgname,
      'full_name' => orgname
    })
end
create_user(username, password, first_name, last_name, email) click to toggle source
# File lib/chef_webui/session.rb, line 46
def create_user(username, password, first_name, last_name, email)
  post('users',
    {
      'authenticity_token' => @authenticity_token,
      'user[username]' => username,
      'user[first_name]' => first_name,
      'user[last_name]' => last_name,
      'user[email]' => email,
      'user[password]' => password,
      'user[password_confirmation]' => password,
      'commit' => 'Submit'
    })
  if !@agent.page.content =~ /Your Private Chef user account has been created./
    raise_webui_error
  end
end
current_environment() click to toggle source
# File lib/chef_webui/session.rb, line 96
def current_environment
  @agent.page.at('#Environment option[selected="selected"]').text.gsub(/\s+/, ' ')
end
current_org() click to toggle source
# File lib/chef_webui/session.rb, line 92
def current_org
  @agent.page.at('#header h1 a[href="/nodes"]').text.gsub(/\s+/, ' ')
end
current_user() click to toggle source
# File lib/chef_webui/session.rb, line 88
def current_user
  @agent.page.at('#user-navigation a').text.gsub(/\s+/, ' ')
end
logged_in() click to toggle source
# File lib/chef_webui/session.rb, line 30
def logged_in
  return @agent.page.content =~ /Logged in as/
end
login(username, password) click to toggle source
# File lib/chef_webui/session.rb, line 17
def login(username, password)
  results = post('login_exec',
    {
      'name' => username,
      'password' => password,
      'commit' => 'Login'
    })

  if !logged_in
    raise_webui_error
  end
end
organizations() click to toggle source
# File lib/chef_webui/session.rb, line 79
def organizations
  get('organizations')
  result = []
  @agent.page.search('#all_associated_organizations td.name_column').each do |org|
    result << org.text.gsub(/\s+/, ' ')
  end
  result
end
regenerate_user_key() click to toggle source
# File lib/chef_webui/session.rb, line 71
def regenerate_user_key
  @agent.post("#{account_url}/account/regen_key",
    {
      'commit' => 'Get a new key',
      'authenticity_token' => @authenticity_token
    }).content
end
regenerate_validator_key(orgname) click to toggle source
# File lib/chef_webui/session.rb, line 63
def regenerate_validator_key(orgname)
  @agent.post("#{manage_url}/organizations/#{orgname}/_regenerate_key",
    {
      '_method' => 'put',
      'authenticity_token' => @authenticity_token
    }).content
end
switch_to_org(orgname) click to toggle source
# File lib/chef_webui/session.rb, line 42
def switch_to_org(orgname)
  post("organizations/#{orgname}/select", {})
end

Private Instance Methods

get(relative_url) click to toggle source
# File lib/chef_webui/session.rb, line 109
def get(relative_url)
  result = @agent.get("#{manage_url}/#{relative_url}")
  retrieve_authenticity_token
  result
end
post(relative_url, parameters) click to toggle source
# File lib/chef_webui/session.rb, line 102
def post(relative_url, parameters)
  parameters['authenticity_token'] = @authenticity_token
  result = @agent.post("#{manage_url}/#{relative_url}", parameters)
  retrieve_authenticity_token
  result
end
raise_webui_error() click to toggle source
# File lib/chef_webui/session.rb, line 134
def raise_webui_error
  raise WebuiError.new, "Account not created:\n#{retrieve_error_text}"
end
retrieve_authenticity_token() click to toggle source
# File lib/chef_webui/session.rb, line 115
def retrieve_authenticity_token
  @authenticity_token = @agent.page.search('//meta[@name="csrf-token"]/@content').text
end
retrieve_error_text() click to toggle source
# File lib/chef_webui/session.rb, line 119
def retrieve_error_text
  error_messages = @agent.page.search('//span[@class="validation-error"]/..').to_a
  error_messages += @agent.page.search('//div[@class="message error"]/p').to_a

  if error_messages.count > 0
    error_text = ""
    error_messages.each do |em|
      error_text << "#{em.text.gsub(/\s+/, ' ')}\n" # get rid of whitespace
    end
  else
    error_text << em.body
  end
  error_text
end