proxmox-api 0.1.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/proxmox_api.rb +67 -7
  3. metadata +50 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbf823b601fbcb8595b19af7580881ef311ce140d1e5d971a9451a3a50c7249a
4
- data.tar.gz: 246a3d239a3ffa4966022e2f28c95addcf9787b6b1fd236b2c163a54dd2b837b
3
+ metadata.gz: f471fb9bb0268e38e3b45da32b2572d29a53d9805e4702e47a6a0c784351f971
4
+ data.tar.gz: 0364feb0db15bea88f86358ca5299e6c16a70f56a045024db1c63957defa26ec
5
5
  SHA512:
6
- metadata.gz: 42b320222e6b040bd9d06fa103f61bb177f63b6d9d8c2c034d5ca243387c9526b5cdb0cae56229cabbe2e45f15722c729e8240e880f47c2f4c3da4d67f2789a1
7
- data.tar.gz: 2617997f456c221ab6fabfd933c3b3c4b51944a51a43a2204403e23265a59ccef0dbb6b09c3e99b09317f8b3c5d8a05622b48cf17a063f735a16f4fb2a2bd317
6
+ metadata.gz: 4172c20fc513e1772ab19d0acdd94d2393601ac5a6ad4067a24e30ac52698208580d2a011ee2047d0d41edf93a081aeaf2ae988fbaf4f78b2da94a72c1c16d29
7
+ data.tar.gz: 95d4895f5e6323b6f0ab9fb79b3bc64e36ed8a735b3d3f9f80fb244db5559f0d744ba587ca047de6290b5dc6408d62124f3754f9357fa4bc4c85ac93613ffd54
data/lib/proxmox_api.rb CHANGED
@@ -3,13 +3,21 @@
3
3
  require 'rest_client'
4
4
  require 'json'
5
5
 
6
+ # This class is wrapper for Proxmox PVE APIv2.
7
+ # See README for usage examples.
8
+ #
9
+ # @author Eugene Lapeko
6
10
  class ProxmoxAPI
7
- AUTH_PARAMS = %i[username realm password otp path privs].freeze
11
+ AUTH_PARAMS = %i[username realm password otp].freeze
12
+ RESOURCE_OPTIONS = %i[headers].freeze
8
13
  REST_METHODS = %i[get post put delete].freeze
9
- SSL_OPTIONS = %i[ssl_client_cert ssl_client_key ssl_ca_file verify_ssl].freeze
10
14
 
15
+ # This class is used to collect api path before request
11
16
  class ApiPath
17
+ # @param [ProxmoxAPI] api ProxmoxAPI object to call when request is executed
12
18
  def initialize(api)
19
+ raise ArgumentError, 'Not an instance of ProxmoxAPI' unless api.is_a? ProxmoxAPI
20
+
13
21
  @api = api
14
22
  @path = []
15
23
  end
@@ -24,7 +32,7 @@ class ProxmoxAPI
24
32
  end
25
33
 
26
34
  def method_missing(method, *args)
27
- return @api.__send__(:submit, method, to_s, *args) if REST_METHODS.include?(method)
35
+ return @api.__send__(:submit, method, to_s, *args) if REST_METHODS.any? { |rm| /^#{rm}!?$/.match? method }
28
36
 
29
37
  @path << method.to_s
30
38
  self
@@ -35,12 +43,46 @@ class ProxmoxAPI
35
43
  end
36
44
  end
37
45
 
46
+ # This exception is raised when Proxmox API returns error code
47
+ #
48
+ # @!attribute [r] response
49
+ # @return [RestClient::Response] answer from Proxmox server
50
+ class ApiException < RuntimeError
51
+ attr_reader :response
52
+
53
+ def initialize(response, description)
54
+ @response = response
55
+
56
+ super description
57
+ end
58
+ end
59
+
60
+ # Constructor method for ProxmoxAPI
61
+ #
62
+ # @param [String] cluster hostname/ip of cluster to control
63
+ # @param [Hash] options cluster connection parameters
64
+ #
65
+ # @option options [String] :username - username to be used for connection
66
+ # @option options [String] :password - password to be used for connection
67
+ # @option options [String] :realm - auth realm, can be given in :username ('user@realm')
68
+ # @option options [String] :token - token to be used instead of username, password, and realm
69
+ # @option options [String] :secret - secret to be used with token
70
+ # @option options [String] :otp - one-time password for two-factor auth
71
+ #
72
+ # @option options [Boolean] :verify_ssl - verify server certificate
73
+ #
74
+ # You can also pass here all ssl options supported by rest-client gem
75
+ # @see https://github.com/rest-client/rest-client
38
76
  def initialize(cluster, options)
77
+ if options.key?(:token) && options.key?(:secret)
78
+ options[:headers] = { Authorization: "PVEAPIToken=#{options[:token]}=#{options[:secret]}" }
79
+ end
80
+
39
81
  @connection = RestClient::Resource.new(
40
82
  "https://#{cluster}:#{options[:port] || 8006}/api2/json/",
41
- options.select { |k, _v| SSL_OPTIONS.include? k }
83
+ options.select { |k, _v| connection_options.include?(k.to_s) }
42
84
  )
43
- @auth_ticket = create_auth_ticket(options.select { |k, _v| AUTH_PARAMS.include? k })
85
+ @auth_ticket = options.key?(:token) ? {} : create_auth_ticket(options.select { |k, _v| AUTH_PARAMS.include? k })
44
86
  end
45
87
 
46
88
  def [](index)
@@ -55,11 +97,23 @@ class ProxmoxAPI
55
97
  true
56
98
  end
57
99
 
100
+ # The list of options to be passed to RestClient object
101
+ def self.connection_options
102
+ RestClient::Request::SSLOptionList.unshift('verify_ssl') + RESOURCE_OPTIONS
103
+ end
104
+
58
105
  private
59
106
 
107
+ def raise_on_failure(response, message = 'Proxmox API request failed')
108
+ return unless response.code.to_i >= 400
109
+
110
+ raise ApiException.new(response, message)
111
+ end
112
+
60
113
  def create_auth_ticket(options)
61
114
  @connection['access/ticket'].post options do |response, _request, _result, &_block|
62
- # TODO: raise unless response.code == 200
115
+ raise_on_failure(response, 'Proxmox authentication failure')
116
+
63
117
  data = JSON.parse(response.body, symbolize_names: true)[:data]
64
118
  {
65
119
  cookies: { PVEAuthCookie: data[:ticket] },
@@ -80,8 +134,14 @@ class ProxmoxAPI
80
134
  end
81
135
 
82
136
  def submit(method, url, data = {})
137
+ if /!$/.match? method
138
+ method = method.to_s.tr('!', '').to_sym
139
+ skip_raise = true
140
+ end
141
+
83
142
  @connection[url].__send__(method, *prepare_options(method, data)) do |response|
84
- # TODO: raise unless response.code == 200
143
+ raise_on_failure(response) unless skip_raise
144
+
85
145
  JSON.parse(response.body, symbolize_names: true)[:data]
86
146
  end
87
147
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proxmox-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Lapeko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-27 00:00:00.000000000 Z
11
+ date: 2022-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -38,7 +38,49 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.1'
41
- description: A library to manage Proxmox VE cluster using APIv2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Proxmox VE REST API wrapper
42
84
  email: eugene@lapeko.info
43
85
  executables: []
44
86
  extensions: []
@@ -50,7 +92,8 @@ files:
50
92
  homepage: https://github.com/L-Eugene/proxmox-api
51
93
  licenses:
52
94
  - MIT
53
- metadata: {}
95
+ metadata:
96
+ rubygems_mfa_required: 'true'
54
97
  post_install_message:
55
98
  rdoc_options: []
56
99
  require_paths:
@@ -59,15 +102,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
102
  requirements:
60
103
  - - ">="
61
104
  - !ruby/object:Gem::Version
62
- version: '2.4'
105
+ version: '2.5'
63
106
  required_rubygems_version: !ruby/object:Gem::Requirement
64
107
  requirements:
65
108
  - - ">="
66
109
  - !ruby/object:Gem::Version
67
110
  version: '0'
68
111
  requirements: []
69
- rubygems_version: 3.0.8
112
+ rubygems_version: 3.1.6
70
113
  signing_key:
71
114
  specification_version: 4
72
- summary: A library to manage Proxmox VE cluster
115
+ summary: Proxmox VE REST API wrapper
73
116
  test_files: []