Skip to content

Commit

Permalink
Merge pull request #287 from emilymye/fixes
Browse files Browse the repository at this point in the history
Test fixes
  • Loading branch information
icco authored Dec 25, 2017
2 parents b78696c + f8d93c7 commit cd57efc
Show file tree
Hide file tree
Showing 39 changed files with 269 additions and 246 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ There are two ways to access [Google Cloud Storage](https://cloud.google.com/sto

Google Compute Engine is a Virtual Machine hosting service. Currently it is built on version [v1](https://developers.google.com/compute/docs/reference/v1/) of the GCE API.

As of 2015-12-07, we believe Fog for Google Compute engine (`Fog::Compute::Google`) is feature complete.

If you are using Fog to interact with GCE, please keep Fog up to date and [file issues](https://github.com/fog/fog-google/issues) for any anomalies you see or features you would like.
As of 2017-12-15, we are still working on making Fog for Google Compute engine (`Fog::Compute::Google`) feature complete. If you are using Fog to interact with GCE, please keep Fog up to date and [file issues](https://github.com/fog/fog-google/issues) for any anomalies you see or features you would like.

## SQL

Fog implements [v1beta4](https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/) of the Google Cloud SQL Admin API. As of 11/6/17, Cloud SQL is mostly feature-complete. Please [file issues](https://github.com/fog/fog-google/issues) for any anomalies you see or features you would like as we finish
Fog implements [v1beta4](https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/) of the Google Cloud SQL Admin API. As of 2017-11-06, Cloud SQL is mostly feature-complete. Please [file issues](https://github.com/fog/fog-google/issues) for any anomalies you see or features you would like as we finish
adding remaining features.

## DNS
Expand Down
9 changes: 5 additions & 4 deletions lib/fog/bin/google.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Google # deviates from other bin stuff to accomodate gem
# deviates from other bin stuff to accomodate gem
module Google
class << self
def class_for(key)
case key
Expand Down Expand Up @@ -62,7 +63,7 @@ def available?
end

# Then make sure we have all of the requirements
for service in services
services.each do |service|
begin
service = class_for(service)
availability &&= service.requirements.all? { |requirement| Fog.credentials.include?(requirement) }
Expand All @@ -75,8 +76,8 @@ def available?
end

if availability
for service in services
for collection in class_for(service).collections
services.each do |service|
class_for(service).collections.each do |collection|
next if respond_to?(collection)
class_eval <<-EOS, __FILE__, __LINE__
def self.#{collection}
Expand Down
5 changes: 5 additions & 0 deletions lib/fog/compute/google/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Address < Fog::Model

IN_USE_STATE = "IN_USE".freeze
RESERVED_STATE = "RESERVED".freeze
RESERVING_STATE = "RESERVING".freeze

def server
return nil if !in_use? || users.nil? || users.empty?
Expand Down Expand Up @@ -67,6 +68,10 @@ def in_use?
status == IN_USE_STATE
end

def ready?
status != RESERVING_STATE
end

private

def associate(nic_name, async = true)
Expand Down
30 changes: 15 additions & 15 deletions lib/fog/compute/google/models/backend_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ class Google
class BackendService < Fog::Model
identity :name

attribute :backends, :aliases => "backends"
attribute :creation_timestamp, :aliases => "kind"
attribute :description, :aliases => "description"
attribute :fingerprint, :aliases => "fingerprint"
attribute :backends
attribute :creation_timestamp
attribute :description
attribute :fingerprint
attribute :health_checks, :aliases => "healthChecks"
attribute :id, :aliases => "id"
attribute :kind, :aliases => "kind"
attribute :port, :aliases => "port"
attribute :protocol, :aliases => "protocol"
attribute :id
attribute :kind
attribute :port
attribute :protocol
attribute :self_link, :aliases => "selfLink"
attribute :timeout_sec, :aliases => "timeoutSec"

def save
requires :name, :health_checks

options = {
"description" => description,
"backends" => backends,
"fingerprint" => fingerprint,
"healthChecks" => health_checks,
"port" => port,
"protocol" => protocol,
"timeoutSec" => timeout_sec
:description => description,
:backends => backends,
:fingerprint => fingerprint,
:health_checks => health_checks,
:port => port,
:protocol => protocol,
:timeout_sec => timeout_sec
}

data = service.insert_backend_service(name, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/compute/google/models/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Network < Fog::Model
attribute :subnetworks

def save
requires :identity, :i_pv4_range
requires :identity, :ipv4_range

data = service.insert_network(identity, attributes)
operation = Fog::Compute::Google::Operations.new(:service => service)
Expand Down
35 changes: 25 additions & 10 deletions lib/fog/compute/google/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Server < Fog::Compute::Server
#
# If you want to pass in a Hash, see {#set_metadata}.
# If you want to access the metadata items as a Hash, see
# {#metadata_items}.
# {#metadata_as_h}.
#
# @example Metadata in API format
#
Expand Down Expand Up @@ -135,7 +135,7 @@ class Server < Fog::Compute::Server
attribute :start_restricted, :aliases => "startRestricted"

# @return [String]
attribute :state, :aliases => "status"
attribute :status, :aliases => "status"

# @return [String]
attribute :status_message, :aliases => "statusMessage"
Expand Down Expand Up @@ -257,7 +257,7 @@ def detach_disk(device_name, async = true)

# Returns metadata items as a Hash.
# @return [Hash<String, String>] items
def metadata_items
def metadata_as_h
if metadata.nil? || metadata[:items].nil? || metadata[:items].empty?
return {}
end
Expand Down Expand Up @@ -373,11 +373,11 @@ def set_tags(new_tags = [], async = true)
end

def provisioning?
state == PROVISIONING
status == PROVISIONING
end

def ready?
state == RUNNING
status == RUNNING
end

def zone_name
Expand Down Expand Up @@ -418,12 +418,8 @@ def map_scopes(scopes)
def save(username: nil, public_key: nil)
requires :name
requires :machine_type
requires :zone_name
requires :disks

unless service.zones.detect { |zone| zone.name == zone_name }
raise ArgumentError.new "#{zone_name.inspect} is either down or you don't have permission to use it."
end
requires :zone

generate_ssh_key_metadata(username, public_key) if public_key

Expand All @@ -435,6 +431,25 @@ def save(username: nil, public_key: nil)
)
end

if attributes[:external_ip]
if options[:network_interfaces].nil? || options[:network_interfaces].empty?
options[:network_interfaces] = [
{
:network => "global/networks/#{GOOGLE_COMPUTE_DEFAULT_NETWORK}"
}
]
end

# Add external IP as default access config if given
options[:network_interfaces][0][:access_configs] = [
{
:name => "External NAT",
:type => "ONE_TO_ONE_NAT",
:nat_ip => attributes[:external_ip]
}
]
end

data = service.insert_server(name, zone_name, options)

operation = Fog::Compute::Google::Operations
Expand Down
37 changes: 31 additions & 6 deletions lib/fog/compute/google/models/servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def get(identity, zone = nil)
nil
end

def bootstrap(public_key_path = "~/.ssh/id_rsa.pub", opts = {})
def bootstrap(public_key_path: nil, **opts)
user = ENV["USER"]
public_key = get_public_key(public_key_path)

name = "fog-#{Time.now.to_i}"
zone_name = "us-central1-f"

Expand Down Expand Up @@ -69,16 +72,38 @@ def bootstrap(public_key_path = "~/.ssh/id_rsa.pub", opts = {})
)
data[:machine_type] = "n1-standard-1" unless data[:machine_type]

public_key = File.read(File.expand_path(public_key_path))
user = ENV["USER"]

server = new(data)
server.save(:username => user, :public_key => public_key)

# TODO: I don't think this is currently possible - remove or enable.
# TODO: sshable? was removed, needs to be fixed for tests
# server.wait_for { sshable? }
server
end

private

# Defaults to:
# 1. ~/.ssh/google_compute_engine.pub
# 2. ~/.ssh/id_rsa.pub
PUBLIC_KEY_DEFAULTS = %w(
~/.ssh/google_compute_engine.pub
~/.ssh/id_rsa.pub
).freeze
def get_public_key(public_key_path)
unless public_key_path
PUBLIC_KEY_DEFAULTS.each do |path|
if File.exist?(File.expand_path(path))
public_key_path = path
break
end
end
end

if public_key_path.nil? || public_key_path.empty?
raise ArgumentError("cannot bootstrap instance without public key file")
end

File.read(File.expand_path(public_key_path))
end
end
end
end
Expand Down
15 changes: 2 additions & 13 deletions lib/fog/compute/google/models/target_instances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,15 @@ def get(target_instance, zone = nil)
if zone
response = service.get_target_instance(target_instance, zone).to_h
else
response = all_aggregated(
:filter => "name eq .*#{target_instance}"
).first
response = all(:filter => "name eq #{target_instance}").first
response = response.attributes unless response.nil?
end
return nil if response.nil?
new(response)
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
end

def all_aggregated(filter: nil, max_results: nil,
order_by: nil, page_token: nil)
service.list_aggregated_target_instances(
:filter => filter,
:max_results => max_results,
:order_by => order_by,
:page_token => page_token
).items.map { |_, v| v.to_h[:target_instances] }.flatten
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/compute/google/models/target_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def save
requires :name, :region

data = service.insert_target_pool(
name, region, attributes.reject(&:nil?)
name, region, attributes.reject { |_k, v| v.nil? }
)
operation = Fog::Compute::Google::Operations
.new(:service => service)
Expand Down
5 changes: 4 additions & 1 deletion lib/fog/compute/google/models/target_pools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def all(region: nil, filter: nil, max_results: nil, order_by: nil, page_token: n
def get(identity, region = nil)
response = nil
if region.nil?
response = all(:filter => "name eq #{identity}").first.attributes
data = all(:filter => "name eq #{identity}").first
unless data.nil?
response = data.attributes
end
else
response = service.get_target_pool(identity, region).to_h
end
Expand Down
51 changes: 7 additions & 44 deletions lib/fog/compute/google/requests/insert_backend_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,18 @@ module Fog
module Compute
class Google
class Mock
def insert_backend_service(backend_service_name, opts = {})
id = Fog::Mock.random_numbers(19).to_s
data[:backend_services][backend_service_name] = {
"kind" => "compute#backendService",
"id" => id,
"creationTimestamp" => Time.now.iso8601,
"name" => backend_service_name,
"description" => "",
"backends" => [
{
"description" => "",
"group" => 'https://www.googleapis.com/resourceviews/v1beta1/projects#{@project}/zones/us-central1-a/zoneViews/name',
"balancingMode" => "RATE",
"capacityScaler" => 1.1,
"maxRate" => 0.5
}
],
"healthChecks" => [opts["health_check"]],
"timeoutSec" => 30,
"port" => 80,
"protocol" => "TCP",
"selfLink" => "https://www.googleapis.com/compute/#{api_version}/projects/#{@project}/global/backendServices/#{backend_service_name}"
}

operation = random_operation
data[:operations][operation] = {
"kind" => "compute#operation",
"id" => Fog::Mock.random_numbers(19).to_s,
"name" => operation,
"zone" => "https://www.googleapis.com/compute/#{api_version}/projects/#{@project}/global",
"operationType" => "insert",
"targetLink" => "https://www.googleapis.com/compute/#{api_version}/projects/#{@project}/global/backendServces/#{backend_service_name}",
"targetId" => id,
"status" => Fog::Compute::Google::Operation::PENDING_STATE,
"user" => "123456789012-qwertyuiopasdfghjkl1234567890qwe@developer.gserviceaccount.com",
"progress" => 0,
"insertTime" => Time.now.iso8601,
"startTime" => Time.now.iso8601,
"selfLink" => "https://www.googleapis.com/compute/#{api_version}/projects/#{@project}/global/operations/#{operation}"
}

build_excon_response(data[:operations][operation])
def insert_backend_service(_backend_service_name, _opts = {})
Fog::Mock.not_implemented
end
end

class Real
def insert_backend_service(backend_service_name, opts = {})
service_object = ::Google::Apis::ComputeV1::BackendService.new(:name => backend_service_name)
@compute.insert_backend_service(@project, service_object, :options => opts)
options = opts.reject { |_k, v| v.nil? }
.merge(:name => backend_service_name)

be_service = ::Google::Apis::ComputeV1::BackendService.new(options)
@compute.insert_backend_service(@project, be_service)
end
end
end
Expand Down
Loading

0 comments on commit cd57efc

Please sign in to comment.