Skip to content

Commit

Permalink
Refactor compute get methods
Browse files Browse the repository at this point in the history
This should fix both fog#33 and fog#352 for compute
  • Loading branch information
Temikus committed Jul 22, 2018
1 parent 2e25d1c commit 5470452
Show file tree
Hide file tree
Showing 31 changed files with 159 additions and 125 deletions.
12 changes: 9 additions & 3 deletions lib/fog/compute/google/models/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ def all(region: nil, filter: nil, max_results: nil, order_by: nil, page_token: n
load(data.map(&:to_h))
end

def get(identity, region)
if address = service.get_address(identity, region).to_h
new(address)
def get(identity, region = nil)
if region
address = service.get_address(identity, region).to_h
return new(address)
elsif identity
response = all(:filter => "name eq #{identity}",
:max_results => 1)
address = response.first unless response.empty?
return address
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/backend_services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all(_filters = {})
end

def get(identity)
if backend_service = service.get_backend_service(identity)
new(backend_service.to_h)
if identity
backend_service = service.get_backend_service(identity).to_h
return new(backend_service)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
11 changes: 5 additions & 6 deletions lib/fog/compute/google/models/disk_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ def all(zone: nil, filter: nil, max_results: nil,
end

def get(identity, zone = nil)
response = nil
if zone
response = service.get_disk_type(identity, zone).to_h
disk_type = service.get_disk_type(identity, zone).to_h
return new(disk_type)
else
disk_types = all(:filter => "name eq .*#{identity}")
response = disk_types.first.attributes unless disk_types.empty?
response = all(:filter => "name eq .*#{identity}")
disk_type = response.first unless response.empty?
return disk_type
end
return nil if response.nil?
new(response)
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
Expand Down
14 changes: 10 additions & 4 deletions lib/fog/compute/google/models/disks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ def all(zone: nil, filter: nil, max_results: nil, order_by: nil,
load(data.map(&:to_h))
end

def get(identity, zone)
response = service.get_disk(identity, zone)
return nil if response.nil?
new(response.to_h)
def get(identity, zone = nil)
if zone
disk = service.get_disk(identity, zone).to_h
return new(disk)
elsif identity
response = all(:filter => "name eq #{identity}",
:max_results => 1)
disk = response.first unless response.empty?
return disk
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/firewalls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all(opts = {})
end

def get(identity)
if firewall = service.get_firewall(identity)
new(firewall.to_h)
if identity
firewall = service.get_firewall(identity).to_h
return new(firewall)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
10 changes: 4 additions & 6 deletions lib/fog/compute/google/models/forwarding_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@ def all(region: nil, filter: nil, max_results: nil,
end

def get(identity, region = nil)
response = nil
if region
response = service.get_forwarding_rule(identity, region).to_h
forwarding_rule = service.get_forwarding_rule(identity, region).to_h
return new(forwarding_rule)
elsif identity
response = all(
:filter => "name eq #{identity}", :max_results => 1
).first
response = response.attributes unless response.nil?
forwarding_rule = response.first unless response.empty?
return forwarding_rule
end

return nil if response.nil?
new(response)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/global_addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all(options = {})
end

def get(identity)
if address = service.get_global_address(identity).to_h
new(address)
if identity
address = service.get_global_address(identity).to_h
return new(address)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/global_forwarding_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all(opts = {})
end

def get(identity)
if rule = service.get_global_forwarding_rule(identity).to_h
new(rule)
if identity
rule = service.get_global_forwarding_rule(identity).to_h
return new(rule)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
7 changes: 4 additions & 3 deletions lib/fog/compute/google/models/http_health_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ def all(_filters = {})
end

def get(identity)
response = service.get_http_health_check(identity)
return nil if response.nil?
new(response.to_h)
if identity
http_health_check = service.get_http_health_check(identity).to_h
return new(http_health_check)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
Expand Down
27 changes: 14 additions & 13 deletions lib/fog/compute/google/models/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,22 @@ def current

def get(identity, project = nil)
project.nil? ? projects = all_projects : projects = [project]
data = nil
projects.each do |proj|
begin
data = service.get_image(identity, proj).to_h
data[:project] = proj
rescue ::Google::Apis::ClientError => e
next if e.status_code == 404
break
else
break
if identity
projects.each do |proj|
begin
response = service.get_image(identity, proj).to_h
# TODO: Remove this - see #405
response[:project] = proj
image = response
return new(image)
rescue ::Google::Apis::ClientError => e
next if e.status_code == 404
break
else
break
end
end
end

return nil if data.nil?
new(data)
end

def get_from_family(family, project = nil)
Expand Down
12 changes: 7 additions & 5 deletions lib/fog/compute/google/models/instance_group_managers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ def all(zone: nil, filter: nil, max_results: nil,

def get(identity, zone = nil)
if zone
if instance_group_manager = service.get_instance_group_manager(identity, zone)
new(instance_group_manager.to_h)
end
else
all(:filter => "name eq .*#{identity}").first
instance_group_manager = service.get_instance_group_manager(identity, zone).to_h
return new(instance_group_manager)
elsif identity
response = all(:filter => "name eq .*#{identity}",
:max_results => 1)
instance_group_manager = response.first unless response.empty?
return instance_group_manager
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
18 changes: 8 additions & 10 deletions lib/fog/compute/google/models/instance_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ def all(filters = {})
end

def get(identity, zone = nil)
if zone.nil?
zones = service.list_aggregated_instance_groups(:filter => "name eq .*#{identity}").items
instance_groups = zones.each_value.map(&:instance_groups).compact.first
if instance_groups
zone = instance_groups.first.zone.split("/")[-1]
end
end

if instance_group = service.get_instance_group(identity, zone)
new(instance_group.to_h)
if zone
instance_group = service.get_instance_group(identity, zone).to_h
new(instance_group)
elsif identity
response = all(:filter => "name eq #{identity}",
:max_results => 1)
instance_group = response.first unless response.empty?
return instance_group
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/instance_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all
end

def get(identity)
if instance_template = service.get_instance_template(identity)
new(instance_template.to_h)
if identity
instance_template = service.get_instance_template(identity).to_h
return new(instance_template)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
17 changes: 13 additions & 4 deletions lib/fog/compute/google/models/machine_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ def all(zone: nil, filter: nil, max_results: nil, order_by: nil, page_token: nil
load(data.map(&:to_h) || [])
end

def get(identity, zone)
machine_type = service.get_machine_type(identity, zone).to_h
return nil if machine_type.nil?
new(machine_type)
def get(identity, zone = nil)
if zone
machine_type = service.get_machine_type(identity, zone).to_h
return new(machine_type)
elsif identity
# This isn't very functional since it just shows the first available
# machine type globally, but needed due to overall compatibility
# See: https://github.com/fog/fog-google/issues/352
response = all(:filter => "name eq #{identity}",
:max_results => 1)
machine_type = response.first unless response.empty?
return machine_type
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/networks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all
end

def get(identity)
if network = service.get_network(identity).to_h
new(network)
if identity
network = service.get_network(identity).to_h
return new(network)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
14 changes: 7 additions & 7 deletions lib/fog/compute/google/models/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def all(zone: nil, region: nil, filter: nil, max_results: nil,

def get(identity, zone = nil, region = nil)
if !zone.nil?
response = service.get_zone_operation(zone, identity)
operation = service.get_zone_operation(zone, identity).to_h
return new(operation)
elsif !region.nil?
response = service.get_region_operation(region, identity)
else
response = service.get_global_operation(identity)
operation = service.get_region_operation(region, identity).to_h
return new(operation)
elsif identity
operation = service.get_global_operation(identity).to_h
return new(operation)
end

return nil if response.nil?
new(response.to_h)
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class Projects < Fog::Collection
model Fog::Compute::Google::Project

def get(identity)
if project = service.get_project(identity).to_h
new(project)
if identity
project = service.get_project(identity).to_h
return new(project)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/regions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all
end

def get(identity)
if region = service.get_region(identity).to_h
new(region)
if identity
region = service.get_region(identity).to_h
return new(region)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/compute/google/models/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def all
end

def get(identity)
if route = service.get_route(identity).to_h
new(route)
if identity
route = service.get_route(identity).to_h
return new(route)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
14 changes: 7 additions & 7 deletions lib/fog/compute/google/models/servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def all(zone: nil, filter: nil, max_results: nil,

# TODO: This method needs to take self_links as well as names
def get(identity, zone = nil)
response = nil
if zone
response = service.get_server(identity, zone).to_h
elseif identity
server = all(:filter => "name eq .*#{identity}").first
response = server.attributes if server
server = service.get_server(identity, zone).to_h
return new(server)
elsif identity
response = all(:filter => "name eq .*#{identity}",
:max_results => 1)
server = response.first unless response.empty?
return server
end
return nil if response.nil?
new(response)
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
Expand Down
9 changes: 5 additions & 4 deletions lib/fog/compute/google/models/snapshots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ def all
load(items)
end

def get(snap_id)
response = service.get_snapshot(snap_id)
return nil if response.nil?
new(response.to_h)
def get(identity)
if identity
snapshot = service.get_snapshot(identity).to_h
return new(snapshot)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
Expand Down
7 changes: 4 additions & 3 deletions lib/fog/compute/google/models/ssl_certificates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ class Google
class SslCertificates < Fog::Collection
model Fog::Compute::Google::SslCertificate

def get(certificate_name)
if certificate = service.get_ssl_certificate(certificate_name)
new(certificate.to_h)
def get(identity)
if identity
ssl_certificate = service.get_ssl_certificate(certificate_name).to_h
return new(ssl_certificate)
end
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
Expand Down
Loading

0 comments on commit 5470452

Please sign in to comment.