Skip to content

Commit

Permalink
Merge pull request #164 from dougiteixeira/repairs-permissions-commands
Browse files Browse the repository at this point in the history
Add repairs for failed command permissions
  • Loading branch information
dougiteixeira authored Nov 12, 2023
2 parents 3251bcc + 2e71a96 commit db96f51
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
40 changes: 37 additions & 3 deletions custom_components/proxmoxve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ def get_api_client(self) -> ProxmoxAPI:


def call_api_post_status(
self,
proxmox: ProxmoxAPI,
api_category: ProxmoxType,
command: str,
Expand All @@ -702,14 +703,47 @@ def call_api_post_status(
result = proxmox(
["nodes", node, api_category, vm_id, "status", ProxmoxCommand.SUSPEND]
).post(todisk=1)

else:
result = proxmox(
["nodes", node, api_category, vm_id, "status", command]
).post()

except (ResourceException, ConnectTimeout) as err:
except ResourceException as error:
if error.status_code == 403:
if api_category is ProxmoxType.Node:
issue_id=f"{self.config_entry.entry_id}_{node}_command_forbiden"
resource=f"{api_category.capitalize()} {node}"
permission_check = f"['perm','/nodes/{node}',['Sys.PowerMgmt']]"
elif api_category in (ProxmoxType.QEMU, ProxmoxType.LXC):
issue_id=f"{self.config_entry.entry_id}_{vm_id}_command_forbiden"
resource=f"{api_category.upper()} {vm_id}"
permission_check = f"['perm','/vms/{vm_id}',['VM.PowerMgmt']]"
else:
raise ValueError(
f"Resource not categorized correctly: Proxmox {api_category.upper()} {command} error - {error}",
) from error

async_create_issue(
self.hass,
DOMAIN,
issue_id,
is_fixable=False,
severity=IssueSeverity.ERROR,
translation_key="resource_command_forbiden",
translation_placeholders={
"resource": resource,
"user": self.config_entry.data[CONF_USERNAME],
"permission": permission_check,
"command": command,
},
)
raise ValueError(
f"Proxmox {api_category.upper()} {command} error - {error}",
) from error
except ConnectTimeout as error:
raise ValueError(
f"Proxmox {api_category} {command} error - {err}",
) from err
f"Proxmox {api_category.upper()} {command} error - {error}",
) from error

return result
2 changes: 2 additions & 0 deletions custom_components/proxmoxve/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def __init__(
super().__init__(coordinator, unique_id, description)

self._attr_device_info = info_device
self.config_entry = config_entry

def _button_press():
"""Post start command & tell HA state is on."""
Expand All @@ -289,6 +290,7 @@ def _button_press():
vm_id = resource_id

call_api_post_status(
self,
proxmox=proxmox_client.get_api_client(),
node=node,
vm_id=vm_id,
Expand Down
4 changes: 4 additions & 0 deletions custom_components/proxmoxve/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
"resource_exception_forbiden": {
"description": "User `{user}` does not have sufficient permissions to access resource `{resource}`.\n\nTip on required permissions:\n* `{permission}`\n\nPlease check documentation and user permissions.",
"title": "Permissions error for `{resource}`"
},
"resource_command_forbiden": {
"description": "User `{user}` does not have sufficient permissions to execute command `{command}` on resource `{resource}`.\n\nTip on required permissions:\n* `{permission}`\n\nPlease check documentation and user permissions.",
"title": "Permission error for `{resource}` command"
}
},
"options": {
Expand Down
4 changes: 4 additions & 0 deletions custom_components/proxmoxve/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
"resource_exception_forbiden": {
"description": "User `{user}` does not have sufficient permissions to access resource `{resource}`.\n\nTip on required permissions:\n* `{permission}`\n\nPlease check documentation and user permissions.",
"title": "Permissions error for `{resource}`"
},
"resource_command_forbiden": {
"description": "User `{user}` does not have sufficient permissions to execute command `{command}` on resource `{resource}`.\n\nTip on required permissions:\n* `{permission}`\n\nPlease check documentation and user permissions.",
"title": "Permission error for `{resource}` command"
}
},
"options": {
Expand Down
4 changes: 4 additions & 0 deletions custom_components/proxmoxve/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@
"resource_exception_forbiden": {
"description": "O usuário `{user}` não tem permissões suficientes para acessar o recurso `{resource}`.\n\nDica sobre as permissões necessárias:\n* `{permission}`\n\nPor favor, verifique a documentação e as permissões do usuário.",
"title": "Erro de permissão para `{resource}`"
},
"resource_command_forbiden": {
"description": "O usuário `{user}` não tem permissões suficientes para executar o comando `{command}` no recurso `{resource}`.\n\nDica sobre as permissões necessárias:\n* `{permission}`\n\nPor favor, verifique a documentação e as permissões do usuário .",
"title": "Erro de permissão para comando em `{resource}`"
}
},
"options": {
Expand Down

0 comments on commit db96f51

Please sign in to comment.