diff --git a/CHANGELOG.md b/CHANGELOG.md index 3de5d9adc..3155eda0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +#### v2.2.1 + - Fixed issue #88 (firmware bundle file size). Uses multipart-post now + ### v2.2.0 - Added the 'rest' and 'update' commands to the CLI - Removed the --force option from the create_from_file CLI command diff --git a/lib/oneview-sdk/resource/firmware_bundle.rb b/lib/oneview-sdk/resource/firmware_bundle.rb index 733cd5d3d..be10d08bf 100644 --- a/lib/oneview-sdk/resource/firmware_bundle.rb +++ b/lib/oneview-sdk/resource/firmware_bundle.rb @@ -8,12 +8,13 @@ # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR # CONDITIONS OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. +require 'openssl' +require 'net/http/post/multipart' module OneviewSDK # Firmware bundle resource implementation class FirmwareBundle BASE_URI = '/rest/firmware-bundles'.freeze - BOUNDARY = '----011000010111000001101001'.freeze # Uploads a firmware bundle file # @param [OneviewSDK::Client] client The client object for the OneView appliance @@ -22,15 +23,28 @@ class FirmwareBundle def self.add(client, file_path) fail NotFound, "ERROR: File '#{file_path}' not found!" unless File.file?(file_path) options = {} - options['Content-Type'] = "multipart/form-data; boundary=#{BOUNDARY}" + options['Content-Type'] = 'multipart/form-data' + options['X-Api-Version'] = client.api_version.to_s + options['auth'] = client.token options['uploadfilename'] = File.basename(file_path) - options['body'] = "--#{BOUNDARY}\r\n" - options['body'] << "Content-Disposition: form-data; name=\"file\"; filename=\"#{File.basename(file_path)}\"\r\n" - options['body'] << "Content-Type: application/octet-stream; Content-Transfer-Encoding: binary\r\n\r\n" - options['body'] << "#{IO.binread(file_path)}\r\n--#{BOUNDARY}--" - response = client.rest_post(BASE_URI, options) - data = client.response_handler(response) - OneviewSDK::FirmwareDriver.new(client, data) + url = URI.parse(URI.escape("#{client.url}#{BASE_URI}")) + + File.open(file_path) do |file| + req = Net::HTTP::Post::Multipart.new( + url.path, + { 'file' => UploadIO.new(file, 'application/octet-stream', File.basename(file_path)) }, + options + ) + + http_request = Net::HTTP.new(url.host, url.port) + http_request.use_ssl = true + http_request.verify_mode = OpenSSL::SSL::VERIFY_NONE + http_request.start do |http| + response = http.request(req) + data = client.response_handler(response) + return OneviewSDK::FirmwareDriver.new(client, data) + end + end end end end diff --git a/lib/oneview-sdk/version.rb b/lib/oneview-sdk/version.rb index 2bcdeba1c..a7444202f 100644 --- a/lib/oneview-sdk/version.rb +++ b/lib/oneview-sdk/version.rb @@ -11,5 +11,5 @@ # Gem version defined here module OneviewSDK - VERSION = '2.2.0'.freeze + VERSION = '2.2.1'.freeze end diff --git a/oneview-sdk.gemspec b/oneview-sdk.gemspec index c855ba9b5..6334171cd 100644 --- a/oneview-sdk.gemspec +++ b/oneview-sdk.gemspec @@ -32,6 +32,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'thor' spec.add_runtime_dependency 'highline' spec.add_runtime_dependency 'pry' + spec.add_runtime_dependency 'multipart-post' spec.add_development_dependency 'bundler' spec.add_development_dependency 'rspec' diff --git a/spec/unit/resource/firmware_bundle_spec.rb b/spec/unit/resource/firmware_bundle_spec.rb index 5b08803b1..e4fbcf82c 100644 --- a/spec/unit/resource/firmware_bundle_spec.rb +++ b/spec/unit/resource/firmware_bundle_spec.rb @@ -9,17 +9,13 @@ end it 'returns a FirmwareDriver resource' do - expect(@client).to receive(:rest_post).with( - OneviewSDK::FirmwareBundle::BASE_URI, - hash_including( - 'body' => %r{Content-Type: application/octet-stream; Content-Transfer-Encoding: binary\r\n\r\nFAKE FILE CONTENT\r\n--}, - 'uploadfilename' => 'file.tar', - 'Content-Type' => "multipart/form-data; boundary=#{OneviewSDK::FirmwareBundle::BOUNDARY}" - ) - ).and_return(FakeResponse.new({})) - expect(File).to receive('file?').and_return(true) - allow(IO).to receive(:binread).and_return('FAKE FILE CONTENT') - OneviewSDK::FirmwareBundle.add(@client, 'file.tar') + allow_any_instance_of(Net::HTTP).to receive(:request).and_return(FakeResponse.new({}, 200)) + allow_any_instance_of(Net::HTTP).to receive(:connect).and_return(true) + allow(@client).to receive(:response_handler).and_return(uri: '/rest/firmware-drivers/f1') + allow(File).to receive(:file?).and_return(true) + allow(File).to receive(:open).with('file.tar').and_yield('FAKE FILE CONTENT') + allow(UploadIO).to receive(:new).and_return('FAKE FILE CONTENT') + expect(OneviewSDK::FirmwareBundle.add(@client, 'file.tar').class).to eq(OneviewSDK::FirmwareDriver) end end end