Class: Chef::Provisioning::GoogleDriver::Client::Instances

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

Overview

Wraps an Instances service of the GCE API.

Constant Summary

Constants inherited from GoogleBase

GoogleBase::NOT_FOUND, GoogleBase::OK, GoogleBase::SLEEP_SECONDS, GoogleBase::TRIES

Instance Attribute Summary

Attributes inherited from GoogleBase

#google, #project, #zone

Instance Method Summary collapse

Methods inherited from GoogleBase

#compute, #default_parameters, #initialize, #is_not_found?, #make_request, #operation_response, #raise_if_error

Constructor Details

This class inherits a constructor from Chef::Provisioning::GoogleDriver::Client::GoogleBase

Instance Method Details

#create(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/chef/provisioning/google_driver/client/instances.rb', line 25

def create(options = {})
  response = make_request(
    compute.instances.insert,
    nil,
    options
  )
  operation_response(response)
end

#default_create_options(name) ⇒ Object

This returns the minimum set of options needed to create a Google instance. It adds required options (like name) to the object. cloud.google.com/compute/docs/instances#startinstanceapi



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/provisioning/google_driver/client/instances.rb', line 61

def default_create_options(name)
  {
    machineType: "zones/#{zone}/machineTypes/f1-micro",
    name: name,
    disks: [{
      deviceName: name,
      autoDelete: true,
      boot: true,
      initializeParams: {
        sourceImage: "projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150316",
      },
      type: "PERSISTENT",
    }],
    networkInterfaces: [{
      network: "global/networks/default",
      name: "nic0",
      accessConfigs: [{
        type: "ONE_TO_ONE_NAT",
        name: "External NAT",
      }],
    }],
  }
end

#delete(name) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/chef/provisioning/google_driver/client/instances.rb', line 34

def delete(name)
  response = make_request(
    compute.instances.delete,
    { instance: name }
  )
  operation_response(response)
end

#get(name) ⇒ Object

Retrieves the instance with the given name. If the instance doesn’t exist, returns nil.



15
16
17
18
19
20
21
22
23
# File 'lib/chef/provisioning/google_driver/client/instances.rb', line 15

def get(name)
  response = make_request(
    compute.instances.get,
    { instance: name }
  )
  return nil if is_not_found?(response)
  raise_if_error(response)
  Instance.new(response)
end

#start(name) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/chef/provisioning/google_driver/client/instances.rb', line 42

def start(name)
  response = make_request(
    compute.instances.start,
    { instance: name }
  )
  operation_response(response)
end

#stop(name) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/chef/provisioning/google_driver/client/instances.rb', line 50

def stop(name)
  response = make_request(
    compute.instances.stop,
    { instance: name }
  )
  operation_response(response)
end

#wait_for_status(action_handler, instance, status) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/chef/provisioning/google_driver/client/instances.rb', line 85

def wait_for_status(action_handler, instance, status)
  Retryable.retryable(tries: TRIES, sleep: SLEEP_SECONDS, matching: /reach status/) do |retries, exception|
    action_handler.report_progress("  waited #{retries * SLEEP_SECONDS}/#{TRIES * SLEEP_SECONDS}s for instance #{instance.name} to become #{status}")
    instance = get(instance.name)
    raise GoogleComputeError, "Instance #{instance.name} didn't reach status #{status} yet." unless instance.status == status
    instance
  end
end