Class: Gcloud::Dns::Zone
- Inherits:
-
Object
- Object
- Gcloud::Dns::Zone
- Defined in:
- lib/gcloud/dns/zone.rb,
lib/gcloud/dns/zone/list.rb,
lib/gcloud/dns/zone/transaction.rb
Overview
# DNS Zone
The managed zone is the container for DNS records for the same DNS name suffix and has a set of name servers that accept and responds to queries. A project can have multiple managed zones, but they must each have a unique name.
Defined Under Namespace
Classes: List, Transaction
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#add(name, type, ttl, data, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Adds a record to the Zone.
-
#change(change_id) ⇒ Gcloud::Dns::Change?
(also: #find_change, #get_change)
Retrieves an existing change by id.
-
#changes(token: nil, max: nil, order: nil) ⇒ Array<Gcloud::Dns::Change>
(also: #find_changes)
Retrieves the list of changes belonging to the zone.
-
#clear! ⇒ Object
Removes non-essential records from the zone.
-
#created_at ⇒ Object
The time that this resource was created on the server.
-
#delete(force: false) ⇒ Boolean
Permanently deletes the zone.
-
#description ⇒ Object
A string of at most 1024 characters associated with this resource for the user’s convenience.
-
#dns ⇒ Object
The DNS name of this managed zone, for instance “example.com.”.
-
#export(path) ⇒ File
Exports the zone to a local [DNS zone file](en.wikipedia.org/wiki/Zone_file).
-
#fqdn(domain_name) ⇒ String
This helper converts the given domain name or subdomain (e.g., ‘www`) fragment to a [fully qualified domain name (FQDN)](en.wikipedia.org/wiki/Fully_qualified_domain_name) for the zone’s #dns.
-
#id ⇒ Object
Unique identifier for the resource; defined by the server.
-
#import(path_or_io, only: nil, except: nil, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Imports resource records from a [DNS zone file](en.wikipedia.org/wiki/Zone_file), adding the new records to the zone, without removing any existing records from the zone.
-
#initialize ⇒ Zone
constructor
A new instance of Zone.
-
#modify(name, type, skip_soa: nil, soa_serial: nil) {|record| ... } ⇒ Gcloud::Dns::Change
Modifies records on the Zone.
-
#name ⇒ Object
User assigned name for this resource.
-
#name_server_set ⇒ Object
Optionally specifies the NameServerSet for this ManagedZone.
-
#name_servers ⇒ Object
Delegate your managed_zone to these virtual name servers; defined by the server.
-
#record(name, type, ttl, data) ⇒ Gcloud::Dns::Record
(also: #new_record)
Creates a new, unsaved Record that can be added to a Zone.
-
#records(name = nil, type = nil, token: nil, max: nil) ⇒ Array<Gcloud::Dns::Record>
(also: #find_records)
Retrieves the list of records belonging to the zone.
-
#remove(name, type, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Removes records from the Zone.
-
#replace(name, type, ttl, data, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Replaces existing records on the Zone.
- #to_zonefile ⇒ Object
-
#update(additions = [], deletions = [], skip_soa: nil, soa_serial: nil) {|tx| ... } ⇒ Gcloud::Dns::Change
Adds and removes Records from the zone.
Constructor Details
#initialize ⇒ Zone
Returns a new instance of Zone.
56 57 58 59 |
# File 'lib/gcloud/dns/zone.rb', line 56 def initialize @connection = nil @gapi = {} end |
Instance Attribute Details
#connection ⇒ Object
48 49 50 |
# File 'lib/gcloud/dns/zone.rb', line 48 def connection @connection end |
#gapi ⇒ Object
52 53 54 |
# File 'lib/gcloud/dns/zone.rb', line 52 def gapi @gapi end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
730 731 732 733 734 735 |
# File 'lib/gcloud/dns/zone.rb', line 730 def self.from_gapi gapi, conn new.tap do |f| f.gapi = gapi f.connection = conn end end |
Instance Method Details
#add(name, type, ttl, data, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Adds a record to the Zone. In order to update existing records, or add and delete records in the same transaction, use #update.
This operation automatically updates the SOA record serial number unless prevented with the ‘skip_soa` option. See #update for details.
576 577 578 579 |
# File 'lib/gcloud/dns/zone.rb', line 576 def add name, type, ttl, data, skip_soa: nil, soa_serial: nil update [record(name, type, ttl, data)], [], skip_soa: skip_soa, soa_serial: soa_serial end |
#change(change_id) ⇒ Gcloud::Dns::Change? Also known as: find_change, get_change
Retrieves an existing change by id.
193 194 195 196 197 198 199 200 201 |
# File 'lib/gcloud/dns/zone.rb', line 193 def change change_id ensure_connection! resp = connection.get_change id, change_id if resp.success? Change.from_gapi resp.data, self else nil end end |
#changes(token: nil, max: nil, order: nil) ⇒ Array<Gcloud::Dns::Change> Also known as: find_changes
Retrieves the list of changes belonging to the zone.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/gcloud/dns/zone.rb', line 254 def changes token: nil, max: nil, order: nil ensure_connection! # Fix the sort options order = adjust_change_sort_order order sort = "changeSequence" if order # Continue with the API call resp = connection.list_changes id, token: token, max: max, order: order, sort: sort if resp.success? Change::List.from_response resp, self else fail ApiError.from_response(resp) end end |
#clear! ⇒ Object
Removes non-essential records from the zone. Only NS and SOA records will be kept.
168 169 170 171 172 |
# File 'lib/gcloud/dns/zone.rb', line 168 def clear! non_essential = records.all.reject { |r| %w(SOA NS).include?(r.type) } change = update [], non_essential change.wait_until_done! unless change.nil? end |
#created_at ⇒ Object
The time that this resource was created on the server.
113 114 115 116 117 |
# File 'lib/gcloud/dns/zone.rb', line 113 def created_at Time.parse @gapi["creationTime"] rescue nil end |
#delete(force: false) ⇒ Boolean
Permanently deletes the zone.
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/gcloud/dns/zone.rb', line 144 def delete force: false clear! if force ensure_connection! resp = connection.delete_zone id if resp.success? true else fail ApiError.from_response(resp) end end |
#description ⇒ Object
A string of at most 1024 characters associated with this resource for the user’s convenience. Has no effect on the managed zone’s function.
89 90 91 |
# File 'lib/gcloud/dns/zone.rb', line 89 def description @gapi["description"] end |
#dns ⇒ Object
The DNS name of this managed zone, for instance “example.com.”.
81 82 83 |
# File 'lib/gcloud/dns/zone.rb', line 81 def dns @gapi["dnsName"] end |
#export(path) ⇒ File
Exports the zone to a local [DNS zone file](en.wikipedia.org/wiki/Zone_file).
381 382 383 384 385 |
# File 'lib/gcloud/dns/zone.rb', line 381 def export path File.open path, "w" do |f| f.write to_zonefile end end |
#fqdn(domain_name) ⇒ String
This helper converts the given domain name or subdomain (e.g., ‘www`) fragment to a [fully qualified domain name (FQDN)](en.wikipedia.org/wiki/Fully_qualified_domain_name) for the zone’s #dns. If the argument is already a FQDN, it is returned unchanged.
724 725 726 |
# File 'lib/gcloud/dns/zone.rb', line 724 def fqdn domain_name Connection.fqdn domain_name, dns end |
#id ⇒ Object
Unique identifier for the resource; defined by the server.
64 65 66 |
# File 'lib/gcloud/dns/zone.rb', line 64 def id @gapi["id"] end |
#import(path_or_io, only: nil, except: nil, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Imports resource records from a [DNS zone file](en.wikipedia.org/wiki/Zone_file), adding the new records to the zone, without removing any existing records from the zone.
Because the Google Cloud DNS API only accepts a single resource record for each ‘name` and `type` combination (with multiple `data` elements), the zone file’s records are merged as necessary. During this merge, the lowest ‘ttl` of the merged records is used. If none of the merged records have a `ttl` value, the zone file’s global TTL is used for the record.
The zone file’s SOA and NS records are not imported, because the zone was given SOA and NS records when it was created. These generated records point to Cloud DNS name servers.
This operation automatically updates the SOA record serial number unless prevented with the ‘skip_soa` option. See #update for details.
The Google Cloud DNS service requires that record names and data use fully-qualified addresses. The @ symbol is not accepted, nor are unqualified subdomain addresses like www. If your zone file contains such values, you may need to pre-process it in order for the import operation to succeed.
435 436 437 438 439 440 441 |
# File 'lib/gcloud/dns/zone.rb', line 435 def import path_or_io, only: nil, except: nil, skip_soa: nil, soa_serial: nil except = (Array(except).map(&:to_s).map(&:upcase) + %w(SOA NS)).uniq importer = Gcloud::Dns::Importer.new self, path_or_io additions = importer.records only: only, except: except update additions, [], skip_soa: skip_soa, soa_serial: soa_serial end |
#modify(name, type, skip_soa: nil, soa_serial: nil) {|record| ... } ⇒ Gcloud::Dns::Change
Modifies records on the Zone. Records matching the ‘name` and `type` are yielded to the block where they can be modified.
This operation automatically updates the SOA record serial number unless prevented with the ‘skip_soa` option. See #update for details.
695 696 697 698 699 700 |
# File 'lib/gcloud/dns/zone.rb', line 695 def modify name, type, skip_soa: nil, soa_serial: nil existing = records(name, type).all.to_a updated = existing.map(&:dup) updated.each { |r| yield r } update updated, existing, skip_soa: skip_soa, soa_serial: soa_serial end |
#name ⇒ Object
User assigned name for this resource. Must be unique within the project. The name must be 1-32 characters long, must begin with a letter, end with a letter or digit, and only contain lowercase letters, digits or dashes.
74 75 76 |
# File 'lib/gcloud/dns/zone.rb', line 74 def name @gapi["name"] end |
#name_server_set ⇒ Object
Optionally specifies the NameServerSet for this ManagedZone. A NameServerSet is a set of DNS name servers that all host the same ManagedZones. Most users will leave this field unset.
106 107 108 |
# File 'lib/gcloud/dns/zone.rb', line 106 def name_server_set @gapi["nameServerSet"] end |
#name_servers ⇒ Object
Delegate your managed_zone to these virtual name servers; defined by the server.
97 98 99 |
# File 'lib/gcloud/dns/zone.rb', line 97 def name_servers Array(@gapi["nameServers"]) end |
#record(name, type, ttl, data) ⇒ Gcloud::Dns::Record Also known as: new_record
Creates a new, unsaved Record that can be added to a Zone.
358 359 360 |
# File 'lib/gcloud/dns/zone.rb', line 358 def record name, type, ttl, data Gcloud::Dns::Record.new fqdn(name), type, ttl, data end |
#records(name = nil, type = nil, token: nil, max: nil) ⇒ Array<Gcloud::Dns::Record> Also known as: find_records
Retrieves the list of records belonging to the zone. Records can be filtered by name and type. The name argument can be a subdomain (e.g., ‘www`) fragment for convenience, but notice that the retrieved record’s domain name is always fully-qualified.
330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/gcloud/dns/zone.rb', line 330 def records name = nil, type = nil, token: nil, max: nil ensure_connection! name = fqdn(name) if name resp = connection.list_records id, name, type, token: token, max: max if resp.success? Record::List.from_response resp, self else fail ApiError.from_response(resp) end end |
#remove(name, type, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Removes records from the Zone. The records are looked up before they are removed. In order to update existing records, or add and remove records in the same transaction, use #update.
This operation automatically updates the SOA record serial number unless prevented with the ‘skip_soa` option. See #update for details.
610 611 612 613 |
# File 'lib/gcloud/dns/zone.rb', line 610 def remove name, type, skip_soa: nil, soa_serial: nil update [], records(name, type).all.to_a, skip_soa: skip_soa, soa_serial: soa_serial end |
#replace(name, type, ttl, data, skip_soa: nil, soa_serial: nil) ⇒ Gcloud::Dns::Change
Replaces existing records on the Zone. Records matching the ‘name` and `type` are replaced. In order to update existing records, or add and delete records in the same transaction, use #update.
This operation automatically updates the SOA record serial number unless prevented with the ‘skip_soa` option. See #update for details.
652 653 654 655 656 |
# File 'lib/gcloud/dns/zone.rb', line 652 def replace name, type, ttl, data, skip_soa: nil, soa_serial: nil update [record(name, type, ttl, data)], records(name, type).all.to_a, skip_soa: skip_soa, soa_serial: soa_serial end |
#to_zonefile ⇒ Object
659 660 661 |
# File 'lib/gcloud/dns/zone.rb', line 659 def to_zonefile records.all.map(&:to_zonefile_records).flatten.join("\n") end |
#update(additions = [], deletions = [], skip_soa: nil, soa_serial: nil) {|tx| ... } ⇒ Gcloud::Dns::Change
Adds and removes Records from the zone. All changes are made in a single API request.
The best way to add, remove, and update multiple records in a single [transaction](cloud.google.com/dns/records) is with a block. See Transaction.
If the SOA record for the zone is not present in ‘additions` or `deletions` (and if present in one, it should be present in the other), it will be added to both, and its serial number will be incremented by adding `1`. This update to the SOA record can be prevented with the `skip_soa` option. To provide your own value or behavior for the new serial number, use the `soa_serial` option.
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/gcloud/dns/zone.rb', line 509 def update additions = [], deletions = [], skip_soa: nil, soa_serial: nil # Handle only sending in options if additions.is_a?(::Hash) && deletions.empty? && .empty? = additions additions = [] elsif deletions.is_a?(::Hash) && .empty? = deletions deletions = [] end additions = Array additions deletions = Array deletions if block_given? updater = Zone::Transaction.new self yield updater additions += updater.additions deletions += updater.deletions end to_add = additions - deletions to_remove = deletions - additions return nil if to_add.empty? && to_remove.empty? unless skip_soa || detect_soa(to_add) || detect_soa(to_remove) increment_soa to_add, to_remove, soa_serial end create_change to_add, to_remove end |