kong_schema 1.1.1 → 1.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ccf080d9311fc62088bf690a8d86af06fae4a97
4
- data.tar.gz: fcba834c6e444a18c805e08487504a42186abe67
3
+ metadata.gz: 413c7b7271ed7feca20d06caeaf0bd671bf5d253
4
+ data.tar.gz: b79a0818ae360b7df3e7e9dc6a3ffc84398d3e05
5
5
  SHA512:
6
- metadata.gz: 932e679e8c807a03e75e73ebdfe510a4374442ba6fcec7b43c7312532ef38d189c14005ad26cdbc7f2222faf12f75e21d53543ab3700e125f67dbeb8bd7270e3
7
- data.tar.gz: dc34b51e197f958ab0708cd5a1bc6be8607fa7769780324a5f7b4e7f6ce5724da45593cfcd3e645d185e6f379a9ee2dcc52135f8e7c38bf5f04dc46c46790704
6
+ metadata.gz: fab914455b8b170f43ff70537e9604ac8e4489b6264356277257ddaff95f6df25c8cc36cae2107b5fd60987cb9a9bb4455e06319c97e67ea8824a35ce567b194
7
+ data.tar.gz: e1861af086a278bd9257469dccc314cc26211814bac1db35d8800171ad480a234b539c4424e25c40ac86ca13b081171577882c47f42dedee5c236ec091992726
@@ -1,3 +1,8 @@
1
+ ## 1.2.0
2
+
3
+ - Added a new CLI command: `kong_schema reset` for wiping out the Kong database
4
+ (still through its API)
5
+
1
6
  ## 1.1.1
2
7
 
3
8
  - Fixed an issue assigning the "methods" attribute of Kong::Api objects
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kong_schema (1.1.0)
4
+ kong_schema (1.2.0)
5
5
  diffy (~> 3.1)
6
6
  gli (~> 2.16)
7
7
  kong (~> 0.3)
@@ -12,7 +12,7 @@ GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
14
  diff-lcs (1.3)
15
- diffy (3.1.0)
15
+ diffy (3.2.0)
16
16
  docile (1.1.5)
17
17
  equatable (0.5.0)
18
18
  excon (0.49.0)
data/README.md CHANGED
@@ -29,9 +29,22 @@ end
29
29
  ## Usage
30
30
 
31
31
  Write your desired configuration for Kong in a file using either YAML or JSON.
32
- The supported "directives" are described later in this document.
32
+ The supported "directives" are described later in this document. Then you can
33
+ use the `kong_schema` binary to perform various actions.
33
34
 
34
- For now we'll assume we have such a config file:
35
+ Run `kong_schema --help` to see the available commands.
36
+
37
+ ```shell
38
+ # apply configuration
39
+ kong_schema up [path/to/config.yml]
40
+
41
+ # reset configuration
42
+ kong_schema reset [path/to/config.yml]
43
+ ```
44
+
45
+ ## Example
46
+
47
+ Let's assume we have such a config file:
35
48
 
36
49
  ```yaml
37
50
  # file: config/kong.yml
@@ -41,12 +54,12 @@ kong:
41
54
  - name: application-api
42
55
  hosts:
43
56
  - api.application.dev
57
+ preserve_host: true
58
+ strip_uri: false
44
59
  upstream_url: http://application-api-lb
45
60
  uris:
46
61
  - /api/.+
47
62
  - /auth/.+
48
- preserve_host: true
49
- strip_uri: false
50
63
  upstreams:
51
64
  - name: application-api-lb
52
65
  targets:
@@ -57,7 +70,7 @@ kong:
57
70
  Then if we run the following command:
58
71
 
59
72
  ```shell
60
- bundle exec kong_schema up config/kong.yml
73
+ kong_schema up config/kong.yml
61
74
  ```
62
75
 
63
76
  kong_schema will read the directives found under the `kong` dictionary and
@@ -155,9 +168,11 @@ Nice and easy!
155
168
 
156
169
  - name: String
157
170
  - host: String
171
+ - methods: Array<String>
172
+ - preserve_host: Boolean
173
+ - strip_uri: Boolean
158
174
  - upstream_url: String
159
175
  - uris: Array<String>
160
- - preserve_host: Boolean
161
176
 
162
177
  ### `upstreams: Array<Kong::Upstream>`
163
178
 
@@ -165,8 +180,8 @@ Nice and easy!
165
180
  configuration:
166
181
 
167
182
  - name: String
168
- - ?slots: Number
169
- - ?orderlist: Array<Number>
183
+ - slots: Number
184
+ - orderlist: Array<Number>
170
185
 
171
186
  ### `targets: Array<Kong::Target>`
172
187
 
@@ -175,7 +190,7 @@ configuration:
175
190
 
176
191
  - upstream_id: String
177
192
  - target: String
178
- - ?weight: Number
193
+ - weight: Number
179
194
 
180
195
  ## TODO
181
196
 
@@ -14,9 +14,10 @@ module KongSchema
14
14
 
15
15
  version KongSchema::VERSION
16
16
 
17
+ sort_help :manually
18
+
17
19
  desc 'Apply configuration from a .yml or .json file.'
18
20
  arg(:config_file)
19
-
20
21
  command :up do |c|
21
22
  c.flag([ 'k', 'key' ], {
22
23
  default_value: 'kong',
@@ -38,12 +39,33 @@ module KongSchema
38
39
  })
39
40
 
40
41
  c.action do |global_options, options, args|
41
- help_now! "Missing path to .yml or .json config file" if args.first.nil?
42
+ bail! "Missing path to .yml or .json config file" if args.first.nil?
42
43
 
43
44
  up(filepath: args.first, options: options)
44
45
  end
45
46
  end
46
47
 
48
+ desc 'Reset Kong configuration completely.'
49
+ arg(:config_file)
50
+ command :reset do |c|
51
+ c.flag([ 'k', 'key' ], {
52
+ default_value: 'kong',
53
+ desc: 'The root configuration property key.',
54
+ arg_name: 'NAME'
55
+ })
56
+
57
+ c.switch([ 'confirm' ], {
58
+ default_value: true,
59
+ desc: 'Prompt for confirmation before applying changes.'
60
+ })
61
+
62
+ c.action do |global_options, options, args|
63
+ bail! "Missing path to .yml or .json config file" if args.first.nil?
64
+
65
+ reset(filepath: args.first, options: options)
66
+ end
67
+ end
68
+
47
69
  super(argv)
48
70
  end
49
71
 
@@ -56,19 +78,31 @@ module KongSchema
56
78
 
57
79
  schema.scan(config).tap do |changes|
58
80
  if changes.empty?
59
- puts "#{pastel.green('✓')} Nothing to update."
81
+ puts "#{green('✓')} Nothing to update."
60
82
  else
61
83
  puts KongSchema::Reporter.report(changes, object_format: options[:format].to_sym)
62
84
 
63
- if TTY::Prompt.new.yes?('Commit the changes to Kong?', default: false)
85
+ if !options[:confirm] || yes?('Commit the changes to Kong?')
64
86
  schema.commit(config, changes)
65
87
 
66
- puts "#{pastel.green('✓')} Kong has been reconfigured!"
88
+ puts "#{green('✓')} Kong has been reconfigured!"
67
89
  end
68
90
  end
69
91
  end
70
92
  end
71
93
 
94
+ def reset(filepath:, options:)
95
+ pastel = Pastel.new
96
+ schema = KongSchema::Schema
97
+ config = read_property(load_file(filepath), options[:key])
98
+
99
+ if !options[:confirm] || yes?("You are about to completely reset Kong's database. Proceed?")
100
+ KongSchema::Client.purge(config)
101
+
102
+ puts "#{green('✓')} Kong reset."
103
+ end
104
+ end
105
+
72
106
  def load_file(filepath)
73
107
  if filepath.end_with?('.json')
74
108
  JSON.parse(File.read(filepath))
@@ -84,5 +118,25 @@ module KongSchema
84
118
  config.fetch(key.to_s)
85
119
  end
86
120
  end
121
+
122
+ def yes?(message, default: false)
123
+ TTY::Prompt.new.yes?(message, default: default)
124
+ end
125
+
126
+ def red(text)
127
+ pastel.red(text)
128
+ end
129
+
130
+ def green(text)
131
+ pastel.green(text)
132
+ end
133
+
134
+ def pastel
135
+ @pastel ||= Pastel.new
136
+ end
137
+
138
+ def bail!(reason)
139
+ help_now! red("✘ #{reason}")
140
+ end
87
141
  end
88
142
  end
@@ -26,5 +26,17 @@ module KongSchema
26
26
  ensure
27
27
  Kong::Client.api_url = api_url
28
28
  end
29
+
30
+ # Reset Kong's database by removing all objects through the API.
31
+ def self.purge(config)
32
+ connect(config) do
33
+ KongSchema::Resource::Upstream.all.each do |upstream|
34
+ upstream.targets.each(&:delete)
35
+ upstream.delete
36
+ end
37
+
38
+ KongSchema::Resource::Api.all.each(&:delete)
39
+ end
40
+ end
29
41
  end
30
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KongSchema
4
- VERSION = "1.1.1".freeze
4
+ VERSION = "1.2.0".freeze
5
5
  end
@@ -12,13 +12,6 @@ class KongSchemaTestUtils
12
12
  end
13
13
 
14
14
  def reset_kong
15
- KongSchema::Client.connect(generate_config) do
16
- KongSchema::Resource::Upstream.all.each do |upstream|
17
- upstream.targets.each(&:delete)
18
- upstream.delete
19
- end
20
-
21
- KongSchema::Resource::Api.all.each(&:delete)
22
- end
15
+ KongSchema::Client.purge(generate_config)
23
16
  end
24
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kong_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmad Amireh