Skip to content

Commit

Permalink
Merge pull request #90 from HewlettPackard/bugfix/88-firmware-bundle
Browse files Browse the repository at this point in the history
Fixes for firmware bundle
  • Loading branch information
jsmartt authored Sep 30, 2016
2 parents ecd5bbc + 3288cbf commit de7ee9a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 23 additions & 9 deletions lib/oneview-sdk/resource/firmware_bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/oneview-sdk/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

# Gem version defined here
module OneviewSDK
VERSION = '2.2.0'.freeze
VERSION = '2.2.1'.freeze
end
1 change: 1 addition & 0 deletions oneview-sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 7 additions & 11 deletions spec/unit/resource/firmware_bundle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit de7ee9a

Please sign in to comment.