Class: Kitchen::Driver::Proxmox::ApiClient
- Inherits:
-
Object
- Object
- Kitchen::Driver::Proxmox::ApiClient
- Defined in:
- lib/kitchen/driver/proxmox/api_client.rb
Overview
HTTP client for the Proxmox VE REST API. Authenticates via API tokens. Provides convenience methods for VM lifecycle operations.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#ssl_verify ⇒ Object
readonly
Returns the value of attribute ssl_verify.
-
#token_id ⇒ Object
readonly
Returns the value of attribute token_id.
-
#token_secret ⇒ Object
readonly
Returns the value of attribute token_secret.
Instance Method Summary collapse
- #agent_network_interfaces(node:, vm_id:) ⇒ Object
- #clone_vm(node:, template_id:, new_id:, **options) ⇒ Object
- #configure_vm(node:, vm_id:, cpus: nil, memory: nil, network_bridge: nil) ⇒ Object
- #destroy_vm(node:, vm_id:, purge: true) ⇒ Object
-
#initialize(base_url:, token_id:, token_secret:, ssl_verify: true) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #list_nodes ⇒ Object
- #list_templates ⇒ Object
- #next_vm_id ⇒ Object
- #start_vm(node:, vm_id:) ⇒ Object
- #stop_vm(node:, vm_id:) ⇒ Object
- #task_status(node:, upid:) ⇒ Object
- #vm_config(node:, vm_id:) ⇒ Object
- #vm_status(node:, vm_id:) ⇒ Object
- #wait_for_task(node:, upid:, timeout: 300, interval: 2) ⇒ Object
Constructor Details
#initialize(base_url:, token_id:, token_secret:, ssl_verify: true) ⇒ ApiClient
Returns a new instance of ApiClient.
18 19 20 21 22 23 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 18 def initialize(base_url:, token_id:, token_secret:, ssl_verify: true) @base_url = base_url.chomp('/') @token_id = token_id @token_secret = token_secret @ssl_verify = ssl_verify end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
16 17 18 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 16 def base_url @base_url end |
#ssl_verify ⇒ Object (readonly)
Returns the value of attribute ssl_verify.
16 17 18 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 16 def ssl_verify @ssl_verify end |
#token_id ⇒ Object (readonly)
Returns the value of attribute token_id.
16 17 18 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 16 def token_id @token_id end |
#token_secret ⇒ Object (readonly)
Returns the value of attribute token_secret.
16 17 18 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 16 def token_secret @token_secret end |
Instance Method Details
#agent_network_interfaces(node:, vm_id:) ⇒ Object
85 86 87 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 85 def agent_network_interfaces(node:, vm_id:) get("/api2/json/nodes/#{node}/qemu/#{vm_id}/agent/network-get-interfaces") end |
#clone_vm(node:, template_id:, new_id:, **options) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 29 def clone_vm(node:, template_id:, new_id:, **) full = .fetch(:full, true) body = { newid: new_id, full: full ? 1 : 0, target: node } body[:name] = [:name] if [:name] body[:pool] = [:pool] if [:pool] body[:storage] = [:storage] if [:storage] post("/api2/json/nodes/#{node}/qemu/#{template_id}/clone", body) end |
#configure_vm(node:, vm_id:, cpus: nil, memory: nil, network_bridge: nil) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 38 def configure_vm(node:, vm_id:, cpus: nil, memory: nil, network_bridge: nil) body = {} body[:cores] = cpus if cpus body[:memory] = memory if memory body[:net0] = "virtio,bridge=#{network_bridge}" if network_bridge return nil if body.empty? put("/api2/json/nodes/#{node}/qemu/#{vm_id}/config", body) end |
#destroy_vm(node:, vm_id:, purge: true) ⇒ Object
56 57 58 59 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 56 def destroy_vm(node:, vm_id:, purge: true) params = purge ? { purge: 1 } : {} delete("/api2/json/nodes/#{node}/qemu/#{vm_id}", params) end |
#list_nodes ⇒ Object
89 90 91 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 89 def list_nodes get('/api2/json/nodes') end |
#list_templates ⇒ Object
93 94 95 96 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 93 def list_templates resources = get('/api2/json/cluster/resources?type=vm') resources.select { |r| r['template'] == 1 } end |
#next_vm_id ⇒ Object
25 26 27 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 25 def next_vm_id get('/api2/json/cluster/nextid') end |
#start_vm(node:, vm_id:) ⇒ Object
48 49 50 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 48 def start_vm(node:, vm_id:) post("/api2/json/nodes/#{node}/qemu/#{vm_id}/status/start") end |
#stop_vm(node:, vm_id:) ⇒ Object
52 53 54 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 52 def stop_vm(node:, vm_id:) post("/api2/json/nodes/#{node}/qemu/#{vm_id}/status/stop") end |
#task_status(node:, upid:) ⇒ Object
69 70 71 72 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 69 def task_status(node:, upid:) encoded = URI.encode_www_form_component(upid) get("/api2/json/nodes/#{node}/tasks/#{encoded}/status") end |
#vm_config(node:, vm_id:) ⇒ Object
65 66 67 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 65 def vm_config(node:, vm_id:) get("/api2/json/nodes/#{node}/qemu/#{vm_id}/config") end |
#vm_status(node:, vm_id:) ⇒ Object
61 62 63 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 61 def vm_status(node:, vm_id:) get("/api2/json/nodes/#{node}/qemu/#{vm_id}/status/current") end |
#wait_for_task(node:, upid:, timeout: 300, interval: 2) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kitchen/driver/proxmox/api_client.rb', line 74 def wait_for_task(node:, upid:, timeout: 300, interval: 2) deadline = Time.now + timeout loop do status = task_status(node:, upid:) return status if status['status'] == 'stopped' raise "Task timeout after #{timeout}s: #{upid}" if Time.now > deadline sleep interval end end |