Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and cleanup for MiqSwiftStorage #398

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/gems/pending/util/object_storage/miq_swift_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ def initialize(settings)
@bucket_name = URI(@settings[:uri]).host

raise "username and password are required values!" if @settings[:username].nil? || @settings[:password].nil?
_scheme, _userinfo, @host, @port, _registry, @mount_path, _opaque, query, _fragment = URI.split(URI.encode(@settings[:uri]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the @mount_path variable unused before? Seems odd that it was being assigned here if we didn't need it. @jerryk55 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, I see that it was being used to set @container_name, but because it's an instance variable (rather than a local) I'm being cautious about it being used elsewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just took a closer look. Looks like this is only used in mount sessions (rather than object stores).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should probably remove the @mount_path and just make it a local variable so it can be garbage collected, but I decided to leave it for now. Can do a follow up later to fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just looked at the code I wrote, and I did do that... ignore me...

_scheme, _userinfo, @host, @port, _registry, path, _opaque, query, _fragment = URI.split(URI.encode(@settings[:uri]))
query_params(query) if query
@swift = nil
@username = @settings[:username]
@password = @settings[:password]
@container_name = @mount_path[0] == File::Separator ? @mount_path[1..-1] : @mount_path

# Omit leading slash (if it exists), and grab the rest of the characters
# before the next file separator
@container_name = path.gsub(/^\/?([^\/]+)\/.*/, '\1')
end

def uri_to_object_path(remote_file)
Expand Down Expand Up @@ -52,15 +55,17 @@ def upload_single(dest_uri)
}
#
# Because of how `Fog::OpenStack` (and probably `Fog::Core`) is designed,
# it has hidden the functionality to provide a block for streaming uploads
# that is available out of the box with Excon.
# it has hidden the functionality to provide a block for streaming
# uploads that is available out of the box with Excon.
#
# we use .send here because #request is private
# we can't use #put_object (public) directly because it doesn't allow a 202 response code,
# which is what swift responds with when we pass it the :request_block
# (This allows us to stream the response in chunks)
#
# we can't use #put_object (public) directly because it doesn't allow a
# 202 response code, which is what swift responds with when we pass it
# the :request_block (This allows us to stream the response in chunks)
#
swift_file.service.send(:request, params)

clear_split_vars
rescue Excon::Errors::Unauthorized => err
msg = "Access to Swift container #{@container_name} failed due to a bad username or password. #{err}"
Expand Down
32 changes: 24 additions & 8 deletions spec/util/object_storage/miq_swift_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@
describe MiqSwiftStorage do
let(:object_storage) { described_class.new(:uri => uri, :username => 'user', :password => 'pass') }

context "using a uri without query parameters" do
let(:uri) { "swift://foo.com/abc/def" }
describe "#initialize" do
context "using a uri with query parameters" do
let(:uri) { "swift://foo.com:5678/abc/def?region=region&api_version=v3&security_protocol=non-ssl" }

it "#initialize sets the container_name" do
container_name = object_storage.container_name
expect(container_name).to eq("abc")
end

it "#initialize sets the container_name" do
container_name = object_storage.container_name
expect(container_name).to eq("abc/def")
it "#uri_to_object_path returns a new object path" do
result = object_storage.uri_to_object_path(uri)
expect(result).to eq("def")
end
end

it "#uri_to_object_path returns a new object path" do
result = object_storage.uri_to_object_path(uri)
expect(result).to eq("def")
context "using a uri without query parameters" do
let(:uri) { "swift://foo.com/abc/def/my_file.tar.gz" }

it "#initialize sets the container_name" do
container_name = object_storage.container_name
expect(container_name).to eq("abc")
end

it "#uri_to_object_path returns a new object path" do
result = object_storage.uri_to_object_path(uri)
expect(result).to eq("def/my_file.tar.gz")
end
end
end

Expand Down