Class: Chef::Provisioning::GoogleDriver::Client::GoogleBase

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/google_driver/client/google_base.rb

Direct Known Subclasses

Instances, OperationsBase, Projects

Constant Summary collapse

TRIES =

TODO make these configurable and find a good place where to put them.

30
SLEEP_SECONDS =
5
NOT_FOUND =
404
OK =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(google, project, zone) ⇒ GoogleBase

Returns a new instance of GoogleBase.



19
20
21
22
23
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 19

def initialize(google, project, zone)
  @google = google
  @project = project
  @zone = zone
end

Instance Attribute Details

#googleObject (readonly)

Returns the value of attribute google.



17
18
19
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 17

def google
  @google
end

#projectObject (readonly)

Returns the value of attribute project.



17
18
19
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 17

def project
  @project
end

#zoneObject (readonly)

Returns the value of attribute zone.



17
18
19
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 17

def zone
  @zone
end

Instance Method Details

#computeObject



38
39
40
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 38

def compute
  @compute ||= google.discovered_api("compute")
end

#default_parametersObject



42
43
44
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 42

def default_parameters
  { project: project, zone: zone }
end

#is_not_found?(response) ⇒ Boolean

Takes a response of an API call and returns true iff this has status not found.

Returns:

  • (Boolean)


56
57
58
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 56

def is_not_found?(response)
  response[:status] == NOT_FOUND
end

#make_request(method, parameters = nil, body = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 25

def make_request(method, parameters = nil, body = nil)
  response = google.execute(
    api_method: method,
    parameters: default_parameters.merge(parameters || {}),
    body_object: body
  )
  {
    status: response.response.status,
    body: FFI_Yajl::Parser.parse(response.response.body,
                                 symbolize_keys: true),
  }
end

#operation_response(response) ⇒ Object

Takes the response of an API call and if the response contains an error, it raises an error. If the response was successful, it returns an operation.



49
50
51
52
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 49

def operation_response(response)
  raise_if_error(response)
  Operation.new(response)
end

#raise_if_error(response) ⇒ Object

Takes a response of an API call and raises an error if the call was unsuccessful.



62
63
64
65
66
67
# File 'lib/chef/provisioning/google_driver/client/google_base.rb', line 62

def raise_if_error(response)
  # TODO Display warnings in some way.
  if response[:status] != OK || response[:body][:error]
    raise GoogleComputeError, response
  end
end