0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-05 05:24:01 +00:00
terraform-provider-proxmox/proxmox/cluster/firewall/security_groups.go
Pavel Boldyrev 98e1cff7fe
feat: Add firewall resources (#246)
* refactoring existing cluster / firewall API for better composition

* add basic security groups API
fix linter errors

* add rules API

* fix after renaming resourceVirtualEnvironmentClusterIPSet

* fix linter errors

* make linter happy

* even more refactoring

* tidy up datasources

* in refactoring spree

* update examples

* fix firewall resource/datasource & client error handling

* add ipset(s) datasource

* update docs

* add security group resource with rules

* docs

* fix security group update, TODO: rule update

* fix after rebase

* add rule update, extract common rule schema, refactor group

* fix linter  errors

* bump linter for ci

* make alias and ipset reusable

* make security group reusable

* refactor datasources

* add security group datasources

* fix linter errors

* update docs

TODO: documentation for group datasources

* add sg docs, update doc index

* minor cleanup

* fix examples & tests

* stub for firewall-level options and rules

* extract firewall interface

* add firewall options and rules on the cluster level

TODO: issues with rule list management

* refactor all resources format AGAIN, now more flat, without complex subresources

* sort out hierarchy of APIs and remove duplication in API wrappers

* bring back security group

* finally, working rules

* restore cluster firewall option

* add containers support

* add options

* move rules back under security group, update docs

* fix vm_id / container_id attrs

* add examples

* cleanup

* more cleanup


Release-As: 0.17.0-rc1
2023-04-02 18:01:10 -04:00

114 lines
3.3 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 firewall
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"sort"
)
type SecurityGroup interface {
CreateGroup(ctx context.Context, d *GroupCreateRequestBody) error
ListGroups(ctx context.Context) ([]*GroupListResponseData, error)
UpdateGroup(ctx context.Context, d *GroupUpdateRequestBody) error
DeleteGroup(ctx context.Context, group string) error
}
// GroupCreateRequestBody contains the data for a security group create request.
type GroupCreateRequestBody struct {
Group string `json:"group" url:"group"`
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
Digest *string `json:"digest,omitempty" url:"digest,omitempty"`
}
// GroupListResponseData contains the data from a group list response.
type GroupListResponseData struct {
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
Group string `json:"group" url:"group"`
Digest string `json:"digest" url:"digest"`
}
// GroupListResponseBody contains the data from a group get response.
type GroupListResponseBody struct {
Data []*GroupListResponseData `json:"data,omitempty"`
}
// GroupUpdateRequestBody contains the data for a group update request.
type GroupUpdateRequestBody struct {
Group string `json:"group" url:"group"`
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
ReName *string `json:"rename,omitempty" url:"rename,omitempty"`
Digest *string `json:"digest,omitempty" url:"digest,omitempty"`
}
func (c *Client) securityGroupsPath() string {
return "cluster/firewall/groups"
}
// CreateGroup create new security group.
func (c *Client) CreateGroup(ctx context.Context, d *GroupCreateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPost, c.securityGroupsPath(), d, nil)
if err != nil {
return fmt.Errorf("error creating security group: %w", err)
}
return nil
}
// ListGroups retrieve list of security groups.
func (c *Client) ListGroups(ctx context.Context) ([]*GroupListResponseData, error) {
resBody := &GroupListResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.securityGroupsPath(), nil, resBody)
if err != nil {
return nil, fmt.Errorf("error retrieving security groups: %w", err)
}
if resBody.Data == nil {
return nil, errors.New("the server did not include a data object in the response")
}
sort.Slice(resBody.Data, func(i, j int) bool {
return resBody.Data[i].Group < resBody.Data[j].Group
})
return resBody.Data, nil
}
// UpdateGroup update security group.
func (c *Client) UpdateGroup(ctx context.Context, d *GroupUpdateRequestBody) error {
err := c.DoRequest(
ctx,
http.MethodPost,
c.securityGroupsPath(),
d,
nil,
)
if err != nil {
return fmt.Errorf("error updating security group: %w", err)
}
return nil
}
// DeleteGroup delete security group.
func (c *Client) DeleteGroup(ctx context.Context, group string) error {
err := c.DoRequest(
ctx,
http.MethodDelete,
fmt.Sprintf("%s/%s", c.securityGroupsPath(), url.PathEscape(group)),
nil,
nil,
)
if err != nil {
return fmt.Errorf("error deleting security group '%s': %w", group, err)
}
return nil
}