Class: Gcloud::Dns::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/dns/connection.rb

Overview

Represents the connection to DNS, as well as expose the API calls.

Constant Summary collapse

API_VERSION =

:nodoc:

"v1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials) ⇒ Connection

Creates a new Connection instance.



32
33
34
35
36
37
38
39
# File 'lib/gcloud/dns/connection.rb', line 32

def initialize project, credentials #:nodoc:
  @project = project
  @credentials = credentials
  @client = Google::APIClient.new application_name:    "gcloud-ruby",
                                  application_version: Gcloud::VERSION
  @client.authorization = @credentials.client
  @dns = @client.discovered_api "dns", API_VERSION
end

Instance Attribute Details

#credentialsObject

:nodoc:



28
29
30
# File 'lib/gcloud/dns/connection.rb', line 28

def credentials
  @credentials
end

#projectObject

Returns the value of attribute project.



27
28
29
# File 'lib/gcloud/dns/connection.rb', line 27

def project
  @project
end

Class Method Details

.fqdn(name, origin_dns) ⇒ Object

Fully Qualified Domain Name



139
140
141
142
143
144
145
146
147
# File 'lib/gcloud/dns/connection.rb', line 139

def self.fqdn name, origin_dns #:nodoc:
  name = name.to_s.strip
  return name if self.ip_addr? name
  name = origin_dns if name.empty?
  name = origin_dns if name == "@"
  name = "#{name}.#{origin_dns}" unless name.include? "."
  name = "#{name}." unless name.end_with? "."
  name
end

.ip_addr?(name) ⇒ Boolean

:nodoc:



153
154
155
156
157
158
# File 'lib/gcloud/dns/connection.rb', line 153

def self.ip_addr? name #:nodoc:
  IPAddr.new name
  true
rescue IPAddr::Error
  false
end

Instance Method Details

#create_change(zone_id, additions, deletions) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gcloud/dns/connection.rb', line 111

def create_change zone_id, additions, deletions
  change = { "kind"      => "dns#change",
             "additions" => Array(additions),
             "deletions" => Array(deletions) }

  @client.execute(
    api_method: @dns.changes.create,
    parameters: { project: @project, managedZone: zone_id },
    body_object: change
  )
end

#create_zone(zone_name, zone_dns, description: nil, name_server_set: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gcloud/dns/connection.rb', line 67

def create_zone zone_name, zone_dns, description: nil,
                name_server_set: nil
  body = { kind: "dns#managedZone",
           name: zone_name, dnsName: zone_dns,
           description: (description || ""),
           nameServerSet: name_server_set
         }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @dns.managed_zones.create,
    parameters: { project: @project },
    body_object: body
  )
end

#delete_zone(zone_id) ⇒ Object



82
83
84
85
86
87
# File 'lib/gcloud/dns/connection.rb', line 82

def delete_zone zone_id
  @client.execute(
    api_method: @dns.managed_zones.delete,
    parameters: { project: @project, managedZone: zone_id }
  )
end

#get_change(zone_id, change_id) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/gcloud/dns/connection.rb', line 89

def get_change zone_id, change_id
  @client.execute(
    api_method: @dns.changes.get,
    parameters: { project: @project, managedZone: zone_id,
                  changeId: change_id }
  )
end

#get_project(project_id = @project) ⇒ Object



41
42
43
44
45
46
# File 'lib/gcloud/dns/connection.rb', line 41

def get_project project_id = @project
  @client.execute(
    api_method: @dns.projects.get,
    parameters: { project: project_id }
  )
end

#get_zone(zone_id) ⇒ Object



48
49
50
51
52
53
# File 'lib/gcloud/dns/connection.rb', line 48

def get_zone zone_id
  @client.execute(
    api_method: @dns.managed_zones.get,
    parameters: { project: @project, managedZone: zone_id }
  )
end

#inspectObject

:nodoc:



160
161
162
# File 'lib/gcloud/dns/connection.rb', line 160

def inspect #:nodoc:
  "#{self.class}(#{@project})"
end

#list_changes(zone_id, token: nil, max: nil, order: nil, sort: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gcloud/dns/connection.rb', line 97

def list_changes zone_id, token: nil, max: nil, order: nil, sort: nil
  params = { project: @project, managedZone: zone_id,
             pageToken: token,
             maxResults: max,
             sortBy: sort,
             sortOrder: order
           }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @dns.changes.list,
    parameters: params
  )
end

#list_records(zone_id, name = nil, type = nil, token: nil, max: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gcloud/dns/connection.rb', line 123

def list_records zone_id, name = nil, type = nil, token: nil, max: nil
  params = { project: @project, managedZone: zone_id,
             pageToken: token,
             maxResults: max,
             name: name,
             type: type
           }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @dns.resource_record_sets.list,
    parameters: params
  )
end

#list_zones(token: nil, max: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gcloud/dns/connection.rb', line 55

def list_zones token: nil, max: nil
  params = { project: @project,
             pageToken: token,
             maxResults: max
           }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @dns.managed_zones.list,
    parameters: params
  )
end