chambermaid 0.2.0 → 0.3.0

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: ceb4e76a467a39e91b32a40efff57e8fe8d213e28ea38c9742569af98eac7e0f
4
- data.tar.gz: dfdb963dece1c57711fae828eaeebf53490ce2569f6fa339839d2c01a24787ca
3
+ metadata.gz: ed3bfd126164a3fd94e16ccdf453af29f9091f9139074bb80b922fd237db3453
4
+ data.tar.gz: d609e14f50b7b085a38dfbcc4379920015d2b184e11fa0dbf2c26e294a238990
5
5
  SHA512:
6
- metadata.gz: 2997b5bf940b31802cb900d50b042820885da4209fff8e2231228994fbb3daf3efa4c979fb6a41f37a1512e3cea96440e3744b8ca2939dcd2543cc1287c1e960
7
- data.tar.gz: cc518b62b452ea0c63e83b04fd3535345ed335a671fffd6fba6c184addfdaa45a9f29553a825d55e97921598300675e1c054d496dd30a850ca520968170f15be
6
+ metadata.gz: 567267cf7ea6992aa545ad5e745eb6f6c5653366526bfee94873894a31d8c235bd2ebc1ed3c54d79fc362b46093a0fa004c4e79ee77dbfcfd9b371cc36439875
7
+ data.tar.gz: d65da6cc45e6fbfc6c53e09a3de5a55a89a9d707385944c8868db6a50de6b7b83d30b235ff2084e50dff27daab9a1d668b2d4c1bb4789640031425b1b33f0918
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chambermaid (0.2.0)
4
+ chambermaid (0.3.0)
5
5
  aws-sdk-ssm (~> 1.85)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
26
26
 
27
27
  ```ruby
28
28
  Chambermaid.add_namespace("/my/param/namespace")
29
+ Chambermaid.add_service("my-chamber-service")
29
30
  ```
30
31
 
31
32
  **Configuration Block**
@@ -35,6 +36,7 @@ Chambermaid.add_namespace("/my/param/namespace")
35
36
 
36
37
  Chambermaid.configure do |config|
37
38
  config.add_namespace("/my/param/namespace")
39
+ config.add_service("my-chamber-service")
38
40
 
39
41
  # Set `overload: true` to choose these params over existing
40
42
  # ones in ENV when they are merged together
@@ -42,6 +44,11 @@ Chambermaid.configure do |config|
42
44
  end
43
45
  ```
44
46
 
47
+ **Reload SSM into ENV**
48
+ ```ruby
49
+ Chambermaid.reload!
50
+ ```
51
+
45
52
  **Restore ENV to original state**
46
53
  ```ruby
47
54
  Chambermaid.restore!
@@ -1,16 +1,19 @@
1
+ require "chambermaid/environment"
2
+ require "chambermaid/namespace"
1
3
  require "chambermaid/parameter_store"
2
4
 
3
5
  module Chambermaid
4
6
  module Base
5
7
  def self.extended(base)
6
8
  # Make a copy of ENV before we mess it all up
7
- @@_original_env = ENV.to_h.dup
9
+ @@_original_env = ENV.to_h.dup.freeze
8
10
  end
9
11
 
10
12
  extend self
11
13
 
12
14
  def configure
13
15
  yield self
16
+ load!
14
17
  end
15
18
 
16
19
  # @todo
@@ -18,16 +21,18 @@ module Chambermaid
18
21
  raise "Namespaces must be defined" unless @namespaces
19
22
  end
20
23
 
24
+ # Load SSM into ENV
25
+ def load!
26
+ @namespaces.each(&:load!)
27
+ end
28
+
21
29
  # @todo
22
30
  def reload!
23
- restore!
24
- @namespaces.each do |ns|
25
- ns[:store].reload!
26
- update_env!(
27
- params: ns[:store].params,
28
- overload: ns[:overload]
29
- )
30
- end
31
+ @namespaces.each(&:reload!)
32
+ end
33
+
34
+ def unload!
35
+ @namespaces.each(&:unload!)
31
36
  end
32
37
 
33
38
  # Restore ENV to its original state
@@ -43,30 +48,66 @@ module Chambermaid
43
48
  # true - replace any duplicate ENV keys with new params
44
49
  # false - keep any existing duplicate ENV key values
45
50
  #
46
- # @raise
51
+ # @raise [ArgumentError]
52
+ # when `path` is not a string
53
+ #
54
+ # @example
55
+ # Chambermaid.add_namespace("/my/param/namespace")
56
+ #
57
+ # @example overload duplicate ENV vars
58
+ # Chambermaid.add_namespace("/my/param/namespace", overload: true)
47
59
  def add_namespace(path, overload: false)
48
- @namespaces ||= []
49
- # raise "namespace already included in ENV" unless @namespaces[path].nil?
60
+ raise ArgumentError.new("`path` must be a string") unless path.is_a?(String)
61
+ raise ArgumentError.new("`overload` must be a boolean") unless [true, false].include?(overload)
50
62
 
51
- store = ParameterStore.load!(path: path)
52
- @namespaces << { store: store, overload: overload }
53
- update_env!(params: store.params, overload: overload)
63
+ namespaces << Namespace.new(path: path, overload: overload)
54
64
  end
55
65
 
56
- # Inject into ENV
66
+ # Immediately load an AWS SSM parameter namespace to ENV
57
67
  #
58
- # @param [Hash] params
68
+ # @param [String] path
59
69
  # @param [Boolean] overload
60
70
  # true - replace any duplicate ENV keys with new params
61
71
  # false - keep any existing duplicate ENV key values
62
- def update_env!(params:, overload:)
63
- if overload
64
- ENV.update(params)
65
- else
66
- current_env = ENV.to_h.dup
67
- new_env = params.merge(current_env)
68
- ENV.replace(new_env)
69
- end
72
+ #
73
+ # @raise [ArgumentError]
74
+ # when `path` is not a string
75
+ #
76
+ # @example
77
+ # Chambermaid.add_namespace!("/my/param/namespace")
78
+ #
79
+ # @example overload duplicate ENV vars
80
+ # Chambermaid.add_namespace("/my/param/namespace", overload: true)
81
+ def add_namespace!(path, overload: false)
82
+ raise ArgumentError.new("`path` must be a string") unless path.is_a?(String)
83
+ raise ArgumentError.new("`overload` must be a boolean") unless [true, false].include?(overload)
84
+
85
+ namespaces << Namespace.load!(path: path, overload: overload)
86
+ end
87
+
88
+ # Add all secrets from Chamber service to ENV
89
+ #
90
+ # @param [String] service
91
+ # @param [Boolean] overload
92
+ # true - replace any duplicate ENV keys with new params
93
+ # false - keep any existing duplicate ENV key values
94
+ #
95
+ # @example
96
+ # Chambermaid.add_service("my-chamber-service")
97
+ #
98
+ # @example overload duplicate ENV vars
99
+ # Chambermaid.add_service("my-chamber-service", overload: true)
100
+ #
101
+ # @see {Chambermaid::Base.add_namespace}
102
+ def add_service(service, overload: false)
103
+ service = "/#{service}" unless service[0] == "/"
104
+ add_namespace(service)
105
+ end
106
+
107
+ private
108
+
109
+ def namespaces
110
+ @namespaces ||= []
70
111
  end
71
112
  end
72
113
  end
@@ -0,0 +1,68 @@
1
+ module Chambermaid
2
+ class Environment < Hash
3
+ attr_reader :params
4
+
5
+ # Create new Chambermaid::Environment
6
+ #
7
+ # @param [Hash] params
8
+ #
9
+ # @raise [ArgumentError]
10
+ # if params is not type Hash
11
+ #
12
+ # @return [Chambermaid::Environment]
13
+ def initialize(params)
14
+ validate_params!(params)
15
+ stash_current_env!
16
+ update(format_env(params))
17
+ end
18
+
19
+ # Generate a dotenv (.env) compatible string
20
+ #
21
+ # @return [String] dotenv compatible string
22
+ def to_dotenv
23
+ to_h.inject("") do |env_str, param|
24
+ env_str + "#{param[0]}=#{param[1]}\n"
25
+ end
26
+ end
27
+
28
+ # Write a .env file
29
+ #
30
+ # @param [String] file_path
31
+ def to_file!(file_path)
32
+ File.open(file_path, "wb") do |f|
33
+ f.write(to_dotenv)
34
+ end
35
+ end
36
+
37
+ # Inject into ENV without overwriting duplicates
38
+ def load!
39
+ each { |k, v| ENV[k] ||= v }
40
+ end
41
+
42
+ # Inject into ENV and overwrite duplicates
43
+ def overload!
44
+ each { |k, v| ENV[k] = v }
45
+ end
46
+
47
+ # Restore to original ENV
48
+ def unload!
49
+ ENV.replace(@_original_env)
50
+ end
51
+
52
+ private
53
+
54
+ def stash_current_env!
55
+ @_original_env ||= ENV.to_h.dup.freeze
56
+ end
57
+
58
+ def format_env(params)
59
+ params.map{|k,v| [k.upcase, v]}.to_h
60
+ end
61
+
62
+ def validate_params!(params)
63
+ unless params.is_a?(Hash)
64
+ raise ArgumentError.new("`params` must be a hash")
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,42 @@
1
+ module Chambermaid
2
+ class Namespace
3
+ # @param [String] path
4
+ # @param [Boolean] overload
5
+ def initialize(path:, overload: false)
6
+ @path = path
7
+ @overload = overload
8
+
9
+ @store = ParameterStore.new(path: path)
10
+ @env = Environment.new({})
11
+ end
12
+
13
+ def self.load!(path:, overload: false)
14
+ namespace = new(path: path, overload: overload)
15
+ namespace.load!
16
+ namespace
17
+ end
18
+
19
+ def load!
20
+ @store.load!
21
+ load_env!
22
+ end
23
+
24
+ def reload!
25
+ @env.unload!
26
+ @store.reload!
27
+ load_env!
28
+ end
29
+
30
+ def unload!
31
+ @env.unload!
32
+ end
33
+
34
+ private
35
+
36
+ # Inject into ENV
37
+ def load_env!
38
+ @env.replace(@store.params)
39
+ @overload ? @env.overload! : @env.load!
40
+ end
41
+ end
42
+ end
@@ -15,6 +15,10 @@ module Chambermaid
15
15
  fetch_ssm_params!
16
16
  end
17
17
 
18
+ def loaded?
19
+ !@params_list.empty?
20
+ end
21
+
18
22
  def self.load!(path:)
19
23
  store = new(path: path)
20
24
  store.load!
@@ -29,12 +33,6 @@ module Chambermaid
29
33
 
30
34
  alias :to_h :params
31
35
 
32
- def to_env
33
- params.inject("") do |env_str, param|
34
- env_str + "#{param[0]}=#{param[1]}\n"
35
- end
36
- end
37
-
38
36
  private
39
37
 
40
38
  def client
@@ -63,7 +61,7 @@ module Chambermaid
63
61
 
64
62
  def clear_params!
65
63
  @params = nil
66
- @params_list = nil
64
+ @params_list = []
67
65
  end
68
66
  end
69
67
  end
@@ -1,3 +1,3 @@
1
1
  module Chambermaid
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chambermaid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Zimmerman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-01 00:00:00.000000000 Z
11
+ date: 2020-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-ssm
@@ -45,6 +45,8 @@ files:
45
45
  - chambermaid.gemspec
46
46
  - lib/chambermaid.rb
47
47
  - lib/chambermaid/base.rb
48
+ - lib/chambermaid/environment.rb
49
+ - lib/chambermaid/namespace.rb
48
50
  - lib/chambermaid/parameter_store.rb
49
51
  - lib/chambermaid/version.rb
50
52
  homepage: https://github.com/mileszim/chambermaid