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

[google|dns] Add Zone model

- Add Zone model and test
- Refactor zone mock request to use a single Hash
- create_managed_zone requires description, don't default it to blank
- Fix indentation
This commit is contained in:
Ferran Rodenas 2014-09-17 18:26:36 -07:00
parent ca011b5dca
commit 2680f8b7d4
10 changed files with 213 additions and 94 deletions

View file

@ -14,6 +14,10 @@ module Fog
# MODELS
model_path 'fog/google/models/dns'
# Zone
model :zone
collection :zones
# Project
model :project
collection :projects
@ -32,27 +36,24 @@ module Fog
request :get_project
class Mock
include Fog::Google::Shared
include Fog::Google::Shared
def initialize(options)
shared_initialize(options[:google_project], GOOGLE_DNS_API_VERSION, GOOGLE_DNS_BASE_URL)
end
def self.data(api_version)
@data ||= {}
end
def self.data(api_version)
@data ||= {}
end
def self.reset
@data = nil
end
def self.reset
@data = nil
end
def data(project=@project)
self.class.data(api_version)[project] ||= {
:managed_zones => {
:by_id => {},
:by_name => {},
},
}
:managed_zones => {},
}
end
def reset_data

View file

@ -0,0 +1,48 @@
require 'fog/core/model'
module Fog
module DNS
class Google
##
# Managed Zone resource
#
# @see https://developers.google.com/cloud-dns/api/v1beta1/managedZones
class Zone < Fog::Model
identity :id
attribute :creation_time, :aliases => 'creationTime'
attribute :description
attribute :domain, :aliases => 'dnsName'
attribute :kind
attribute :name
attribute :nameservers, :aliases => 'nameServers'
##
# Deletes a previously created Managed Zone
#
# @return [Boolean] If the Managed Zone has been deleted
def destroy
requires :identity
service.delete_managed_zone(self.identity)
true
end
##
# Creates a new Managed Zone
#
# @return [Fog::DNS::Google::Zone] Managed Zone
# @raise [Fog::Errors::Error] If Managed Zone already exists
def save
requires :name, :domain, :description
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
data = service.create_managed_zone(self.name, self.domain, self.description)
merge_attributes(data.body)
self
end
end
end
end
end

View file

@ -0,0 +1,34 @@
require 'fog/core/collection'
require 'fog/google/models/dns/zone'
module Fog
module DNS
class Google
class Zones < Fog::Collection
model Fog::DNS::Google::Zone
##
# Enumerates Managed Zones that have been created but not yet deleted
#
# @return [Array<Fog::DNS::Google::Zone>] List of Managed Zone resources
def all
data = service.list_managed_zones.body['managedZones'] || []
load(data)
end
##
# Fetches the representation of an existing Managed Zone
#
# @param [String] name_or_id Managed Zone name or identity
# @return [Fog::DNS::Google::Zone] Managed Zone resource
def get(name_or_id)
if zone = service.get_managed_zone(name_or_id).body
new(zone)
end
rescue Fog::Errors::NotFound
nil
end
end
end
end
end

View file

@ -2,48 +2,49 @@ require 'date'
module Fog
module DNS
class Google
class Mock
def create_managed_zone(zone_name, dns_name, descr='')
id = Fog::Mock.random_numbers(19).to_s
object = {
"kind" => "dns#managedZone",
"id" => id,
"creationTime" => DateTime.now.strftime('%FT%T.%LZ'),
"name" => zone_name,
"dnsName" => dns_name,
"description" => descr,
"nameServers" => [
"ns-cloud-e1.googledomains.com.",
"ns-cloud-e2.googledomains.com.",
"ns-cloud-e3.googledomains.com.",
"ns-cloud-e4.googledomains.com.",
],
}
self.data[:managed_zones][:by_name][zone_name] = object
self.data[:managed_zones][:by_id][id] = object
build_excon_response(object)
end
end
##
# Creates a new Managed Zone.
#
# @see https://developers.google.com/cloud-dns/api/v1beta1/managedZones/create
class Real
def create_managed_zone(zone_name, dns_name, descr='')
def create_managed_zone(name, dns_name, description)
api_method = @dns.managed_zones.create
parameters = {
'project' => @project,
}
body_object = {
'name' => zone_name,
'dnsName' => dns_name,
}
body_object['description'] = descr unless descr.nil?
'name' => name,
'dnsName' => dns_name,
'description' => description,
}
request(api_method, parameters, body_object)
end
end
class Mock
def create_managed_zone(name, dns_name, description)
id = Fog::Mock.random_numbers(19).to_s
data = {
'kind' => 'dns#managedZone',
'id' => id,
'creationTime' => DateTime.now.strftime('%FT%T.%LZ'),
'name' => name,
'dnsName' => dns_name,
'description' => description,
'nameServers' => [
'ns-cloud-e1.googledomains.com.',
'ns-cloud-e2.googledomains.com.',
'ns-cloud-e3.googledomains.com.',
'ns-cloud-e4.googledomains.com.',
],
}
self.data[:managed_zones][id] = data
build_excon_response(data)
end
end
end
end
end

View file

@ -1,39 +1,35 @@
module Fog
module DNS
class Google
class Mock
def delete_managed_zone(zone_name_or_id)
if self.data[:managed_zones][:by_name].has_key?(zone_name_or_id)
zone_name = zone_name_or_id
zone = self.data[:managed_zones][:by_name][zone_name]
zone_id = zone['id']
elsif self.data[:managed_zones][:by_id].has_key?(zone_name_or_id)
zone_id = zone_name_or_id
zone = self.data[:managed_zones][:by_name][zone_id]
zone_name = zone['name']
else
raise Fog::Errors::NotFound, "The 'parameters.managedZone' resource named '#{zone_name_or_id}' does not exist."
end
self.data[:managed_zones][:by_name].delete(zone_name)
self.data[:managed_zones][:by_id].delete(zone_id)
build_excon_response(nil)
end
end
##
# Deletes a previously created Managed Zone.
#
# @see https://developers.google.com/cloud-dns/api/v1beta1/managedZones/delete
class Real
def delete_managed_zone(zone_name_or_id)
def delete_managed_zone(name_or_id)
api_method = @dns.managed_zones.delete
parameters = {
'project' => @project,
'managedZone' => zone_name_or_id,
'managedZone' => name_or_id,
}
request(api_method, parameters)
end
end
class Mock
def delete_managed_zone(name_or_id)
if self.data[:managed_zones].has_key?(name_or_id)
self.data[:managed_zones].delete(name_or_id)
elsif zone = self.data[:managed_zones].values.find { |zone| zone['name'] = name_or_id }
self.data[:managed_zones].delete(zone['id'])
else
raise Fog::Errors::NotFound, "The 'parameters.managedZone' resource named '#{name_or_id}' does not exist."
end
build_excon_response(nil)
end
end
end
end
end

View file

@ -1,31 +1,37 @@
module Fog
module DNS
class Google
class Mock
def get_managed_zone(zone_name_or_id)
if self.data[:managed_zones][:by_name].has_key?(zone_name_or_id)
build_excon_response(self.data[:managed_zones][:by_name][zone_name_or_id])
elsif self.data[:managed_zones][:by_id].has_key?(zone_name_or_id)
build_excon_response(self.data[:managed_zones][:by_id][zone_name_or_id])
else
raise Fog::Errors::NotFound, "The 'parameters.managedZone' resource named '#{zone_name_or_id}' does not exist."
end
end
end
##
# Fetches the representation of an existing Managed Zone.
#
# @see https://developers.google.com/cloud-dns/api/v1beta1/managedZones/get
class Real
def get_managed_zone(zone_name_or_id)
def get_managed_zone(name_or_id)
api_method = @dns.managed_zones.get
parameters = {
'project' => @project,
'managedZone' => zone_name_or_id,
'managedZone' => name_or_id,
}
request(api_method, parameters)
end
end
class Mock
def get_managed_zone(name_or_id)
if self.data[:managed_zones].has_key?(name_or_id)
data = self.data[:managed_zones][name_or_id]
else
data = self.data[:managed_zones].values.find { |zone| zone['name'] = name_or_id }
end
unless data
raise Fog::Errors::NotFound, "The 'parameters.managedZone' resource named '#{name_or_id}' does not exist."
end
build_excon_response(data)
end
end
end
end
end

View file

@ -1,16 +1,10 @@
module Fog
module DNS
class Google
class Mock
def list_managed_zones()
zones = self.data[:managed_zones][:by_id].values
build_excon_response({
"kind" => "dns#managedZonesListResponse",
"managedZones" => zones,
})
end
end
##
# Enumerates Managed Zones that have been created but not yet deleted.
#
# @see hhttps://developers.google.com/cloud-dns/api/v1beta1/managedZones/list
class Real
def list_managed_zones()
api_method = @dns.managed_zones.list
@ -21,6 +15,17 @@ module Fog
request(api_method, parameters)
end
end
class Mock
def list_managed_zones()
body = {
'kind' => 'dns#managedZonesListResponse',
'managedZones' => self.data[:managed_zones].values,
}
build_excon_response(body)
end
end
end
end
end

View file

@ -0,0 +1,14 @@
Shindo.tests('Fog::DNS[:google] | zone 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
params = {
:name => Fog::Mock.random_letters(16),
:domain => ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain,
:description => 'Fog test domain'
}
model_tests(Fog::DNS[:google].zones, params)
end

View file

@ -0,0 +1,14 @@
Shindo.tests('Fog::DNS[:google] | zones 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
params = {
:name => Fog::Mock.random_letters(16),
:domain => ENV['FOG_TEST_GOOGLE_DNS_ZONE'] || generate_unique_domain,
:description => 'Fog test domain'
}
collection_tests(Fog::DNS[:google].zones, params)
end

View file

@ -33,7 +33,7 @@ Shindo.tests('Fog::DNS[:google] | managed_zone requests', ['google']) do
tests("#create_managed_zone").data_matches_schema(
@managed_zone_schema, {:allow_extra_keys => false}) do
@google.create_managed_zone(zone_name, zone_dns_name).body
@google.create_managed_zone(zone_name, zone_dns_name, 'Fog test domain').body
end
tests("#get_managed_zone") do