fog-proxmox 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +7 -0
  2. data/.bundle/config +4 -0
  3. data/.codeclimate.yml +14 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +35 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
  6. data/.gitignore +5 -0
  7. data/.gitlab-ci.yml +42 -0
  8. data/.rubocop.yml +13 -0
  9. data/.ruby-gemset +1 -0
  10. data/.solargraph.yml +10 -0
  11. data/.travis.yml +26 -0
  12. data/.vscode/launch.json +114 -0
  13. data/.vscode/settings.json +45 -0
  14. data/.vscode/tasks.json +27 -0
  15. data/CODE_OF_CONDUCT.md +74 -0
  16. data/CONTRIBUTING.md +20 -0
  17. data/CONTRIBUTORS.md +9 -0
  18. data/Gemfile +23 -0
  19. data/Gemfile.lock +146 -0
  20. data/ISSUE_TEMPLATE.md +43 -0
  21. data/LICENSE +674 -0
  22. data/README.md +58 -0
  23. data/Rakefile +53 -0
  24. data/SUPPORT.md +9 -0
  25. data/bin/console +29 -0
  26. data/bin/setup +29 -0
  27. data/docs/compute.md +846 -0
  28. data/docs/connection_parameters.md +45 -0
  29. data/docs/getting_started.md +53 -0
  30. data/docs/identity.md +357 -0
  31. data/examples/compute.rb +279 -0
  32. data/examples/identity.rb +195 -0
  33. data/fog-proxmox.gemspec +63 -0
  34. data/fogproxmox.png +0 -0
  35. data/lib/fog/compute/proxmox.rb +131 -0
  36. data/lib/fog/compute/proxmox/models/container.rb +97 -0
  37. data/lib/fog/compute/proxmox/models/container_config.rb +90 -0
  38. data/lib/fog/compute/proxmox/models/containers.rb +40 -0
  39. data/lib/fog/compute/proxmox/models/disk.rb +67 -0
  40. data/lib/fog/compute/proxmox/models/disks.rb +49 -0
  41. data/lib/fog/compute/proxmox/models/interface.rb +50 -0
  42. data/lib/fog/compute/proxmox/models/interfaces.rb +46 -0
  43. data/lib/fog/compute/proxmox/models/node.rb +93 -0
  44. data/lib/fog/compute/proxmox/models/nodes.rb +45 -0
  45. data/lib/fog/compute/proxmox/models/server.rb +212 -0
  46. data/lib/fog/compute/proxmox/models/server_config.rb +116 -0
  47. data/lib/fog/compute/proxmox/models/servers.rb +70 -0
  48. data/lib/fog/compute/proxmox/models/snapshot.rb +70 -0
  49. data/lib/fog/compute/proxmox/models/snapshots.rb +55 -0
  50. data/lib/fog/compute/proxmox/models/storage.rb +61 -0
  51. data/lib/fog/compute/proxmox/models/storages.rb +60 -0
  52. data/lib/fog/compute/proxmox/models/task.rb +84 -0
  53. data/lib/fog/compute/proxmox/models/tasks.rb +71 -0
  54. data/lib/fog/compute/proxmox/models/volume.rb +61 -0
  55. data/lib/fog/compute/proxmox/models/volumes.rb +67 -0
  56. data/lib/fog/compute/proxmox/requests/action_server.rb +47 -0
  57. data/lib/fog/compute/proxmox/requests/check_vmid.rb +41 -0
  58. data/lib/fog/compute/proxmox/requests/clone_server.rb +46 -0
  59. data/lib/fog/compute/proxmox/requests/create_backup.rb +44 -0
  60. data/lib/fog/compute/proxmox/requests/create_server.rb +45 -0
  61. data/lib/fog/compute/proxmox/requests/create_snapshot.rb +47 -0
  62. data/lib/fog/compute/proxmox/requests/create_spice.rb +46 -0
  63. data/lib/fog/compute/proxmox/requests/create_term.rb +46 -0
  64. data/lib/fog/compute/proxmox/requests/create_vnc.rb +46 -0
  65. data/lib/fog/compute/proxmox/requests/delete_server.rb +47 -0
  66. data/lib/fog/compute/proxmox/requests/delete_snapshot.rb +48 -0
  67. data/lib/fog/compute/proxmox/requests/delete_volume.rb +40 -0
  68. data/lib/fog/compute/proxmox/requests/get_node.rb +44 -0
  69. data/lib/fog/compute/proxmox/requests/get_server_config.rb +45 -0
  70. data/lib/fog/compute/proxmox/requests/get_server_status.rb +45 -0
  71. data/lib/fog/compute/proxmox/requests/get_snapshot.rb +47 -0
  72. data/lib/fog/compute/proxmox/requests/get_storage.rb +44 -0
  73. data/lib/fog/compute/proxmox/requests/get_task.rb +44 -0
  74. data/lib/fog/compute/proxmox/requests/get_vnc.rb +46 -0
  75. data/lib/fog/compute/proxmox/requests/get_volume.rb +42 -0
  76. data/lib/fog/compute/proxmox/requests/list_nodes.rb +42 -0
  77. data/lib/fog/compute/proxmox/requests/list_servers.rb +42 -0
  78. data/lib/fog/compute/proxmox/requests/list_snapshots.rb +43 -0
  79. data/lib/fog/compute/proxmox/requests/list_storages.rb +42 -0
  80. data/lib/fog/compute/proxmox/requests/list_tasks.rb +42 -0
  81. data/lib/fog/compute/proxmox/requests/list_volumes.rb +42 -0
  82. data/lib/fog/compute/proxmox/requests/log_task.rb +45 -0
  83. data/lib/fog/compute/proxmox/requests/migrate_server.rb +44 -0
  84. data/lib/fog/compute/proxmox/requests/move_disk.rb +46 -0
  85. data/lib/fog/compute/proxmox/requests/move_volume.rb +46 -0
  86. data/lib/fog/compute/proxmox/requests/next_vmid.rb +39 -0
  87. data/lib/fog/compute/proxmox/requests/resize_container.rb +46 -0
  88. data/lib/fog/compute/proxmox/requests/resize_server.rb +43 -0
  89. data/lib/fog/compute/proxmox/requests/rollback_snapshot.rb +46 -0
  90. data/lib/fog/compute/proxmox/requests/status_task.rb +44 -0
  91. data/lib/fog/compute/proxmox/requests/stop_task.rb +41 -0
  92. data/lib/fog/compute/proxmox/requests/template_server.rb +43 -0
  93. data/lib/fog/compute/proxmox/requests/update_server.rb +46 -0
  94. data/lib/fog/compute/proxmox/requests/update_snapshot.rb +45 -0
  95. data/lib/fog/identity/proxmox.rb +132 -0
  96. data/lib/fog/identity/proxmox/models/activedirectory.rb +57 -0
  97. data/lib/fog/identity/proxmox/models/domain.rb +61 -0
  98. data/lib/fog/identity/proxmox/models/domains.rb +115 -0
  99. data/lib/fog/identity/proxmox/models/group.rb +60 -0
  100. data/lib/fog/identity/proxmox/models/groups.rb +50 -0
  101. data/lib/fog/identity/proxmox/models/ldap.rb +57 -0
  102. data/lib/fog/identity/proxmox/models/oath.rb +50 -0
  103. data/lib/fog/identity/proxmox/models/pam.rb +46 -0
  104. data/lib/fog/identity/proxmox/models/permission.rb +69 -0
  105. data/lib/fog/identity/proxmox/models/permissions.rb +61 -0
  106. data/lib/fog/identity/proxmox/models/pool.rb +84 -0
  107. data/lib/fog/identity/proxmox/models/pools.rb +50 -0
  108. data/lib/fog/identity/proxmox/models/principal.rb +47 -0
  109. data/lib/fog/identity/proxmox/models/pve.rb +46 -0
  110. data/lib/fog/identity/proxmox/models/role.rb +61 -0
  111. data/lib/fog/identity/proxmox/models/roles.rb +51 -0
  112. data/lib/fog/identity/proxmox/models/user.rb +74 -0
  113. data/lib/fog/identity/proxmox/models/users.rb +50 -0
  114. data/lib/fog/identity/proxmox/models/yubico.rb +51 -0
  115. data/lib/fog/identity/proxmox/requests/add_permission.rb +41 -0
  116. data/lib/fog/identity/proxmox/requests/change_password.rb +41 -0
  117. data/lib/fog/identity/proxmox/requests/check_permissions.rb +44 -0
  118. data/lib/fog/identity/proxmox/requests/create_domain.rb +40 -0
  119. data/lib/fog/identity/proxmox/requests/create_group.rb +40 -0
  120. data/lib/fog/identity/proxmox/requests/create_pool.rb +40 -0
  121. data/lib/fog/identity/proxmox/requests/create_role.rb +40 -0
  122. data/lib/fog/identity/proxmox/requests/create_user.rb +40 -0
  123. data/lib/fog/identity/proxmox/requests/delete_domain.rb +40 -0
  124. data/lib/fog/identity/proxmox/requests/delete_group.rb +40 -0
  125. data/lib/fog/identity/proxmox/requests/delete_pool.rb +39 -0
  126. data/lib/fog/identity/proxmox/requests/delete_role.rb +40 -0
  127. data/lib/fog/identity/proxmox/requests/delete_user.rb +39 -0
  128. data/lib/fog/identity/proxmox/requests/get_domain.rb +41 -0
  129. data/lib/fog/identity/proxmox/requests/get_group.rb +41 -0
  130. data/lib/fog/identity/proxmox/requests/get_pool.rb +41 -0
  131. data/lib/fog/identity/proxmox/requests/get_role.rb +41 -0
  132. data/lib/fog/identity/proxmox/requests/get_user.rb +40 -0
  133. data/lib/fog/identity/proxmox/requests/list_domains.rb +41 -0
  134. data/lib/fog/identity/proxmox/requests/list_groups.rb +41 -0
  135. data/lib/fog/identity/proxmox/requests/list_permissions.rb +41 -0
  136. data/lib/fog/identity/proxmox/requests/list_pools.rb +41 -0
  137. data/lib/fog/identity/proxmox/requests/list_roles.rb +41 -0
  138. data/lib/fog/identity/proxmox/requests/list_users.rb +42 -0
  139. data/lib/fog/identity/proxmox/requests/read_version.rb +43 -0
  140. data/lib/fog/identity/proxmox/requests/remove_permission.rb +42 -0
  141. data/lib/fog/identity/proxmox/requests/update_domain.rb +41 -0
  142. data/lib/fog/identity/proxmox/requests/update_group.rb +41 -0
  143. data/lib/fog/identity/proxmox/requests/update_pool.rb +41 -0
  144. data/lib/fog/identity/proxmox/requests/update_role.rb +41 -0
  145. data/lib/fog/identity/proxmox/requests/update_user.rb +41 -0
  146. data/lib/fog/network/proxmox.rb +83 -0
  147. data/lib/fog/network/proxmox/models/network.rb +82 -0
  148. data/lib/fog/network/proxmox/models/networks.rb +54 -0
  149. data/lib/fog/network/proxmox/models/node.rb +70 -0
  150. data/lib/fog/network/proxmox/models/nodes.rb +45 -0
  151. data/lib/fog/network/proxmox/requests/create_network.rb +44 -0
  152. data/lib/fog/network/proxmox/requests/delete_network.rb +45 -0
  153. data/lib/fog/network/proxmox/requests/get_network.rb +46 -0
  154. data/lib/fog/network/proxmox/requests/get_node.rb +44 -0
  155. data/lib/fog/network/proxmox/requests/list_networks.rb +43 -0
  156. data/lib/fog/network/proxmox/requests/list_nodes.rb +42 -0
  157. data/lib/fog/network/proxmox/requests/power_node.rb +46 -0
  158. data/lib/fog/network/proxmox/requests/update_network.rb +46 -0
  159. data/lib/fog/proxmox.rb +128 -0
  160. data/lib/fog/proxmox/core.rb +139 -0
  161. data/lib/fog/proxmox/errors.rb +64 -0
  162. data/lib/fog/proxmox/hash.rb +33 -0
  163. data/lib/fog/proxmox/helpers/controller_helper.rb +55 -0
  164. data/lib/fog/proxmox/helpers/cpu_helper.rb +45 -0
  165. data/lib/fog/proxmox/helpers/disk_helper.rb +84 -0
  166. data/lib/fog/proxmox/helpers/nic_helper.rb +62 -0
  167. data/lib/fog/proxmox/json.rb +32 -0
  168. data/lib/fog/proxmox/models/collection.rb +85 -0
  169. data/lib/fog/proxmox/models/model.rb +58 -0
  170. data/lib/fog/proxmox/variables.rb +40 -0
  171. data/lib/fog/proxmox/version.rb +24 -0
  172. data/lib/fog/storage/proxmox.rb +29 -0
  173. data/spec/compute_spec.rb +408 -0
  174. data/spec/fixtures/proxmox/compute/containers.yml +5398 -0
  175. data/spec/fixtures/proxmox/compute/identity_ticket.yml +40 -0
  176. data/spec/fixtures/proxmox/compute/servers.yml +10571 -0
  177. data/spec/fixtures/proxmox/compute/snapshots.yml +1228 -0
  178. data/spec/fixtures/proxmox/compute/storages.yml +120 -0
  179. data/spec/fixtures/proxmox/compute/tasks.yml +200 -0
  180. data/spec/fixtures/proxmox/identity/auth.yml +44 -0
  181. data/spec/fixtures/proxmox/identity/domains.yml +531 -0
  182. data/spec/fixtures/proxmox/identity/groups.yml +324 -0
  183. data/spec/fixtures/proxmox/identity/identity_ticket.yml +40 -0
  184. data/spec/fixtures/proxmox/identity/permissions.yml +565 -0
  185. data/spec/fixtures/proxmox/identity/pools.yml +488 -0
  186. data/spec/fixtures/proxmox/identity/read_version.yml +42 -0
  187. data/spec/fixtures/proxmox/identity/roles.yml +324 -0
  188. data/spec/fixtures/proxmox/identity/users.yml +646 -0
  189. data/spec/fixtures/proxmox/network/identity_ticket.yml +40 -0
  190. data/spec/fixtures/proxmox/network/networks.yml +413 -0
  191. data/spec/helpers/controller_helper_spec.rb +77 -0
  192. data/spec/helpers/cpu_helper_spec.rb +91 -0
  193. data/spec/helpers/disk_helper_spec.rb +104 -0
  194. data/spec/helpers/nic_helper_spec.rb +73 -0
  195. data/spec/identity_spec.rb +316 -0
  196. data/spec/network_spec.rb +67 -0
  197. data/spec/proxmox_vcr.rb +102 -0
  198. data/spec/spec_helper.rb +39 -0
  199. metadata +535 -0
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ # Copyright 2018 Tristan Robert
3
+
4
+ # This file is part of Fog::Proxmox.
5
+
6
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+
11
+ # Fog::Proxmox is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ # frozen_string_literal: true
20
+
21
+ require 'fog/proxmox/json'
22
+
23
+ module Fog
24
+ module Network
25
+ class Proxmox
26
+ # class Real power_node request
27
+ class Real
28
+ def power_node(path_params, body_params)
29
+ node = path_params[:node]
30
+ response = request(
31
+ expects: [200],
32
+ method: 'POST',
33
+ path: "nodes/#{node}/status",
34
+ body: URI.encode_www_form(body_params)
35
+ )
36
+ Fog::Proxmox::Json.get_data(response)
37
+ end
38
+ end
39
+
40
+ # class Mock power_node request
41
+ class Mock
42
+ def power_node; end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/json'
21
+
22
+ module Fog
23
+ module Network
24
+ class Proxmox
25
+ # class Real update_network request
26
+ class Real
27
+ def update_network(path_params, body_params)
28
+ node = path_params[:node]
29
+ type = body_params[:type]
30
+ iface = path_params[:iface]
31
+ response = request(
32
+ expects: [200],
33
+ method: 'PUT',
34
+ path: "nodes/#{node}/network/#{iface}",
35
+ body: URI.encode_www_form(body_params)
36
+ )
37
+ Fog::Proxmox::Json.get_data(response)
38
+ end
39
+ end
40
+
41
+ # class Mock update_network request
42
+ class Mock
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+ # Copyright 2018 Tristan Robert
3
+
4
+ # This file is part of Fog::Proxmox.
5
+
6
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+
11
+ # Fog::Proxmox is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ # frozen_string_literal: true
20
+
21
+ require 'fog/proxmox/version'
22
+ require 'fog/proxmox/core'
23
+ require 'fog/proxmox/json'
24
+ require 'fog/core'
25
+ require 'fog/json'
26
+
27
+ module Fog
28
+ # Identity module
29
+ module Identity
30
+ autoload :Proxmox, File.expand_path('identity/proxmox', __dir__)
31
+ end
32
+ # Compute module
33
+ module Compute
34
+ autoload :Proxmox, File.expand_path('compute/proxmox', __dir__)
35
+ end
36
+ # Storage module
37
+ module Storage
38
+ autoload :Proxmox, File.expand_path('storage/proxmox', __dir__)
39
+ end
40
+ # Network module
41
+ module Network
42
+ autoload :Proxmox, File.expand_path('network/proxmox', __dir__)
43
+ end
44
+
45
+ # Proxmox module
46
+ module Proxmox
47
+ extend Fog::Provider
48
+ service(:identity, 'Identity')
49
+ service(:compute, 'Compute')
50
+ service(:storage, 'Storage')
51
+ service(:network, 'Network')
52
+
53
+ @cache = {}
54
+
55
+ # Default lifetime ticket is 2 hours
56
+ @ticket_lifetime = 2 * 60 * 60
57
+
58
+ class << self
59
+ attr_accessor :cache
60
+ attr_reader :version
61
+ end
62
+
63
+ def self.clear_cache
64
+ Fog::Proxmox.cache = {}
65
+ end
66
+
67
+ def self.authenticate(options, connection_options = {})
68
+ get_tokens(options, connection_options)
69
+ @cache
70
+ end
71
+
72
+ def self.authenticated?
73
+ !@cache.empty?
74
+ end
75
+
76
+ def self.extract_password(options)
77
+ ticket = options[:pve_ticket]
78
+ options[:pve_password] = ticket unless ticket
79
+ end
80
+
81
+ def self.get_tokens(options, connection_options = {})
82
+ username = options[:pve_username].to_s
83
+ password = options[:pve_password].to_s
84
+ url = options[:pve_url]
85
+ extract_password(options)
86
+ uri = URI.parse(url)
87
+ @api_path = uri.path
88
+ connection_options = connection_options.merge(path_prefix: @api_path)
89
+ retrieve_tokens(uri, connection_options, username, password) unless authenticated?
90
+ end
91
+
92
+ def self.retrieve_tokens(uri, connection_options, username, password)
93
+ request = {
94
+ expects: [200, 204],
95
+ headers: { 'Accept' => 'application/json' },
96
+ body: "username=#{username}&password=#{password}",
97
+ method: 'POST',
98
+ path: 'access/ticket'
99
+ }
100
+
101
+ connection = Fog::Core::Connection.new(
102
+ uri.to_s,
103
+ false,
104
+ connection_options
105
+ )
106
+
107
+ response = connection.request(request)
108
+ data = Json.get_data(response)
109
+ ticket = data['ticket']
110
+ username = data['username']
111
+ csrftoken = data['CSRFPreventionToken']
112
+
113
+ now = Time.now
114
+ deadline = Time.at(now.to_i + @ticket_lifetime)
115
+ save_token(username, ticket, csrftoken, deadline)
116
+ end
117
+
118
+ def self.save_token(username, ticket, csrftoken, deadline)
119
+ @cache = {
120
+ username: username,
121
+ ticket: ticket,
122
+ csrftoken: csrftoken,
123
+ deadline: deadline
124
+ }
125
+ Fog::Proxmox.cache = @cache
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/json'
21
+ require 'fog/proxmox/variables'
22
+
23
+ module Fog
24
+ module Proxmox
25
+ # Core module
26
+ module Core
27
+ attr_accessor :pve_ticket
28
+ attr_reader :pve_csrftoken
29
+ attr_reader :pve_username
30
+ attr_reader :deadline
31
+ attr_reader :principal
32
+
33
+ # fallback
34
+ def self.not_found_class
35
+ Fog::Compute::Proxmox::NotFound
36
+ end
37
+
38
+ def initialize_identity(options)
39
+ @principal = nil
40
+ @pve_must_reauthenticate = true
41
+ @pve_ticket = nil
42
+ Fog::Proxmox::Variables.to_variables(self, options, 'pve')
43
+ @pve_uri = URI.parse(@pve_url)
44
+ @pve_must_reauthenticate = true unless @pve_ticket
45
+ missing_credentials = []
46
+ missing_credentials << :pve_username unless @pve_username
47
+
48
+ unless @pve_ticket
49
+ missing_credentials << :pve_password unless @pve_password
50
+ end
51
+
52
+ raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
53
+ end
54
+
55
+ def credentials
56
+ options = {
57
+ provider: 'proxmox',
58
+ pve_url: @pve_uri.to_s,
59
+ pve_ticket: @pve_ticket,
60
+ pve_csrftoken: @pve_csrftoken,
61
+ pve_username: @pve_username
62
+ }
63
+ pve_options.merge options
64
+ end
65
+
66
+ def reload
67
+ @connection.reset
68
+ end
69
+
70
+ private
71
+
72
+ def request(params)
73
+ retried = false
74
+ begin
75
+ response = @connection.request(params.merge(
76
+ headers: headers(params[:method], params[:headers])
77
+ ))
78
+ rescue Excon::Errors::Unauthorized => error
79
+ # token expiration and token renewal possible
80
+ if error.response.body != 'Bad username or password' && @pve_can_reauthenticate && !retried
81
+ authenticate
82
+ retried = true
83
+ retry
84
+ # bad credentials or token renewal not possible
85
+ else
86
+ raise error
87
+ end
88
+ rescue Excon::Errors::HTTPStatusError => error
89
+ raise case error
90
+ when Excon::Errors::NotFound
91
+ self.class.not_found_class.slurp(error)
92
+ else
93
+ error
94
+ end
95
+ end
96
+ response
97
+ end
98
+
99
+ def headers(method, additional_headers)
100
+ additional_headers ||= {}
101
+ headers_hash = { 'Accept' => 'application/json' }
102
+ # CSRF token is required to PUT, POST and DELETE http requests
103
+ if %w[PUT POST DELETE].include? method
104
+ headers_hash.store('CSRFPreventionToken', @pve_csrftoken)
105
+ end
106
+ # ticket must be present in cookie
107
+ headers_hash.store('Cookie', "PVEAuthCookie=#{@pve_ticket}") if @pve_ticket
108
+ headers_hash.merge additional_headers
109
+ headers_hash
110
+ end
111
+
112
+ def pve_options
113
+ Fog::Proxmox::Variables.to_hash(self, 'pve')
114
+ end
115
+
116
+ def authenticate
117
+ unless @principal
118
+ options = pve_options
119
+ options[:pve_ticket] = @pve_must_reauthenticate ? nil : @pve_ticket
120
+ credentials = Fog::Proxmox.authenticate(options, @connection_options)
121
+ @principal = credentials
122
+ @pve_username = credentials[:username]
123
+ @pve_ticket = credentials[:ticket]
124
+ @pve_deadline = credentials[:deadline]
125
+ @pve_csrftoken = credentials[:csrftoken]
126
+ @pve_must_reauthenticate = false
127
+ end
128
+
129
+ @host = @pve_uri.host
130
+ @api_path = @pve_uri.path
131
+ @api_path.sub!(%r{/$}, '')
132
+ @port = @pve_uri.port
133
+ @scheme = @pve_uri.scheme
134
+
135
+ true
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module Fog
21
+ module Proxmox
22
+ module Errors
23
+ # class ServiceError
24
+ class ServiceError < Fog::Errors::Error
25
+ attr_reader :response_data
26
+
27
+ def self.slurp(error)
28
+ if error.response.body.empty?
29
+ data = nil
30
+ message = nil
31
+ else
32
+ data = Fog::JSON.decode(error.response.body)
33
+ message = data['message']
34
+ data1 = data.values.first
35
+ message = data1['message'] if message.nil? && !data1.nil?
36
+ end
37
+
38
+ new_error = super(error, message)
39
+ new_error.instance_variable_set(:@response_data, data)
40
+ new_error
41
+ end
42
+ end
43
+
44
+ # class ServiceUnavailable error
45
+ class ServiceUnavailable < ServiceError; end
46
+
47
+ # class BadRequest error
48
+ class BadRequest < ServiceError
49
+ attr_reader :validation_errors
50
+
51
+ def self.slurp(error)
52
+ new_error = super(error)
53
+ unless new_error.response_data.nil? || new_error.response_data['badRequest'].nil?
54
+ new_error.instance_variable_set(:@validation_errors, new_error.response_data['badRequest']['validationErrors'])
55
+ end
56
+ new_error
57
+ end
58
+ end
59
+
60
+ # class InterfaceNotImplemented error
61
+ class InterfaceNotImplemented < Fog::Errors::Error; end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module Fog
21
+ module Proxmox
22
+ # module Hash mixins
23
+ module Hash
24
+ def self.stringify(hash)
25
+ string = ''
26
+ hash.each do |key, value|
27
+ string += "#{key}=#{value},"
28
+ end
29
+ string.slice(0, string.length - 1)
30
+ end
31
+ end
32
+ end
33
+ end