Skip to content

Commit 5c884e0

Browse files
committed
refactor code structure, remove outdated test (azure storage cli client is not building the config any more
1 parent 4c7d0cc commit 5c884e0

File tree

3 files changed

+70
-105
lines changed

3 files changed

+70
-105
lines changed

lib/cloud_controller/blobstore/storage_cli/storage_cli_client.rb

Lines changed: 45 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -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

spec/unit/lib/cloud_controller/blobstore/storage_cli/azure_storage_cli_client_spec.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Blobstore
3737

3838
after { tmp_cfg.close! }
3939

40-
subject(:client) { AzureStorageCliClient.new(provider: 'AzureRM', directory_key: directory_key, resource_type: resource_type, root_dir: 'bommel') }
40+
subject(:client) { AzureStorageCliClient.new(provider: 'AzureRM', directory_key: directory_key, resource_type: resource_type, root_dir: 'bommel', config_path: 'path') }
4141
let(:directory_key) { 'my-bucket' }
4242
let(:resource_type) { 'resource_pool' }
4343
let(:downloaded_file) do
@@ -76,16 +76,6 @@ module Blobstore
7676
end
7777
end
7878

79-
describe 'config file' do
80-
it 'builds a valid config file' do
81-
expect(client.instance_variable_get(:@config_file)).to be_a(String)
82-
expect(File.exist?(client.instance_variable_get(:@config_file))).to be true
83-
expect(File.read(client.instance_variable_get(:@config_file))).to eq(
84-
'{"provider":"AzureRM","account_name":"some-account-name","account_key":"some-access-key","container_name":"my-bucket","environment":"AzureCloud"}'
85-
)
86-
end
87-
end
88-
8979
describe '#cli_path' do
9080
it 'returns the default CLI path' do
9181
expect(client.cli_path).to eq('/var/vcap/packages/azure-storage-cli/bin/azure-storage-cli')

spec/unit/lib/cloud_controller/blobstore/storage_cli/storage_cli_client_spec.rb

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,30 @@ def build_client(resource_type)
170170
)
171171
end
172172

173-
it 'raises when YAML load fails' do
174-
File.write(packages_cfg.path, { provider: 'AzureRM',
175-
account_key: 'bommelkey',
176-
account_name: 'bommel',
177-
container_name: 'bommelcontainer',
178-
environment: 'BommelCloud' }.to_json)
179-
180-
expect(VCAP::CloudController::YAMLConfig).to receive(:safe_load_file).
181-
with(packages_cfg.path).
182-
and_raise(StandardError.new('parse blew up'))
183-
184-
expect do
185-
build_client('packages')
186-
end.to raise_error(
187-
CloudController::Blobstore::BlobstoreError,
188-
/Failed to load storage-cli config/
189-
)
173+
it 'raises BlobstoreError on invalid JSON' do
174+
File.write(droplets_cfg.path, '{not json')
175+
expect {
176+
StorageCliClient.build(directory_key: 'dir', root_dir: 'root', resource_type: 'droplets')
177+
}.to raise_error(CloudController::Blobstore::BlobstoreError, /Failed to parse storage-cli JSON/)
178+
end
179+
180+
it 'raises when JSON is not an object' do
181+
File.write(droplets_cfg.path, '[]')
182+
expect {
183+
StorageCliClient.build(directory_key: 'dir', root_dir: 'root', resource_type: 'droplets')
184+
}.to raise_error(CloudController::Blobstore::BlobstoreError, /must be a JSON object/)
185+
end
186+
187+
%w[account_key account_name container_name environment].each do |k|
188+
it "raises when #{k} missing" do
189+
cfg = { 'provider' => 'AzureRM', 'account_key' => 'a', 'account_name' => 'b',
190+
'container_name' => 'c', 'environment' => 'd' }
191+
cfg.delete(k)
192+
File.write(droplets_cfg.path, cfg.to_json)
193+
expect {
194+
StorageCliClient.build(directory_key: 'dir', root_dir: 'root', resource_type: 'droplets')
195+
}.to raise_error(CloudController::Blobstore::BlobstoreError, /Missing required keys.*#{k}/)
196+
end
190197
end
191198
end
192199

0 commit comments

Comments
 (0)