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.



68
69
70
71
72
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
# File 'lib/fog/google/compute.rb', line 68

def initialize(options)


  base_url = 'https://www.googleapis.com/compute/'
  api_version = 'v1beta14'
  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.



66
67
68
# File 'lib/fog/google/compute.rb', line 66

def project
  @project
end

Instance Method Details

#build_response(result) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/fog/google/compute.rb', line 121

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"]
  else
    response.status = 200
  end
  response
end

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fog/google/compute.rb', line 105

def build_result(api_method, parameters, body_object=nil)
  if body_object
    #p api_method, parameters
    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
# 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|
      data = get_server(server_name, zone['name']).body
      if data["error"].nil?
        zone_name = zone['name']
      end
    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

#get_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/get_disk.rb', line 15

def get_disk(disk_name, zone_name=@default_zone)
  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
# File 'lib/fog/google/requests/compute/get_server.rb', line 15

def get_server(server_name, zone_name)
  api_method = @compute.instances.get
  parameters = {
    'project' => @project,
    'zone' => zone_name,
    '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) ⇒ Object



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

def insert_disk(disk_name, disk_size, zone_name=@default_zone)
  api_method = @compute.disks.insert
  parameters = {
    'project' => @project,
    'zone' => zone_name
  }
  body_object = {
    'name' => disk_name,
    'sizeGb' => disk_size,
  }

  result = self.build_result(api_method, parameters,
                             body_object=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, image_name, zone_name, machine_name, network_name = @default_network) ⇒ Object



15
16
17
18
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
# File 'lib/fog/google/requests/compute/insert_server.rb', line 15

def insert_server(server_name, image_name,
                  zone_name, machine_name,
                  network_name=@default_network)

  # We need to check if the image is owned by the user or a global image.
  if get_image(image_name, @project).data['code'] == 200
    image_url = @api_url + @project + "/global/images/#{image_name}"
  else
    image_url = @api_url + "google/global/images/#{image_name}"
  end

  api_method = @compute.instances.insert
  parameters = {
    'project' => @project,
    'zone' => zone_name,
  }
  body_object = {
    'name' => server_name,
    'image' => image_url,
    'machineType' => @api_url + @project + "/global/machineTypes/#{machine_name}",
    'networkInterfaces' => [{
      'network' => @api_url + @project + "/global/networks/#{network_name}"
    }]
  }

  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_imagesObject



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

def list_images
  api_method = @compute.images.list
  parameters = {
    'project' => @project
  }

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

#list_machine_typesObject



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

def list_machine_types
  api_method = @compute.machine_types.list
  parameters = {
    'project' => 'google'
  }

  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