class Dennis::Zone

Public Class Methods

all(client, query: nil, view: nil, tags: nil, page: nil, per_page: nil) click to toggle source
# File lib/dennis/zone.rb, line 13
def all(client, query: nil, view: nil, tags: nil, page: nil, per_page: nil)
  request = client.api.create_request(:get, 'zones')
  request.arguments[:query] = query if query
  request.arguments[:view] = view if view
  request.arguments[:tags] = tags if tags
  request.arguments[:page] = page if page
  request.arguments[:per_page] = per_page if per_page
  PaginatedArray.create(request.perform.hash, 'zones') do |hash|
    new(client, hash)
  end
end
all_for_group(client, group, query: nil, view: nil, tags: nil, page: nil, per_page: nil) click to toggle source
# File lib/dennis/zone.rb, line 25
def all_for_group(client, group, query: nil, view: nil, tags: nil, page: nil, per_page: nil)
  request = client.api.create_request(:get, 'groups/:group/zones')
  request.arguments[:group] = group
  request.arguments[:query] = query if query
  request.arguments[:view] = view if view
  request.arguments[:tags] = tags if tags
  request.arguments[:page] = page if page
  request.arguments[:per_page] = per_page if per_page
  PaginatedArray.create(request.perform.hash, 'zones') do |hash|
    new(client, hash)
  end
end
create(client, group:, allow_sub_domains_of_zones_in_other_groups: nil, **properties) click to toggle source
# File lib/dennis/zone.rb, line 46
def create(client,
           group:,
           allow_sub_domains_of_zones_in_other_groups: nil,
           **properties)
  request = client.api.create_request(:post, 'zones')
  request.arguments[:group] = group
  request.arguments[:properties] = properties
  unless allow_sub_domains_of_zones_in_other_groups.nil?
    request.arguments[:allow_sub_domains_of_zones_in_other_groups] = allow_sub_domains_of_zones_in_other_groups
  end
  new(client, request.perform.hash['zone'])
rescue ApiaClient::RequestError => e
  raise GroupNotFoundError if e.code == 'group_not_found'
  raise ValidationError, e.detail['errors'] if e.code == 'validation_error'

  raise
end
create_or_update(client, group:, **properties) click to toggle source
# File lib/dennis/zone.rb, line 64
def create_or_update(client, group:, **properties)
  if properties[:external_reference].nil?
    raise Dennis::ExternalReferenceRequiredError, 'An external_reference must be provided to use create_or_update'
  end

  zone = find_by(client, :external_reference, properties[:external_reference])
  if zone.nil?
    create(client, group: group, **properties)
  else
    zone.update(properties)
    zone
  end
end
find_by(client, field, value) click to toggle source
# File lib/dennis/zone.rb, line 38
def find_by(client, field, value)
  request = client.api.create_request(:get, 'zones/:zone')
  request.arguments[:zone] = { field => value }
  new(client, request.perform.hash['zone'])
rescue ApiaClient::RequestError => e
  e.code == 'zone_not_found' ? nil : raise
end
new(client, hash) click to toggle source
# File lib/dennis/zone.rb, line 80
def initialize(client, hash)
  @client = client
  @hash = hash
end

Public Instance Methods

always_verified?() click to toggle source
# File lib/dennis/zone.rb, line 113
def always_verified?
  @hash['always_verified']
end
create_or_update_record(**properties) click to toggle source
# File lib/dennis/zone.rb, line 158
def create_or_update_record(**properties)
  Record.create_or_update(@client, id, **properties)
end
create_record(**properties) click to toggle source
# File lib/dennis/zone.rb, line 154
def create_record(**properties)
  Record.create(@client, zone: { id: id }, **properties)
end
created_at() click to toggle source
# File lib/dennis/zone.rb, line 140
def created_at
  return nil if @hash['created_at'].nil?
  return @hash['created_at'] if @hash['created_at'].is_a?(Time)

  Time.at(@hash['created_at'])
end
default_ttl() click to toggle source
# File lib/dennis/zone.rb, line 125
def default_ttl
  @hash['default_ttl']
end
delete() click to toggle source
# File lib/dennis/zone.rb, line 186
def delete
  req = @client.api.create_request(:delete, 'zones/:zone')
  req.arguments['zone'] = { id: id }
  req.perform
  true
end
external_reference() click to toggle source
# File lib/dennis/zone.rb, line 93
def external_reference
  @hash['external_reference']
end
group() click to toggle source
# File lib/dennis/zone.rb, line 133
def group
  return @group if @group
  return nil unless @hash['group']

  @group = Group.new(@client, @hash['group'])
end
id() click to toggle source
# File lib/dennis/zone.rb, line 85
def id
  @hash['id']
end
name() click to toggle source
# File lib/dennis/zone.rb, line 89
def name
  @hash['name']
end
nameservers_checked_at() click to toggle source
# File lib/dennis/zone.rb, line 101
def nameservers_checked_at
  parse_time(@hash['nameservers_checked_at'])
end
nameservers_verified?() click to toggle source
# File lib/dennis/zone.rb, line 105
def nameservers_verified?
  @hash['nameservers_verified']
end
nameservers_verified_at() click to toggle source
# File lib/dennis/zone.rb, line 97
def nameservers_verified_at
  parse_time(@hash['nameservers_verified_at'])
end
record(value, field: :id) click to toggle source
# File lib/dennis/zone.rb, line 166
def record(value, field: :id)
  record = Record.find_by(@client, field, value)
  return nil if record.nil?
  return nil if record.zone.id != id

  record
end
records(**options) click to toggle source
# File lib/dennis/zone.rb, line 162
def records(**options)
  Record.all(@client, { id: id }, **options)
end
reverse_dns?() click to toggle source
# File lib/dennis/zone.rb, line 117
def reverse_dns?
  @hash['reverse_dns']
end
stale_verification?() click to toggle source
# File lib/dennis/zone.rb, line 121
def stale_verification?
  @hash['stale_verification']
end
tags() click to toggle source
# File lib/dennis/zone.rb, line 129
def tags
  @hash['tags']
end
update(properties) click to toggle source
# File lib/dennis/zone.rb, line 174
def update(properties)
  req = @client.api.create_request(:patch, 'zones/:zone')
  req.arguments['zone'] = { id: id }
  req.arguments['properties'] = properties
  @hash = req.perform.hash['zone']
  true
rescue ApiaClient::RequestError => e
  raise ValidationError, e.detail['errors'] if e.code == 'validation_error'

  raise
end
updated_at() click to toggle source
# File lib/dennis/zone.rb, line 147
def updated_at
  return nil if @hash['updated_at'].nil?
  return @hash['updated_at'] if @hash['updated_at'].is_a?(Time)

  Time.at(@hash['updated_at'])
end
verified?() click to toggle source
# File lib/dennis/zone.rb, line 109
def verified?
  @hash['verified']
end
verify_nameservers() click to toggle source
# File lib/dennis/zone.rb, line 193
def verify_nameservers
  req = @client.api.create_request(:post, 'zones/:zone/verify_nameservers')
  req.arguments['zone'] = { id: id }
  @hash = req.perform.hash['zone']
  verified?
rescue ApiaClient::RequestError => e
  raise unless e.code == 'nameservers_already_verified'

  true
end

Private Instance Methods

parse_time(time) click to toggle source
# File lib/dennis/zone.rb, line 206
def parse_time(time)
  return nil if time.nil?
  return time if time.is_a?(Time)

  Time.at(time.to_i)
end