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:
parent
6dadbf7306
commit
763c7f2e05
15 changed files with 530 additions and 1 deletions
|
@ -22,6 +22,10 @@ module Fog
|
|||
model :record
|
||||
collection :records
|
||||
|
||||
# Change
|
||||
model :change
|
||||
collection :changes
|
||||
|
||||
# Project
|
||||
model :project
|
||||
collection :projects
|
||||
|
@ -39,6 +43,11 @@ module Fog
|
|||
# Record
|
||||
request :list_resource_record_sets
|
||||
|
||||
# Change
|
||||
request :create_change
|
||||
request :get_change
|
||||
request :list_changes
|
||||
|
||||
# Project
|
||||
request :get_project
|
||||
|
||||
|
@ -61,6 +70,7 @@ module Fog
|
|||
self.class.data(api_version)[project] ||= {
|
||||
:managed_zones => {},
|
||||
:resource_record_sets => {},
|
||||
:changes => {},
|
||||
}
|
||||
end
|
||||
|
||||
|
|
40
lib/fog/google/models/dns/change.rb
Normal file
40
lib/fog/google/models/dns/change.rb
Normal 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
|
42
lib/fog/google/models/dns/changes.rb
Normal file
42
lib/fog/google/models/dns/changes.rb
Normal 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
|
|
@ -15,6 +15,72 @@ module Fog
|
|||
attribute :ttl
|
||||
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
|
||||
|
|
|
@ -21,6 +21,16 @@ module Fog
|
|||
rescue Fog::Errors::NotFound
|
||||
[]
|
||||
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
|
||||
|
|
|
@ -17,6 +17,19 @@ module Fog
|
|||
attribute :name
|
||||
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
|
||||
#
|
||||
|
@ -33,7 +46,7 @@ module Fog
|
|||
#
|
||||
# @return Array<Fog::DNS::Google::Record>] List of Resource Record Sets for the Managed Zone
|
||||
def records
|
||||
@records ||= begin
|
||||
@records = begin
|
||||
Fog::DNS::Google::Records.new(
|
||||
:service => service,
|
||||
:zone => self
|
||||
|
|
64
lib/fog/google/requests/dns/create_change.rb
Normal file
64
lib/fog/google/requests/dns/create_change.rb
Normal 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
|
|
@ -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)
|
||||
end
|
||||
|
|
42
lib/fog/google/requests/dns/get_change.rb
Normal file
42
lib/fog/google/requests/dns/get_change.rb
Normal 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
|
41
lib/fog/google/requests/dns/list_changes.rb
Normal file
41
lib/fog/google/requests/dns/list_changes.rb
Normal 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
|
28
tests/google/models/dns/change_tests.rb
Normal file
28
tests/google/models/dns/change_tests.rb
Normal 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
|
36
tests/google/models/dns/changes_tests.rb
Normal file
36
tests/google/models/dns/changes_tests.rb
Normal 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
|
38
tests/google/models/dns/record_tests.rb
Normal file
38
tests/google/models/dns/record_tests.rb
Normal 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
|
|
@ -15,6 +15,10 @@ Shindo.tests('Fog::DNS[:google] | zone model', ['google']) do
|
|||
tests('success') do
|
||||
@zone = Fog::DNS[:google].zones.create(params)
|
||||
|
||||
tests('#changes').succeeds do
|
||||
@zone.changes
|
||||
end
|
||||
|
||||
tests('#records').succeeds do
|
||||
@zone.records
|
||||
end
|
||||
|
|
86
tests/google/requests/dns/change_tests.rb
Normal file
86
tests/google/requests/dns/change_tests.rb
Normal 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
|
Loading…
Reference in a new issue