fog-proxmox 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +7 -0
  2. data/.bundle/config +4 -0
  3. data/.codeclimate.yml +14 -0
  4. data/.github/CODE_OF_CONDUCT.md +74 -0
  5. data/.github/CONTRIBUTING.md +20 -0
  6. data/.github/CONTRIBUTORS.md +9 -0
  7. data/.github/FUNDING.yml +12 -0
  8. data/.github/ISSUE_TEMPLATE.md +43 -0
  9. data/.github/ISSUE_TEMPLATE/bug_report.md +35 -0
  10. data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
  11. data/.github/SUPPORT.md +9 -0
  12. data/.github/fogproxmox.png +0 -0
  13. data/.github/workflows/ci.yml +79 -0
  14. data/.gitignore +8 -0
  15. data/.rubocop.yml +13 -0
  16. data/.ruby-gemset +1 -0
  17. data/.solargraph.yml +10 -0
  18. data/.vscode/launch.json +96 -0
  19. data/.vscode/settings.json +45 -0
  20. data/.vscode/tasks.json +27 -0
  21. data/Gemfile +23 -0
  22. data/LICENSE +674 -0
  23. data/README.md +79 -0
  24. data/Rakefile +58 -0
  25. data/docs/compute.md +858 -0
  26. data/docs/connection_parameters.md +45 -0
  27. data/docs/getting_started.md +53 -0
  28. data/docs/identity.md +357 -0
  29. data/examples/compute.rb +276 -0
  30. data/examples/identity.rb +190 -0
  31. data/fog-proxmox.gemspec +63 -0
  32. data/lib/fog/compute/proxmox.rb +126 -0
  33. data/lib/fog/compute/proxmox/models/disk.rb +76 -0
  34. data/lib/fog/compute/proxmox/models/disks.rb +51 -0
  35. data/lib/fog/compute/proxmox/models/interface.rb +52 -0
  36. data/lib/fog/compute/proxmox/models/interfaces.rb +44 -0
  37. data/lib/fog/compute/proxmox/models/node.rb +95 -0
  38. data/lib/fog/compute/proxmox/models/nodes.rb +39 -0
  39. data/lib/fog/compute/proxmox/models/server.rb +254 -0
  40. data/lib/fog/compute/proxmox/models/server_config.rb +148 -0
  41. data/lib/fog/compute/proxmox/models/servers.rb +80 -0
  42. data/lib/fog/compute/proxmox/models/snapshot.rb +83 -0
  43. data/lib/fog/compute/proxmox/models/snapshots.rb +47 -0
  44. data/lib/fog/compute/proxmox/models/storage.rb +64 -0
  45. data/lib/fog/compute/proxmox/models/storages.rb +51 -0
  46. data/lib/fog/compute/proxmox/models/task.rb +79 -0
  47. data/lib/fog/compute/proxmox/models/tasks.rb +64 -0
  48. data/lib/fog/compute/proxmox/models/volume.rb +61 -0
  49. data/lib/fog/compute/proxmox/models/volumes.rb +58 -0
  50. data/lib/fog/compute/proxmox/requests/action_server.rb +45 -0
  51. data/lib/fog/compute/proxmox/requests/clone_server.rb +44 -0
  52. data/lib/fog/compute/proxmox/requests/create_backup.rb +42 -0
  53. data/lib/fog/compute/proxmox/requests/create_server.rb +43 -0
  54. data/lib/fog/compute/proxmox/requests/create_snapshot.rb +46 -0
  55. data/lib/fog/compute/proxmox/requests/create_spice.rb +44 -0
  56. data/lib/fog/compute/proxmox/requests/create_term.rb +44 -0
  57. data/lib/fog/compute/proxmox/requests/create_vnc.rb +44 -0
  58. data/lib/fog/compute/proxmox/requests/delete_server.rb +45 -0
  59. data/lib/fog/compute/proxmox/requests/delete_snapshot.rb +47 -0
  60. data/lib/fog/compute/proxmox/requests/delete_volume.rb +40 -0
  61. data/lib/fog/compute/proxmox/requests/get_node_statistics.rb +44 -0
  62. data/lib/fog/compute/proxmox/requests/get_server_config.rb +60 -0
  63. data/lib/fog/compute/proxmox/requests/get_server_status.rb +63 -0
  64. data/lib/fog/compute/proxmox/requests/get_snapshot_config.rb +44 -0
  65. data/lib/fog/compute/proxmox/requests/get_task.rb +41 -0
  66. data/lib/fog/compute/proxmox/requests/get_vnc.rb +44 -0
  67. data/lib/fog/compute/proxmox/requests/get_volume.rb +42 -0
  68. data/lib/fog/compute/proxmox/requests/list_nodes.rb +48 -0
  69. data/lib/fog/compute/proxmox/requests/list_servers.rb +113 -0
  70. data/lib/fog/compute/proxmox/requests/list_snapshots.rb +66 -0
  71. data/lib/fog/compute/proxmox/requests/list_storages.rb +46 -0
  72. data/lib/fog/compute/proxmox/requests/list_tasks.rb +44 -0
  73. data/lib/fog/compute/proxmox/requests/list_volumes.rb +44 -0
  74. data/lib/fog/compute/proxmox/requests/log_task.rb +44 -0
  75. data/lib/fog/compute/proxmox/requests/migrate_server.rb +44 -0
  76. data/lib/fog/compute/proxmox/requests/move_disk.rb +43 -0
  77. data/lib/fog/compute/proxmox/requests/move_volume.rb +43 -0
  78. data/lib/fog/compute/proxmox/requests/next_vmid.rb +42 -0
  79. data/lib/fog/compute/proxmox/requests/resize_container.rb +43 -0
  80. data/lib/fog/compute/proxmox/requests/resize_server.rb +43 -0
  81. data/lib/fog/compute/proxmox/requests/rollback_snapshot.rb +48 -0
  82. data/lib/fog/compute/proxmox/requests/status_task.rb +41 -0
  83. data/lib/fog/compute/proxmox/requests/stop_task.rb +41 -0
  84. data/lib/fog/compute/proxmox/requests/template_server.rb +44 -0
  85. data/lib/fog/compute/proxmox/requests/update_server.rb +44 -0
  86. data/lib/fog/compute/proxmox/requests/update_snapshot.rb +45 -0
  87. data/lib/fog/identity/proxmox.rb +130 -0
  88. data/lib/fog/identity/proxmox/models/domain.rb +65 -0
  89. data/lib/fog/identity/proxmox/models/domain_type.rb +48 -0
  90. data/lib/fog/identity/proxmox/models/domains.rb +45 -0
  91. data/lib/fog/identity/proxmox/models/group.rb +47 -0
  92. data/lib/fog/identity/proxmox/models/groups.rb +44 -0
  93. data/lib/fog/identity/proxmox/models/permission.rb +65 -0
  94. data/lib/fog/identity/proxmox/models/permissions.rb +44 -0
  95. data/lib/fog/identity/proxmox/models/pool.rb +89 -0
  96. data/lib/fog/identity/proxmox/models/pools.rb +46 -0
  97. data/lib/fog/identity/proxmox/models/principal.rb +42 -0
  98. data/lib/fog/identity/proxmox/models/role.rb +48 -0
  99. data/lib/fog/identity/proxmox/models/roles.rb +44 -0
  100. data/lib/fog/identity/proxmox/models/user.rb +60 -0
  101. data/lib/fog/identity/proxmox/models/users.rb +44 -0
  102. data/lib/fog/identity/proxmox/requests/change_password.rb +41 -0
  103. data/lib/fog/identity/proxmox/requests/check_permissions.rb +41 -0
  104. data/lib/fog/identity/proxmox/requests/create_domain.rb +40 -0
  105. data/lib/fog/identity/proxmox/requests/create_group.rb +40 -0
  106. data/lib/fog/identity/proxmox/requests/create_pool.rb +40 -0
  107. data/lib/fog/identity/proxmox/requests/create_role.rb +40 -0
  108. data/lib/fog/identity/proxmox/requests/create_user.rb +40 -0
  109. data/lib/fog/identity/proxmox/requests/delete_domain.rb +40 -0
  110. data/lib/fog/identity/proxmox/requests/delete_group.rb +40 -0
  111. data/lib/fog/identity/proxmox/requests/delete_pool.rb +39 -0
  112. data/lib/fog/identity/proxmox/requests/delete_role.rb +40 -0
  113. data/lib/fog/identity/proxmox/requests/delete_user.rb +39 -0
  114. data/lib/fog/identity/proxmox/requests/get_domain.rb +41 -0
  115. data/lib/fog/identity/proxmox/requests/get_group.rb +41 -0
  116. data/lib/fog/identity/proxmox/requests/get_pool.rb +41 -0
  117. data/lib/fog/identity/proxmox/requests/get_role.rb +41 -0
  118. data/lib/fog/identity/proxmox/requests/get_user.rb +40 -0
  119. data/lib/fog/identity/proxmox/requests/list_domains.rb +41 -0
  120. data/lib/fog/identity/proxmox/requests/list_groups.rb +41 -0
  121. data/lib/fog/identity/proxmox/requests/list_permissions.rb +41 -0
  122. data/lib/fog/identity/proxmox/requests/list_pools.rb +41 -0
  123. data/lib/fog/identity/proxmox/requests/list_roles.rb +40 -0
  124. data/lib/fog/identity/proxmox/requests/list_users.rb +42 -0
  125. data/lib/fog/identity/proxmox/requests/read_version.rb +39 -0
  126. data/lib/fog/identity/proxmox/requests/update_domain.rb +41 -0
  127. data/lib/fog/identity/proxmox/requests/update_group.rb +40 -0
  128. data/lib/fog/identity/proxmox/requests/update_permissions.rb +41 -0
  129. data/lib/fog/identity/proxmox/requests/update_pool.rb +41 -0
  130. data/lib/fog/identity/proxmox/requests/update_role.rb +41 -0
  131. data/lib/fog/identity/proxmox/requests/update_user.rb +41 -0
  132. data/lib/fog/network/proxmox.rb +83 -0
  133. data/lib/fog/network/proxmox/models/network.rb +76 -0
  134. data/lib/fog/network/proxmox/models/networks.rb +48 -0
  135. data/lib/fog/network/proxmox/models/node.rb +74 -0
  136. data/lib/fog/network/proxmox/models/nodes.rb +39 -0
  137. data/lib/fog/network/proxmox/requests/create_network.rb +41 -0
  138. data/lib/fog/network/proxmox/requests/delete_network.rb +42 -0
  139. data/lib/fog/network/proxmox/requests/get_network.rb +43 -0
  140. data/lib/fog/network/proxmox/requests/get_node.rb +41 -0
  141. data/lib/fog/network/proxmox/requests/list_networks.rb +43 -0
  142. data/lib/fog/network/proxmox/requests/list_nodes.rb +42 -0
  143. data/lib/fog/network/proxmox/requests/power_node.rb +43 -0
  144. data/lib/fog/network/proxmox/requests/update_network.rb +42 -0
  145. data/lib/fog/proxmox.rb +115 -0
  146. data/lib/fog/proxmox/attributes.rb +33 -0
  147. data/lib/fog/proxmox/core.rb +135 -0
  148. data/lib/fog/proxmox/errors.rb +64 -0
  149. data/lib/fog/proxmox/hash.rb +39 -0
  150. data/lib/fog/proxmox/helpers/controller_helper.rb +64 -0
  151. data/lib/fog/proxmox/helpers/cpu_helper.rb +42 -0
  152. data/lib/fog/proxmox/helpers/disk_helper.rb +159 -0
  153. data/lib/fog/proxmox/helpers/ip_helper.rb +100 -0
  154. data/lib/fog/proxmox/helpers/nic_helper.rb +115 -0
  155. data/lib/fog/proxmox/json.rb +32 -0
  156. data/lib/fog/proxmox/string.rb +31 -0
  157. data/lib/fog/proxmox/variables.rb +40 -0
  158. data/lib/fog/proxmox/version.rb +24 -0
  159. data/lib/fog/storage/proxmox.rb +29 -0
  160. data/spec/compute_spec.rb +446 -0
  161. data/spec/fixtures/proxmox/compute/containers.yml +12737 -0
  162. data/spec/fixtures/proxmox/compute/identity_ticket.yml +40 -0
  163. data/spec/fixtures/proxmox/compute/nodes.yml +121 -0
  164. data/spec/fixtures/proxmox/compute/servers.yml +24679 -0
  165. data/spec/fixtures/proxmox/compute/snapshots.yml +3321 -0
  166. data/spec/fixtures/proxmox/compute/storages.yml +159 -0
  167. data/spec/fixtures/proxmox/compute/tasks.yml +685 -0
  168. data/spec/fixtures/proxmox/identity/auth.yml +44 -0
  169. data/spec/fixtures/proxmox/identity/domains.yml +701 -0
  170. data/spec/fixtures/proxmox/identity/groups.yml +363 -0
  171. data/spec/fixtures/proxmox/identity/identity_ticket.yml +40 -0
  172. data/spec/fixtures/proxmox/identity/permissions.yml +803 -0
  173. data/spec/fixtures/proxmox/identity/pools.yml +1191 -0
  174. data/spec/fixtures/proxmox/identity/read_version.yml +42 -0
  175. data/spec/fixtures/proxmox/identity/renew.yml +40 -0
  176. data/spec/fixtures/proxmox/identity/roles.yml +363 -0
  177. data/spec/fixtures/proxmox/identity/users.yml +763 -0
  178. data/spec/fixtures/proxmox/network/identity_ticket.yml +40 -0
  179. data/spec/fixtures/proxmox/network/networks.yml +372 -0
  180. data/spec/fixtures/proxmox/pve.home +34 -0
  181. data/spec/hash_spec.rb +42 -0
  182. data/spec/helpers/controller_helper_spec.rb +145 -0
  183. data/spec/helpers/cpu_helper_spec.rb +70 -0
  184. data/spec/helpers/disk_helper_spec.rb +210 -0
  185. data/spec/helpers/ip_helper_spec.rb +160 -0
  186. data/spec/helpers/nic_helper_spec.rb +136 -0
  187. data/spec/identity_spec.rb +322 -0
  188. data/spec/network_spec.rb +67 -0
  189. data/spec/proxmox_vcr.rb +95 -0
  190. data/spec/spec_helper.rb +40 -0
  191. metadata +547 -0
@@ -0,0 +1,42 @@
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
+ class Network
23
+ # class Real update_network request
24
+ class Real
25
+ def update_network(path_params, body_params)
26
+ node = path_params[:node]
27
+ iface = path_params[:iface]
28
+ request(
29
+ expects: [200],
30
+ method: 'PUT',
31
+ path: "nodes/#{node}/network/#{iface}",
32
+ body: URI.encode_www_form(body_params)
33
+ )
34
+ end
35
+ end
36
+
37
+ # class Mock update_network request
38
+ class Mock
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,115 @@
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
+ # Proxmox module
29
+ module Proxmox
30
+ extend Fog::Provider
31
+ autoload :Identity, File.expand_path('identity/proxmox', __dir__)
32
+ autoload :Compute, File.expand_path('compute/proxmox', __dir__)
33
+ autoload :Storage, File.expand_path('storage/proxmox', __dir__)
34
+ autoload :Network, File.expand_path('network/proxmox', __dir__)
35
+ service(:identity, 'Identity')
36
+ service(:compute, 'Compute')
37
+ service(:storage, 'Storage')
38
+ service(:network, 'Network')
39
+
40
+ @credentials = {}
41
+
42
+ class << self
43
+ attr_reader :credentials
44
+ attr_reader :version
45
+ end
46
+
47
+ def self.clear_credentials
48
+ @credentials = {}
49
+ end
50
+
51
+ def self.authenticate(options, connection_options = {})
52
+ get_credentials(options, connection_options)
53
+ self
54
+ end
55
+
56
+ def self.authenticated?
57
+ !@credentials.empty?
58
+ end
59
+
60
+ def self.credentials_has_expired?
61
+ authenticated? && @credentials[:deadline] < Time.now
62
+ end
63
+
64
+ def self.extract_password(options)
65
+ ticket = options[:pve_ticket]
66
+ ticket ? ticket : options[:pve_password].to_s
67
+ end
68
+
69
+ def self.get_credentials(options, connection_options = {})
70
+ pve_ticket_lifetime = options[:pve_ticket_lifetime]
71
+ # Default lifetime ticket is 2 hours
72
+ ticket_lifetime = pve_ticket_lifetime ? pve_ticket_lifetime : 2 * 60 * 60
73
+ username = options[:pve_username].to_s
74
+ password = extract_password(options)
75
+ url = options[:pve_url]
76
+ uri = URI.parse(url)
77
+ @api_path = uri.path
78
+ connection_options = connection_options.merge(path_prefix: @api_path)
79
+ password = @credentials[:ticket] if credentials_has_expired?
80
+ request_credentials(uri, connection_options, username, password, ticket_lifetime)
81
+ end
82
+
83
+ def self.request_credentials(uri, connection_options, username, password, ticket_lifetime)
84
+ request = {
85
+ expects: [200, 204],
86
+ headers: { 'Accept' => 'application/json' },
87
+ body: URI.encode_www_form(username: username, password: password),
88
+ method: 'POST',
89
+ path: 'access/ticket'
90
+ }
91
+ connection = Fog::Core::Connection.new(
92
+ uri.to_s,
93
+ false,
94
+ connection_options
95
+ )
96
+ response = connection.request(request)
97
+ data = Json.get_data(response)
98
+ ticket = data['ticket']
99
+ username = data['username']
100
+ csrftoken = data['CSRFPreventionToken']
101
+ epoch = Time.now.to_i + ticket_lifetime
102
+ deadline = Time.at(epoch)
103
+ save_credentials(username, ticket, csrftoken, deadline)
104
+ end
105
+
106
+ def self.save_credentials(username, ticket, csrftoken, deadline)
107
+ @credentials = {
108
+ username: username,
109
+ ticket: ticket,
110
+ csrftoken: csrftoken,
111
+ deadline: deadline
112
+ }
113
+ end
114
+ end
115
+ 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 Attributes mixins
23
+ module Attributes
24
+ def self.set_attr(attr_name, attributes, new_attributes)
25
+ attributes[attr_name.to_sym] = new_attributes[attr_name] unless new_attributes[attr_name].nil?
26
+ end
27
+ def self.set_attr_and_sym(attr_name, attributes, new_attributes)
28
+ self.set_attr(attr_name, attributes, new_attributes)
29
+ self.set_attr(attr_name.to_sym, attributes, new_attributes)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,135 @@
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
+ require 'fog/proxmox/json'
23
+
24
+ module Fog
25
+ module Proxmox
26
+ # Core module
27
+ module Core
28
+ attr_accessor :pve_ticket
29
+ attr_reader :pve_csrftoken
30
+ attr_reader :pve_username
31
+ attr_reader :deadline
32
+ attr_reader :principal
33
+
34
+ # fallback
35
+ def self.not_found_class
36
+ Fog::Proxmox::Compute::NotFound
37
+ end
38
+
39
+ def initialize_identity(options)
40
+ @principal = nil
41
+ @pve_ticket = nil
42
+ Fog::Proxmox::Variables.to_variables(self, options, 'pve')
43
+ @pve_uri = URI.parse(@pve_url)
44
+ missing_credentials = []
45
+ missing_credentials << :pve_username unless @pve_username
46
+
47
+ unless @pve_ticket
48
+ missing_credentials << :pve_password unless @pve_password
49
+ end
50
+
51
+ raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
52
+ end
53
+
54
+ def credentials
55
+ options = {
56
+ provider: 'proxmox',
57
+ pve_url: @pve_uri.to_s,
58
+ pve_ticket: @pve_ticket,
59
+ pve_csrftoken: @pve_csrftoken,
60
+ pve_username: @pve_username
61
+ }
62
+ pve_options.merge options
63
+ end
64
+
65
+ def reload
66
+ @connection.reset
67
+ end
68
+
69
+ private
70
+
71
+ def request(params)
72
+ retried = false
73
+ begin
74
+ response = @connection.request(params.merge(
75
+ headers: headers(params[:method], params[:headers])
76
+ ))
77
+ rescue Excon::Errors::Unauthorized => error
78
+ # token expiration and token renewal possible
79
+ if error.response.body != 'Bad username or password' && !retried
80
+ authenticate
81
+ retried = true
82
+ retry
83
+ # bad credentials or token renewal not possible
84
+ else
85
+ raise error
86
+ end
87
+ rescue Excon::Errors::HTTPStatusError => error
88
+ raise case error
89
+ when Excon::Errors::NotFound
90
+ self.class.not_found_class.slurp(error)
91
+ else
92
+ error
93
+ end
94
+ end
95
+ Fog::Proxmox::Json.get_data(response)
96
+ end
97
+
98
+ def headers(method, additional_headers)
99
+ additional_headers ||= {}
100
+ headers_hash = { 'Accept' => 'application/json' }
101
+ # CSRF token is required to PUT, POST and DELETE http requests
102
+ if %w[PUT POST DELETE].include? method
103
+ headers_hash.store('CSRFPreventionToken', @pve_csrftoken)
104
+ end
105
+ # ticket must be present in cookie
106
+ headers_hash.store('Cookie', "PVEAuthCookie=#{@pve_ticket}") if @pve_ticket
107
+ headers_hash.merge additional_headers
108
+ headers_hash
109
+ end
110
+
111
+ def pve_options
112
+ Fog::Proxmox::Variables.to_hash(self, 'pve')
113
+ end
114
+
115
+ def authenticate
116
+ options = pve_options
117
+ @pve_ticket = options[:pve_ticket]
118
+ Fog::Proxmox.authenticate(options, @connection_options)
119
+ @principal = Fog::Proxmox.credentials
120
+ @pve_username = Fog::Proxmox.credentials[:username]
121
+ @pve_ticket = Fog::Proxmox.credentials[:ticket]
122
+ @pve_deadline = Fog::Proxmox.credentials[:deadline]
123
+ @pve_csrftoken = Fog::Proxmox.credentials[:csrftoken]
124
+
125
+ @host = @pve_uri.host
126
+ @api_path = @pve_uri.path
127
+ @api_path.sub!(%r{/$}, '')
128
+ @port = @pve_uri.port
129
+ @scheme = @pve_uri.scheme
130
+
131
+ true
132
+ end
133
+ end
134
+ end
135
+ 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,39 @@
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
+
25
+ def self.stringify(hash)
26
+ filtered = hash.reject { |_key,value| value.to_s.empty? }
27
+ a = filtered.to_a.collect { |item| item.join('=') }
28
+ a.join(',')
29
+ end
30
+
31
+ def self.flatten(hash)
32
+ filtered = hash.reject { |_key,value| value.to_s.empty? }
33
+ a = filtered.to_a.collect { |item| item.join(': ') }
34
+ a.join(',')
35
+ end
36
+
37
+ end
38
+ end
39
+ 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 ControllerHelper mixins
23
+ module ControllerHelper
24
+
25
+ CONTROLLERS = %w[ide sata scsi virtio mp rootfs].freeze
26
+ def self.extract(name, controller_value)
27
+ matches = controller_value.match(/[,]{0,1}#{name}[=]{1}(?<name_value>[\w\/\.\:]+)/)
28
+ matches ? matches[:name_value] : matches
29
+ end
30
+
31
+ def self.extract_index(name, key)
32
+ matches = key.to_s.match(/#{name}(?<index>\d+)/)
33
+ index = matches ? matches[:index] : matches
34
+ index ? index.to_i : -1
35
+ end
36
+
37
+ def self.valid?(name, key)
38
+ key.to_s.match(/^#{name}(\d*)$/)
39
+ end
40
+
41
+ def self.last_index(name, values)
42
+ return -1 if values.empty?
43
+ indexes = []
44
+ values.each do |value|
45
+ index = extract_index(name, value)
46
+ indexes.push(index) if index
47
+ end
48
+ indexes.sort
49
+ indexes.empty? ? -1 : indexes.last
50
+ end
51
+
52
+ def self.select(hash,name)
53
+ hash.select { |key| valid?(name, key.to_s) }
54
+ end
55
+
56
+ def self.collect_controllers(attributes)
57
+ controllers = {}
58
+ CONTROLLERS.each { |controller| controllers.merge!(select(attributes, controller)) }
59
+ controllers
60
+ end
61
+
62
+ end
63
+ end
64
+ end