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.



6
7
8
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 6

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

Instance Method Details

#delete_disk(zone, disk_name) ⇒ Object



86
87
88
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 86

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

#delete_instance(zone, instance_identity) ⇒ Object



62
63
64
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 62

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

#disk(zone, name) ⇒ Object



82
83
84
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 82

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

#image(uuid) ⇒ Object



74
75
76
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 74

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



68
69
70
71
72
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 68

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



78
79
80
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 78

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

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

—— RESOURCES ——

Raises:

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


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

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



34
35
36
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 34

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

#instances(zone, **attrs) ⇒ Object



38
39
40
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 38

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

#machine_types(zone) ⇒ Object



50
51
52
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 50

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

#networksObject



46
47
48
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 46

def networks
  list('networks')
end

#project_idObject



10
11
12
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 10

def project_id
  @auth_json['project_id']
end

#serial_port_output(zone, instance_identity) ⇒ Object



100
101
102
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 100

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



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

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



54
55
56
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 54

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

#stop(zone, instance_identity) ⇒ Object



58
59
60
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 58

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

#wait_forObject



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

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



42
43
44
# File 'app/lib/foreman_google/google_compute_adapter.rb', line 42

def zones
  list('zones')
end