Class: DeisClient

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

Constant Summary collapse

REQUEST_TIMEOUT =

seconds

180
VERIFY_SSL =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller_uri, username, password, mock = false) ⇒ DeisClient

Returns a new instance of DeisClient.

Raises:



13
14
15
16
17
18
19
20
# File 'lib/deis_client.rb', line 13

def initialize(controller_uri, username, password, mock=false)
  @mock = mock
  raise DeisError.new("No username or password detected!") if username.nil? || password.nil?
  raise DeisError.new("You must specify a URI for Deis controller!") unless controller_uri =~ /\A#{URI::regexp}\z/
  @deis_controller = controller_uri
  (username, password)
  self
end

Instance Attribute Details

#user_tokenObject (readonly)

Returns the value of attribute user_token.



8
9
10
# File 'lib/deis_client.rb', line 8

def user_token
  @user_token
end

Instance Method Details

#app_create(app_name) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/deis_client.rb', line 33

def app_create(app_name)
  if @mock
    {}
  else
    payload = app_name.nil? ? Hash.new : {"id" => app_name}
    response = RestClient::Request.execute(:method => :post, :url => apps_url, :payload => payload.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    JSON.parse response.body
  end
end

#app_destroy(app_name) ⇒ Object

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/deis_client.rb', line 43

def app_destroy(app_name)
  raise DeisError.new("App name is required") if app_name.nil?
  if @mock
    {}
  else
    response = RestClient::Request.execute(:method => :delete, :url => app_url(app_name), :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    response.code == 204
  end
end

#app_restart(app_name) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
# File 'lib/deis_client.rb', line 67

def app_restart(app_name)
  raise DeisError.new("App name is required") if app_name.nil?
  if @mock
    false
  else
    response = RestClient::Request.execute(:method => :post, :url => app_restart_url(app_name), :payload => {}.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    response.code == 200
  end
end

#app_scale(app_name, process_type, process_count) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/deis_client.rb', line 53

def app_scale(app_name, process_type, process_count)
  raise DeisError.new("App name is required") if app_name.nil?
  raise DeisError.new("Process type is required. Supported values are 'web' or 'worker'") unless ["web", "worker"].include? process_type
  raise DeisError.new("Process count is required") unless process_count.is_a? Fixnum
  raise DeisError.new("Process count cannnot be negative") if process_count < 0
  if @mock
    false
  else
    payload = {process_type => process_count}
    response = RestClient::Request.execute(:method => :post, :url => scale_url(app_name), :payload => payload.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    response.code == 204
  end
end

#cert_add(common_name, certificate, private_key) ⇒ Object

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/deis_client.rb', line 90

def cert_add(common_name, certificate, private_key)
  raise DeisError.new("Common name is required") if common_name.nil?
  raise DeisError.new("Certificate name is required") if certificate.nil?
  raise DeisError.new("Private key is required") if private_key.nil?
  if @mock
    {}
  else
    payload = {"common_name" => common_name, "certificate" => certificate, "key" => private_key}
    response = RestClient::Request.execute(:method => :post, :url => certs_url, :payload => payload.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    JSON.parse response.body
  end
end

#cert_remove(common_name) ⇒ Object

Raises:



103
104
105
106
107
108
109
110
111
112
# File 'lib/deis_client.rb', line 103

def cert_remove(common_name)
  raise DeisError.new("Common name is required") if common_name.nil?
  if @mock
    {}
  else
    url = "#{certs_url}#{common_name}"
    response = RestClient::Request.execute(:method => :delete, :url => url, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    response.code == 204
  end
end

#command_run(app_name, command, timeout_in_seconds = REQUEST_TIMEOUT) ⇒ Object

Raises:



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/deis_client.rb', line 169

def command_run(app_name, command, timeout_in_seconds=REQUEST_TIMEOUT)
  raise DeisError.new("App name is required") if app_name.nil?
  raise DeisError.new("Command string is required") if command.nil?
  if @mock
    {}
  else
    payload = {"command" => command}
    response = RestClient::Request.execute(:method => :post, :url => command_run_url(app_name), :payload => payload.to_json, :timeout => timeout_in_seconds, :verify_ssl => VERIFY_SSL, :headers => headers)
    JSON.parse response.body
  end
end

#config_get(app_name) ⇒ Object

Raises:



149
150
151
152
153
154
155
156
157
158
# File 'lib/deis_client.rb', line 149

def config_get(app_name)
  raise DeisError.new("App name is required") if app_name.nil?
  if @mock
    {}
  else
    response = RestClient::Request.execute(:method => :get, :url => config_url(app_name), :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    hash = JSON.parse response.body
    hash["values"]
  end
end

#config_set(app_name, config_hash = {}) ⇒ Object

Raises:



138
139
140
141
142
143
144
145
146
147
# File 'lib/deis_client.rb', line 138

def config_set(app_name, config_hash={})
  raise DeisError.new("App name is required") if app_name.nil?
  if @mock || config_hash.empty?
    {}
  else
    payload = {"values" => config_hash}
    response = RestClient::Request.execute(:method => :post, :url => config_url(app_name), :payload => payload.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    JSON.parse response.body
  end
end

#domain_add(app_name, domain_name) ⇒ Object

Raises:



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/deis_client.rb', line 114

def domain_add(app_name, domain_name)
  raise DeisError.new("App name is required") if app_name.nil?
  raise DeisError.new("Domain name is required") if domain_name.nil?
  if @mock
    {}
  else
    payload = {"domain" => domain_name}
    response = RestClient::Request.execute(:method => :post, :url => domains_url(app_name), :payload => payload.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    JSON.parse response.body
  end
end

#domain_remove(app_name, domain_name) ⇒ Object

Raises:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/deis_client.rb', line 126

def domain_remove(app_name, domain_name)
  raise DeisError.new("App name is required") if app_name.nil?
  raise DeisError.new("Domain name is required") if domain_name.nil?
  if @mock
    {}
  else
    url = "#{domains_url(app_name)}#{domain_name}"
    response = RestClient::Request.execute(:method => :delete, :url => url, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    response.code == 204
  end
end

#key_add(user_name, ssh_public_key) ⇒ Object

Raises:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/deis_client.rb', line 78

def key_add(user_name, ssh_public_key)
  raise DeisError.new("Username is required") if user_name.nil?
  raise DeisError.new("SSH key is required") if ssh_public_key.nil?
  if @mock
    {}
  else
    payload = {"id" => user_name, "public" => ssh_public_key}
    response = RestClient::Request.execute(:method => :post, :url => keys_url, :payload => payload.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
    JSON.parse response.body
  end
end

#login(username, password) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/deis_client.rb', line 22

def (username, password)
  if @mock
    @user_token = "ABC123"
  else
    payload = {"username" => username, "password" => password}
    response = RestClient::Request.execute(:method => :post, :url => , :payload => payload.to_json, :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => {:content_type => :json, :accept => :json})
    body = JSON.parse response.body
    @user_token = body.fetch('token')
  end
end

#logs_get(app_name) ⇒ Object

Raises:



160
161
162
163
164
165
166
167
# File 'lib/deis_client.rb', line 160

def logs_get(app_name)
  raise DeisError.new("App name is required") if app_name.nil?
  if @mock
    {}
  else
    RestClient::Request.execute(:method => :get, :url => log_url(app_name), :timeout => REQUEST_TIMEOUT, :verify_ssl => VERIFY_SSL, :headers => headers)
  end
end