dapi-client 0.1.2 → 0.1.3

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: a9475f64c82e86742b38333c59b329a2a2f0873920cfa99a46f2ef0b099ea176
4
- data.tar.gz: 7fcd29bf890170ba4f7f42b17849cb36538b1e43d81dfd8122978dcdb0dd79aa
3
+ metadata.gz: 9e0f07cc669169a046a40ea32e1560801a51711ae4b4a242b177c302daf9915d
4
+ data.tar.gz: 30d5902bab99804b96080253cc3253f1cec060a885771e29ee91f12b48b23b77
5
5
  SHA512:
6
- metadata.gz: 0253ccf3080e0b77b994b9d5035193ac7f04e2df77be52c6679a47e9f9e8b6d4a8716f50bc0c03ea52f8ac552c04822ebd7759ac14ccfe1cd9fc58185b4f004a
7
- data.tar.gz: 51ef22b2d7a6619130382a25c176499d5ead0768541d75516d999de9c6a854f00017cbc2d455457f35ac6c74fd7c394bf821d2ff961ecad24a3177a63782779c
6
+ metadata.gz: 63264c21d46017aaf1739c314982b2705dc9583f8afd12d6f8cdfc9cec0582bad26d2fe317986d271a6a4ce63f5e89b4b91386a76e6062f26ca6abd8aa48d4b0
7
+ data.tar.gz: 7f0b4a9e75714a82300ee1483b339eb64ee1b50c506efb3e4d21dc4132c1534c981011c7197a618ebd66a38e9bc65da8a906838e4744e483d6dca1e9ebe9106a
data/README.md CHANGED
@@ -36,7 +36,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
36
36
  To run the tests in an isolated docker-compose graph:
37
37
 
38
38
  ```
39
- docker-compose build test && docker-compose run test
39
+ docker-compose down --volumes --remove-orphans && \
40
+ docker-compose build test && \
41
+ docker-compose run test
40
42
  ```
41
43
 
42
44
  To bring dapi up on a local host port to run tests against it:
@@ -8,14 +8,20 @@ module Dapi
8
8
  OPTIMISTIC_LOCKING_CHECK_WAIT = 3.0 unless defined?(OPTIMISTIC_LOCKING_CHECK_WAIT)
9
9
  OPTIMISTIC_LOCKING_RETRIES = 5 unless defined?(OPTIMISTIC_LOCKING_RETRIES)
10
10
 
11
- def initialize(url:, token:, http: HTTP)
11
+ def initialize(url:, token:, http: HTTP, check_wait: OPTIMISTIC_LOCKING_CHECK_WAIT, retries: OPTIMISTIC_LOCKING_RETRIES)
12
12
  @url = url
13
13
  @http = http
14
14
  @token = token
15
+ @check_wait = check_wait
16
+ @retries = retries
15
17
  end
16
18
 
17
19
  def delete_record(zone_name, lvalue, type)
18
- with_optimistic_locking { delete_record!(zone_name, lvalue, type) }
20
+ modify_zone_records(
21
+ zone_name,
22
+ modify: ->(records) { records.reject { |r| r["name"] == lvalue && r["type"] == type } },
23
+ check: ->(records) { not records.detect { |r| r["name"] == lvalue && r["type"] == type } }
24
+ )
19
25
  end
20
26
 
21
27
  def get_zone(zone_name)
@@ -24,15 +30,38 @@ module Dapi
24
30
 
25
31
  # Useful for test suites, and staging domains
26
32
  def truncate_zone(zone_name)
27
- with_optimistic_locking { truncate_zone!(zone_name) }
33
+ modify_zone_records(
34
+ zone_name,
35
+ modify: ->(records) { records.reject { |r| r["type"] == "A" && r["name"] != "@" } },
36
+ check: ->(records) { not records.detect { |r| r["type"] == "A" && r["name"] != "@" } }
37
+ )
28
38
  end
29
39
 
30
- def upsert_record(zone_name, lvalue, type, rvalue, ttl)
31
- with_optimistic_locking { upsert_record!(zone_name, lvalue, type, rvalue, ttl) }
40
+ def upsert_record(zone_name, lvalue, type, rvalue, ttl = nil)
41
+ modify_zone_records(
42
+ zone_name,
43
+ modify: ->(records) {
44
+ records.
45
+ reject { |r| r["name"] == lvalue && r["type"] == type }.
46
+ push("name" => lvalue, "type" => type, "value" => rvalue, "ttl" => ttl)
47
+ },
48
+ check: ->(records) { records.detect { |r| r["name"] == lvalue && r["type"] == type && r["value"] == rvalue && r["ttl"] == ttl } }
49
+ )
32
50
  end
33
51
 
34
52
  private
35
53
 
54
+ def modify_zone_records(zone_name, modify:, check:)
55
+ with_optimistic_locking do
56
+ zone = get_zone(zone_name)
57
+ zone["records"] = modify.call(zone["records"])
58
+ unwrap { http.post(@url + "zone/" + zone_name, json: {data: zone}) }
59
+ optimistic_lock_check(zone_name) do |modified|
60
+ check.call(modified["records"])
61
+ end
62
+ end
63
+ end
64
+
36
65
  def http
37
66
  @http.auth(@token)
38
67
  end
@@ -55,43 +84,15 @@ module Dapi
55
84
  raise Dapi::Client::Error, "error parsing server response (JSON)"
56
85
  end
57
86
 
58
- def delete_record!(zone_name, lvalue, type)
59
- zone = get_zone(zone_name)
60
- zone["records"].reject! { |r| r["name"] == lvalue && r["type"] == type }
61
- unwrap { http.post(@url + "zone/" + zone_name, json: {data: zone}) }
62
- optimistic_lock_check(zone_name) do |check|
63
- not check["records"].detect { |r| r["name"] == lvalue && r["type"] == type }
64
- end
65
- end
66
-
67
- def truncate_zone!(zone_name)
68
- zone = get_zone(zone_name)
69
- zone["records"].reject! { |r| r["type"] == "A" && r["name"] != "@" }
70
- unwrap { http.post(@url + "zone/" + zone_name, json: {data: zone}) }
71
- optimistic_lock_check(zone_name) do |check|
72
- not check["records"].detect { |r| r["type"] == "A" && r["name"] != "@" }
73
- end
74
- end
75
-
76
- def upsert_record!(zone_name, lvalue, type, rvalue, ttl)
77
- zone = get_zone(zone_name)
78
- zone["records"].reject! { |r| r["name"] == lvalue && r["type"] == type }
79
- zone["records"] << {"name" => lvalue, "type" => type, "value" => rvalue, "ttl" => ttl}
80
- unwrap { http.post(@url + "zone/" + zone_name, json: {data: zone}) }
81
- optimistic_lock_check(zone_name) do |check|
82
- check["records"].detect { |r| r["name"] == lvalue && r["type"] == type && r["value"] == rvalue && r["ttl"] == ttl }
83
- end
84
- end
85
-
86
87
  def optimistic_lock_check(zone_name, &block)
87
- sleep(OPTIMISTIC_LOCKING_CHECK_WAIT)
88
+ sleep(@check_wait)
88
89
  get_zone(zone_name).tap do |zone|
89
90
  raise Dapi::Client::OptimisticLockFailure, "upsert_record: optimistic locking failed" unless block.call(zone)
90
91
  end
91
92
  end
92
93
 
93
94
  def with_optimistic_locking(&block)
94
- retries = OPTIMISTIC_LOCKING_RETRIES
95
+ retries = @retries
95
96
  begin
96
97
  block.call
97
98
  rescue Dapi::Client::OptimisticLockFailure
@@ -105,7 +106,6 @@ module Dapi
105
106
  end
106
107
  end
107
108
 
108
-
109
109
  end
110
110
 
111
111
  end
@@ -1,5 +1,5 @@
1
1
  module Dapi
2
2
  class Client
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dapi-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sheldon Hearn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-12 00:00:00.000000000 Z
11
+ date: 2019-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http