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,276 @@
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
+ # There are basically two modes of operation for these specs.
21
+ #
22
+ # 1. ENV[PROXMOX_URL] exists: talk to an actual Proxmox server and record HTTP
23
+ # traffic in VCRs at "spec/debug" (credentials are read from the conventional
24
+ # environment variables: PROXMOX_URL, PROXMOX_USERNAME, PROXMOX_PASSWORD)
25
+ # 2. otherwise (Travis, etc): use VCRs at "spec/fixtures/proxmox/#{service}"
26
+
27
+ require 'fog/proxmox'
28
+
29
+ proxmox_url = 'https://172.26.49.146:8006/api2/json'
30
+ proxmox_username = 'root@pam'
31
+ proxmox_password = 'proxmox01'
32
+
33
+ # Create service compute
34
+ compute = Fog::Proxmox::Compute.new(
35
+ proxmox_url: proxmox_url,
36
+ proxmox_username: proxmox_username,
37
+ proxmox_password: proxmox_password
38
+ )
39
+
40
+ # Create pools
41
+ pool_hash = { poolid: 'pool1' }
42
+ pool1 = compute.pools.create(pool_hash)
43
+
44
+ # Get one pool by id
45
+ pool1 = compute.pools.get 'pool1'
46
+
47
+ # Update pool
48
+ pool1.comment 'pool 1'
49
+ pool1.update
50
+
51
+ # List all pools
52
+ compute.pools.all
53
+
54
+ # List pool by pool
55
+ compute.pools.each do |pool|
56
+ # pool ...
57
+ end
58
+
59
+ # Delete pool
60
+ pool1.destroy
61
+
62
+ # Create servers
63
+
64
+ # Get node owner
65
+ node_name = 'proxmox'
66
+ node = compute.nodes.get node_name
67
+
68
+ # Get next free vmid
69
+ vmid = node.servers.next_id
70
+ server_hash = { vmid: vmid }
71
+
72
+ # Create server
73
+ node.servers.create(server_hash)
74
+
75
+ # Check already used vmid
76
+ node.servers.id_valid? vmid
77
+
78
+ # Get server
79
+ server = node.servers.get vmid
80
+
81
+ # Update config server
82
+ # Add cdrom empty
83
+ config_hash = { ide2: 'none,media=cdrom' }
84
+ server.update(config_hash)
85
+ # Attach a hdd
86
+ virtio0 = { id: 'virtio0', storage: storage.storage, size: '1' }
87
+ ide0 = { id: 'ide0', storage: storage.storage, size: '1' }
88
+ options = { backup: 0, replicate: 0 }
89
+ server.attach(virtio0, options)
90
+ server.attach(ide0, options)
91
+ # Resize disk server
92
+ server.extend('virtio0', '+1G')
93
+ # Move disk server
94
+ server.move('virtio0', 'local')
95
+ # Detach a disk
96
+ server.detach 'ide0'
97
+ # Remove it
98
+ server.detach 'unused0'
99
+ # Detach another device
100
+ server.detach 'ide2'
101
+ # Add network interface
102
+ config_hash = { net0: 'virtio,bridge=vmbr0' }
103
+ server.update(config_hash)
104
+ # Add start at boot, keyboard fr, linux 3.x os type, kvm hardware disabled (proxmox guest in virtualbox)
105
+ config_hash = { onboot: 1, keyboard: 'fr', ostype: 'l26', kvm: 0 }
106
+ server.update(config_hash)
107
+ # Get configuration model
108
+ config = server.config
109
+ # Get nics config
110
+ nics = server.config.nics
111
+ nics[:net0]
112
+ # Get hdd controllers (ide, sata, scsi or virtio) config
113
+ # All return hashes with key equals to controller id
114
+ disks = server.config.disks
115
+ ide0 = disks.get('ide0')
116
+ virtio0 = disks.get('virtio0')
117
+ # Get mac_addresses
118
+ server.config.mac_adresses
119
+ # List all servers
120
+ servers_all = node.servers.all
121
+
122
+ # Start server
123
+ server.action('start')
124
+ # Wait until task is complete
125
+ server.wait_for { ready? }
126
+ # Suspend server
127
+ server.action('suspend')
128
+ # Wait until task is complete
129
+ server.wait_for { server.qmpstatus == 'paused' }
130
+ # Resume server
131
+ server.action('resume')
132
+ # Wait until task is complete
133
+ server.wait_for { ready? }
134
+ # Stop server
135
+ server.action('stop')
136
+ # Wait until task is complete
137
+ server.wait_for { server.status == 'stopped' }
138
+
139
+ # Backup a server
140
+ server.backup(compress: 'lzo')
141
+
142
+ # Fetch a backup volume (first one)
143
+ volume = server.backups.first
144
+
145
+ # Restore it
146
+ server.restore volume
147
+
148
+ # Delete a backup
149
+ volume.destroy
150
+
151
+ # Snapshot a server
152
+ server.snapshots.create(name: 'snapshot1')
153
+
154
+ # Fetch it
155
+ snapshot = server.snapshots.get 'snapshot1'
156
+ # Fetch all
157
+ server.snapshots.all
158
+
159
+ # Update snapshot
160
+ snapshot.description 'Snapshot 1'
161
+ snapshot.update
162
+
163
+ # Delete snapshot
164
+ snapshot.destroy
165
+
166
+ # Fetch disk images
167
+ server.config.disks
168
+
169
+ # Delete server
170
+ server.destroy
171
+
172
+ # Create containers
173
+ node_name = 'proxmox'
174
+ node = compute.nodes.get node_name
175
+ ostemplate = 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz'
176
+ container_hash = {
177
+ vmid: vmid,
178
+ storage: 'local-lvm',
179
+ ostemplate: ostemplate,
180
+ password: 'proxmox01',
181
+ rootfs: 'local-lvm:1'
182
+ }
183
+
184
+ # Get next free vmid
185
+ vmid = node.containers.next_id
186
+
187
+ node.containers.create(container_hash)
188
+ # Check already used vmid
189
+ valid = node.containers.id_valid? vmid
190
+
191
+ # Get container
192
+ container = node.containers.get(vmid)
193
+
194
+ # Get container config
195
+ container.config
196
+ # Update config container
197
+ # Attach an aditional mount point
198
+ mp0 = { id: 'mp0', storage: 'local-lvm', size: '1' }
199
+ options = { mp: '/opt/app', backup: 0, replicate: 0, quota: 1 }
200
+ container.attach(mp0, options)
201
+ # Resize rootfs container
202
+ container.extend('rootfs', '+1G')
203
+ # Move rootfs container and delete original
204
+ container.move('rootfs', 'local-lvm', delete: 1)
205
+ # Detach a mount point
206
+ container.detach 'mp0'
207
+ # Remove it
208
+ container.detach 'unused0'
209
+ # Add network interface
210
+ config_hash = { net0: 'bridge=vmbr0,name=eth0,ip=dhcp,ip6=dhcp' }
211
+ container.update(config_hash)
212
+ # Add start at boot, linux os type alpine
213
+ config_hash = { onboot: 1, ostype: 'alpine' }
214
+ container.update(config_hash)
215
+ # Get mac_addresses
216
+ container.mac_adresses
217
+ # Get interfaces
218
+ container.config.interfaces
219
+ # Get additional mount points
220
+ container.config.mount_points
221
+ # List all servers
222
+ containers_all = node.containers.all
223
+
224
+ # Start container
225
+ container.action('start')
226
+ # Wait until task is complete
227
+ container.wait_for { ready? }
228
+ # Stop container
229
+ container.action('stop')
230
+ # Wait until task is complete
231
+ container.wait_for { container.status == 'stopped' }
232
+
233
+ # Backup a container
234
+ container.backup(compress: 'lzo')
235
+
236
+ # Fetch a backup volume (first one)
237
+ volume = container.backups.first
238
+
239
+ # Restore it
240
+ container.restore volume
241
+
242
+ # Delete a backup
243
+ volume.destroy
244
+
245
+ # Snapshot a container
246
+ container.snapshots.create(name: 'snapshot1')
247
+
248
+ # Fetch it
249
+ snapshot = container.snapshots.get 'snapshot1'
250
+ # Fetch all
251
+ container.snapshots.all
252
+
253
+ # Update snapshot
254
+ snapshot.description 'Snapshot 1'
255
+ snapshot.update
256
+
257
+ # Delete snapshot
258
+ snapshot.destroy
259
+
260
+ # Fetch additional mount points
261
+ container.config.mount_points
262
+ # Fetch network interfaces
263
+ container.config.interfaces
264
+
265
+ # Delete container
266
+ container.destroy
267
+
268
+ # List 1 task
269
+ filters = { limit: 1 }
270
+ node = compute.nodes.get 'proxmox'
271
+ tasks = node.tasks.all(filters)
272
+ # Get task
273
+ upid = tasks[0].upid
274
+ task = node.tasks.get(upid)
275
+ # Stop task
276
+ task.stop
@@ -0,0 +1,203 @@
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 'fog/proxmox'
29
+
30
+ proxmox_url = 'https://172.26.49.146:8006/api2/json'
31
+ proxmox_username = 'root@pam'
32
+ proxmox_password = 'proxmox01'
33
+
34
+ # Create service identity with access ticket
35
+ identity = Fog::Proxmox::Identity.new(
36
+ proxmox_url: proxmox_url,
37
+ proxmox_auth_method: 'access_ticket',
38
+ proxmox_username: proxmox_username,
39
+ proxmox_password: proxmox_password
40
+ )
41
+
42
+ # or with a user token
43
+ identity = Fog::Proxmox::Identity.new(
44
+ proxmox_url: proxmox_url,
45
+ proxmox_auth_method: 'user_token',
46
+ proxmox_userid: proxmox_username,
47
+ proxmox_tokenid: 'root1',
48
+ proxmox_token: 'ed6402b4-641d-46b1-b20a-33ba9ba12f54'
49
+ )
50
+
51
+ # Get proxmox version
52
+ identity.read_version
53
+
54
+ # Create a new user
55
+ bob_hash = {
56
+ userid: 'bobsinclar@proxmox',
57
+ password: 'bobsinclar1',
58
+ firstname: 'Bob',
59
+ lastname: 'Sinclar',
60
+ email: 'bobsinclar@proxmox.com'
61
+ }
62
+
63
+ identity.users.create(bob_hash)
64
+
65
+ # Get a user by id
66
+ bob = identity.users.get 'bobsinclar@proxmox'
67
+
68
+ # List all users
69
+ identity.users.all
70
+
71
+ # List user by user
72
+ identity.users.each do |user|
73
+ # user ...
74
+ end
75
+
76
+ # Update user
77
+ bob.comment = 'novelist'
78
+ # add groups
79
+ bob.groups = %w[group1]
80
+ bob.update
81
+
82
+ # List user permissions
83
+ bob.permissions
84
+
85
+ # Delete user
86
+ bob.destroy
87
+
88
+ # Create groups
89
+ group_hash = { groupid: 'group1' }
90
+ identity.domains.create(group_hash)
91
+
92
+ # Get one group by id
93
+ group1 = identity.groups.get 'group1'
94
+
95
+ # Update group
96
+ group1.comment 'Group 1'
97
+ group1.update
98
+
99
+ # List all groups
100
+ identity.groups.all
101
+
102
+ # List group by group
103
+ identity.groups.each do |group|
104
+ # group ...
105
+ end
106
+
107
+ # Delete group
108
+ group1.destroy
109
+
110
+ # Create roles
111
+ role_hash = { roleid: 'role1' }
112
+ identity.roles.create(role_hash)
113
+
114
+ # Get one role by id
115
+ role1 = identity.roles.get 'role1'
116
+
117
+ # Update role
118
+ role1.comment 'Role 1'
119
+ role1.update
120
+
121
+ # List all roles
122
+ identity.roles.all
123
+
124
+ # List role by role
125
+ identity.roles.each do |role|
126
+ # role ...
127
+ end
128
+
129
+ # Delete role
130
+ role1.destroy
131
+
132
+ # Create a new domain (authentication server)
133
+ # Three types: PAM, PROXMOX, LDAP and ActiveDirectory
134
+ # PAM and PROXMOX already exist by default
135
+ # LDAP sample:
136
+ ldap_hash = {
137
+ realm: 'LDAP',
138
+ type: 'ldap',
139
+ base_dn: 'ou=People,dc=ldap-test,dc=com',
140
+ user_attr: 'LDAP',
141
+ server1: 'localhost',
142
+ port: 389,
143
+ default: 0,
144
+ secure: 0
145
+ }
146
+ # ActiveDirectory sample:
147
+ # ad_hash = {
148
+ # realm: 'ActiveDirectory',
149
+ # type: 'ad',
150
+ # domain: 'proxmox.com',
151
+ # server1: 'localhost',
152
+ # port: 389,
153
+ # default: 0,
154
+ # secure: 0
155
+ # }
156
+
157
+ identity.domains.create(ldap_hash)
158
+
159
+ # List domains
160
+ identity.domains.each do |domain|
161
+ # domain ...
162
+ end
163
+
164
+ # Find domain by id
165
+ ldap = identity.domains.get ldap_hash[:realm]
166
+
167
+ # Update domain
168
+ ldap.type.comment = 'Test domain LDAP'
169
+ # Two types of Two Factors Authentication (TFA): oath and yubico
170
+ ldap.type.tfa = 'type=oath,step=30,digits=8'
171
+ # ad.type.tfa = 'type=yubico,id=1,key=2,url=http://localhost'
172
+ ldap.update
173
+
174
+ # Delete domain
175
+ ldap.destroy
176
+
177
+ # Add a user permission
178
+ permission_hash = {
179
+ type: 'user'
180
+ path: '/access',
181
+ roleid: 'PROXMOXUserAdmin',
182
+ ugid: bob_hash[:userid]
183
+ }
184
+ # Add a group permission
185
+ # permission_hash = {
186
+ # type: 'group'
187
+ # path: '/access',
188
+ # roleid: 'PROXMOXUserAdmin',
189
+ # ugid: 'group1'
190
+ # }
191
+ permission = identity.permissions.create(permission_hash)
192
+
193
+ # List all permissions
194
+ identity.permissions.all
195
+
196
+ # List permission by permission
197
+ identity.permissions.each do |permission|
198
+ # permission ...
199
+ end
200
+
201
+ # Remove permission
202
+ permission = identity.get(permission_hash)
203
+ permission.destroy
@@ -0,0 +1,34 @@
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
+
28
+ def self.set_attr_and_sym(attr_name, attributes, new_attributes)
29
+ set_attr(attr_name, attributes, new_attributes)
30
+ set_attr(attr_name.to_sym, attributes, new_attributes)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,96 @@
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 Auth
28
+ module Token
29
+ class AccessTicket
30
+ include Fog::Proxmox::Auth::Token
31
+
32
+ NAME = 'access_ticket'
33
+
34
+ attr_reader :csrf_token
35
+
36
+ class URIError < RuntimeError; end
37
+
38
+ EXPIRATION_DELAY = 2 * 60 * 60
39
+
40
+ def auth_method
41
+ 'POST'
42
+ end
43
+
44
+ def auth_path(_params = {})
45
+ '/access/ticket'
46
+ end
47
+
48
+ def auth_body(params = {})
49
+ raise URIError, 'URI params is required' if params.nil? || params.empty?
50
+
51
+ if params[:proxmox_username].nil? || params[:proxmox_username].empty?
52
+ raise URIError,
53
+ 'proxmox_username is required'
54
+ end
55
+ if params[:proxmox_password].nil? || params[:proxmox_password].empty?
56
+ raise URIError,
57
+ 'proxmox_password is required'
58
+ end
59
+
60
+ URI.encode_www_form(username: params[:proxmox_username], password: params[:proxmox_password])
61
+ end
62
+
63
+ def headers(method = 'GET', _params = {}, additional_headers = {})
64
+ headers_hash = {}
65
+ @data ||= {}
66
+ unless @data.empty?
67
+ headers_hash.store('Cookie', "PVEAuthCookie=#{@data['ticket']}")
68
+ if %w[PUT POST DELETE].include? method
69
+ headers_hash.store('CSRFPreventionToken', @data['CSRFPreventionToken'])
70
+ end
71
+ end
72
+ headers_hash.merge! additional_headers
73
+ headers_hash
74
+ end
75
+
76
+ def build_credentials(_proxmox_options, data)
77
+ @token = data['ticket']
78
+ @expires = Time.now.utc.to_i + EXPIRATION_DELAY
79
+ @userid = data['username']
80
+ @csrf_token = data['CSRFPreventionToken']
81
+ end
82
+
83
+ def missing_credentials(options)
84
+ missing_credentials = []
85
+ missing_credentials << :proxmox_username unless options[:proxmox_username]
86
+ missing_credentials << :proxmox_password unless options[:proxmox_password]
87
+ return if missing_credentials.empty?
88
+
89
+ raise ArgumentError,
90
+ "Missing required arguments: #{missing_credentials.join(', ')}"
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,107 @@
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 Auth
28
+ module Token
29
+ class UserToken
30
+ include Fog::Proxmox::Auth::Token
31
+
32
+ NAME = 'user_token'
33
+
34
+ attr_reader :token_id
35
+
36
+ class URIError < RuntimeError; end
37
+
38
+ def auth_method
39
+ 'GET'
40
+ end
41
+
42
+ def auth_path(params = {})
43
+ raise URIError, 'URI params are required' if params.nil? || params.empty?
44
+
45
+ if params[:proxmox_userid].nil? || params[:proxmox_userid].empty?
46
+ raise URIError,
47
+ 'proxmox_userid is required'
48
+ end
49
+ if params[:proxmox_tokenid].nil? || params[:proxmox_tokenid].empty?
50
+ raise URIError,
51
+ 'proxmox_tokenid is required'
52
+ end
53
+
54
+ "/access/users/#{URI.encode_www_form_component(params[:proxmox_userid])}/token/#{params[:proxmox_tokenid]}"
55
+ end
56
+
57
+ def auth_body(_params = {})
58
+ ''
59
+ end
60
+
61
+ def no_token?(params)
62
+ (params.respond_to?(:proxmox_token) || params[:proxmox_token].nil? || params[:proxmox_token].empty?) && (@token.nil? || @token.empty?)
63
+ end
64
+
65
+ def set_credentials(params)
66
+ token = @token
67
+ token = params[:proxmox_token] if token.empty?
68
+ token_id = @token_id
69
+ token_id = params[:proxmox_tokenid] if token_id.empty?
70
+ userid = @userid
71
+ userid = params[:proxmox_userid] if userid.empty?
72
+ { userid: userid, token_id: token_id, token: token }
73
+ end
74
+
75
+ def headers(_method = 'GET', params = {}, additional_headers = {})
76
+ raise URIError, 'User token is required' if no_token?(params)
77
+
78
+ credentials = set_credentials(params)
79
+ headers_hash = {}
80
+ headers_hash.store('Authorization',
81
+ "PVEAPIToken=#{credentials[:userid]}!#{credentials[:token_id]}=#{credentials[:token]}")
82
+ headers_hash.merge! additional_headers
83
+ headers_hash
84
+ end
85
+
86
+ def build_credentials(proxmox_options, data)
87
+ @expires = data['expire']
88
+ @token = proxmox_options[:proxmox_token]
89
+ @token_id = proxmox_options[:proxmox_tokenid]
90
+ @userid = proxmox_options[:proxmox_userid]
91
+ end
92
+
93
+ def missing_credentials(options)
94
+ missing_credentials = []
95
+ missing_credentials << :proxmox_userid unless options[:proxmox_userid]
96
+ missing_credentials << :proxmox_tokenid unless options[:proxmox_tokenid]
97
+ missing_credentials << :proxmox_token unless options[:proxmox_token]
98
+ return if missing_credentials.empty?
99
+
100
+ raise ArgumentError,
101
+ "Missing required arguments: #{missing_credentials.join(', ')}"
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end