cloud_party 0.1.5 → 0.1.6

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
  SHA256:
3
- metadata.gz: 89606fd3a44695c89167d12280241e0e41bf50639bd833d1821a429387e5b5e0
4
- data.tar.gz: 5bb0758290a653ccf6660119f65f11e8b31b104b6872cd1cd62f8a5bdb4cae26
3
+ metadata.gz: 1a99a96cb3a4dd7c2f4586ea783603c598023b29adb568f307cdfa2ffee6d479
4
+ data.tar.gz: 346df770759f10809a2e84cee9cb56b87808cb10d7efba62273d7eb0fb0d51e9
5
5
  SHA512:
6
- metadata.gz: ab996b258d91496f5701b296f9c46241a30a5e710a2c655b1917fee209876eb3329f0fffa370168cca0e6fbd72982fa3022c8b378faf6b13ab13ad91f71d8cb1
7
- data.tar.gz: b2dc08ecf21e5672346a8ffecdbd46eb656348d146e2f868962c9f420f960457424f86d4cdbbf39537060e8b891204bf82150e9d473f0557f8f4baa7e891ecb5
6
+ metadata.gz: 8de5deacb4758570d871028a0feae56cfe9a10fc0c4baaefd31f639d9fda14a4b084a635b4f9b46c059a31dc84ca3881a2ed4d5de716447b61afeb477c496ec0
7
+ data.tar.gz: 4c727ffdf69646101e242c7ac215b54afd2af0d7c33266166f882eacc8c779ff862743f3c2f0601425ea26466aeee82f6bbcef27d783e2825e7ff63c3f470b6e
@@ -10,26 +10,44 @@ module CloudParty
10
10
  end
11
11
  end
12
12
  class RequestError < StandardError
13
- def initialize(obj:, method:, code:, response:, endpoint:)
14
- @obj = obj
13
+ def initialize(message, method, endpoint, code, body)
14
+ super(message)
15
15
  @method = method
16
16
  @endpoint = endpoint
17
17
  @code = code
18
- @response = response
18
+ @body = body
19
19
  end
20
20
 
21
21
  def to_s
22
22
  [error_string.squish, extra_string].join("\n")
23
23
  end
24
24
 
25
- def self.extra_string
25
+ def extra_string
26
26
  # This method should be overridden
27
27
  end
28
28
 
29
29
  # override error_string to provide your own error_string
30
- def self.error_string
30
+ def error_string
31
31
  # This method should be overridden
32
32
  end
33
33
  end
34
+ class InputError < ArgumentError
35
+ attr_reader :input, :obj
36
+ # @param [String] message message
37
+ # @param [Object] obj object
38
+ # @param [String] input input string
39
+ def initialize(message, obj, input)
40
+ super(message)
41
+ @obj = obj
42
+ @input = input
43
+ end
44
+ end
45
+ class NoDefinedZoneError < StandardError
46
+ attr_reader :obj
47
+ def initialize(message, obj)
48
+ super(message)
49
+ @obj = obj
50
+ end
51
+ end
34
52
  end
35
53
  end
@@ -17,11 +17,11 @@ module CloudParty
17
17
  end
18
18
 
19
19
  class UnknownError < RequestError
20
- def initialize(obj:, method:, response:, endpoint: nil, code:)
20
+ def initialize(message, method, endpoint, code, response)
21
21
  super
22
22
  end
23
23
 
24
- def self.error_string
24
+ def error_string
25
25
  <<~HEREDOC
26
26
  An error with the request has occurred, please make
27
27
  sure the method verb, endpoint, and credentials are
@@ -29,14 +29,14 @@ module CloudParty
29
29
  HEREDOC
30
30
  end
31
31
 
32
- def self.extra_string
32
+ def extra_string
33
33
  <<~HEREDOC
34
34
  Credentials Context: #{@obj&.class&.cfg}
35
35
 
36
36
  Method Verb: #{@method}
37
37
  Endpoint: #{@endpoint}
38
38
  HTTP Status Code: #{@code}
39
- Response Body: #{@response.body}
39
+ Response Body: #{@response&.body}
40
40
  HEREDOC
41
41
  end
42
42
  end
@@ -6,5 +6,6 @@ module CloudParty
6
6
  autoload :Memberships, 'cloud_party/nodes/memberships'
7
7
  autoload :IPs, 'cloud_party/nodes/ips'
8
8
  autoload :Zones, 'cloud_party/nodes/zones'
9
+ autoload :DNSRecords, 'cloud_party/nodes/dns_records'
9
10
  end
10
11
  end
@@ -0,0 +1,99 @@
1
+ #
2
+ # Copyright 2019 Ken Spencer / IotaSpencer
3
+ #
4
+ #
5
+ # File: lib/cloud_party/nodes/dns_records
6
+ # Created: 9/14/19
7
+ #
8
+ # License is in project root, MIT License is in use.
9
+ require 'cloud_party/context'
10
+ require 'cloud_party/responses'
11
+ module CloudParty
12
+ module Nodes
13
+ class DNSRecords
14
+ include CloudParty::Context
15
+ include HTTParty
16
+ base_uri 'api.cloudflare.com:443/client/v4'
17
+ headers 'X-Auth-Email' => cfg.email,
18
+ 'X-Auth-Key' => cfg.api_key,
19
+ 'Content-Type' => 'application/json',
20
+ 'User-Agent' => "CloudParty/#{CloudParty::VERSION}"
21
+
22
+ def self.id_by_name(zone)
23
+ options = {
24
+ match: 'all',
25
+ name: zone,
26
+ order: 'name'
27
+ }
28
+ if @options.nil?
29
+ @options = options
30
+ else
31
+ @options.merge!(options)
32
+ end
33
+ zone = CloudParty::Responses::Zones.new(:get, '/zones', get('/zones', query: @options), @options).result
34
+ if zone.is_a?(Array)
35
+ if zone.size > 1
36
+ raise CloudParty::Errors::ResultError.new()
37
+ else
38
+ zone.first.fetch(:id, nil)
39
+
40
+ end
41
+ end
42
+
43
+ end
44
+ def initialize(options = {})
45
+ super()
46
+ @options = options
47
+ end
48
+
49
+ def list
50
+ CloudParty::Responses::DNSRecords.new(:get, '/zones/:id/dns_records', self.class.get("/zones/#{@@zone}/dns_records", @options), @options)
51
+ end
52
+
53
+ def get(id)
54
+ CloudParty::Responses::DNSRecords.new(:get, '/zones/:id/dns_records', self.class.get("/zones/#{@@zone}/dns_records", @options), @options)
55
+ end
56
+
57
+ def add(type, name, content, opts, zone:)
58
+ zone_id = nil
59
+ options = {
60
+ type: type,
61
+ name: name,
62
+ content: content
63
+ }
64
+ ttl = opts.fetch('ttl', nil)
65
+ priority = opts.fetch('priority', nil)
66
+ proxied = opts.fetch('proxied', nil)
67
+ options.merge!(ttl: ttl) unless ttl.nil?
68
+ options.merge!(priority: priority) unless priority.nil?
69
+ options.merge!(proxied: proxied) unless proxied.nil?
70
+ if zone
71
+ zone_options = {
72
+ match: 'all',
73
+ name: zone,
74
+ order: 'name'
75
+ }
76
+ zone_id = CloudParty::Responses::Zones.new(:get, '/zones', self.class.get('/zones', query: zone_options), @options).result.first.fetch(:id, nil)
77
+ elsif self.class.class_variable_defined?(:@@zone)
78
+ zone_id = @@zone
79
+ else
80
+ raise CloudParty::Errors::NoDefinedZoneError.new("neither the keyword 'zone:' nor the class variable @@zone ended up being defined.", nil)
81
+ end
82
+
83
+ CloudParty::Responses::DNSRecords.new(
84
+ :post,
85
+ '/zones/:id/dns_records',
86
+ self.class.post("/zones/#{zone_id}/dns_records", body: options.to_json),
87
+ @options)
88
+ end
89
+ def rem(id, zone: nil)
90
+ zone_id = id_by_name(zone)
91
+ CloudParty::Responses::DNSRecords.new(
92
+ :delete,
93
+ '/zones/:id/dns_records/:identifier',
94
+ self.class.delete("/zones/#{zone_id}")
95
+ )
96
+ end
97
+ end
98
+ end
99
+ end
@@ -39,35 +39,6 @@ module CloudParty
39
39
  def get(id)
40
40
  CloudParty::Responses::Zones.new(:get, '/zones/:id', self.class.get("/zones/#{id}"), @options)
41
41
  end
42
-
43
- def add_record(type, name, content, opts, zone:)
44
- zone_id = nil
45
- options = {
46
- type: type,
47
- name: name,
48
- content: content
49
- }
50
- ttl = opts.fetch('ttl', nil)
51
- priority = opts.fetch('priority', nil)
52
- proxied = opts.fetch('proxied', nil)
53
- options.merge!(ttl: ttl) unless ttl.nil?
54
- options.merge!(priority: priority) unless priority.nil?
55
- options.merge!(proxied: proxied) unless proxied.nil?
56
- if zone
57
- zone_options = {
58
- match: 'all',
59
- name: zone,
60
- order: 'name'
61
- }
62
- zone_id = CloudParty::Responses::Zones.new(:get, '/zones', get('/zones', query: zone_options), @options).result.first.fetch(:id, nil)
63
- end
64
-
65
- CloudParty::Responses::Zones.new(
66
- :post,
67
- '/zones/',
68
- self.class.get("/zones/#{@@zone || zone_id}/dns_records", options),
69
- @options)
70
- end
71
42
  end
72
43
  end
73
44
  end
@@ -10,6 +10,7 @@ module CloudParty
10
10
  autoload :Memberships, 'cloud_party/responses/memberships'
11
11
  autoload :IPs, 'cloud_party/responses/ips'
12
12
  autoload :Zones, 'cloud_party/responses/zones'
13
+ autoload :DNSRecords, 'cloud_party/responses/dns_records'
13
14
 
14
15
  module ResponseMethods
15
16
 
@@ -0,0 +1,189 @@
1
+ require 'cloud_party/responses/nodes/dns_records'
2
+ require 'cloud_party/exception'
3
+ require 'cloud_party/exceptions'
4
+ module CloudParty
5
+ module Responses
6
+ # '/zones' endpoint response object
7
+ class DNSRecords
8
+ include CloudParty::Response
9
+ # @raise [CloudParty::APIError] if the library encountered
10
+ # an error and {#successful?} is false
11
+ # @raise [CloudParty::UnRecognizedEndpointError] if the library encountered
12
+ # an unknown endpoint for this class
13
+ def initialize(method_name, endpoint, response, options)
14
+ @code = response.code
15
+ @body = JSON.parse(response.body, symbolize_names: true)
16
+ @parsed_response = response.parsed_response
17
+ @success = @body[:success]
18
+ unless successful?
19
+ message = <<~MESSAGE
20
+ Unable to #{method_name.to_s.upcase} to endpoint:
21
+ #{endpoint}. Inspect CloudParty::APIError#response
22
+ for further details
23
+ MESSAGE
24
+ raise CloudParty::Errors::APIError.new(message, response)
25
+ end
26
+ @results = []
27
+ if endpoint =~ /^\/zones\/:id\/dns_records\/?$/
28
+ @body[:result].each do |res|
29
+ @results << CloudParty::Responses::Result.new(res)
30
+ end
31
+ elsif endpoint =~ /^\/zones\/:id\/dns_records\/:identifier\/?$/
32
+ if method_name == :get
33
+ @results = CloudParty::Responses::Result.new(@body[:result])
34
+ end
35
+ else
36
+ raise Errors::UnRecognizedEndpointError.new(endpoint, self.class)
37
+ end
38
+ if endpoint =~ /^\/zones\/:id\/dns_records\/import\/?$/
39
+ if @body.fetch(:timing, nil)
40
+ @timing = CloudParty::Responses::Timing.new(@body[:timing])
41
+ end
42
+ @errors = []
43
+ @body[:errors].each do |err|
44
+ @errors << CloudParty::Responses::Error.new(err)
45
+ end
46
+ @messages = []
47
+ @body[:messages].each do |msg|
48
+ @messages << CloudParty::Responses::Message.new(msg)
49
+ end
50
+ end
51
+ end
52
+
53
+ attr_reader :errors
54
+ attr_reader :messages
55
+ attr_reader :results
56
+
57
+ def successful?
58
+ @success
59
+ end
60
+
61
+ alias success successful?
62
+
63
+ def result
64
+ if check_result_type(@body[:result]) == 'Array'
65
+ CloudParty::Responses::Result.new(@body[:result].first)
66
+ elsif check_result_type(@body[:result]) == 'Hash'
67
+ CloudParty::Responses::Result.new(@body[:result])
68
+ else
69
+ raise CloudParty::Errors::UnRecognizedResultTypeError.new(@body[:result].class)
70
+ end
71
+ end
72
+
73
+ def inspect
74
+ wanted_methods = %i[success messages errors results]
75
+ our_methods = methods.select do |m|
76
+ wanted_methods.include? m
77
+ end
78
+ outputs = []
79
+ our_methods.sort.each do |m|
80
+ outputs << "#{m}=#{send(m)}"
81
+ end
82
+ "#<Response: #{outputs.join(', ')}>"
83
+ end
84
+
85
+ def to_s
86
+ inspect
87
+ end
88
+ end
89
+
90
+ class Result
91
+ attr :id, :name, :type, :content, :proxiable, :proxied, :ttl, :locked, :zone_id, :zone_name, :created_on, :modified_on, :meta
92
+
93
+ def initialize(result)
94
+ @result = result
95
+ @result.each do |k, v|
96
+ instance_variable_set(:"@#{k}", v) unless %i[created_on modified_on].include?(k)
97
+ end
98
+ date_methods = %i[created_on modified_on]
99
+ date_methods.each do |date|
100
+ instance_variable_set(:"@#{date}", DateTime.parse(@result[date]))
101
+ end
102
+ end
103
+
104
+
105
+ def inspect
106
+ unchanged = %i[proxiable proxied locked]
107
+ strings = %i[id name type content zone_id zone_name]
108
+ date_methods = %i[created_on modified_on]
109
+ outputs = []
110
+ unchanged.each do |m|
111
+ outputs << "#{m}=#{send(m)}"
112
+ end
113
+ strings.each do |string|
114
+ outputs << "#{string}='#{send(string)}'"
115
+ end
116
+ date_methods.each do |date|
117
+ outputs << "#{date}=<#{send(date)}>"
118
+ end
119
+ "#<Result #{outputs.join(', ')}>"
120
+ end
121
+
122
+ def to_s
123
+ inspect
124
+ end
125
+ end
126
+ class Error
127
+ def initialize(error)
128
+ @error = error
129
+ @code = error.fetch(:code, nil)
130
+ @message = error.fetch(:message, nil)
131
+ end
132
+
133
+ attr_reader :code
134
+
135
+ attr_reader :message
136
+
137
+ def inspect
138
+ to_s
139
+ end
140
+
141
+ def to_s
142
+ wanted_methods = %i[code message]
143
+ our_methods = methods.select do |m|
144
+ wanted_methods.include? m
145
+ end
146
+ outputs = []
147
+ our_methods.each do |m|
148
+ outputs << "#{m}=#{send(m)}"
149
+ end
150
+ "#<Error: #{output.join(', ')}>"
151
+ end
152
+ end
153
+ class Message
154
+ def initialize(message)
155
+ @message = message
156
+ end
157
+
158
+ def inspect
159
+ to_s
160
+ end
161
+
162
+ def to_s
163
+ @messages.join(', ')
164
+ end
165
+ end
166
+ class Timing
167
+ attr_reader :start_time, :end_time, :process_time
168
+
169
+ def initialize(hsh)
170
+ @entries = []
171
+ hsh.each do |key, value|
172
+ @entries << "#{key}=#{value}"
173
+ end
174
+ start_time = DateTime.iso8601(hsh.dig(:start_time))
175
+ end_time = DateTime.iso8601(hsh.dig(:end_time))
176
+ process_time = hsh.dig(:process_time).to_i
177
+
178
+ end
179
+
180
+ def to_s
181
+ "#<Timing: #{@entries}>"
182
+ end
183
+
184
+ def inspect
185
+ to_s
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,2 @@
1
+ require 'cloud_party/responses/nodes/dns_records/timing'
2
+ require 'cloud_party/responses/nodes/dns_records/meta'
@@ -0,0 +1,25 @@
1
+ module CloudParty
2
+ module Responses
3
+ module Node
4
+ class Meta
5
+ attr_reader :auto_added, :managed_by_apps, :managed_by_argo_tunnel
6
+ def initialize(hsh)
7
+ @entries = []
8
+ hsh.each do |key, value|
9
+ @entries << "#{key}=#{value}"
10
+ end
11
+ auto_added = DateTime.iso8601(hsh.dig(:auto_added))
12
+ managed_by_apps = DateTime.iso8601(hsh.dig(:managed_by_apps))
13
+ managed_by_argo_tunnel = hsh.dig(:managed_by_argo_tunnel)
14
+
15
+ end
16
+ def to_s
17
+ "#<Meta: #{@entries}>"
18
+ end
19
+ def inspect
20
+ to_s
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -26,13 +26,13 @@ module CloudParty
26
26
  raise CloudParty::Errors::APIError.new(message, response)
27
27
  end
28
28
  @results = []
29
- case endpoint
30
- when '/zones'
31
-
29
+ if endpoint =~ /^\/zones\/?$/
32
30
  @body[:result].each do |res|
33
31
  @results << CloudParty::Responses::Result.new(res)
34
32
  end
35
- when '/zones/:id'
33
+ elsif endpoint =~ /^\/zones\/:id\/dns_records\/?$/
34
+ raise CloudParty::Errors::RequestError.new("Use CloudParty::Nodes::DNSRecords for this endpoint.", method_name, endpoint, @code, nil)
35
+ elsif endpoint =~ /^\/zones\/:id\/?$/
36
36
  @result = CloudParty::Responses::Result.new(@body[:result])
37
37
  @results << @result
38
38
  else
@@ -81,14 +81,14 @@ module CloudParty
81
81
  end
82
82
  end
83
83
  class Result
84
- attr_reader :id, :name, :development_mode, :original_registar, :original_dnshost, :status, :paused, :type, :permissions
84
+ attr_reader :id, :name, :development_mode, :original_registar, :original_dnshost, :status, :paused, :type, :permissions, :content
85
85
  def initialize(result)
86
86
  @result = result
87
87
  @result.each do |k, v|
88
- @plan = CloudParty::Responses::Node::Plan.new(@result.dig(:plan))
89
- @plan_pending = CloudParty::Responses::Node::PlanPending.new(@result.dig(:plan_pending))
90
- @account = CloudParty::Responses::Node::Account.new(@result.dig(:account))
91
- @permissions = CloudParty::Responses::Node::Permissions.new(@result.dig(:permissions))
88
+ @plan = CloudParty::Responses::Node::Plan.new(@result.dig(:plan)) if @result.fetch(:plan, nil)
89
+ @plan_pending = CloudParty::Responses::Node::PlanPending.new(@result.dig(:plan_pending)) if @result.fetch(:plan_pending, nil)
90
+ @account = CloudParty::Responses::Node::Account.new(@result.dig(:account)) if @result.fetch(:account, nil)
91
+ @permissions = CloudParty::Responses::Node::Permissions.new(@result.dig(:permissions)) if @result.fetch(:permissions, nil)
92
92
  instance_variable_set(:"@#{k}", v) unless %i[plan plan_pending account permissions].include?(k)
93
93
  end
94
94
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CloudParty
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloud_party
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Spencer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-10 00:00:00.000000000 Z
11
+ date: 2019-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -210,16 +210,20 @@ files:
210
210
  - lib/cloud_party/exceptions/un_recognized_result_type_error.rb
211
211
  - lib/cloud_party/nodes.rb
212
212
  - lib/cloud_party/nodes/accounts.rb
213
+ - lib/cloud_party/nodes/dns_records.rb
213
214
  - lib/cloud_party/nodes/ips.rb
214
215
  - lib/cloud_party/nodes/memberships.rb
215
216
  - lib/cloud_party/nodes/zones.rb
216
217
  - lib/cloud_party/response.rb
217
218
  - lib/cloud_party/responses.rb
218
219
  - lib/cloud_party/responses/accounts.rb
220
+ - lib/cloud_party/responses/dns_records.rb
219
221
  - lib/cloud_party/responses/ips.rb
220
222
  - lib/cloud_party/responses/memberships.rb
221
223
  - lib/cloud_party/responses/nodes.rb
222
224
  - lib/cloud_party/responses/nodes/accounts.rb
225
+ - lib/cloud_party/responses/nodes/dns_records.rb
226
+ - lib/cloud_party/responses/nodes/dns_records/meta.rb
223
227
  - lib/cloud_party/responses/nodes/memberships.rb
224
228
  - lib/cloud_party/responses/nodes/memberships/account.rb
225
229
  - lib/cloud_party/responses/nodes/memberships/permissions.rb