0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-04 04:22:59 +00:00
terraform-provider-proxmox/proxmoxtf/datasource/firewall/security_group.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

123 lines
3.0 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"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/bpg/terraform-provider-proxmox/proxmox/cluster/firewall"
)
const (
mkSecurityGroupName = "name"
mkSecurityGroupComment = "comment"
mkRules = "rules"
)
// SecurityGroupSchema defines the schema for the security group data source.
func SecurityGroupSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
mkSecurityGroupName: {
Type: schema.TypeString,
Description: "Security group name",
Required: true,
},
mkSecurityGroupComment: {
Type: schema.TypeString,
Description: "Security group comment",
Computed: true,
},
mkRules: {
Type: schema.TypeList,
Description: "List of rules",
Computed: true,
Elem: &schema.Resource{Schema: RuleSchema()},
},
}
}
// SecurityGroupRead reads the security group.
func SecurityGroupRead(ctx context.Context, api firewall.SecurityGroup, d *schema.ResourceData) diag.Diagnostics {
var diags diag.Diagnostics
name := d.Get(mkSecurityGroupName).(string)
allGroups, err := api.ListGroups(ctx)
if err != nil {
return diag.FromErr(err)
}
for _, v := range allGroups {
if v.Group == name {
err = d.Set(mkSecurityGroupName, v.Group)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkSecurityGroupComment, v.Comment)
diags = append(diags, diag.FromErr(err)...)
break
}
}
// rules := d.Get(mkRules).([]interface{})
// ruleIDs, err := fw.ListGroupRules(ctx, name)
// if err != nil {
// if strings.Contains(err.Error(), "no such security group") {
// d.SetId("")
// return nil
// }
// return diag.FromErr(err)
// }
// for _, id := range ruleIDs {
// ruleMap := map[string]interface{}{}
// err = readGroupRule(ctx, fw, name, id.Pos, ruleMap)
// if err != nil {
// diags = append(diags, diag.FromErr(err)...)
// } else {
// rules = append(rules, ruleMap)
// }
// }
// if diags.HasError() {
// return diags
// }
// err = d.Set(mkRules, rules)
// diags = append(diags, diag.FromErr(err)...)
d.SetId(name)
return diags
}
// func readGroupRule(
// ctx context.Context,
// fw firewall.API,
// group string,
// pos int,
// ruleMap map[string]interface{},
// ) error {
// rule, err := fw.GetGroupRule(ctx, group, pos)
// if err != nil {
// if strings.Contains(err.Error(), "no such security group") {
// return nil
// }
// return fmt.Errorf("error reading rule %d for group %s: %w", pos, group, err)
// }
//
// baseRuleToMap(&rule.BaseRule, ruleMap)
//
// // pos in the map should be int!
// ruleMap[mkRulePos] = pos
// ruleMap[mkRuleAction] = rule.Action
// ruleMap[mkRuleType] = rule.Type
//
// return nil
// }