0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 02:31:10 +00:00
terraform-provider-proxmox/proxmox/access/roles.go
Pavel Boldyrev 1f006aa82b
feat: API client cleanup and refactoring (#323)
* cleanup 1

* continue refactoring

* more refactoring

* move VMs under nodes

* move container and other apis under nodes

* cleanups

* enabled revive.exported linter & add comments to exported stuff

* enable godot linter

* enable wsl linter

* enable thelper linter

* enable govet linter

* cleanup after rebase

* cleanup after rebase

* extract SSH ops into a separate interface

* fix linter error

* move ssh code to its own package

* cleaning up VirtualEnvironmentClient receivers

* on the finish line

* not sure what else I forgot... 🤔

* fix ssh connection and upload

* renaming client interfaces

* final cleanups
2023-05-26 01:32:51 +00:00

101 lines
2.4 KiB
Go

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package access
import (
"context"
"fmt"
"net/http"
"net/url"
"sort"
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
"github.com/bpg/terraform-provider-proxmox/proxmox/types"
)
func (c *Client) rolesPath() string {
return c.ExpandPath("roles")
}
func (c *Client) rolePath(id string) string {
return fmt.Sprintf("%s/%s", c.rolesPath(), url.PathEscape(id))
}
// CreateRole creates an access role.
func (c *Client) CreateRole(ctx context.Context, d *RoleCreateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPost, c.rolesPath(), d, nil)
if err != nil {
return fmt.Errorf("error creating role: %w", err)
}
return nil
}
// DeleteRole deletes an access role.
func (c *Client) DeleteRole(ctx context.Context, id string) error {
err := c.DoRequest(ctx, http.MethodDelete, c.rolePath(id), nil, nil)
if err != nil {
return fmt.Errorf("error deleting role: %w", err)
}
return nil
}
// GetRole retrieves an access role.
func (c *Client) GetRole(ctx context.Context, id string) (*types.CustomPrivileges, error) {
resBody := &RoleGetResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.rolePath(id), nil, resBody)
if err != nil {
return nil, fmt.Errorf("error getting role: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
sort.Strings(*resBody.Data)
return resBody.Data, nil
}
// ListRoles retrieves a list of access roles.
func (c *Client) ListRoles(ctx context.Context) ([]*RoleListResponseData, error) {
resBody := &RoleListResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.rolesPath(), nil, resBody)
if err != nil {
return nil, fmt.Errorf("error listing roles: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
sort.Slice(resBody.Data, func(i, j int) bool {
return resBody.Data[i].ID < resBody.Data[j].ID
})
for i := range resBody.Data {
if resBody.Data[i].Privileges != nil {
sort.Strings(*resBody.Data[i].Privileges)
}
}
return resBody.Data, nil
}
// UpdateRole updates an access role.
func (c *Client) UpdateRole(ctx context.Context, id string, d *RoleUpdateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPut, c.rolePath(id), d, nil)
if err != nil {
return fmt.Errorf("error updating role: %w", err)
}
return nil
}