globodns_client 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd7ba5a0fefa7e06cabe7d77e6d71235c1c9feaf
4
- data.tar.gz: bb245192fe6ba4f17142106a0f6a68c98f6d816c
3
+ metadata.gz: 7300f512dc21e6fe0fbffe785909a011052f5f5b
4
+ data.tar.gz: 93251bb520a97c317a7094bcc8ca8f8f7ae0e6dd
5
5
  SHA512:
6
- metadata.gz: '0943026588a318eb62a88510f4b1cd7d3591b49b6be51eb58840e575a80870b625e0321b2e335d9c3d93dceb1b0ecab8eb0035435a3c5e1458282812b11bb20d'
7
- data.tar.gz: 0721e82fc8f5426ed2e85c2f4614b2793d980b07431cdb5b9d75e0bf42e0a41de3ad433bc69994a6c7caab173ae84f0e4d5f873202be408ff915d31de08b416f
6
+ metadata.gz: 017d974ca203a02c4156c1b14c94309f54c482db8fa503084dbf917de7b6a961c1b13e64fa2153d288c47cda017e6babe481ffe3a43a48733e000c4ab06d6e6a
7
+ data.tar.gz: 20e968a171d48e194bcc9e9fb77ffc5be5b1269da3311b1382f9e9c2e55d93e11de141c94b4e97682962c8fb4c4c2333dba85a3d99745841b96d382cedd337b6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.1](https://github.com/globocom/globodns-client-ruby/releases/tag/v0.2.1) (21/08/17)
4
+ ##### - Enhancements
5
+ * Added CNAME records operations (get and create)
6
+ * Function to get a domain (from a record (record + domain) name of any type or only the domain name)
7
+
3
8
  ## [0.2.0](https://github.com/globocom/globodns-client-ruby/releases/tag/v0.2.0) (25/01/17)
4
9
  ##### - Enhancements
5
10
  * Authenticantion using bearer_token
@@ -9,4 +14,4 @@
9
14
 
10
15
 
11
16
  ## [0.1.x]
12
- * Authentication using auth_token
17
+ * Authentication using auth_token
@@ -29,8 +29,9 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_dependency "rest-client"
31
31
 
32
- spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "bundler"
33
33
  spec.add_development_dependency "rake", "~> 10.0"
34
34
  spec.add_development_dependency "mocha"
35
35
  spec.add_development_dependency "test-unit"
36
+ spec.add_development_dependency "byebug"
36
37
  end
@@ -14,6 +14,21 @@ module GlobodnsClient
14
14
  @bearer_token = token
15
15
  end
16
16
 
17
+ def get_domain(key)
18
+ domain = key.dup
19
+ domain.chomp!('.')
20
+ res = {}
21
+ while domain.count('.') >= 1
22
+ res = request('get','domain', domain)
23
+ if !res.empty?
24
+ domain = ""
25
+ else
26
+ domain.gsub!(/^([a-zA-Z0-9\-_]*.)/,'')
27
+ end
28
+ end
29
+ res unless res.empty?
30
+ end
31
+
17
32
  def get_zone(key, kind = 'A')
18
33
  res = request('get','domain', key, nil, kind)
19
34
  if kind.eql?('A') && !res.empty?
@@ -24,7 +39,7 @@ module GlobodnsClient
24
39
  end
25
40
 
26
41
  def get_zone_recursive(key, kind = 'A')
27
- if kind.eql?('A')
42
+ if kind.eql?('A') or kind.eql?('CNAME')
28
43
  domain = key.split('.', 2).last
29
44
  elsif kind.eql?('PTR')
30
45
  if key.include?('in-addr.arpa')
@@ -38,7 +53,7 @@ module GlobodnsClient
38
53
  end
39
54
  res = request('get','domain', domain, nil, kind)
40
55
  if res.empty?
41
- if domain.count('.') > 1 && kind == 'A' || domain.count('.') > 2 && kind == 'PTR'
56
+ if domain.count('.') > 1 && (kind == 'A' or kind == 'CNAME') || domain.count('.') > 2 && kind == 'PTR'
42
57
  res = get_zone_recursive(domain, kind)
43
58
  else
44
59
  raise GlobodnsClient::NotFound, "Couldn't find a proper zone for '#{key}'"
@@ -47,27 +62,32 @@ module GlobodnsClient
47
62
  res
48
63
  end
49
64
 
50
- def get_record(key, kind, zones=[])
51
- zones.flatten!
65
+ def get_record(key, kind, zone)
66
+ zone.flatten!
52
67
  res = []
53
- zones = get_zone(key, kind) if zones.nil?
54
- zones.each do |zone|
55
- host = get_host(key, zone, kind)
56
- response = request('get', 'record', host, zone['domain']['id'], kind)
57
- response.each do |r|
58
- res << r[kind.downcase] unless r[kind.downcase].nil?
59
- end
68
+ zone = get_zone(key, kind) if zone.nil?
69
+ host = get_host(key, zone, kind)
70
+ response = request('get', 'record', host, zone[0][:domain][:id], kind)
71
+ response.each do |r|
72
+ res << r[kind.downcase.to_sym] unless r[kind.downcase.to_sym].nil?
60
73
  end
61
74
  res.empty? ? false : res
62
75
  end
63
76
 
64
77
  def new_record(key, kind, value)
65
- zone = get_zone(key, kind)
66
- if record = get_record(key, kind, zone)
67
- raise GlobodnsClient::AlreadyExists, "Item (#{key}) already exists with reference (#{record['content']})"
68
- else
78
+ zone = get_domain key
79
+ unless zone.nil?
80
+ a_records = get_record(key, 'A', zone)
81
+ case kind
82
+ when 'A'
83
+ raise GlobodnsClient::AlreadyExists, "Item (#{key}) already exists with reference (#{a_records[0][:content]})" if a_records.collect{|r| r[:content]}.include? value
84
+ when 'CNAME'
85
+ cname_records = get_record(key, 'CNAME', zone)
86
+ raise GlobodnsClient::AlreadyExists, "Item (#{key}) already exists" if a_records or cname_records
87
+ end
88
+
69
89
  host = get_host(key, zone, kind)
70
- response = request('post', 'record', host, zone['id'], kind, value)
90
+ response = request('post', 'record', host, zone[0][:domain][:id], kind, value)
71
91
  end
72
92
  begin
73
93
  schedule_export
@@ -78,30 +98,28 @@ module GlobodnsClient
78
98
 
79
99
  def delete_record(key, kind)
80
100
  zone = get_zone(key, kind)
81
- unless record = get_record(key, kind, zone)
101
+ unless records = get_record(key, kind, zone)
82
102
  raise GlobodnsClient::NotFound, "Record not found for (#{key})"
83
103
  end
84
- response = request('delete', 'record', nil, record['id'])
104
+ response=[]
105
+ records.each do |record|
106
+ response << request('delete', 'record', nil, record[:id])
107
+ end
85
108
  begin
86
109
  schedule_export
87
110
  rescue Exception
111
+ ensure
112
+ response
88
113
  end
89
- response
90
114
  end
91
115
 
92
116
  private
93
117
 
94
118
  def get_host(key, zone, kind)
95
- if kind.eql?('A')
96
- host = key.split('.'+zone['domain']['name'])[0]
97
- # não sei pq fiz esse bloco em baixo (if/else)
98
- # if key == zone['domain']['name']
99
- # host = nil
100
- # else
101
- # host = key.split('.'+zone['domain']['name'])[0]
102
- # end
119
+ if kind.eql?('A') or kind.eql?('CNAME')
120
+ host = key.split('.'+zone[0][:domain][:name])[0]
103
121
  elsif kind.eql?('PTR')
104
- case zone['domain']['name'].count('.')
122
+ case zone[0][:domain][:name].count('.')
105
123
  when 4, 5
106
124
  host = key.split('.').last
107
125
  when 3
@@ -132,10 +150,12 @@ module GlobodnsClient
132
150
 
133
151
  case method
134
152
  when 'get'
135
- if type.eql?('A')
153
+ if type.eql?('A') or type.eql?('CNAME')
136
154
  headers[:params] = {query: value}
137
155
  elsif type.eql?('PTR')
138
156
  headers[:params] = {query: value, reverse: true}
157
+ elsif kind.eql?('domain')
158
+ headers[:params] = {query: value}
139
159
  else
140
160
  raise "Not implemented"
141
161
  end
@@ -159,7 +179,7 @@ module GlobodnsClient
159
179
  if response.code < 200 || response.code > 399
160
180
  raise "Couldn't get a response from GloboDNS - code (#{response.code} / message #{response.body})"
161
181
  end
162
- method.eql?('delete') ? "" : JSON.parse(response.body)
182
+ method.eql?('delete') ? "" : JSON.parse(response.body, {:symbolize_names => true, :object_class => Hash})
163
183
  end
164
184
  end
165
185
 
@@ -1,3 +1,3 @@
1
1
  module GlobodnsClient
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globodns_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernesto Thorp
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-25 00:00:00.000000000 Z
11
+ date: 2017-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.10'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.10'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Simple client for GloboDNS API.
84
98
  email:
85
99
  - ernesto@corp.globo.com
@@ -123,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
137
  version: '0'
124
138
  requirements: []
125
139
  rubyforge_project:
126
- rubygems_version: 2.4.8
140
+ rubygems_version: 2.6.11
127
141
  signing_key:
128
142
  specification_version: 4
129
143
  summary: Simple client for GloboDNS API.