Class: Morpheus::VirtualImagesInterface
- Inherits:
-
APIClient
- Object
- APIClient
- Morpheus::VirtualImagesInterface
- Defined in:
- lib/morpheus/api/virtual_images_interface.rb
Instance Method Summary collapse
- #convert(id, payload) ⇒ Object
- #create(payload) ⇒ Object
- #destroy(id, params = {}) ⇒ Object
- #destroy_file(id, filename) ⇒ Object
- #destroy_location(resource_id, id, params = {}, headers = {}) ⇒ Object
- #download_chunked(id, outfile, params = {}) ⇒ Object
- #get(options = nil) ⇒ Object
- #get_location(resource_id, id, params = {}, headers = {}) ⇒ Object
- #list(params = {}) ⇒ Object
- #list_locations(resource_id, params = {}, headers = {}) ⇒ Object
- #location_base_path(resource_id) ⇒ 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
Instance Method Details
#convert(id, payload) ⇒ Object
47 48 49 50 51 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 47 def convert(id, payload) url = "#{@base_url}/api/virtual-images/#{id}/convert" headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' } execute(method: :post, url: url, headers: headers, payload: payload.to_json) end |
#create(payload) ⇒ Object
35 36 37 38 39 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 35 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, params = {}) ⇒ Object
53 54 55 56 57 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 53 def destroy(id, params={}) url = "#{@base_url}/api/virtual-images/#{id}" headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' } execute(method: :delete, url: url, params: params, headers: headers) end |
#destroy_file(id, filename) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 152 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 |
#destroy_location(resource_id, id, params = {}, headers = {}) ⇒ Object
204 205 206 207 208 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 204 def destroy_location(resource_id, id, params = {}, headers={}) validate_id!(resource_id) validate_id!(id) execute(method: :delete, url: "#{location_base_path(resource_id)}/#{id}", params: params, headers: headers) end |
#download_chunked(id, outfile, params = {}) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 160 def download_chunked(id, outfile, params={}) raise "#{self.class}.download_chunked() passed a blank id!" if id.to_s == '' url = "#{@base_url}/api/virtual-images/#{id}/download" headers = { params: params, authorization: "Bearer #{@access_token}" } opts = {method: :get, url: url, headers: headers} # execute(opts, {parse_json: false}) if Dir.exist?(outfile) raise "outfile is invalid. It is the name of an existing directory: #{outfile}" end # if @verify_ssl == false # opts[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE # end if @dry_run return opts end http_response = nil File.open(outfile, 'w') {|f| block = proc { |response| response.read_body do |chunk| # writing to #{outfile} ..." f.write chunk end } opts[:block_response] = block http_response = Morpheus::RestClient.execute(opts) } return http_response end |
#get(options = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 16 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/#{}" elsif .is_a?(String) headers[:params]['name'] = end execute(method: :get, url: url, headers: headers) end |
#get_location(resource_id, id, params = {}, headers = {}) ⇒ Object
198 199 200 201 202 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 198 def get_location(resource_id, id, params={}, headers={}) validate_id!(resource_id) validate_id!(id) execute(method: :get, url: "#{location_base_path(resource_id)}/#{id}", params: params, headers: headers) end |
#list(params = {}) ⇒ Object
29 30 31 32 33 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 29 def list(params={}) url = "#{@base_url}/api/virtual-images" headers = { params: params, authorization: "Bearer #{@access_token}" } execute(method: :get, url: url, headers: headers) end |
#list_locations(resource_id, params = {}, headers = {}) ⇒ Object
193 194 195 196 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 193 def list_locations(resource_id, params={}, headers={}) validate_id!(resource_id) execute(method: :get, url: "#{location_base_path(resource_id)}", params: params, headers: headers) end |
#location_base_path(resource_id) ⇒ Object
189 190 191 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 189 def location_base_path(resource_id) "/api/virtual-images/#{resource_id}/locations" end |
#update(id, payload) ⇒ Object
41 42 43 44 45 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 41 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 70 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] = Morpheus::BodyIO.new(rd) if Gem::Version.new(HTTP::VERSION) >= Gem::Version.new("6.0.0") response = http.post(url, **http_opts) else response = http.post(url, http_opts) end 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 if Gem::Version.new(HTTP::VERSION) >= Gem::Version.new("6.0.0") response = http.post(url, **http_opts) else response = http.post(url, http_opts) end 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
144 145 146 147 148 149 150 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 144 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
7 8 9 10 11 12 13 14 |
# File 'lib/morpheus/api/virtual_images_interface.rb', line 7 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 |