Module: Octo::Helpers::KongHelper

Included in:
Authorization, KongBridge
Defined in:
lib/octocore/helpers/kong_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_ratelimiting_plugin(apikey, consumer_id, config) ⇒ String

Add a Kong ratelimiting plugin

Parameters:

  • apikey (String)

    The apikey of the client

  • consumer_id (String)

    The consumer_id of the client

  • config (String)

    The configuration of the plugin

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/octocore/helpers/kong_helper.rb', line 79

def add_ratelimiting_plugin(apikey, consumer_id, config)

  url = '/apis/' + apikey + '/plugins/'
  payload = {
    name: "rate-limiting",
    consumer_id: consumer_id,
    config: config
  }

  response = process_kong_request(url, :POST, payload)

  if response['id']
    response['id'].to_s
  end
end

#apislist(payload = {}) ⇒ Hash

List of APIS

Parameters:

  • payload (Hash) (defaults to: {})

    filter values

Returns:

  • (Hash)

    All the apis data



116
117
118
119
120
# File 'lib/octocore/helpers/kong_helper.rb', line 116

def apislist(payload = {})
  url = '/apis/'
  res = process_kong_request(url, :GET, payload)
  res['data']
end

#consumerlist(payload = {}) ⇒ Hash

List of Client

Parameters:

  • payload (Hash) (defaults to: {})

    The payload to send

Returns:

  • (Hash)

    All the clients data



98
99
100
101
102
# File 'lib/octocore/helpers/kong_helper.rb', line 98

def consumerlist(payload = {})
  url = '/consumers/'
  res = process_kong_request(url, :GET, payload)
  res['data']
end

#create_keyauth(username, keyauth_key) ⇒ Hash

Add Key of client for Key Authorization

Parameters:

  • username (String)

    The username of the client

  • keyauth_key (String)

    The Authorization key of the client

Returns:

  • (Hash)

    Response



65
66
67
68
69
70
71
72
# File 'lib/octocore/helpers/kong_helper.rb', line 65

def create_keyauth(username, keyauth_key)

  url = '/consumers/'+ username +'/key-auth'
  payload = {
    key: keyauth_key
  }
  process_kong_request(url, :POST, payload)
end

#delete_api(name) ⇒ String

Delete apis from Kong

Parameters:

  • name (String)

    The name of the api

Returns:



125
126
127
128
129
# File 'lib/octocore/helpers/kong_helper.rb', line 125

def delete_api(name)
  url = '/apis/' + name
  payload = {}
  process_kong_request(url, :DELETE, payload)
end

#delete_consumer(username) ⇒ String

Delete Consumers from Kong

Parameters:

  • username (String)

    The username of the Client

Returns:



107
108
109
110
111
# File 'lib/octocore/helpers/kong_helper.rb', line 107

def delete_consumer(username)
  url = '/consumers/' + username
  payload = {}
  process_kong_request(url, :DELETE, payload)
end

#kong_urlString

Fetch Kong URL

Returns:



13
14
15
16
17
18
19
20
# File 'lib/octocore/helpers/kong_helper.rb', line 13

def kong_url
  kong_config = Octo.get_config :kong
  if kong_config.has_key?(:url)
    kong_config[:url]
  else
    'http://127.0.0.1:8001'
  end
end

#process_kong_request(url, method, payload) ⇒ Hash

Process Every Kong Request

Parameters:

  • url (String)

    The url of the kong request

  • method (Key)

    The request method

  • payload (Hash)

    The request body

Returns:

  • (Hash)

    Response



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/octocore/helpers/kong_helper.rb', line 27

def process_kong_request (url, method, payload)
  begin

    url = kong_url + url
    header = {
      'Accept' => 'application/json, text/plain, */*',
      'Content-Type' => 'application/json'
    }
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host,uri.port)

    case method
    when :GET
      req = Net::HTTP::Get.new(uri, header) # GET Method
    when :POST
      req = Net::HTTP::Post.new(uri.path, header) # POST Method
    when :PUT
      req = Net::HTTP::Put.new(uri.path, header) # PUT Method
    when :PATCH
      req = Net::HTTP::Patch.new(uri.path, header) # PATCH Method
    when :DELETE
      req = Net::HTTP::Delete.new(uri.path, header) # DELETE Method
    else
      # Default Case
    end

    req.body = "#{ payload.to_json }"
    res = http.request(req)
    JSON.parse(res.body) # Returned Data
  rescue Exception => e
    { message: e.to_s }.to_json
  end
end