Class: ForemanGoogle::GoogleComputeAdapter

Inherits:
Object
  • Object
show all
Defined in:
app/lib/foreman_google/google_compute_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(auth_json_string:) ⇒ GoogleComputeAdapter

Returns a new instance of GoogleComputeAdapter.



4
5
6
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 4

def initialize(auth_json_string:)
  @auth_json = JSON.parse(auth_json_string)
end

Instance Method Details

#delete_disk(zone, disk_name) ⇒ Object



84
85
86
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 84

def delete_disk(zone, disk_name)
  delete('disks', zone, disk: disk_name)
end

#delete_instance(zone, instance_identity) ⇒ Object



60
61
62
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 60

def delete_instance(zone, instance_identity)
  manage_instance(:delete, zone: zone, instance: instance_identity)
end

#disk(zone, name) ⇒ Object



80
81
82
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 80

def disk(zone, name)
  get('disks', disk: name, zone: zone)
end

#image(uuid) ⇒ Object



72
73
74
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 72

def image(uuid)
  images.find { |img| img.id == uuid }
end

#images(filter: nil) ⇒ Object

Setting filter to ‘(deprecated.state != “DEPRECATED”) AND (deprecated.state != “OBSOLETE”)’ doesn’t work and returns empty array, no idea what is happening there



66
67
68
69
70
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 66

def images(filter: nil)
  projects = [project_id] + all_projects
  all_images = projects.map { |project| list_images(project, filter: filter) }
  all_images.flatten.reject(&:deprecated)
end

#insert_disk(zone, disk_attrs = {}) ⇒ Object



76
77
78
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 76

def insert_disk(zone, disk_attrs = {})
  insert('disks', zone, disk_resource: disk_attrs)
end

#insert_instance(zone, attrs = {}) ⇒ Object

—— RESOURCES ——

Raises:

  • (::Google::Cloud::Error)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 14

def insert_instance(zone, attrs = {})
  response = resource_client('instances').insert(project: project_id, zone: zone, instance_resource: attrs)
  operation_attrs = { zone: zone, operation: response.operation.id.to_s }

  wait_for do
    get('zone_operations', **operation_attrs).status == :DONE
  end

  e = get('zone_operations', **operation_attrs).error

  return response unless e

  raise ::Google::Cloud::Error, e.errors.first.message
end

#instance(zone, instance_identity) ⇒ Object

Returns an Google::Instance identified by instance_identity within given zone.

Parameters:

  • zone (String)

    eighter full url or just zone name

  • instance_identity (String)

    eighter an instance name or its id



32
33
34
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 32

def instance(zone, instance_identity)
  get('instances', instance: instance_identity, zone: zone)
end

#instances(zone, **attrs) ⇒ Object



36
37
38
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 36

def instances(zone, **attrs)
  list('instances', zone: zone, **attrs)
end

#machine_types(zone) ⇒ Object



48
49
50
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 48

def machine_types(zone)
  list('machine_types', zone: zone)
end

#networksObject



44
45
46
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 44

def networks
  list('networks')
end

#project_idObject



8
9
10
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 8

def project_id
  @auth_json['project_id']
end

#serial_port_output(zone, instance_identity) ⇒ Object



98
99
100
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 98

def serial_port_output(zone, instance_identity)
  manage_instance(:get_serial_port_output, zone: zone, instance: instance_identity)
end

#set_disk_auto_delete(zone, instance_identity) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 88

def set_disk_auto_delete(zone, instance_identity)
  instance = instance(zone, instance_identity)
  instance.disks.each do |disk|
    manage_instance :set_disk_auto_delete, zone: zone,
      device_name: disk.device_name,
      instance: instance_identity,
      auto_delete: true
  end
end

#start(zone, instance_identity) ⇒ Object



52
53
54
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 52

def start(zone, instance_identity)
  manage_instance(:start, zone: zone, instance: instance_identity)
end

#stop(zone, instance_identity) ⇒ Object



56
57
58
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 56

def stop(zone, instance_identity)
  manage_instance(:stop, zone: zone, instance: instance_identity)
end

#wait_forObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 102

def wait_for
  timeout = 60
  duration = 0
  start = Time.zone.now

  loop do
    break if yield

    raise "The specified wait_for timeout (#{timeout} seconds) was exceeded" if duration > timeout

    sleep(1)
    duration = Time.zone.now - start
  end

  { duration: duration }
end

#zonesObject



40
41
42
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 40

def zones
  list('zones')
end