/
resource_policy_attachment.go
97 lines (87 loc) · 2.92 KB
/
resource_policy_attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package border0
import (
"context"
"fmt"
"log"
"strings"
border0client "github.com/borderzero/border0-go/client"
"github.com/borderzero/terraform-provider-border0/internal/diagnostics"
"github.com/borderzero/terraform-provider-border0/internal/schemautil"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourcePolicyAttachment() *schema.Resource {
return &schema.Resource{
Description: "Attaches a managed policy to a socket.",
ReadContext: resourcePolicyAttachmentRead,
CreateContext: resourcePolicyAttachmentCreate,
DeleteContext: resourcePolicyAttachmentDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"policy_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the policy to attach.",
},
"socket_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the socket to attach the policy to.",
},
},
}
}
func resourcePolicyAttachmentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(border0client.Requester)
ids := strings.Split(d.Id(), ":")
if len(ids) != 2 {
return diag.Errorf("Invalid ID format: %s. Correct format is policyID:socketID", d.Id())
}
policyID, socketID := ids[0], ids[1]
policy, err := client.Policy(ctx, policyID)
if !d.IsNewResource() && border0client.NotFound(err) {
log.Printf("[WARN] Policy (%s) not found, removing from state", policyID)
d.SetId("")
return nil
}
if err != nil {
return diagnostics.Error(err, "Failed to fetch policy")
}
for _, eachSocketID := range policy.SocketIDs {
if eachSocketID == socketID {
return schemautil.SetValues(d, map[string]any{
"policy_id": policyID,
"socket_id": socketID,
})
}
}
return nil
}
func resourcePolicyAttachmentCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(border0client.Requester)
policyID := d.Get("policy_id").(string)
socketID := d.Get("socket_id").(string)
err := client.AttachPolicyToSocket(ctx, policyID, socketID)
if err != nil {
return diagnostics.Error(err, "Failed to attach policy to socket")
}
d.SetId(fmt.Sprintf("%s:%s", policyID, socketID))
return resourcePolicyAttachmentRead(ctx, d, m)
}
func resourcePolicyAttachmentDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(border0client.Requester)
ids := strings.Split(d.Id(), ":")
if len(ids) != 2 {
return diag.Errorf("Invalid ID format: %s. Correct format is policyID:socketID", d.Id())
}
policyID, socketID := ids[0], ids[1]
if err := client.RemovePolicyFromSocket(ctx, policyID, socketID); err != nil {
return diagnostics.Error(err, "Failed to remove policy from socket")
}
d.SetId("")
return nil
}