Class: Morpheus::VirtualImagesInterface
- Inherits:
-
APIClient
- Object
- APIClient
- Morpheus::VirtualImagesInterface
- Defined in:
- lib/morpheus/api/virtual_images_interface.rb
Defined Under Namespace
Classes: BodyIO
Instance Method Summary collapse
- #create(payload) ⇒ Object
- #destroy(id) ⇒ Object
- #destroy_file(id, filename) ⇒ Object
- #get(options = nil) ⇒ Object
-
#initialize(access_token, refresh_token, expires_at = nil, base_url = nil) ⇒ VirtualImagesInterface
constructor
A new instance of VirtualImagesInterface.
- #list(options = nil) ⇒ Object
- #update(id, payload) ⇒ Object
-
#upload(id, image_file, filename = nil, do_gzip = false) ⇒ Object
no multipart.
- #upload_by_url(id, file_url, filename = nil) ⇒ Object
- #virtual_image_types(options = {}) ⇒ Object
Constructor Details
#initialize(access_token, refresh_token, expires_at = nil, base_url = nil) ⇒ VirtualImagesInterface
Returns a new instance of VirtualImagesInterface.
7 8 9 10 11 12 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 7 def initialize(access_token, refresh_token,expires_at = nil, base_url=nil) @access_token = access_token @refresh_token = refresh_token @base_url = base_url @expires_at = expires_at end |
Instance Method Details
#create(payload) ⇒ Object
43 44 45 46 47 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 43 def create(payload) url = "#{@base_url}/api/virtual-images" headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' } execute(method: :post, url: url, headers: headers, payload: payload.to_json) end |
#destroy(id) ⇒ Object
55 56 57 58 59 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 55 def destroy(id) url = "#{@base_url}/api/virtual-images/#{id}" headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' } execute(method: :delete, url: url, headers: headers) end |
#destroy_file(id, filename) ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 167 def destroy_file(id, filename) url = "#{@base_url}/api/virtual-images/#{id}/files" #url = "#{@base_url}/api/virtual-images/#{id}/files/#{filename}" headers = { params: {}, :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' } headers[:params][:filename] = filename execute(method: :delete, url: url, headers: headers) end |
#get(options = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 23 def get(=nil) url = "#{@base_url}/api/virtual-images" headers = { params: {}, authorization: "Bearer #{@access_token}" } if .is_a?(Hash) headers[:params].merge!() elsif .is_a?(Numeric) url = "#{@base_url}/api/virtual-images/#{options}" elsif .is_a?(String) headers[:params]['name'] = end execute(method: :get, url: url, headers: headers) end |
#list(options = nil) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 36 def list(=nil) url = "#{@base_url}/api/virtual-images" headers = { params: {}, authorization: "Bearer #{@access_token}" } headers[:params].merge!() execute(method: :get, url: url, headers: headers) end |
#update(id, payload) ⇒ Object
49 50 51 52 53 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 49 def update(id, payload) url = "#{@base_url}/api/virtual-images/#{id}" headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' } execute(method: :put, url: url, headers: headers, payload: payload.to_json) end |
#upload(id, image_file, filename = nil, do_gzip = false) ⇒ Object
no multipart
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 93 def upload(id, image_file, filename=nil, do_gzip=false) filename = filename || File.basename(image_file) url = "#{@base_url}/api/virtual-images/#{id}/upload" headers = { :params => {}, :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/octet-stream'} headers[:params][:filename] = filename payload = image_file #execute(method: :post, url: url, headers: headers, payload: payload, timeout: 36000) # Using http.rb instead of RestClient # todo: execute() should support :driver http_opts = {} if Morpheus::RestClient.ssl_verification_enabled? == false ctx = OpenSSL::SSL::SSLContext.new ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE http_opts[:ssl_context] = ctx # opts[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE end start_time = Time.now query_params = headers.delete(:params) || {} file_size = image_file.size if File.blockdev?(image_file) file_size = `blockdev --getsz '#{File.absolute_path(image_file)}'`.strip().to_i * 512 end if do_gzip # http = http.use(:auto_deflate) headers['Content-Encoding'] = 'gzip' headers['Content-Type'] = 'application/gzip' headers['Content-Length'] = file_size #headers['Transfer-Encoding'] = 'Chunked' query_params['extractedContentLength'] = file_size if @dry_run return {method: :post, url: url, headers: headers, params: query_params, payload: payload} end http = HTTP.headers(headers) http_opts[:params] = query_params rd, wr = IO.pipe Thread.new { gz = Zlib::GzipWriter.new(wr) File.open(payload) do |fp| while chunk = fp.read(10 * 1024 * 1024) do gz.write chunk end end gz.close } http_opts[:body] = BodyIO.new(rd) response = http.post(url, http_opts) else if @dry_run return {method: :post, url: url, headers: headers, params: query_params, payload: payload} end headers['Content-Length'] = file_size http = HTTP.headers(headers) http_opts[:params] = query_params http_opts[:body] = payload response = http.post(url, http_opts) end # puts "Took #{Time.now.to_i - start_time.to_i}" # return response return JSON.parse(response.body.to_s) end |
#upload_by_url(id, file_url, filename = nil) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 159 def upload_by_url(id, file_url, filename=nil) url = "#{@base_url}/api/virtual-images/#{id}/upload" headers = { :params => {}, :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/octet-stream'} headers[:params][:url] = file_url headers[:params][:filename] = filename if filename execute(method: :post, url: url, headers: headers, timeout: 172800) end |
#virtual_image_types(options = {}) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 14 def virtual_image_types(={}) url = "#{@base_url}/api/virtual-image-types" headers = { params: {}, authorization: "Bearer #{@access_token}" } if .is_a?(Hash) headers[:params].merge!() end execute(method: :get, url: url, headers: headers) end |