Skip to content

Commit e56336d

Browse files
committed
Implement Storage#upload() request
1 parent a147bee commit e56336d

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

lib/fog/proxmox/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def request(params)
9393
authenticate! if expired?
9494
request_options = params.merge(path: "#{@path}/#{params[:path]}",
9595
headers: @auth_token.headers(
96-
params[:method], params.respond_to?(:headers) ? params[:headers] : {}, {}
96+
params[:method], {}, params.key?(:headers) ? params[:headers] : {}
9797
))
9898
response = @connection.request(request_options)
9999
rescue Excon::Errors::Unauthorized => e

lib/fog/proxmox/storage.rb

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,57 @@ module Fog
2222
module Proxmox
2323
# Procmox storage service
2424
class Storage < Fog::Service
25+
requires :proxmox_url, :proxmox_auth_method
26+
recognizes :proxmox_token, :proxmox_tokenid, :proxmox_userid, :persistent, :proxmox_username, :proxmox_password
27+
2528
# Models
26-
model_path 'fog/proxmox/storage'
29+
model_path 'fog/proxmox/storage/models'
30+
31+
request_path 'fog/proxmox/storage/requests'
32+
33+
request :upload
34+
35+
# Mock class
36+
class Mock
37+
attr_reader :config
38+
39+
def initialize(options = {})
40+
@proxmox_uri = URI.parse(options[:proxmox_url])
41+
@proxmox_auth_method = options[:proxmox_auth_method]
42+
@proxmox_tokenid = options[:proxmox_tokenid]
43+
@proxmox_userid = options[:proxmox_userid]
44+
@proxmox_username = options[:proxmox_username]
45+
@proxmox_password = options[:proxmox_password]
46+
@proxmox_token = options[:proxmox_token]
47+
@proxmox_path = @proxmox_uri.path
48+
@config = options
49+
end
50+
end
51+
52+
# Real class
53+
class Real
54+
include Fog::Proxmox::Core
55+
56+
def self.not_found_class
57+
Fog::Proxmox::Storage::NotFound
58+
end
59+
60+
def config
61+
self
62+
end
63+
64+
def config_service?
65+
true
66+
end
67+
68+
private
69+
70+
def configure(source)
71+
source.instance_variables.each do |v|
72+
instance_variable_set(v, source.instance_variable_get(v))
73+
end
74+
end
75+
end
2776
end
2877
end
2978
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require 'securerandom'
4+
5+
module Fog
6+
module Proxmox
7+
class Storage
8+
# class Real upload
9+
class Real
10+
def upload(path_params, body_params)
11+
node = path_params[:node]
12+
storage = path_params[:storage]
13+
body, content_type = self.class.build_formdata(body_params)
14+
request(
15+
expects: [200],
16+
method: 'POST',
17+
path: "nodes/#{node}/storage/#{storage}/upload",
18+
body: body,
19+
headers: { 'Content-Type' => content_type }
20+
)
21+
end
22+
23+
private_class_method def self.build_formdata(body_params)
24+
boundary = '-' * 30 + SecureRandom.hex(15)
25+
26+
body = "--#{boundary}" + Excon::CR_NL
27+
body << 'Content-Disposition: form-data; name="content"' << Excon::CR_NL << Excon::CR_NL
28+
body << body_params[:content] << Excon::CR_NL
29+
body << "--#{boundary}" << Excon::CR_NL
30+
body << %(Content-Disposition: form-data; name="filename"; filename="#{body_params[:filename]}") << Excon::CR_NL
31+
body << 'Content-Type: ' << (body_params[:content_type] || 'application/octet-stream') << Excon::CR_NL << Excon::CR_NL
32+
body << body_params[:file].read << Excon::CR_NL
33+
body << "--#{boundary}--" << Excon::CR_NL
34+
35+
[body, %(multipart/form-data; boundary=#{boundary})]
36+
end
37+
end
38+
39+
# class Mock upload
40+
class Mock
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)