@@ -22,101 +22,81 @@ def register(provider, klass)
2222 def build ( directory_key :, root_dir :, resource_type : nil , min_size : nil , max_size : nil )
2323 raise 'Missing resource_type' if resource_type . nil?
2424
25- cfg = fetch_and_validate_config! ( resource_type )
25+ cfg = fetch_config ( resource_type )
2626 provider = cfg [ 'provider' ]
2727
2828 key = provider . to_s
2929 impl_class = registry [ key ] || registry [ key . downcase ] || registry [ key . upcase ]
3030 raise "No storage CLI client registered for provider #{ provider } " unless impl_class
3131
32- impl_class . new ( provider :, directory_key :, root_dir :, resource_type :, min_size :, max_size :)
32+ impl_class . new ( provider : provider , directory_key : directory_key , root_dir : root_dir , resource_type : resource_type , min_size : min_size , max_size : max_size ,
33+ config_path : config_path_for ( resource_type ) )
3334 end
3435
35- def fetch_and_validate_config! ( resource_type )
36- path = config_path_for! ( resource_type )
36+ RESOURCE_TYPE_KEYS = {
37+ 'droplets' => :storage_cli_config_file_droplets ,
38+ 'buildpack_cache' => :storage_cli_config_file_droplets ,
39+ 'buildpacks' => :storage_cli_config_file_buildpacks ,
40+ 'packages' => :storage_cli_config_file_packages ,
41+ 'resource_pool' => :storage_cli_config_file_resource_pool
42+ } . freeze
3743
38- begin
39- json = Oj . load ( File . read ( path ) )
40- rescue StandardError => e
41- raise BlobstoreError . new ( "Failed to parse storage-cli config JSON at #{ path } : #{ e . message } " )
42- end
44+ def fetch_config ( resource_type )
45+ path = config_path_for ( resource_type )
46+ validate_config_path! ( path )
4347
48+ json = fetch_json ( path )
49+ validate_json_object! ( json , path )
4450 validate_required_keys! ( json , path )
51+
4552 json
4653 end
4754
48- def config_path_for! ( resource_type )
49- key =
50- case resource_type . to_s
51- when 'droplets' , 'buildpack_cache' then :storage_cli_config_file_droplets
52- when 'buildpacks' then :storage_cli_config_file_buildpacks
53- when 'packages' then :storage_cli_config_file_packages
54- when 'resource_pool' then :storage_cli_config_file_resource_pool
55- else
56- raise BlobstoreError . new ( "Unknown resource_type: #{ resource_type } " )
57- end
58-
59- path = VCAP ::CloudController ::Config . config . get ( key )
60- raise BlobstoreError . new ( "storage-cli config file not found or not readable at: #{ path . inspect } " ) unless path && File . file? ( path ) && File . readable? ( path )
61-
62- path
55+ def config_path_for ( resource_type )
56+ normalized = resource_type . to_s
57+ key = RESOURCE_TYPE_KEYS . fetch ( normalized ) do
58+ raise BlobstoreError . new ( "Unknown resource_type: #{ resource_type } " )
59+ end
60+ VCAP ::CloudController ::Config . config . get ( key )
6361 end
6462
65- def validate_required_keys! ( json , path )
66- validate_provider! ( json , path )
67- required = %w[
68- account_key
69- account_name
70- container_name
71- environment
72- ]
73- missing = required . reject { |k | json . key? ( k ) && !json [ k ] . to_s . strip . empty? }
74- return if missing . empty?
63+ def fetch_json ( path )
64+ Oj . load ( File . read ( path ) )
65+ rescue Oj ::ParseError , EncodingError => e
66+ raise BlobstoreError . new ( "Failed to parse storage-cli JSON at #{ path } : #{ e . message } " )
67+ end
7568
76- raise BlobstoreError . new ( "Missing required keys in config file #{ path } : #{ missing . join ( ', ' ) } (json: #{ json } )" )
69+ def validate_config_path! ( path )
70+ return if path && File . file? ( path ) && File . readable? ( path )
71+
72+ raise BlobstoreError . new ( "Storage-cli config file not found or not readable at: #{ path . inspect } " )
73+ end
74+
75+ def validate_json_object! ( json , path )
76+ raise BlobstoreError . new ( "Config at #{ path } must be a JSON object" ) unless json . is_a? ( Hash )
7777 end
7878
79- def validate_provider !( json , path )
80- provider = json [ 'provider' ]
81- return unless provider . nil? || provider . to_s . strip . empty?
79+ def validate_required_keys !( json , path )
80+ provider = json [ 'provider' ] . to_s . strip
81+ raise BlobstoreError . new ( "No provider specified in config file: #{ path . inspect } " ) if provider . empty?
8282
83- raise BlobstoreError . new ( "No provider specified in config file: #{ path . inspect } json: #{ json } " )
83+ required = %w[ account_key account_name container_name environment ]
84+ missing = required . reject { |k | json . key? ( k ) && !json [ k ] . to_s . strip . empty? }
85+ return if missing . empty?
86+
87+ raise BlobstoreError . new ( "Missing required keys in #{ path } : #{ missing . join ( ', ' ) } " )
8488 end
8589 end
8690
87- def initialize ( provider :, directory_key :, resource_type :, root_dir :, min_size : nil , max_size : nil )
91+ def initialize ( provider :, directory_key :, resource_type :, root_dir :, min_size : nil , max_size : nil , config_path : )
8892 @cli_path = cli_path
8993 @directory_key = directory_key
9094 @resource_type = resource_type . to_s
9195 @root_dir = root_dir
9296 @min_size = min_size || 0
9397 @max_size = max_size
9498 @provider = provider
95-
96- file_path = case @resource_type
97- when 'droplets' , 'buildpack_cache'
98- VCAP ::CloudController ::Config . config . get ( :storage_cli_config_file_droplets )
99- when 'buildpacks'
100- VCAP ::CloudController ::Config . config . get ( :storage_cli_config_file_buildpacks )
101- when 'packages'
102- VCAP ::CloudController ::Config . config . get ( :storage_cli_config_file_packages )
103- when 'resource_pool'
104- VCAP ::CloudController ::Config . config . get ( :storage_cli_config_file_resource_pool )
105- else
106- raise BlobstoreError . new ( "Unknown resource_type: #{ @resource_type } " )
107- end
108-
109- unless file_path && File . file? ( file_path ) && File . readable? ( file_path )
110- raise BlobstoreError . new ( "storage-cli config file not found or not readable at: #{ file_path . inspect } " )
111- end
112-
113- begin
114- VCAP ::CloudController ::YAMLConfig . safe_load_file ( file_path )
115- rescue StandardError => e
116- raise BlobstoreError . new ( "Failed to load storage-cli config at #{ file_path } : #{ e . message } " )
117- end
118-
119- @config_file = file_path
99+ @config_file = config_path
120100 logger . info ( 'storage_cli_config_selected' , resource_type : @resource_type , path : @config_file )
121101 end
122102
@@ -251,18 +231,6 @@ def build_config(connection_config)
251231 raise NotImplementedError
252232 end
253233
254- def write_config_file ( config )
255- # TODO: Consider to move the config generation into capi-release
256- config_dir = File . join ( tmpdir , 'blobstore-configs' )
257- FileUtils . mkdir_p ( config_dir )
258-
259- config_file_path = File . join ( config_dir , "#{ @directory_key } .json" )
260- File . open ( config_file_path , 'w' , 0o600 ) do |f |
261- f . write ( Oj . dump ( config . transform_keys ( &:to_s ) ) )
262- end
263- config_file_path
264- end
265-
266234 def tmpdir
267235 VCAP ::CloudController ::Config . config . get ( :directories , :tmpdir )
268236 rescue StandardError
0 commit comments