Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[google|compute] Add Addresses support
- Add "Addresses" model and collection - Add "list_aggregated_addresses" request - Modify "insert" request to allow "description" property
- Loading branch information
Showing
5 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'fog/core/model' | ||
|
||
module Fog | ||
module Compute | ||
class Google | ||
|
||
## | ||
# Represents an Address resource | ||
# | ||
# @see https://developers.google.com/compute/docs/reference/latest/addresses | ||
class Address < Fog::Model | ||
identity :name | ||
|
||
attribute :kind | ||
attribute :id | ||
attribute :address | ||
attribute :creation_timestamp, :aliases => 'creationTimestamp' | ||
attribute :description | ||
attribute :region | ||
attribute :self_link, :aliases => 'selfLink' | ||
attribute :status | ||
attribute :users | ||
|
||
IN_USE_STATE = 'IN_USE' | ||
RESERVED_STATE = 'RESERVED' | ||
|
||
def save | ||
requires :identity, :region | ||
|
||
data = service.insert_address(identity, self.region, self.attributes) | ||
operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], nil, data.body['region']) | ||
operation.wait_for { !pending? } | ||
reload | ||
end | ||
|
||
def destroy | ||
requires :identity, :region | ||
|
||
data = service.delete_address(identity, self.region.split('/')[-1]) | ||
Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], nil, data.body['region']) | ||
end | ||
|
||
def reload | ||
requires :identity, :region | ||
|
||
data = collection.get(identity, self.region.split('/')[-1]) | ||
merge_attributes(data.attributes) | ||
self | ||
end | ||
|
||
def in_use? | ||
self.status == IN_USE_STATE | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'fog/core/collection' | ||
require 'fog/google/models/compute/address' | ||
|
||
module Fog | ||
module Compute | ||
class Google | ||
|
||
class Addresses < Fog::Collection | ||
model Fog::Compute::Google::Address | ||
|
||
def all(filters = {}) | ||
if filters[:region] | ||
data = service.list_addresses(filters[:region]).body['items'] || [] | ||
else | ||
data = [] | ||
service.list_aggregated_addresses.body['items'].each_value do |region| | ||
data.concat(region['addresses']) if region['addresses'] | ||
end | ||
end | ||
load(data) | ||
end | ||
|
||
def get(identity, region) | ||
if address = service.get_address(identity, region).body | ||
new(address) | ||
end | ||
rescue Fog::Errors::NotFound | ||
nil | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
lib/fog/google/requests/compute/list_aggregated_addresses.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Fog | ||
module Compute | ||
class Google | ||
|
||
class Mock | ||
def list_aggregated_addresses | ||
Fog::Mock.not_implemented | ||
end | ||
end | ||
|
||
class Real | ||
def list_aggregated_addresses | ||
api_method = @compute.addresses.aggregated_list | ||
parameters = { | ||
'project' => @project, | ||
} | ||
|
||
result = self.build_result(api_method, parameters) | ||
response = self.build_response(result) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |