proxmox-api 0.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 (5) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/LICENSE +21 -0
  4. data/lib/proxmox_api.rb +88 -0
  5. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cbf823b601fbcb8595b19af7580881ef311ce140d1e5d971a9451a3a50c7249a
4
+ data.tar.gz: 246a3d239a3ffa4966022e2f28c95addcf9787b6b1fd236b2c163a54dd2b837b
5
+ SHA512:
6
+ metadata.gz: 42b320222e6b040bd9d06fa103f61bb177f63b6d9d8c2c034d5ca243387c9526b5cdb0cae56229cabbe2e45f15722c729e8240e880f47c2f4c3da4d67f2789a1
7
+ data.tar.gz: 2617997f456c221ab6fabfd933c3b3c4b51944a51a43a2204403e23265a59ccef0dbb6b09c3e99b09317f8b3c5d8a05622b48cf17a063f735a16f4fb2a2bd317
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Eugene Lapeko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rest_client'
4
+ require 'json'
5
+
6
+ class ProxmoxAPI
7
+ AUTH_PARAMS = %i[username realm password otp path privs].freeze
8
+ 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
+
11
+ class ApiPath
12
+ def initialize(api)
13
+ @api = api
14
+ @path = []
15
+ end
16
+
17
+ def to_s
18
+ @path.join('/')
19
+ end
20
+
21
+ def [](index)
22
+ @path << index.to_s
23
+ self
24
+ end
25
+
26
+ def method_missing(method, *args)
27
+ return @api.__send__(:submit, method, to_s, *args) if REST_METHODS.include?(method)
28
+
29
+ @path << method.to_s
30
+ self
31
+ end
32
+
33
+ def respond_to_missing?(*)
34
+ true
35
+ end
36
+ end
37
+
38
+ def initialize(cluster, options)
39
+ @connection = RestClient::Resource.new(
40
+ "https://#{cluster}:#{options[:port] || 8006}/api2/json/",
41
+ options.select { |k, _v| SSL_OPTIONS.include? k }
42
+ )
43
+ @auth_ticket = create_auth_ticket(options.select { |k, _v| AUTH_PARAMS.include? k })
44
+ end
45
+
46
+ def [](index)
47
+ ApiPath.new(self)[index]
48
+ end
49
+
50
+ def method_missing(method, *args)
51
+ ApiPath.new(self).__send__(method, *args)
52
+ end
53
+
54
+ def respond_to_missing?(*)
55
+ true
56
+ end
57
+
58
+ private
59
+
60
+ def create_auth_ticket(options)
61
+ @connection['access/ticket'].post options do |response, _request, _result, &_block|
62
+ # TODO: raise unless response.code == 200
63
+ data = JSON.parse(response.body, symbolize_names: true)[:data]
64
+ {
65
+ cookies: { PVEAuthCookie: data[:ticket] },
66
+ CSRFPreventionToken: data[:CSRFPreventionToken]
67
+ }
68
+ end
69
+ end
70
+
71
+ def prepare_options(method, data)
72
+ case method
73
+ when :post, :put
74
+ [data, @auth_ticket]
75
+ when :delete
76
+ [@auth_ticket]
77
+ when :get
78
+ [@auth_ticket.merge(data)]
79
+ end
80
+ end
81
+
82
+ def submit(method, url, data = {})
83
+ @connection[url].__send__(method, *prepare_options(method, data)) do |response|
84
+ # TODO: raise unless response.code == 200
85
+ JSON.parse(response.body, symbolize_names: true)[:data]
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxmox-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eugene Lapeko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ description: A library to manage Proxmox VE cluster using APIv2
42
+ email: eugene@lapeko.info
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - Gemfile
48
+ - LICENSE
49
+ - lib/proxmox_api.rb
50
+ homepage: https://github.com/L-Eugene/proxmox-api
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '2.4'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.0.8
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A library to manage Proxmox VE cluster
73
+ test_files: []