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

Implement Storage#upload() request #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/fog/proxmox/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def request(params)
authenticate! if expired?
request_options = params.merge(path: "#{@path}/#{params[:path]}",
headers: @auth_token.headers(
params[:method], params.respond_to?(:headers) ? params[:headers] : {}, {}
params[:method], {}, params.key?(:headers) ? params[:headers] : {}
))
response = @connection.request(request_options)
rescue Excon::Errors::Unauthorized => e
Expand Down
51 changes: 50 additions & 1 deletion lib/fog/proxmox/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,57 @@ module Fog
module Proxmox
# Procmox storage service
class Storage < Fog::Service
requires :proxmox_url, :proxmox_auth_method
recognizes :proxmox_token, :proxmox_tokenid, :proxmox_userid, :persistent, :proxmox_username, :proxmox_password

# Models
model_path 'fog/proxmox/storage'
model_path 'fog/proxmox/storage/models'

request_path 'fog/proxmox/storage/requests'

request :upload

# Mock class
class Mock
attr_reader :config

def initialize(options = {})
@proxmox_uri = URI.parse(options[:proxmox_url])
@proxmox_auth_method = options[:proxmox_auth_method]
@proxmox_tokenid = options[:proxmox_tokenid]
@proxmox_userid = options[:proxmox_userid]
@proxmox_username = options[:proxmox_username]
@proxmox_password = options[:proxmox_password]
@proxmox_token = options[:proxmox_token]
@proxmox_path = @proxmox_uri.path
@config = options
end
end

# Real class
class Real
include Fog::Proxmox::Core

def self.not_found_class
Fog::Proxmox::Storage::NotFound
end

def config
self
end

def config_service?
true
end

private

def configure(source)
source.instance_variables.each do |v|
instance_variable_set(v, source.instance_variable_get(v))
end
end
end
end
end
end
44 changes: 44 additions & 0 deletions lib/fog/proxmox/storage/requests/upload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'securerandom'

module Fog
module Proxmox
class Storage
# class Real upload
class Real
def upload(path_params, body_params)
node = path_params[:node]
storage = path_params[:storage]
body, content_type = self.class.build_formdata(body_params)
request(
expects: [200],
method: 'POST',
path: "nodes/#{node}/storage/#{storage}/upload",
body: body,
headers: { 'Content-Type' => content_type }
)
end

private_class_method def self.build_formdata(body_params)
boundary = '-' * 30 + SecureRandom.hex(15)

body = "--#{boundary}" + Excon::CR_NL
body << 'Content-Disposition: form-data; name="content"' << Excon::CR_NL << Excon::CR_NL
body << body_params[:content] << Excon::CR_NL
body << "--#{boundary}" << Excon::CR_NL
body << %(Content-Disposition: form-data; name="filename"; filename="#{body_params[:filename]}") << Excon::CR_NL
body << 'Content-Type: ' << (body_params[:content_type] || 'application/octet-stream') << Excon::CR_NL << Excon::CR_NL
body << body_params[:file].read << Excon::CR_NL
body << "--#{boundary}--" << Excon::CR_NL

[body, %(multipart/form-data; boundary=#{boundary})]
end
end

# class Mock upload
class Mock
end
end
end
end
Loading