Module: KongSchema::Client

Defined in:
lib/kong_schema/client.rb

Class Method Summary collapse

Class Method Details

.connect(config, &_) ⇒ Object

Configure Kong::Client to use the host specified in config for the duration of a proc.

Example:

KongSchema::Schema.connect({ 'admin_host' => '127.0.0.1:8001' }) do
  Kong::Api.all()
end


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kong_schema/client.rb', line 15

def self.connect(config, &_)
  api_url = Kong::Client.api_url
  admin_host = config['admin_host']

  if admin_host.nil?
    fail "Missing 'admin_host' property; can not connect to Kong admin!"
  end

  Kong::Client.api_url = "http://#{admin_host}"

  yield Kong::Client
ensure
  Kong::Client.api_url = api_url
end

.purge(config) ⇒ Object

Reset Kong’s database by removing all objects through the API.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kong_schema/client.rb', line 31

def self.purge(config)
  connect(config) do
    KongSchema::Resource::Plugin.all.each(&:delete)

    KongSchema::Resource::Upstream.all.each do |upstream|
      upstream.targets.each(&:delete)
      upstream.delete
    end

    KongSchema::Resource::Api.all.each(&:delete)
  end
end