dapi-client 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3af6232965a19b8ed245ab347a0d82e0da95f8a686d653463fa9e48e9635c30d
4
+ data.tar.gz: d47460d0bbc734c3bb2df565d712f29fe010991ebb60c2c4a2381c348e352921
5
+ SHA512:
6
+ metadata.gz: 91508607d01994bd0d1a63f2775884f44624189c6d505e03f40d74fc00a3e988b9af9210dd8be62a1a9e5eecf955d8cdf9e093ef72efa05bf56cb70e08cf0950
7
+ data.tar.gz: 48e415ba642caf12a1d8753ca011d0661e13bd1a2bcb77a76fb80578039a9d299f04ca7f8377cf0c63d75df7dd95e55306be7d05cf7106d544a7fa2587368066
data/.dockerignore ADDED
@@ -0,0 +1 @@
1
+ /Gemfile.lock
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ /docker-compose.override.yml
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Dockerfile ADDED
@@ -0,0 +1,12 @@
1
+ FROM ruby:2.5
2
+
3
+ RUN mkdir /usr/src/app
4
+
5
+ WORKDIR /usr/local/src
6
+
7
+ ADD Gemfile dapi-client.gemspec ./
8
+ ADD lib/dapi/client/version.rb ./lib/dapi/client/version.rb
9
+ RUN bundle install --jobs=3 --retry=3
10
+ ADD . ./
11
+
12
+ CMD bundle exec rspec --force-color -fd spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dapi_client.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Dapi::Client
2
+
3
+ This is a very thin wrapper over the DAPI HTTP service, providing just enough for OPF.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dapi-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dapi-client
20
+
21
+ ## Usage
22
+
23
+ Example:
24
+
25
+ ```
26
+ dapi = Dapi::Client.new(url: "http://dapi/, token: "secret")
27
+ dapi.upsert_record("example.com", "www", "A", "172.16.0.1", 60)
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ To run the tests in an isolated docker-compose graph:
37
+
38
+ ```
39
+ docker-compose build test && docker-compose run test
40
+ ```
41
+
42
+ To bring dapi up on a local host port to run tests against it:
43
+
44
+ ```
45
+ # Terminal 1
46
+ ln -s docker-compose.local.yml docker-compose.override.yml
47
+ docker-compose up dapi
48
+
49
+ # Terminal 2
50
+ export DAPI_SERVER_TOKEN=test_blah_admin
51
+ export DAPI_SERVER_ROOT=http://localhost/
52
+ bundle exec rspec -fd spec
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/webnifico/dapi-client.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dapi_client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "dapi/client/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dapi-client"
8
+ spec.version = Dapi::Client::VERSION
9
+ spec.authors = ["Sheldon Hearn"]
10
+ spec.email = ["sheldonh@starjuice.net"]
11
+
12
+ spec.summary = %q{DNS API Client}
13
+ spec.description = %q{Client for Kernels DNS API over HTTP.}
14
+ spec.homepage = "https://github.com/webnifico/opf-manifold"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "http", "~> 4.1"
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.8"
29
+ end
@@ -0,0 +1,9 @@
1
+ version: '2.1'
2
+
3
+ services:
4
+ test:
5
+ volumes:
6
+ - .:/usr/src/app
7
+ dapi:
8
+ ports:
9
+ - "80:80"
@@ -0,0 +1,33 @@
1
+ version: '2.1'
2
+
3
+ services:
4
+ test:
5
+ build:
6
+ context: .
7
+ command: /bin/bash -c 'sleep 5 && bundle exec rspec -fd spec'
8
+ environment:
9
+ DAPI_SERVER_TOKEN: test_blah_admin
10
+ DAPI_SERVER_ROOT: http://dapi/
11
+ links:
12
+ - dapi
13
+ dapi:
14
+ image: gitlab.host-h.net:4567/seals/dapi:v3.17.0-staging
15
+ command: bundle exec rackup --server webrick -o 0.0.0.0 -p 80
16
+ environment:
17
+ ENVIRONMENT: test
18
+ DNS_CONFIG_FILE: /usr/local/src/conf/test/dns.yml
19
+ AUTH_CONFIG_FILE: /usr/local/src/conf/test/auth_tokens.yml
20
+ ROLES_CONFIG_FILE: /usr/local/src/conf/test/roles.yml
21
+ DEBUG: "true"
22
+ NSDB_PASSWORD_SA: secret
23
+ NSDB_PASSWORD_DE: secret
24
+ links:
25
+ - nsdb
26
+ expose:
27
+ - 80
28
+ nsdb:
29
+ image: gitlab.host-h.net:4567/seals/dapi:nsdb
30
+ expose:
31
+ - 53
32
+ - 5432
33
+
@@ -0,0 +1,13 @@
1
+ module Dapi
2
+
3
+ class Client
4
+
5
+ class Error < RuntimeError
6
+ end
7
+
8
+ class OptimisticLockFailure < Error
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,5 @@
1
+ module Dapi
2
+ class Client
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,75 @@
1
+ require "dapi/client/error"
2
+ require "http"
3
+
4
+ module Dapi
5
+
6
+ class Client
7
+
8
+ OPTIMISTIC_LOCKING_CHECK_WAIT = 1.0 unless defined?(OPTIMISTIC_LOCKING_CHECK_WAIT)
9
+ OPTIMISTIC_LOCKING_RETRIES = 10 unless defined?(OPTIMISTIC_LOCKING_RETRIES)
10
+
11
+ def initialize(url:, token:, http: HTTP)
12
+ @url = url
13
+ @http = http
14
+ @token = token
15
+ end
16
+
17
+ def get_zone(zone_name)
18
+ unwrap { http.get(@url + "zone/" + zone_name) }
19
+ end
20
+
21
+ def upsert_record(zone_name, lvalue, type, rvalue, ttl)
22
+ retries = OPTIMISTIC_LOCKING_RETRIES
23
+ begin
24
+ upsert_record!(zone_name, lvalue, type, rvalue, ttl)
25
+ rescue Dapi::Client::OptimisticLockFailure
26
+ if retries > 0
27
+ retries -= 1
28
+ sleep(0.1)
29
+ retry
30
+ else
31
+ raise
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def http
39
+ @http.auth(@token)
40
+ end
41
+
42
+ def unwrap(&block)
43
+ res = block.call
44
+ if res.status == 201
45
+ {}
46
+ else
47
+ jsend = JSON.parse(res.body)
48
+ if jsend["status"] == "success"
49
+ jsend["data"]
50
+ elsif jsend["message"]
51
+ raise Dapi::Client::Error, jsend["message"]
52
+ else
53
+ raise Dapi::Client::Error, "unknown DAPI error"
54
+ end
55
+ end
56
+ rescue JSON::ParserError
57
+ raise Dapi::Client::Error, "error parsing server response (JSON)"
58
+ end
59
+
60
+ def upsert_record!(zone_name, lvalue, type, rvalue, ttl)
61
+ zone = get_zone(zone_name)
62
+ zone["records"].reject! { |r| r["name"] == lvalue && r["type"] == type }
63
+ zone["records"] << {"name" => lvalue, "type" => type, "value" => rvalue, "ttl" => ttl}
64
+ unwrap { http.post(@url + "zone/" + zone_name, json: {data: zone}) }
65
+ sleep(OPTIMISTIC_LOCKING_CHECK_WAIT)
66
+ zone = get_zone(zone_name)
67
+ unless zone["records"].detect { |r| r["name"] == lvalue && r["type"] == type && r["value"] == rvalue && r["ttl"] == ttl }
68
+ raise Dapi::Client::OptimisticLockFailure, "upsert_record: optimistic locking failed"
69
+ end
70
+ zone
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,2 @@
1
+ require "dapi"
2
+ require "dapi/client"
data/lib/dapi.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Dapi
2
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dapi-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sheldon Hearn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.8'
69
+ description: Client for Kernels DNS API over HTTP.
70
+ email:
71
+ - sheldonh@starjuice.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".dockerignore"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Dockerfile
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - dapi-client.gemspec
86
+ - docker-compose.local.yml
87
+ - docker-compose.yml
88
+ - lib/dapi-client.rb
89
+ - lib/dapi.rb
90
+ - lib/dapi/client.rb
91
+ - lib/dapi/client/error.rb
92
+ - lib/dapi/client/version.rb
93
+ homepage: https://github.com/webnifico/opf-manifold
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.7.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: DNS API Client
116
+ test_files: []