Class: ChefWebui::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_webui/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manage_url, account_url) ⇒ Session

Returns a new instance of Session.



9
10
11
12
13
14
15
# File 'lib/chef_webui/session.rb', line 9

def initialize(manage_url, )
  @manage_url = manage_url
  @account_url = 
  @agent = Mechanize.new
  @agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  get('login')
end

Instance Attribute Details

#account_urlObject (readonly)

Returns the value of attribute account_url.



7
8
9
# File 'lib/chef_webui/session.rb', line 7

def 
  @account_url
end

#manage_urlObject (readonly)

Returns the value of attribute manage_url.



6
7
8
# File 'lib/chef_webui/session.rb', line 6

def manage_url
  @manage_url
end

Instance Method Details

#create_org(orgname) ⇒ Object



34
35
36
37
38
39
40
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_environmentObject



96
97
98
# File 'lib/chef_webui/session.rb', line 96

def current_environment
  @agent.page.at('#Environment option[selected="selected"]').text.gsub(/\s+/, ' ')
end

#current_orgObject



92
93
94
# File 'lib/chef_webui/session.rb', line 92

def current_org
  @agent.page.at('#header h1 a[href="/nodes"]').text.gsub(/\s+/, ' ')
end

#current_userObject



88
89
90
# File 'lib/chef_webui/session.rb', line 88

def current_user
  @agent.page.at('#user-navigation a').text.gsub(/\s+/, ' ')
end

#logged_inObject



30
31
32
# File 'lib/chef_webui/session.rb', line 30

def logged_in
  return @agent.page.content =~ /Logged in as/
end

#login(username, password) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chef_webui/session.rb', line 17

def (username, password)
  results = post('login_exec',
    {
      'name' => username,
      'password' => password,
      'commit' => 'Login'
    })

  if !logged_in
    raise_webui_error
  end
end

#organizationsObject



79
80
81
82
83
84
85
86
# 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_keyObject



71
72
73
74
75
76
77
# File 'lib/chef_webui/session.rb', line 71

def regenerate_user_key
  @agent.post("#{}/account/regen_key",
    {
      'commit' => 'Get a new key',
      'authenticity_token' => @authenticity_token
    }).content
end

#regenerate_validator_key(orgname) ⇒ Object



63
64
65
66
67
68
69
# 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) ⇒ Object



42
43
44
# File 'lib/chef_webui/session.rb', line 42

def switch_to_org(orgname)
  post("organizations/#{orgname}/select", {})
end