Class: Fog::Compute::Google::Real

Inherits:
Object
  • Object
show all
Includes:
Collections
Defined in:
lib/fog/google/compute.rb,
lib/fog/google/requests/compute/get_disk.rb,
lib/fog/google/requests/compute/get_zone.rb,
lib/fog/google/requests/compute/get_image.rb,
lib/fog/google/requests/compute/get_server.rb,
lib/fog/google/requests/compute/list_disks.rb,
lib/fog/google/requests/compute/list_zones.rb,
lib/fog/google/requests/compute/delete_disk.rb,
lib/fog/google/requests/compute/get_network.rb,
lib/fog/google/requests/compute/insert_disk.rb,
lib/fog/google/requests/compute/list_images.rb,
lib/fog/google/requests/compute/delete_image.rb,
lib/fog/google/requests/compute/get_firewall.rb,
lib/fog/google/requests/compute/insert_image.rb,
lib/fog/google/requests/compute/list_servers.rb,
lib/fog/google/requests/compute/set_metadata.rb,
lib/fog/google/requests/compute/delete_server.rb,
lib/fog/google/requests/compute/insert_server.rb,
lib/fog/google/requests/compute/list_networks.rb,
lib/fog/google/requests/compute/delete_network.rb,
lib/fog/google/requests/compute/insert_network.rb,
lib/fog/google/requests/compute/list_firewalls.rb,
lib/fog/google/requests/compute/delete_firewall.rb,
lib/fog/google/requests/compute/insert_firewall.rb,
lib/fog/google/requests/compute/delete_operation.rb,
lib/fog/google/requests/compute/get_machine_type.rb,
lib/fog/google/requests/compute/list_machine_types.rb,
lib/fog/google/requests/compute/list_zone_operations.rb,
lib/fog/google/requests/compute/list_global_operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Real

Returns a new instance of Real.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fog/google/compute.rb', line 73

def initialize(options)
  base_url = 'https://www.googleapis.com/compute/'
  api_version = 'v1beta15'
  api_scope_url = 'https://www.googleapis.com/auth/compute'

  @project = options[:google_project]
  google_client_email = options[:google_client_email]
  @api_url = base_url + api_version + '/projects/'

  # NOTE: loaded here to avoid requiring this as a core Fog dependency
  begin
    require 'google/api_client'
  rescue LoadError
    Fog::Logger.warning("Please install the google-api-client gem before using this provider.")
  end
  key = ::Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(options[:google_key_location]), 'notasecret')

  @client = ::Google::APIClient.new({
    :application_name => "fog",
    :application_version => Fog::VERSION,
  })

  @client.authorization = Signet::OAuth2::Client.new({
    :audience => 'https://accounts.google.com/o/oauth2/token',
    :auth_provider_x509_cert_url => "https://www.googleapis.com/oauth2/v1/certs",
    :client_x509_cert_url => "https://www.googleapis.com/robot/v1/metadata/x509/#{google_client_email}",
    :issuer => google_client_email,
    :scope => api_scope_url,
    :signing_key => key,
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
  })

  @client.authorization.fetch_access_token!
  @compute = @client.discovered_api('compute', api_version)
  @default_network = 'default'
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



71
72
73
# File 'lib/fog/google/compute.rb', line 71

def project
  @project
end

Instance Method Details

#backoff_if_unfound(&block) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fog/google/compute.rb', line 140

def backoff_if_unfound(&block)
  retries_remaining = 10
  begin
    result = block.call
  rescue Exception => msg
    if msg.to_s.include? 'was not found' and retries_remaining > 0
      retries_remaining -= 1
      sleep 0.1
      retry
    else
      raise msg
    end
  end
  result
end

#build_response(result) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fog/google/compute.rb', line 125

def build_response(result)
  response = Excon::Response.new
  response.body = Fog::JSON.decode(result.body)
  if response.body["error"]
    response.status = response.body["error"]["code"]

    response.body["error"]["errors"].each do |error|
      raise Fog::Errors::Error.new(error["message"])
    end
  else
    response.status = 200
  end
  response
end

#build_result(api_method, parameters, body_object = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fog/google/compute.rb', line 110

def build_result(api_method, parameters, body_object=nil)
  if body_object
    result = @client.execute(
      :api_method => api_method,
      :parameters => parameters,
      :body_object => body_object
    )
  else
    result = @client.execute(
      :api_method => api_method,
      :parameters => parameters
    )
  end
end

#delete_disk(disk_name, zone_name = @default_zone) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/google/requests/compute/delete_disk.rb', line 15

def delete_disk(disk_name, zone_name=@default_zone)
  api_method = @compute.disks.delete
  parameters = {
    'project' => @project,
    'disk' => disk_name,
    'zone' => zone_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#delete_firewall(firewall_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/delete_firewall.rb', line 15

def delete_firewall(firewall_name)
  api_method = @compute.firewalls.delete
  parameters = {
    'project' => @project,
    'firewall' => firewall_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#delete_image(image_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/delete_image.rb', line 15

def delete_image(image_name)
  api_method = @compute.images.delete
  parameters = {
    'project' => @project,
    'image' => image_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#delete_network(network_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/delete_network.rb', line 15

def delete_network(network_name)
  api_method = @compute.networks.delete
  parameters = {
    'project' => @project,
    'network' => network_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#delete_operation(operation_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/delete_operation.rb', line 15

def delete_operation(operation_name)
  api_method = @compute.operations.delete
  parameters = {
    'project' => @project,
    'operation' => operation_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#delete_server(server_name, zone_name = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/google/requests/compute/delete_server.rb', line 15

def delete_server(server_name, zone_name=nil)
  if zone_name.nil?
    list_zones.body['items'].each do |zone|
      if get_server(server_name, zone['name']).status == 200
        zone_name = zone['name']
      end
    end
  else
    if zone_name.is_a? Excon::Response
      zone_name = zone_name.body["name"]
    end
  end

  api_method = @compute.instances.delete
  parameters = {
    'project' => @project,
    'zone' => zone_name,
    'instance' => server_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#format_metadata(metadata) ⇒ Object



15
16
17
# File 'lib/fog/google/requests/compute/insert_server.rb', line 15

def ()
  { "items" => .map {|k,v| {"key" => k, "value" => v}} }
end

#get_disk(disk_name, zone_name = @default_zone) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/google/requests/compute/get_disk.rb', line 15

def get_disk(disk_name, zone_name=@default_zone)
  if zone_name.start_with? 'http'
    zone_name = zone_name.split('/')[-1]
  end
  api_method = @compute.disks.get
  parameters = {
    'project' => @project,
    'disk' => disk_name,
    'zone' => zone_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#get_firewall(firewall_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/get_firewall.rb', line 15

def get_firewall(firewall_name)
  api_method = @compute.firewalls.get
  parameters = {
    'project' => @project,
    'firewall' => firewall_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#get_image(image_name, project = @project) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/get_image.rb', line 15

def get_image(image_name, project=@project)
  api_method = @compute.images.get
  parameters = {
    'image' => image_name,
    'project' => project,
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#get_machine_type(machine_type_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/get_machine_type.rb', line 15

def get_machine_type(machine_type_name)
  api_method = @compute.machine_types.get
  parameters = {
    'project' => 'google',
    'machineType' => machine_type_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#get_network(network_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/get_network.rb', line 15

def get_network(network_name)
  api_method = @compute.networks.get
  parameters = {
    'project' => @project,
    'network' => network_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#get_server(server_name, zone_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/google/requests/compute/get_server.rb', line 15

def get_server(server_name, zone_name)
  if zone_name.is_a? Excon::Response
    zone = zone_name.body["name"]
  else
    zone = zone_name
  end

  api_method = @compute.instances.get
  parameters = {
    'project' => @project,
    'zone' => zone,
    'instance' => server_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#get_zone(zone_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/get_zone.rb', line 15

def get_zone(zone_name)
  api_method = @compute.zones.get
  parameters = {
    'project' => @project,
    'zone' => zone_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#insert_disk(disk_name, disk_size, zone_name = @default_zone, image_name = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/google/requests/compute/insert_disk.rb', line 15

def insert_disk(disk_name, disk_size, zone_name=@default_zone, image_name=nil)
  api_method = @compute.disks.insert
  parameters = {
    'project' => @project,
    'zone' => zone_name
  }
  if image_name
    # We don't know the owner of the image.
    image = images.create({:name => image_name})
    @image_url = @api_url + image.resource_url
    parameters['sourceImage'] = @image_url
  end
  body_object = {
    'name' => disk_name,
    'sizeGb' => disk_size,
  }

  result = self.build_result(api_method, parameters,
                             body_object)
  response = self.build_response(result)
end

#insert_firewall(firewall_name, source_range, allowed, network = @default_network) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/google/requests/compute/insert_firewall.rb', line 15

def insert_firewall(firewall_name, source_range, allowed,
                    network=@default_network)
  api_method = @compute.firewalls.insert
  parameters = {
    'project' => @project,
  }
  body_object = {
    "name" => firewall_name,
    "network" => "#{@api_url}#{@project}/global/networks/#{network}",
    "sourceRanges" => source_range,
    "allowed" => allowed
  }

  result = self.build_result(api_method, parameters,
                             body_object=body_object)
  response = self.build_response(result)
end

#insert_image(image_name, source) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/google/requests/compute/insert_image.rb', line 15

def insert_image(image_name, source)
  api_method = @compute.images.insert
  parameters = {
    'project' => @project,
  }
  body_object = {
    "name" => image_name,
    "sourceType" => "RAW",
    "source" => source,
    "preferredKernel" => '',
  }

  result = self.build_result(
    api_method,
    parameters,
    body_object=body_object)
  response = self.build_response(result)
end

#insert_network(network_name, ip_range) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/google/requests/compute/insert_network.rb', line 15

def insert_network(network_name, ip_range)
  api_method = @compute.networks.insert
  parameters = {
    'project' => @project,
  }
  body_object = {
    'name' => network_name,
    'IPv4Range' => ip_range
  }

  result = self.build_result(api_method, parameters,
                             body_object=body_object)
  response = self.build_response(result)
end

#insert_server(server_name, zone_name, options = {}, *deprecated_args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/google/requests/compute/insert_server.rb', line 19

def insert_server(server_name, zone_name, options={}, *deprecated_args)
  if deprecated_args.length > 0 or not options.is_a? Hash
    raise ArgumentError.new 'Too many parameters specified. This may be the cause of code written for an outdated'\
        ' version of fog. Usage: server_name, zone_name, [options]'
  end
  api_method = @compute.instances.insert
  parameters = {
      'project' => @project,
      'zone' => zone_name,
  }
  body_object = {:name => server_name}

  if options.has_key? 'image'
    image_name = options.delete 'image'
    image = images.get(image_name)
    @image_url = @api_url + image.resource_url
    body_object['image'] = @image_url
  end
  body_object['machineType'] = @api_url + @project + "/zones/#{zone_name}/machineTypes/#{options.delete 'machineType'}"
  networkInterfaces = []
  if @default_network
    networkInterfaces << {
        'network' => @api_url + @project + "/global/networks/#{@default_network}",
        'accessConfigs' => [
            {'type' => 'ONE_TO_ONE_NAT',
             'name' => 'External NAT'}
        ]
    }
  end
  # TODO: add other networks
  body_object['networkInterfaces'] = networkInterfaces

  if options['disks']
    disks = []
    options.delete('disks').each do |disk|
      if disk.is_a? Disk
        disks << disk.get_object
      else
        disks << disk
      end
    end
    body_object['disks'] = disks
  end

  options['metadata'] =  options['metadata'] if options['metadata']

  if options['kernel']
    body_object['kernel'] = @api_url + "google/global/kernels/#{options.delete 'kernel'}"
  end
  body_object.merge! options # Adds in all remaining options that weren't explicitly handled.

  result = self.build_result(api_method, parameters,
                             body_object=body_object)
  response = self.build_response(result)
end

#list_disks(zone_name = @default_zone) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/list_disks.rb', line 15

def list_disks(zone_name=@default_zone)
  api_method = @compute.disks.list
  parameters = {
    'project' => @project,
    'zone' => zone_name
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_firewallsObject



15
16
17
18
19
20
21
22
23
# File 'lib/fog/google/requests/compute/list_firewalls.rb', line 15

def list_firewalls
  api_method = @compute.firewalls.list
  parameters = {
    'project' => @project
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_global_operationsObject



16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/list_global_operations.rb', line 16

def list_global_operations
  api_method = @compute.global_operations.list
  parameters = {
    'project' => @project
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_images(project = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/list_images.rb', line 15

def list_images(project=nil)
  api_method = @compute.images.list
  project=@project if project.nil?
  parameters = {
    'project' => project
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_machine_types(zone_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/list_machine_types.rb', line 15

def list_machine_types(zone_name)
  api_method = @compute.machine_types.list
  parameters = {
    'project' => @project,
    'zone' => zone_name,
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_networksObject



15
16
17
18
19
20
21
22
23
# File 'lib/fog/google/requests/compute/list_networks.rb', line 15

def list_networks
  api_method = @compute.networks.list
  parameters = {
    'project' => @project
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_servers(zone_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/list_servers.rb', line 15

def list_servers(zone_name)
  api_method = @compute.instances.list
  parameters = {
    'project' => @project,
    'zone' => zone_name,
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_zone_operations(zone) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/google/requests/compute/list_zone_operations.rb', line 15

def list_zone_operations(zone)
  api_method = @compute.zone_operations.list
  parameters = {
    'zone' => zone,
    'project' => @project,
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#list_zonesObject



15
16
17
18
19
20
21
22
23
# File 'lib/fog/google/requests/compute/list_zones.rb', line 15

def list_zones
  api_method = @compute.zones.list
  parameters = {
    'project' => @project
  }

  result = self.build_result(api_method, parameters)
  response = self.build_response(result)
end

#set_metadata(instance, zone, metadata = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/google/requests/compute/set_metadata.rb', line 15

def (instance, zone, ={})
  api_method = @compute.instance.
  parameters = {
    'project' => @project,
    'instance' => instance,
    'zone' => zone,
  }
  body_object = {
    "items" => .to_a.map {|pair| { :key => pair[0], :value => pair[1] } }
  }
  result = self.build_result(
    api_method,
    parameters,
    body_object=body_object)
  response = self.build_response(result)
end