Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: API client cleanup and refactoring #323

Merged
merged 23 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ issues:
# Maximum count of issues with the same text. Set to 0 to disable.
# Default is 3.
max-same-issues: 0
include:
- EXC0012
- EXC0014
exclude-rules:
# Exclude duplicate code and function length and complexity checking in test
# files (due to common repeats and long functions in test code)
Expand Down Expand Up @@ -43,6 +46,11 @@ linters-settings:
funlen:
lines: 80
statements: 60
errcheck:
check-blank: true
ignoretests: true
revive:
exported: ["checkPrivateReceivers"]
linters:
enable-all: true
disable:
Expand All @@ -53,6 +61,7 @@ linters:
- ifshort
- interfacer
- maligned
- nosnakecase
- rowserrcheck
- scopelint
- structcheck
Expand All @@ -63,24 +72,16 @@ linters:
- forcetypeassert
- funlen
- gocognit
- govet
# others
- exhaustivestruct
- exhaustruct
- gci
- gochecknoinits
- godot
- goerr113
- gomnd
- grouper
- ireturn
- maintidx
- nlreturn
- nonamedreturns
- nosnakecase
- tagliatelle
- testpackage
- thelper
- varnamelen
- wsl
fast: false
50 changes: 50 additions & 0 deletions proxmox/access/acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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"
"sort"

"github.com/bpg/terraform-provider-proxmox/proxmox/api"
)

func (c *Client) aclPath() string {
return c.ExpandPath("acl")
}

// GetACL retrieves the access control list.
func (c *Client) GetACL(ctx context.Context) ([]*ACLGetResponseData, error) {
resBody := &ACLGetResponseBody{}

err := c.DoRequest(ctx, http.MethodGet, c.aclPath(), nil, resBody)
if err != nil {
return nil, fmt.Errorf("failed to get access control list: %w", err)
}

if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}

sort.Slice(resBody.Data, func(i, j int) bool {
return resBody.Data[i].Path < resBody.Data[j].Path
})

return resBody.Data, nil
}

// UpdateACL updates the access control list.
func (c *Client) UpdateACL(ctx context.Context, d *ACLUpdateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPut, c.aclPath(), d, nil)
if err != nil {
return fmt.Errorf("failed to update access control list: %w", err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/*
* 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/. */
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package proxmox
package access

import "github.com/bpg/terraform-provider-proxmox/proxmox/types"

// VirtualEnvironmentACLGetResponseBody contains the body from an access control list response.
type VirtualEnvironmentACLGetResponseBody struct {
Data []*VirtualEnvironmentACLGetResponseData `json:"data,omitempty"`
// ACLGetResponseBody contains the body from an access control list response.
type ACLGetResponseBody struct {
Data []*ACLGetResponseData `json:"data,omitempty"`
}

// VirtualEnvironmentACLGetResponseData contains the data from an access control list response.
type VirtualEnvironmentACLGetResponseData struct {
// ACLGetResponseData contains the data from an access control list response.
type ACLGetResponseData struct {
Path string `json:"path"`
Propagate *types.CustomBool `json:"propagate,omitempty"`
RoleID string `json:"roleid"`
Type string `json:"type"`
UserOrGroupID string `json:"ugid"`
}

// VirtualEnvironmentACLUpdateRequestBody contains the data for an access control list update request.
type VirtualEnvironmentACLUpdateRequestBody struct {
// ACLUpdateRequestBody contains the data for an access control list update request.
type ACLUpdateRequestBody struct {
Delete *types.CustomBool `json:"delete,omitempty" url:"delete,omitempty,int"`
Groups []string `json:"groups,omitempty" url:"groups,omitempty,comma"`
Path string `json:"path" url:"path"`
Expand Down
23 changes: 23 additions & 0 deletions proxmox/access/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 (
"fmt"

"github.com/bpg/terraform-provider-proxmox/proxmox/api"
)

// Client is an interface for performing requests against the Proxmox 'access' API.
type Client struct {
api.Client
}

// ExpandPath expands a path relative to the client's base path.
func (c *Client) ExpandPath(path string) string {
return fmt.Sprintf("access/%s", path)
}
93 changes: 93 additions & 0 deletions proxmox/access/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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"
)

func (c *Client) groupsPath() string {
return c.ExpandPath("groups")
}

func (c *Client) groupPath(id string) string {
return fmt.Sprintf("%s/%s", c.groupsPath(), url.PathEscape(id))
}

// CreateGroup creates an access group.
func (c *Client) CreateGroup(ctx context.Context, d *GroupCreateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPost, c.groupsPath(), d, nil)
if err != nil {
return fmt.Errorf("failed to create access group: %w", err)
}

return nil
}

// DeleteGroup deletes an access group.
func (c *Client) DeleteGroup(ctx context.Context, id string) error {
err := c.DoRequest(ctx, http.MethodDelete, c.groupPath(id), nil, nil)
if err != nil {
return fmt.Errorf("failed to delete access group: %w", err)
}

return nil
}

// GetGroup retrieves an access group.
func (c *Client) GetGroup(ctx context.Context, id string) (*GroupGetResponseData, error) {
resBody := &GroupGetResponseBody{}

err := c.DoRequest(ctx, http.MethodGet, c.groupPath(id), nil, resBody)
if err != nil {
return nil, fmt.Errorf("failed to get access group: %w", err)
}

if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}

sort.Strings(resBody.Data.Members)

return resBody.Data, nil
}

// ListGroups retrieves a list of access groups.
func (c *Client) ListGroups(ctx context.Context) ([]*GroupListResponseData, error) {
resBody := &GroupListResponseBody{}

err := c.DoRequest(ctx, http.MethodGet, c.groupsPath(), nil, resBody)
if err != nil {
return nil, fmt.Errorf("failed to list access groups: %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
})

return resBody.Data, nil
}

// UpdateGroup updates an access group.
func (c *Client) UpdateGroup(ctx context.Context, id string, d *GroupUpdateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPut, c.groupPath(id), d, nil)
if err != nil {
return fmt.Errorf("failed to update access group: %w", err)
}

return nil
}
40 changes: 40 additions & 0 deletions proxmox/access/groups_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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

// GroupCreateRequestBody contains the data for an access group create request.
type GroupCreateRequestBody struct {
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
ID string `json:"groupid" url:"groupid"`
}

// GroupGetResponseBody contains the body from an access group get response.
type GroupGetResponseBody struct {
Data *GroupGetResponseData `json:"data,omitempty"`
}

// GroupGetResponseData contains the data from an access group get response.
type GroupGetResponseData struct {
Comment *string `json:"comment,omitempty"`
Members []string `json:"members"`
}

// GroupListResponseBody contains the body from an access group list response.
type GroupListResponseBody struct {
Data []*GroupListResponseData `json:"data,omitempty"`
}

// GroupListResponseData contains the data from an access group list response.
type GroupListResponseData struct {
Comment *string `json:"comment,omitempty"`
ID string `json:"groupid"`
}

// GroupUpdateRequestBody contains the data for an access group update request.
type GroupUpdateRequestBody struct {
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
}
Loading