Skip to content

Commit

Permalink
add vm power state
Browse files Browse the repository at this point in the history
  • Loading branch information
matt c committed Oct 28, 2024
1 parent a967544 commit c62058e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
10 changes: 1 addition & 9 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Get the next available VMID for clone/create operations.

**Arguments**:

- `ticket` _str_ - The authentication ticket.
- `ticket` _str, optional_ - The authentication ticket.


**Returns**:
Expand All @@ -107,8 +107,6 @@ Clone a VM or template to a new VMID and assign a new name.

**Arguments**:

- `ticket` _str_ - The authentication ticket.
- `csrf_token` _str_ - The CSRF prevention token.
- `template_id` _int_ - The ID of the template to clone.
- `new_name` _str_ - The new name for the cloned VM.
- `new_id` _int_ - The new VMID for the cloned VM.
Expand All @@ -130,8 +128,6 @@ Assign admin permissions to a user for a given VMID.

**Arguments**:

- `ticket` _str_ - The authentication ticket.
- `csrf_token` _str_ - The CSRF prevention token.
- `vm_id` _int_ - The ID of the VM.
- `user` _str_ - The user to assign admin permissions to.

Expand All @@ -147,8 +143,6 @@ Set the description (Notes) of a VMID.

**Arguments**:

- `ticket` _str_ - The authentication ticket.
- `csrf_token` _str_ - The CSRF prevention token.
- `vm_id` _int_ - The ID of the VM.
- `desc` _str_ - The description to set for the VM.

Expand Down Expand Up @@ -254,8 +248,6 @@ Add a subnet to a given VNET ID.

**Arguments**:

- `ticket` _str_ - The authentication ticket.
- `csrf_token` _str_ - The CSRF prevention token.
- `vnet_id` _int_ - The ID of the VNET.
- `subnet_cidr` _str_ - The CIDR notation of the subnet to add.

Expand Down
34 changes: 33 additions & 1 deletion pveautomate/automate.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ def destroy_subnet(self, vnet, subnet_cidr):
subnet_cidr (str): The CIDR notation of the subnet to add.
"""
vnet_url = f"{self.proxmox_url}/cluster/sdn/vnets/{vnet}/subnets/{subnet_cidr}"

ticket, csrf_token = self.authenticate()

headers = {
Expand All @@ -353,6 +352,39 @@ def destroy_subnet(self, vnet, subnet_cidr):

return str(response.json())

def set_vm_power_status(self, vmid, state):
"""
Set the power state of a VM
Args:
vmid (int): The ID of the VM
state (str): The desired state of the VM. One of "start", "stop", "reset", "shutdown", "suspend", "resume", or "reboot"
"""
ticket, csrf_token = self.authenticate()
if state == "start":
url = f"{self.proxmox_url}/nodes/{self.node}/qemu/{vmid}/status/start"
elif state == "stop":
url = f"{self.proxmox_url}/nodes/{self.node}/qemu/{vmid}/status/stop"
elif state == "reset":
url = f"{self.proxmox_url}/nodes/{self.node}/qemu/{vmid}/status/reset"
elif state == "shutdown":
url = f"{self.proxmox_url}/nodes/{self.node}/qemu/{vmid}/status/shutdown"
elif state == "suspend":
url = f"{self.proxmox_url}/nodes/{self.node}/qemu/{vmid}/status/suspend"
elif state == "resume":
url = f"{self.proxmox_url}/nodes/{self.node}/qemu/{vmid}/status/resume"
elif state == "reboot":
url = f"{self.proxmox_url}/nodes/{self.node}/qemu/{vmid}/status/reboot"
else:
return "Invalid state"

headers = {
"Cookie": f"PVEAuthCookie={ticket}",
"CSRFPreventionToken": csrf_token,
}

response = requests.post(url, headers=headers, verify=self.verify_ssl)


if __name__ == "__main__":
print("Stop it")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="pveautomate",
version="0.2.3",
version="0.2.4",
description="A package to automate Proxmox VE tasks",
long_description=open("README.md").read()
+ "\n\n# Example Usage:\n"
Expand Down
15 changes: 2 additions & 13 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
"ccdc",
)

# print(pm.add_subnet_to_vnet("foobar", "192.167.1.0/24", "192.167.1.1"))
# print(pm.apply_sdn())
# print(pm.create_range([100,101], input("Enter user: ")))
# pm.clone_vm(1002, "loltest", pm.get_next_vm_id())

for vm in range(102, 110):
pm.destroy_vm(vm)

# for i in range(1, 13):
# cidr = f"192.168.{i}.0/24"
# gateway = f"192.168.{i}.1"
# print(pm.add_subnet_to_vnet("foobar", cidr, gateway))
# pm.apply_sdn()

# print(pm.check_if_user("matt@pve"))
pm.assign_admin_vm_permissions(102, "matt@pve")

0 comments on commit c62058e

Please sign in to comment.