fog-proxmox-configlmm 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE +674 -0
  4. data/README.md +110 -0
  5. data/docs/compute.md +874 -0
  6. data/docs/connection_parameters.md +45 -0
  7. data/docs/getting_started.md +53 -0
  8. data/docs/identity.md +382 -0
  9. data/examples/compute.rb +276 -0
  10. data/examples/identity.rb +203 -0
  11. data/lib/fog/proxmox/attributes.rb +34 -0
  12. data/lib/fog/proxmox/auth/token/access_ticket.rb +96 -0
  13. data/lib/fog/proxmox/auth/token/user_token.rb +107 -0
  14. data/lib/fog/proxmox/auth/token.rb +94 -0
  15. data/lib/fog/proxmox/compute/models/disk.rb +94 -0
  16. data/lib/fog/proxmox/compute/models/disks.rb +55 -0
  17. data/lib/fog/proxmox/compute/models/interface.rb +56 -0
  18. data/lib/fog/proxmox/compute/models/interfaces.rb +44 -0
  19. data/lib/fog/proxmox/compute/models/node.rb +93 -0
  20. data/lib/fog/proxmox/compute/models/nodes.rb +39 -0
  21. data/lib/fog/proxmox/compute/models/server.rb +271 -0
  22. data/lib/fog/proxmox/compute/models/server_config.rb +161 -0
  23. data/lib/fog/proxmox/compute/models/servers.rb +80 -0
  24. data/lib/fog/proxmox/compute/models/snapshot.rb +83 -0
  25. data/lib/fog/proxmox/compute/models/snapshots.rb +47 -0
  26. data/lib/fog/proxmox/compute/models/storage.rb +65 -0
  27. data/lib/fog/proxmox/compute/models/storages.rb +51 -0
  28. data/lib/fog/proxmox/compute/models/task.rb +79 -0
  29. data/lib/fog/proxmox/compute/models/tasks.rb +65 -0
  30. data/lib/fog/proxmox/compute/models/volume.rb +68 -0
  31. data/lib/fog/proxmox/compute/models/volumes.rb +58 -0
  32. data/lib/fog/proxmox/compute/requests/action_server.rb +45 -0
  33. data/lib/fog/proxmox/compute/requests/clone_server.rb +44 -0
  34. data/lib/fog/proxmox/compute/requests/create_backup.rb +42 -0
  35. data/lib/fog/proxmox/compute/requests/create_server.rb +43 -0
  36. data/lib/fog/proxmox/compute/requests/create_snapshot.rb +46 -0
  37. data/lib/fog/proxmox/compute/requests/create_spice.rb +44 -0
  38. data/lib/fog/proxmox/compute/requests/create_term.rb +45 -0
  39. data/lib/fog/proxmox/compute/requests/create_vnc.rb +44 -0
  40. data/lib/fog/proxmox/compute/requests/delete_server.rb +45 -0
  41. data/lib/fog/proxmox/compute/requests/delete_snapshot.rb +47 -0
  42. data/lib/fog/proxmox/compute/requests/delete_volume.rb +40 -0
  43. data/lib/fog/proxmox/compute/requests/get_node_statistics.rb +44 -0
  44. data/lib/fog/proxmox/compute/requests/get_server_config.rb +60 -0
  45. data/lib/fog/proxmox/compute/requests/get_server_status.rb +63 -0
  46. data/lib/fog/proxmox/compute/requests/get_snapshot_config.rb +44 -0
  47. data/lib/fog/proxmox/compute/requests/get_task.rb +41 -0
  48. data/lib/fog/proxmox/compute/requests/get_vnc.rb +45 -0
  49. data/lib/fog/proxmox/compute/requests/get_volume.rb +42 -0
  50. data/lib/fog/proxmox/compute/requests/list_nodes.rb +48 -0
  51. data/lib/fog/proxmox/compute/requests/list_servers.rb +113 -0
  52. data/lib/fog/proxmox/compute/requests/list_snapshots.rb +66 -0
  53. data/lib/fog/proxmox/compute/requests/list_storages.rb +46 -0
  54. data/lib/fog/proxmox/compute/requests/list_tasks.rb +44 -0
  55. data/lib/fog/proxmox/compute/requests/list_volumes.rb +44 -0
  56. data/lib/fog/proxmox/compute/requests/log_task.rb +44 -0
  57. data/lib/fog/proxmox/compute/requests/migrate_server.rb +44 -0
  58. data/lib/fog/proxmox/compute/requests/move_disk.rb +43 -0
  59. data/lib/fog/proxmox/compute/requests/move_volume.rb +43 -0
  60. data/lib/fog/proxmox/compute/requests/next_vmid.rb +42 -0
  61. data/lib/fog/proxmox/compute/requests/resize_container.rb +43 -0
  62. data/lib/fog/proxmox/compute/requests/resize_server.rb +43 -0
  63. data/lib/fog/proxmox/compute/requests/rollback_snapshot.rb +48 -0
  64. data/lib/fog/proxmox/compute/requests/status_task.rb +41 -0
  65. data/lib/fog/proxmox/compute/requests/stop_task.rb +41 -0
  66. data/lib/fog/proxmox/compute/requests/template_server.rb +44 -0
  67. data/lib/fog/proxmox/compute/requests/update_server.rb +44 -0
  68. data/lib/fog/proxmox/compute/requests/update_snapshot.rb +45 -0
  69. data/lib/fog/proxmox/compute.rb +141 -0
  70. data/lib/fog/proxmox/core.rb +147 -0
  71. data/lib/fog/proxmox/errors.rb +65 -0
  72. data/lib/fog/proxmox/hash.rb +37 -0
  73. data/lib/fog/proxmox/helpers/controller_helper.rb +63 -0
  74. data/lib/fog/proxmox/helpers/cpu_helper.rb +88 -0
  75. data/lib/fog/proxmox/helpers/disk_helper.rb +185 -0
  76. data/lib/fog/proxmox/helpers/ip_helper.rb +101 -0
  77. data/lib/fog/proxmox/helpers/nic_helper.rb +132 -0
  78. data/lib/fog/proxmox/identity/models/domain.rb +69 -0
  79. data/lib/fog/proxmox/identity/models/domain_type.rb +47 -0
  80. data/lib/fog/proxmox/identity/models/domains.rb +44 -0
  81. data/lib/fog/proxmox/identity/models/group.rb +51 -0
  82. data/lib/fog/proxmox/identity/models/groups.rb +44 -0
  83. data/lib/fog/proxmox/identity/models/permission.rb +66 -0
  84. data/lib/fog/proxmox/identity/models/permissions.rb +46 -0
  85. data/lib/fog/proxmox/identity/models/pool.rb +89 -0
  86. data/lib/fog/proxmox/identity/models/pools.rb +46 -0
  87. data/lib/fog/proxmox/identity/models/principal.rb +42 -0
  88. data/lib/fog/proxmox/identity/models/role.rb +48 -0
  89. data/lib/fog/proxmox/identity/models/roles.rb +44 -0
  90. data/lib/fog/proxmox/identity/models/token.rb +72 -0
  91. data/lib/fog/proxmox/identity/models/token_info.rb +41 -0
  92. data/lib/fog/proxmox/identity/models/tokens.rb +54 -0
  93. data/lib/fog/proxmox/identity/models/user.rb +82 -0
  94. data/lib/fog/proxmox/identity/models/users.rb +44 -0
  95. data/lib/fog/proxmox/identity/requests/change_password.rb +41 -0
  96. data/lib/fog/proxmox/identity/requests/check_permissions.rb +41 -0
  97. data/lib/fog/proxmox/identity/requests/create_domain.rb +40 -0
  98. data/lib/fog/proxmox/identity/requests/create_group.rb +40 -0
  99. data/lib/fog/proxmox/identity/requests/create_pool.rb +40 -0
  100. data/lib/fog/proxmox/identity/requests/create_role.rb +40 -0
  101. data/lib/fog/proxmox/identity/requests/create_token.rb +40 -0
  102. data/lib/fog/proxmox/identity/requests/create_user.rb +40 -0
  103. data/lib/fog/proxmox/identity/requests/delete_domain.rb +40 -0
  104. data/lib/fog/proxmox/identity/requests/delete_group.rb +40 -0
  105. data/lib/fog/proxmox/identity/requests/delete_pool.rb +39 -0
  106. data/lib/fog/proxmox/identity/requests/delete_role.rb +40 -0
  107. data/lib/fog/proxmox/identity/requests/delete_token.rb +40 -0
  108. data/lib/fog/proxmox/identity/requests/delete_user.rb +39 -0
  109. data/lib/fog/proxmox/identity/requests/get_domain.rb +41 -0
  110. data/lib/fog/proxmox/identity/requests/get_group.rb +41 -0
  111. data/lib/fog/proxmox/identity/requests/get_pool.rb +41 -0
  112. data/lib/fog/proxmox/identity/requests/get_role.rb +41 -0
  113. data/lib/fog/proxmox/identity/requests/get_token_info.rb +41 -0
  114. data/lib/fog/proxmox/identity/requests/get_user.rb +41 -0
  115. data/lib/fog/proxmox/identity/requests/list_domains.rb +41 -0
  116. data/lib/fog/proxmox/identity/requests/list_groups.rb +41 -0
  117. data/lib/fog/proxmox/identity/requests/list_permissions.rb +41 -0
  118. data/lib/fog/proxmox/identity/requests/list_pools.rb +41 -0
  119. data/lib/fog/proxmox/identity/requests/list_roles.rb +40 -0
  120. data/lib/fog/proxmox/identity/requests/list_tokens.rb +41 -0
  121. data/lib/fog/proxmox/identity/requests/list_user_permissions.rb +44 -0
  122. data/lib/fog/proxmox/identity/requests/list_users.rb +42 -0
  123. data/lib/fog/proxmox/identity/requests/read_version.rb +39 -0
  124. data/lib/fog/proxmox/identity/requests/update_domain.rb +41 -0
  125. data/lib/fog/proxmox/identity/requests/update_group.rb +40 -0
  126. data/lib/fog/proxmox/identity/requests/update_permissions.rb +41 -0
  127. data/lib/fog/proxmox/identity/requests/update_pool.rb +41 -0
  128. data/lib/fog/proxmox/identity/requests/update_role.rb +41 -0
  129. data/lib/fog/proxmox/identity/requests/update_token.rb +41 -0
  130. data/lib/fog/proxmox/identity/requests/update_user.rb +41 -0
  131. data/lib/fog/proxmox/identity.rb +144 -0
  132. data/lib/fog/proxmox/json.rb +32 -0
  133. data/lib/fog/proxmox/network/models/network.rb +76 -0
  134. data/lib/fog/proxmox/network/models/networks.rb +48 -0
  135. data/lib/fog/proxmox/network/models/node.rb +75 -0
  136. data/lib/fog/proxmox/network/models/nodes.rb +39 -0
  137. data/lib/fog/proxmox/network/requests/create_network.rb +41 -0
  138. data/lib/fog/proxmox/network/requests/delete_network.rb +42 -0
  139. data/lib/fog/proxmox/network/requests/get_network.rb +43 -0
  140. data/lib/fog/proxmox/network/requests/get_node.rb +41 -0
  141. data/lib/fog/proxmox/network/requests/list_networks.rb +43 -0
  142. data/lib/fog/proxmox/network/requests/list_nodes.rb +42 -0
  143. data/lib/fog/proxmox/network/requests/power_node.rb +43 -0
  144. data/lib/fog/proxmox/network/requests/update_network.rb +42 -0
  145. data/lib/fog/proxmox/network.rb +90 -0
  146. data/lib/fog/proxmox/storage/requests/create.rb +23 -0
  147. data/lib/fog/proxmox/storage/requests/download_appliance.rb +24 -0
  148. data/lib/fog/proxmox/storage/requests/list.rb +22 -0
  149. data/lib/fog/proxmox/storage/requests/list_appliances.rb +23 -0
  150. data/lib/fog/proxmox/storage/requests/upload.rb +44 -0
  151. data/lib/fog/proxmox/storage.rb +82 -0
  152. data/lib/fog/proxmox/string.rb +32 -0
  153. data/lib/fog/proxmox/variables.rb +40 -0
  154. data/lib/fog/proxmox/version.rb +24 -0
  155. data/lib/fog/proxmox.rb +53 -0
  156. data/spec/compute_spec.rb +447 -0
  157. data/spec/fixtures/proxmox/compute/common_auth.yml +40 -0
  158. data/spec/fixtures/proxmox/compute/containers.yml +6921 -0
  159. data/spec/fixtures/proxmox/compute/nodes.yml +115 -0
  160. data/spec/fixtures/proxmox/compute/servers.yml +19011 -0
  161. data/spec/fixtures/proxmox/compute/snapshots.yml +1376 -0
  162. data/spec/fixtures/proxmox/compute/storages.yml +151 -0
  163. data/spec/fixtures/proxmox/compute/tasks.yml +465 -0
  164. data/spec/fixtures/proxmox/identity/auth.yml +42 -0
  165. data/spec/fixtures/proxmox/identity/auth_access_ticket.yml +77 -0
  166. data/spec/fixtures/proxmox/identity/auth_user_token.yml +77 -0
  167. data/spec/fixtures/proxmox/identity/common_auth.yml +40 -0
  168. data/spec/fixtures/proxmox/identity/domains.yml +665 -0
  169. data/spec/fixtures/proxmox/identity/groups.yml +345 -0
  170. data/spec/fixtures/proxmox/identity/permissions.yml +911 -0
  171. data/spec/fixtures/proxmox/identity/pools.yml +946 -0
  172. data/spec/fixtures/proxmox/identity/read_version.yml +40 -0
  173. data/spec/fixtures/proxmox/identity/roles.yml +345 -0
  174. data/spec/fixtures/proxmox/identity/tokens.yml +494 -0
  175. data/spec/fixtures/proxmox/identity/users.yml +725 -0
  176. data/spec/fixtures/proxmox/network/common_auth.yml +40 -0
  177. data/spec/fixtures/proxmox/network/networks.yml +375 -0
  178. data/spec/fixtures/proxmox/pve.home +34 -0
  179. data/spec/hash_spec.rb +43 -0
  180. data/spec/helpers/controller_helper_spec.rb +157 -0
  181. data/spec/helpers/cpu_helper_spec.rb +82 -0
  182. data/spec/helpers/disk_helper_spec.rb +304 -0
  183. data/spec/helpers/ip_helper_spec.rb +177 -0
  184. data/spec/helpers/nic_helper_spec.rb +144 -0
  185. data/spec/identity_spec.rb +416 -0
  186. data/spec/network_spec.rb +67 -0
  187. data/spec/proxmox_vcr.rb +97 -0
  188. data/spec/spec_helper.rb +41 -0
  189. metadata +566 -0
@@ -0,0 +1,416 @@
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 'spec_helper'
21
+ require_relative './proxmox_vcr'
22
+
23
+ describe Fog::Proxmox::Identity do
24
+ before :all do
25
+ @proxmox_vcr = ProxmoxVCR.new(
26
+ vcr_directory: 'spec/fixtures/proxmox/identity',
27
+ service_class: Fog::Proxmox::Identity
28
+ )
29
+ @service = @proxmox_vcr.service
30
+ @proxmox_url = @proxmox_vcr.url
31
+ @username = @proxmox_vcr.username
32
+ @password = @proxmox_vcr.password
33
+ @tokenid = @proxmox_vcr.tokenid
34
+ @token = @proxmox_vcr.token
35
+ end
36
+
37
+ it 'authenticates with access ticket' do
38
+ VCR.use_cassette('auth_access_ticket') do
39
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
40
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::AccessTicket::NAME, proxmox_username: @username, proxmox_password: @password)
41
+ _(proc do
42
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
43
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::AccessTicket::NAME, proxmox_username: @username, proxmox_password: 'wrong_password')
44
+ end).must_raise Excon::Errors::Unauthorized
45
+ _(proc do
46
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url, proxmox_username: @username, proxmox_password: @password)
47
+ end).must_raise ArgumentError
48
+ _(proc do
49
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
50
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::AccessTicket::NAME, proxmox_password: @password)
51
+ end).must_raise Fog::Proxmox::Auth::Token::AccessTicket::URIError
52
+ _(proc do
53
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
54
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::AccessTicket::NAME, proxmox_username: @username)
55
+ end).must_raise Fog::Proxmox::Auth::Token::AccessTicket::URIError
56
+ _(proc do
57
+ Fog::Proxmox::Identity.new(proxmox_auth_method: Fog::Proxmox::Auth::Token::AccessTicket::NAME,
58
+ proxmox_username: @username, proxmox_password: 'wrong_password')
59
+ end).must_raise ArgumentError
60
+ end
61
+ end
62
+
63
+ it 'authenticates with user token' do
64
+ VCR.use_cassette('auth_user_token') do
65
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
66
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::UserToken::NAME, proxmox_userid: @username, proxmox_tokenid: @tokenid, proxmox_token: @token)
67
+ _(proc do
68
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
69
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::UserToken::NAME, proxmox_userid: @username, proxmox_tokenid: @tokenid, proxmox_token: 'wrong_token')
70
+ end).must_raise Excon::Errors::Unauthorized
71
+ _(proc do
72
+ Fog::Proxmox::Identity.new(proxmox_auth_method: Fog::Proxmox::Auth::Token::UserToken::NAME,
73
+ proxmox_userid: @username, proxmox_tokenid: @tokenid, proxmox_token: 'wrong_token')
74
+ end).must_raise ArgumentError
75
+ _(proc do
76
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url, proxmox_userid: @username, proxmox_tokenid: @tokenid,
77
+ proxmox_token: 'wrong_token')
78
+ end).must_raise ArgumentError
79
+ _(proc do
80
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
81
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::UserToken::NAME, proxmox_userid: @username, proxmox_token: @token)
82
+ end).must_raise Fog::Proxmox::Auth::Token::UserToken::URIError
83
+ _(proc do
84
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
85
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::UserToken::NAME, proxmox_tokenid: @tokenid, proxmox_token: @token)
86
+ end).must_raise Fog::Proxmox::Auth::Token::UserToken::URIError
87
+ _(proc do
88
+ Fog::Proxmox::Identity.new(proxmox_url: @proxmox_url,
89
+ proxmox_auth_method: Fog::Proxmox::Auth::Token::UserToken::NAME, proxmox_userid: @username, proxmox_tokenid: @tokenid)
90
+ end).must_raise Fog::Proxmox::Auth::Token::UserToken::URIError
91
+ end
92
+ end
93
+
94
+ it 'checks access ticket with path and privs' do
95
+ VCR.use_cassette('auth') do
96
+ principal = { username: @username, password: @password, privs: ['User.Modify'], path: 'access', otp: 'proxmox01' }
97
+ permissions = @service.check_permissions(principal)
98
+ _(permissions).wont_be_nil
99
+ _(permissions).wont_be_empty
100
+ _(permissions['username']).must_equal @username
101
+ _(permissions['cap']).wont_be_empty
102
+ end
103
+ end
104
+
105
+ it 'reads server version' do
106
+ VCR.use_cassette('read_version') do
107
+ version = @service.read_version
108
+ _(version).wont_be_nil
109
+ version.include? 'version'
110
+ end
111
+ end
112
+
113
+ it 'CRUD users' do
114
+ VCR.use_cassette('users') do
115
+ bob_hash = {
116
+ userid: 'bobsinclar@pve',
117
+ password: 'bobsinclar1',
118
+ firstname: 'Bob',
119
+ lastname: 'Sinclar',
120
+ email: 'bobsinclar@proxmox.com'
121
+ }
122
+ # Create 1st time
123
+ @service.users.create(bob_hash)
124
+ # Find by id
125
+ bob = @service.users.get bob_hash[:userid]
126
+ _(bob).wont_be_nil
127
+ # Create 2nd time must fails
128
+ _(proc do
129
+ @service.users.create(bob_hash)
130
+ end).must_raise Excon::Errors::InternalServerError
131
+ # all users
132
+ users_all = @service.users.all
133
+ _(users_all).wont_be_nil
134
+ _(users_all).wont_be_empty
135
+ _(users_all).must_include bob
136
+ # Update
137
+ bob.comment = 'novelist'
138
+ bob.enable = 0
139
+ @service.groups.create(groupid: 'group1')
140
+ @service.groups.create(groupid: 'group2')
141
+ bob.groups = %w[group1 group2]
142
+ bob.update
143
+ # change bob's password with special characters
144
+ bob.password = 'bobsinclar&!.-_2'
145
+ bob.change_password
146
+ # disabled users
147
+ users_disabled = @service.users.all('enabled' => 0)
148
+ _(users_disabled).wont_be_nil
149
+ _(users_disabled).wont_be_empty
150
+ _(users_disabled).must_include bob
151
+ # Delete
152
+ bob.destroy
153
+ group1 = @service.groups.get 'group1'
154
+ group1.destroy
155
+ group2 = @service.groups.get 'group2'
156
+ group2.destroy
157
+ bob = @service.users.get bob_hash[:userid]
158
+ _(bob).must_be_nil
159
+ end
160
+ end
161
+
162
+ it 'CRUD groups' do
163
+ VCR.use_cassette('groups') do
164
+ group_hash = { groupid: 'group1' }
165
+ # Create 1st time
166
+ @service.groups.create(group_hash)
167
+ # Find by id
168
+ group = @service.groups.get group_hash[:groupid]
169
+ _(group).wont_be_nil
170
+ # Create 2nd time must fails
171
+ _(proc do
172
+ @service.groups.create(group_hash)
173
+ end).must_raise Excon::Errors::InternalServerError
174
+ # Update
175
+ group.comment = 'Group 1'
176
+ group.update
177
+ # all groups
178
+ groups_all = @service.groups.all
179
+ _(groups_all).wont_be_nil
180
+ _(groups_all).wont_be_empty
181
+ _(groups_all).must_include group
182
+ # Delete
183
+ group.destroy
184
+ group1 = @service.groups.get group_hash[:groupid]
185
+ _(group1).must_be_nil
186
+ end
187
+ end
188
+
189
+ it 'CRUD roles' do
190
+ VCR.use_cassette('roles') do
191
+ role_hash = { roleid: 'PVETestAuditor' }
192
+ # Create 1st time
193
+ @service.roles.create(role_hash)
194
+ # Find by id
195
+ role = @service.roles.get role_hash[:roleid]
196
+ _(role).wont_be_nil
197
+ # Create 2nd time must fails
198
+ _(proc do
199
+ @service.roles.create(role_hash)
200
+ end).must_raise Excon::Errors::InternalServerError
201
+ # # Update
202
+ role.privs = 'Datastore.Audit Sys.Audit VM.Audit'
203
+ role.update
204
+ # # all groups
205
+ roles_all = @service.roles.all
206
+ _(roles_all).wont_be_nil
207
+ _(roles_all).wont_be_empty
208
+ _(roles_all).must_include role
209
+ # Delete
210
+ role.destroy
211
+ role = @service.roles.get role_hash[:roleid]
212
+ _(role).must_be_nil
213
+ end
214
+ end
215
+
216
+ it 'CRUD domains' do
217
+ VCR.use_cassette('domains') do
218
+ ldap_hash = {
219
+ realm: 'LDAP',
220
+ type: 'ldap',
221
+ base_dn: 'ou=People,dc=ldap-test,dc=com',
222
+ user_attr: 'LDAP',
223
+ server1: 'localhost',
224
+ port: 389,
225
+ default: 0,
226
+ secure: 0
227
+ }
228
+ ad_hash = {
229
+ realm: 'ActiveDirectory',
230
+ type: 'ad',
231
+ domain: 'proxmox.com',
232
+ server1: 'localhost',
233
+ port: 389,
234
+ default: 0,
235
+ secure: 0
236
+ }
237
+ # Create 1st time
238
+ @service.domains.create(ldap_hash)
239
+ # Find by id
240
+ ldap = @service.domains.get ldap_hash[:realm]
241
+ _(ldap).wont_be_nil
242
+ # Create 1st time
243
+ @service.domains.create(ad_hash)
244
+ # Create 2nd time must fails
245
+ _(proc do
246
+ @service.domains.create(ldap_hash)
247
+ end).must_raise Excon::Errors::InternalServerError
248
+ # Create 2nd time must fails
249
+ _(proc do
250
+ @service.domains.create(ad_hash)
251
+ end).must_raise Excon::Errors::InternalServerError
252
+ # Update
253
+ ldap.type.comment = 'Test domain LDAP'
254
+ ldap.type.tfa = 'type=oath,step=30,digits=8'
255
+ ldap.update
256
+ # Find by id
257
+ ad = @service.domains.get ad_hash[:realm]
258
+ _(ad).wont_be_nil
259
+ ad.type.tfa = 'type=yubico,id=1,key=2,url=http://localhost'
260
+ ad.update
261
+ # all groups
262
+ domains_all = @service.domains.all
263
+ _(domains_all).wont_be_nil
264
+ _(domains_all).wont_be_empty
265
+ _(domains_all).must_include ldap
266
+ _(domains_all).must_include ad
267
+ # Delete
268
+ ldap.destroy
269
+ ad.destroy
270
+ ldap = @service.domains.get ldap_hash[:realm]
271
+ _(ldap).must_be_nil
272
+ ad = @service.domains.get ad_hash[:realm]
273
+ _(ad).must_be_nil
274
+ end
275
+ end
276
+
277
+ it 'adds or removes permissions' do
278
+ VCR.use_cassette('permissions') do
279
+ # Add ACL to users
280
+ bob_hash = {
281
+ userid: 'bobsinclar@pve',
282
+ password: 'bobsinclar1',
283
+ firstname: 'Bob',
284
+ lastname: 'Sinclar',
285
+ email: 'bobsinclar@proxmox.com'
286
+ }
287
+ role_hash = {
288
+ roleid: 'PVETestAdmin',
289
+ privs: 'User.Modify,Group.Allocate'
290
+ }
291
+ @service.roles.create(role_hash)
292
+ role = @service.roles.get(role_hash[:roleid])
293
+ _(role).wont_be_nil
294
+ @service.users.create(bob_hash)
295
+ bob = @service.users.get bob_hash[:userid]
296
+ _(bob).wont_be_nil
297
+ permission = @service.permissions.create(type: 'user', roleid: role.roleid, path: '/access', ugid: bob.userid)
298
+ _(permission).wont_be_nil
299
+ # Read all permissions
300
+ permissions = @service.permissions.all
301
+ _(permissions).wont_be_empty
302
+ _(permissions).must_include permission
303
+ # Read user permissions
304
+ _(bob.permissions).wont_be_empty
305
+ _(bob.permissions.keys).must_include '/access'
306
+ _(bob.permissions.keys).must_include '/access/groups'
307
+ # Remove ACL to users
308
+ permissions.destroy(type: 'user', roleid: role.roleid, path: '/access', ugid: bob.userid)
309
+ permission = @service.permissions.get('user', role.roleid, '/access', bob.userid)
310
+ _(permission).must_be_nil
311
+ bob = @service.users.get bob_hash[:userid]
312
+ bob.destroy
313
+ # Add ACL to groups
314
+ group1 = @service.groups.create(groupid: 'group1', comment: 'Group 1')
315
+ permission = @service.permissions.create(type: 'group', roleid: role.roleid, path: '/access',
316
+ ugid: group1.groupid)
317
+ _(permission).wont_be_nil
318
+ # Read new permission
319
+ permissions = @service.permissions.all
320
+ _(permissions).wont_be_empty
321
+ _(permissions).must_include permission
322
+ # Remove ACL to groups
323
+ permissions.destroy(type: 'group', roleid: role.roleid, path: '/access', ugid: group1.groupid)
324
+ permissions = @service.permissions.all
325
+ _(permissions).must_be_empty
326
+ group1.destroy
327
+ role.destroy
328
+ role = @service.roles.get(role_hash[:roleid])
329
+ _(role).must_be_nil
330
+ end
331
+ end
332
+
333
+ it 'CRUD pools' do
334
+ VCR.use_cassette('pools') do
335
+ pool_hash = { poolid: 'pool1' }
336
+ # Create 1st time
337
+ @service.pools.create(pool_hash)
338
+ # Find by id
339
+ pool = @service.pools.get pool_hash[:poolid]
340
+ _(pool).wont_be_nil
341
+ # Create 2nd time must fails
342
+ _(proc do
343
+ @service.pools.create(pool_hash)
344
+ end).must_raise Excon::Errors::InternalServerError
345
+ # Update
346
+ # Add comment
347
+ pool.comment = 'Pool 1'
348
+ pool.update
349
+ # Add storage as member
350
+ pool.add_server 100 # do nothing if server does not exist
351
+ pool.add_storage 'local-lvm'
352
+ _(pool.members).wont_be_nil
353
+ _(pool.members).wont_be_empty
354
+ _(pool.members.size).must_equal 1 # no vm 100
355
+ _(pool.has_server?(100)).must_equal false
356
+ _(pool.has_storage?('local-lvm')).must_equal true
357
+ # all pools
358
+ pools_all = @service.pools.all
359
+ _(pools_all).wont_be_nil
360
+ _(pools_all).wont_be_empty
361
+ _(pools_all).must_include pool
362
+ # Delete
363
+ pool.remove_server 100
364
+ pool.remove_storage 'local-lvm'
365
+ _(pool.members).must_be_empty
366
+ pool.destroy
367
+ pool = @service.pools.get pool_hash[:poolid]
368
+ _(pool).must_be_nil
369
+ end
370
+ end
371
+
372
+ it 'CRUD user tokens' do
373
+ VCR.use_cassette('tokens') do
374
+ # Get user
375
+ bob_hash = {
376
+ userid: 'bobsinclar@pve',
377
+ password: 'bobsinclar1',
378
+ firstname: 'Bob',
379
+ lastname: 'Sinclar',
380
+ email: 'bobsinclar@proxmox.com'
381
+ }
382
+ token_hash = {
383
+ userid: bob_hash[:userid],
384
+ tokenid: 'bobsinclar1'
385
+ }
386
+ @service.users.create(bob_hash)
387
+ bob = @service.users.get token_hash[:userid]
388
+ _(bob).wont_be_nil
389
+ # Create Token
390
+ token = bob.tokens.create(token_hash)
391
+ token_info = token.info
392
+ _(token_info).wont_be_nil
393
+ # all user tokens
394
+ tokens_all = bob.tokens.all
395
+ _(tokens_all).wont_be_nil
396
+ _(tokens_all).wont_be_empty
397
+ _(tokens_all).must_include token
398
+ # Find token info by tokenid
399
+ token_get = bob.tokens.get(token_hash[:tokenid])
400
+ _(token_get).wont_be_nil
401
+ _(token_get).must_equal token
402
+ # Update
403
+ token.comment = 'test'
404
+ token.expire = 0
405
+ token.privsep = 0
406
+ token.update
407
+ # Delete
408
+ token.destroy
409
+ token = bob.tokens.get token_hash[:tokenid]
410
+ _(token).must_be_nil
411
+ bob.destroy
412
+ bob = @service.users.get bob_hash[:userid]
413
+ _(bob).must_be_nil
414
+ end
415
+ end
416
+ end
@@ -0,0 +1,67 @@
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 'spec_helper'
21
+ require_relative './proxmox_vcr'
22
+
23
+ describe Fog::Proxmox::Network do
24
+ before :all do
25
+ @proxmox_vcr = ProxmoxVCR.new(
26
+ vcr_directory: 'spec/fixtures/proxmox/network',
27
+ service_class: Fog::Proxmox::Network
28
+ )
29
+ @service = @proxmox_vcr.service
30
+ @proxmox_url = @proxmox_vcr.url
31
+ @username = @proxmox_vcr.username
32
+ @password = @proxmox_vcr.password
33
+ @tokenid = @proxmox_vcr.tokenid
34
+ @token = @proxmox_vcr.token
35
+ end
36
+
37
+ it 'CRUD networks' do
38
+ VCR.use_cassette('networks') do
39
+ net_hash = {
40
+ iface: 'enp0s10',
41
+ type: 'eth'
42
+ }
43
+ node = @service.nodes.all.first
44
+ # Create 1st time
45
+ node.networks.create(net_hash)
46
+ # Find by id
47
+ network = node.networks.get net_hash[:iface]
48
+ _(network).wont_be_nil
49
+ # Create 2nd time
50
+ _(proc do
51
+ node.networks.create(net_hash)
52
+ end).must_raise Excon::Error::BadRequest
53
+ # all networks
54
+ networks_all = node.networks.all
55
+ _(networks_all).wont_be_nil
56
+ _(networks_all).wont_be_empty
57
+ _(networks_all).must_include network
58
+ # Update
59
+ network.update(comments: 'test')
60
+ node.power('reboot')
61
+ sleep 60
62
+ # Delete
63
+ network.destroy
64
+ node.power('reboot')
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,97 @@
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
+ # There are basically two modes of operation for these specs.
22
+ #
23
+ # 1. ENV[PROXMOX_URL] exists: talk to an actual Proxmox server and record HTTP
24
+ # traffic in VCRs at "spec/debug" (credentials are read from the conventional
25
+ # environment variables: PROXMOX_URL, PROXMOX_USERNAME, PROXMOX_PASSWORD)
26
+ # 2. otherwise (Travis etc.): use VCRs at "spec/fixtures/proxmox/#{service}"
27
+
28
+ require 'vcr'
29
+ require 'fog/proxmox/auth/token'
30
+
31
+ class ProxmoxVCR
32
+ attr_reader :username,
33
+ :password,
34
+ :tokenid,
35
+ :token,
36
+ :service,
37
+ :url
38
+
39
+ def initialize(options)
40
+ @vcr_directory = options[:vcr_directory]
41
+ @service_class = options[:service_class]
42
+ @connection_options = options[:connection_options] || {}
43
+
44
+ use_recorded = !ENV.key?('PROXMOX_URL') || ENV['USE_VCR'] == 'true'
45
+
46
+ if use_recorded
47
+ Fog.interval = 0
48
+ @url = 'https://192.168.56.101:8006/api2/json'
49
+ else
50
+ @url = ENV['PROXMOX_URL']
51
+ end
52
+
53
+ VCR.configure do |config|
54
+ config.allow_http_connections_when_no_cassette = true
55
+ if use_recorded
56
+ config.cassette_library_dir = ENV['SPEC_PATH'] || @vcr_directory
57
+ config.default_cassette_options = { record: :none }
58
+ config.default_cassette_options[:match_requests_on] = %i[method path body]
59
+ else
60
+ config.cassette_library_dir = 'spec/debug'
61
+ config.default_cassette_options = { record: :all }
62
+ end
63
+ config.hook_into :webmock
64
+ config.debug_logger = nil # use $stderr to debug
65
+ end
66
+
67
+ VCR.use_cassette('common_auth') do
68
+ Fog::Proxmox.clear_token_cache
69
+
70
+ @auth_method = Fog::Proxmox::Auth::Token::AccessTicket::NAME
71
+ @username = 'root@pam'
72
+ @password = 'proxmox01'
73
+ @tokenid = 'root1'
74
+ @token = 'ed6402b4-641d-46b1-b20a-33ba9ba12f54'
75
+
76
+ unless use_recorded
77
+ @auth_method = ENV['PROXMOX_AUTH_METHOD'] || options[:auth_method] || @auth_method
78
+ @username = ENV['PROXMOX_USERNAME'] || options[:username] || @username
79
+ @password = ENV['PROXMOX_PASSWORD'] || options[:password] || @password
80
+ @tokenid = ENV['PROXMOX_TOKENID'] || options[:tokenid] || @tokenid
81
+ @token = ENV['PROXMOX_TOKEN'] || options[:token] || @token
82
+ end
83
+
84
+ connection_params = {
85
+ proxmox_url: @url,
86
+ proxmox_auth_method: @auth_method,
87
+ proxmox_username: @username,
88
+ proxmox_password: @password,
89
+ proxmox_tokenid: @tokenid,
90
+ proxmox_token: @token,
91
+ connection_options: @connection_options
92
+ }
93
+
94
+ @service = @service_class.new(connection_params)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,41 @@
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 'simplecov'
22
+
23
+ SimpleCov.start do
24
+ add_filter '/spec/'
25
+ add_group 'Core', 'lib/fog/proxmox'
26
+ add_group 'Auth', 'lib/fog/proxmox/auth'
27
+ add_group 'Identity', 'lib/fog/proxmox/identity'
28
+ add_group 'Compute', 'lib/fog/proxmox/compute'
29
+ add_group 'Network', 'lib/fog/proxmox/network'
30
+ end
31
+
32
+ require 'minitest/autorun'
33
+ require 'vcr'
34
+ require 'fog/core'
35
+ require 'fog/proxmox'
36
+
37
+ VCR.configure do |c|
38
+ c.cassette_library_dir = 'spec/fixtures/proxmox'
39
+ c.hook_into :webmock
40
+ c.debug_logger = nil # use $stderr to debug
41
+ end