1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[google|dns] Add Change resource

- Add Change models, requests and tests
- Add #changes method (and associated test) to Zone
- Add #destroy method (and associated test) to Zone
- Add #modify method (and associated test) to Zone
- Add #save method (and associated test) to Zone
This commit is contained in:
Ferran Rodenas 2014-10-10 16:53:26 -07:00
parent 6dadbf7306
commit 763c7f2e05
15 changed files with 530 additions and 1 deletions

View file

@ -22,6 +22,10 @@ module Fog
model :record model :record
collection :records collection :records
# Change
model :change
collection :changes
# Project # Project
model :project model :project
collection :projects collection :projects
@ -39,6 +43,11 @@ module Fog
# Record # Record
request :list_resource_record_sets request :list_resource_record_sets
# Change
request :create_change
request :get_change
request :list_changes
# Project # Project
request :get_project request :get_project
@ -61,6 +70,7 @@ module Fog
self.class.data(api_version)[project] ||= { self.class.data(api_version)[project] ||= {
:managed_zones => {}, :managed_zones => {},
:resource_record_sets => {}, :resource_record_sets => {},
:changes => {},
} }
end end

View file

@ -0,0 +1,40 @@
require 'fog/core/model'
module Fog
module DNS
class Google
##
# Represents a Change resource
#
# @see https://developers.google.com/cloud-dns/api/v1beta1/changes
class Change < Fog::Model
identity :id
attribute :kind
attribute :start_time, :aliases => 'startTime'
attribute :status
attribute :additions
attribute :deletions
DONE_STATE = 'done'
PENDING_STATE = 'pending'
##
# Checks if the change operation is pending
#
# @return [Boolean] True if the change operation is pending; False otherwise
def pending?
self.status == PENDING_STATE
end
##
# Checks if the change operation is done
#
# @return [Boolean] True if the change operation is done; False otherwise
def ready?
self.status == DONE_STATE
end
end
end
end
end

View file

@ -0,0 +1,42 @@
require 'fog/core/collection'
require 'fog/google/models/dns/zone'
module Fog
module DNS
class Google
class Changes < Fog::Collection
model Fog::DNS::Google::Change
attribute :zone
##
# Enumerates the list of Changes
#
# @return [Array<Fog::DNS::Google::Change>] List of Changes resources
def all
requires :zone
data = service.list_changes(zone.identity).body['changes'] || []
load(data)
rescue Fog::Errors::NotFound
[]
end
##
# Fetches the representation of an existing Change
#
# @param [String] identity Change identity
# @return [Fog::DNS::Google::Change] Change resource
def get(identity)
requires :zone
if change = service.get_change(zone.identity, identity).body
new(change)
end
rescue Fog::Errors::NotFound
nil
end
end
end
end
end

View file

@ -15,6 +15,72 @@ module Fog
attribute :ttl attribute :ttl
attribute :rrdatas attribute :rrdatas
##
# Deletes a previously created Resource Record Sets resource
#
# @return [Boolean] If the Resource Record Set has been deleted
def destroy
requires :name, :type, :ttl, :rrdatas
service.create_change(self.zone.id, [], [resource_record_set_format])
true
end
##
# Modifies a previously created Resource Record Sets resource
#
# @return [Fog::DNS::Google::Record] Resource Record Sets resource
def modify(new_attributes)
requires :name, :type, :ttl, :rrdatas
deletions = resource_record_set_format
merge_attributes(new_attributes)
service.create_change(self.zone.id, [resource_record_set_format], [deletions])
self
end
##
# Creates a new Resource Record Sets resource
#
# @return [Fog::DNS::Google::Record] Resource Record Sets resource
def save
requires :name, :type, :ttl, :rrdatas
service.create_change(self.zone.id, [resource_record_set_format], [])
self
end
##
# Returns the Managed Zone of the Resource Record Sets resource
#
# @return [Fog::DNS::Google::Zone] Managed Zone of the Resource Record Sets resource
def zone
@zone
end
private
##
# Assigns the Managed Zone of the Resource Record Sets resource
#
# @param [Fog::DNS::Google::Zone] new_zone Managed Zone of the Resource Record Sets resource
def zone=(new_zone)
@zone = new_zone
end
##
# Resource Record Sets resource representation
#
def resource_record_set_format
{
'kind' => 'dns#resourceRecordSet',
'name' => self.name,
'type' => self.type,
'ttl' => self.ttl,
'rrdatas' => self.rrdatas,
}
end
end end
end end
end end

View file

@ -21,6 +21,16 @@ module Fog
rescue Fog::Errors::NotFound rescue Fog::Errors::NotFound
[] []
end end
##
# Creates a new instance of a Resource Record Set
#
# @return [Fog::DNS::Google::Record] Resource Record Set resource
def new(attributes = {})
requires :zone
super({ :zone => zone }.merge!(attributes))
end
end end
end end
end end

View file

@ -17,6 +17,19 @@ module Fog
attribute :name attribute :name
attribute :nameservers, :aliases => 'nameServers' attribute :nameservers, :aliases => 'nameServers'
##
# Enumerates the list of Changes for the Managed Zone
#
# @return Array<Fog::DNS::Google::Change>] List of Changes for the Managed Zone
def changes
@changes = begin
Fog::DNS::Google::Changes.new(
:service => service,
:zone => self
)
end
end
## ##
# Deletes a previously created Managed Zone # Deletes a previously created Managed Zone
# #
@ -33,7 +46,7 @@ module Fog
# #
# @return Array<Fog::DNS::Google::Record>] List of Resource Record Sets for the Managed Zone # @return Array<Fog::DNS::Google::Record>] List of Resource Record Sets for the Managed Zone
def records def records
@records ||= begin @records = begin
Fog::DNS::Google::Records.new( Fog::DNS::Google::Records.new(
:service => service, :service => service,
:zone => self :zone => self

View file

@ -0,0 +1,64 @@
module Fog
module DNS
class Google
##
# Atomically updates a ResourceRecordSet collection.
#
# @see https://cloud.google.com/dns/api/v1beta1/changes/create
class Real
def create_change(zone_name_or_id, additions = [], deletions = [])
api_method = @dns.changes.create
parameters = {
'project' => @project,
'managedZone' => zone_name_or_id,
}
body = {
'additions' => additions,
'deletions' => deletions,
}
request(api_method, parameters, body)
end
end
class Mock
def create_change(zone_name_or_id, additions = [], deletions = [])
if self.data[:managed_zones].has_key?(zone_name_or_id)
zone = self.data[:managed_zones][zone_name_or_id]
else
zone = self.data[:managed_zones].values.find { |zone| zone['name'] = zone_name_or_id }
end
unless zone
raise Fog::Errors::NotFound, "The 'parameters.managedZone' resource named '#{zone_name_or_id}' does not exist."
end
deletions.each do |del|
rrset = self.data[:resource_record_sets][zone['id']].reject! { |r| r['name'] == del['name'] && r['type'] == del['type'] }
unless rrset
raise Fog::Errors::NotFound, "The 'entity.change.deletions[0]' resource named '#{del['name']} ('#{del['type']})' does not exist."
end
end
additions.each do |add|
self.data[:resource_record_sets][zone['id']] << add
end
id = self.data[:changes][zone['id']].max_by { |c| c['id'] }['id']
data = {
'kind' => 'dns#change',
'id' => (id.to_i + 1).to_s,
'startTime' => DateTime.now.strftime('%FT%T.%LZ'),
'status' => 'done',
'additions' => additions,
'deletions' => deletions,
}
self.data[:changes][zone['id']] << data
build_excon_response(data)
end
end
end
end
end

View file

@ -64,6 +64,15 @@ module Fog
] ]
}, },
] ]
self.data[:changes][id] = [
{
'kind' => 'dns#change',
'id' => '0',
'startTime' => DateTime.now.strftime('%FT%T.%LZ'),
'status' => 'done',
'additions' => self.data[:resource_record_sets][id],
},
]
build_excon_response(data) build_excon_response(data)
end end

View file

@ -0,0 +1,42 @@
module Fog
module DNS
class Google
##
# Fetches the representation of an existing Change.
#
# @see https://developers.google.com/cloud-dns/api/v1beta1/changes/get
class Real
def get_change(zone_name_or_id, identity)
api_method = @dns.changes.get
parameters = {
'project' => @project,
'managedZone' => zone_name_or_id,
'changeId' => identity,
}
request(api_method, parameters)
end
end
class Mock
def get_change(zone_name_or_id, identity)
if self.data[:managed_zones].has_key?(zone_name_or_id)
zone = self.data[:managed_zones][zone_name_or_id]
else
zone = self.data[:managed_zones].values.find { |zone| zone['name'] = zone_name_or_id }
end
unless zone
raise Fog::Errors::NotFound, "The 'parameters.managedZone' resource named '#{zone_name_or_id}' does not exist."
end
unless data = self.data[:changes][zone['id']].find { |c| c['id'] == identity }
raise Fog::Errors::NotFound, "The 'parameters.changeId' resource named '#{identity}' does not exist."
end
build_excon_response(data)
end
end
end
end
end

View file

@ -0,0 +1,41 @@
module Fog
module DNS
class Google
##
# Enumerates the list of Changes.
#
# @see https://developers.google.com/cloud-dns/api/v1beta1/changes/list
class Real
def list_changes(zone_name_or_id)
api_method = @dns.changes.list
parameters = {
'project' => @project,
'managedZone' => zone_name_or_id,
}
request(api_method, parameters)
end
end
class Mock
def list_changes(zone_name_or_id)
if self.data[:managed_zones].has_key?(zone_name_or_id)
zone = self.data[:managed_zones][zone_name_or_id]
else
zone = self.data[:managed_zones].values.find { |zone| zone['name'] == zone_name_or_id }
end
unless zone
raise Fog::Errors::NotFound, "The 'parameters.managedZone' resource named '#{zone_name_or_id}' does not exist."
end
body = {
'kind' => 'dns#changesListResponse',
'changes' => self.data[:changes][zone['id']],
}
build_excon_response(body)
end
end
end
end
end

View file

@ -0,0 +1,28 @@
Shindo.tests('Fog::DNS[:google] | change model', ['google']) do
# Google requires confirmation of ownership for created domains in some cases.
# If you want to run tests in non-mocked mode, set the environment variable to a domain you own.
unless Fog.mocking? || ENV['FOG_TEST_GOOGLE_DNS_ZONE']
tests('Needs a verified domain, set $FOG_TEST_GOOGLE_DNS_ZONE').pending
end
@dns = Fog::DNS[:google]
@zone = @dns.zones.create(
:name => Fog::Mock.random_letters(16),
:domain => ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain,
:description => 'Fog test domain'
)
tests('success') do
tests('#pending?').succeeds do
@dns.changes(:service => @dns, :zone => @zone).get('0').pending? == false
end
tests('#ready?').succeeds do
@dns.changes(:service => @dns, :zone => @zone).get('0').ready? == true
end
end
@zone.destroy
end

View file

@ -0,0 +1,36 @@
Shindo.tests('Fog::DNS[:google] | changes model', ['google']) do
# Google requires confirmation of ownership for created domains in some cases.
# If you want to run tests in non-mocked mode, set the environment variable to a domain you own.
unless Fog.mocking? || ENV['FOG_TEST_GOOGLE_DNS_ZONE']
tests('Needs a verified domain, set $FOG_TEST_GOOGLE_DNS_ZONE').pending
end
@dns = Fog::DNS[:google]
@zone = @dns.zones.create(
:name => Fog::Mock.random_letters(16),
:domain => ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain,
:description => 'Fog test domain'
)
tests('success') do
tests('#all').succeeds do
@dns.changes(:service => @dns, :zone => @zone).all
end
tests('#get').succeeds do
@dns.changes(:service => @dns, :zone => @zone).get('0')
end
end
tests('failure') do
tests('#get').returns(nil) do
@dns.changes(:service => @dns, :zone => @zone).get(Fog::Mock.random_letters_and_numbers(16))
end
end
@zone.destroy
end

View file

@ -0,0 +1,38 @@
Shindo.tests('Fog::DNS[:google] | record model', ['google']) do
# Google requires confirmation of ownership for created domains in some cases.
# If you want to run tests in non-mocked mode, set the environment variable to a domain you own.
unless Fog.mocking? || ENV['FOG_TEST_GOOGLE_DNS_ZONE']
tests('Needs a verified domain, set $FOG_TEST_GOOGLE_DNS_ZONE').pending
end
@dns = Fog::DNS[:google]
params = {
:name => "#{Fog::Mock.random_letters(16)}.#{ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain}",
:type => 'A',
:ttl => 3600,
:rrdatas => ['192.168.1.1'],
}
tests('success') do
@zone = @dns.zones.create(
:name => Fog::Mock.random_letters(8),
:domain => ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain,
:description => 'Fog test domain'
)
tests('#save').succeeds do
@record = @zone.records.create(params)
end
tests('#modify').succeeds do
@record.modify({ :ttl => 2600 })
end
tests('#destroy').succeeds do
@record.destroy
end
@zone.destroy
end
end

View file

@ -15,6 +15,10 @@ Shindo.tests('Fog::DNS[:google] | zone model', ['google']) do
tests('success') do tests('success') do
@zone = Fog::DNS[:google].zones.create(params) @zone = Fog::DNS[:google].zones.create(params)
tests('#changes').succeeds do
@zone.changes
end
tests('#records').succeeds do tests('#records').succeeds do
@zone.records @zone.records
end end

View file

@ -0,0 +1,86 @@
Shindo.tests('Fog::DNS[:google] | change requests', ['google']) do
# Google requires confirmation of ownership for created domains in some cases.
# If you want to run tests in non-mocked mode, set the environment variable to a domain you own.
unless Fog.mocking? || ENV['FOG_TEST_GOOGLE_DNS_ZONE']
tests('Needs a verified domain, set $FOG_TEST_GOOGLE_DNS_ZONE').pending
end
@dns = Fog::DNS[:google]
@get_change_format = {
'kind' => String,
'id' => String,
'startTime' => String,
'status' => String,
'additions' => Fog::Nullable::Array,
'deletions' => Fog::Nullable::Array,
}
@list_changes_format = {
'kind' => String,
'changes' => [@get_change_format],
}
@zone = @dns.zones.create(
:name => Fog::Mock.random_letters(16),
:domain => ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain,
:description => 'Fog test domain'
)
rrset_resource = {
:name => "#{Fog::Mock.random_letters(16)}.#{ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain}",
:type => 'A',
:ttl => 3600,
:rrdatas => ['192.168.1.1'],
}
tests('success') do
tests('#create_change.additions').formats(@get_change_format) do
@dns.create_change(@zone.identity, [rrset_resource], []).body
end
tests('#create_change.additions.deletions').formats(@get_change_format) do
@dns.create_change(@zone.identity, [rrset_resource], [rrset_resource]).body
end
tests('#create_change.deletions').formats(@get_change_format) do
@dns.create_change(@zone.identity, [], [rrset_resource]).body
end
tests('#list_changes').formats(@list_changes_format) do
@dns.list_changes(@zone.identity).body
end
tests('#get_change').formats(@get_change_format) do
@dns.get_change(@zone.identity, '0').body
end
end
tests('failure') do
tests('#list_changes').raises(Fog::Errors::NotFound) do
@dns.list_changes(generate_unique_domain).body
end
tests('#get_change').raises(Fog::Errors::NotFound) do
@dns.get_change(generate_unique_domain, Fog::Mock.random_letters_and_numbers(16)).body
end
tests('#get_change').raises(Fog::Errors::NotFound) do
@dns.get_change(@zone.identity, Fog::Mock.random_letters_and_numbers(16)).body
end
tests('#create_change').raises(Fog::Errors::NotFound) do
@dns.create_change(generate_unique_domain, [], [rrset_resource]).body
end
tests('#create_change').raises(Fog::Errors::NotFound) do
@dns.create_change(@zone.identity, [], [rrset_resource]).body
end
end
@zone.destroy
end