Skip to content

Commit

Permalink
[google|compute] Complete Operations support
Browse files Browse the repository at this point in the history
- Add missing Operation attributes
- Remove unnecessary attribute aliases
- Add "zone_name" attibute for backward compatibility
- Add "destroy" method for an Operation
- Add "all" method for Operations
- Fix a bug when building an empty response (ie on delete operations)
- Return nil on NotFound Operations (behave like other fog resources)
  • Loading branch information
frodenas committed Apr 10, 2014
1 parent 96a56b9 commit 42c5ae3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/fog/google/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ def build_result(api_method, parameters, body_object=nil)
# result = Google::APIClient::Result
# returns Excon::Response
def build_response(result)
build_excon_response(result.body.nil? ? nil : Fog::JSON.decode(result.body), result.status)
build_excon_response(result.body.nil? || result.body.empty? ? nil : Fog::JSON.decode(result.body), result.status)
end
end

Expand Down
45 changes: 40 additions & 5 deletions lib/fog/google/models/compute/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ class Operation < Fog::Model

identity :name

attribute :kind, :aliases => 'kind'
attribute :id, :aliases => 'id'
attribute :kind
attribute :id
attribute :client_operation_id, :aliases => 'clientOperationId'
attribute :creation_timestamp, :aliases => 'creationTimestamp'
attribute :zone_name, :aliases => 'zone'
attribute :region_name, :aliases => 'region'
attribute :status, :aliases => 'status'
attribute :end_time, :aliases => 'endTime'
attribute :error
attribute :http_error_message, :aliases => 'httpErrorMessage'
attribute :http_error_status_code, :aliases => 'httpErrorStatusCode'
attribute :insert_time, :aliases => 'insertTime'
attribute :operation_type, :aliases => 'operationType'
attribute :progress
attribute :region
attribute :self_link, :aliases => 'selfLink'
attribute :start_time, :aliases => 'startTime'
attribute :status
attribute :status_message, :aliases => 'statusMessage'
attribute :target_id, :aliases => 'targetId'
attribute :target_link, :aliases => 'targetLink'
attribute :user
attribute :warnings
attribute :zone

def ready?
self.status == DONE_STATE
Expand All @@ -24,6 +38,27 @@ def pending?
self.status == PENDING_STATE
end

def region_name
region.nil? ? nil : region.split('/')[-1]
end

def zone_name
zone.nil? ? nil : zone.split('/')[-1]
end

def destroy
requires :identity

if zone
service.delete_zone_operation(zone, identity)
elsif region
service.delete_region_operation(region, identity)
else
service.delete_global_operation(identity)
end
true
end

def reload
requires :identity

Expand Down
14 changes: 13 additions & 1 deletion lib/fog/google/models/compute/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ class Operations < Fog::Collection

model Fog::Compute::Google::Operation

def all(filters = {})
if filters['zone']
data = service.list_zone_operations(filters['zone']).body
elsif filters['region']
data = service.list_region_operations(filters['region']).body
else
data = service.list_global_operations.body
end
load(data['items'] || [])
end

def get(identity, zone=nil, region=nil)
puts "Getting operation #{identity} zone=#{zone} region=#{region}"
if not zone.nil?
response = service.get_zone_operation(zone, identity)
elsif not region.nil?
Expand All @@ -20,6 +30,8 @@ def get(identity, zone=nil, region=nil)
end
return nil if response.nil?
new(response.body)
rescue Fog::Errors::NotFound
nil
end

end
Expand Down

0 comments on commit 42c5ae3

Please sign in to comment.