-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
alias.go
146 lines (117 loc) · 3.1 KB
/
alias.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* 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"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/bpg/terraform-provider-proxmox/proxmox/firewall"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/structure"
)
const (
mkAliasName = "name"
mkAliasCIDR = "cidr"
mkAliasComment = "comment"
)
// Alias returns a resource to manage aliases.
func Alias() *schema.Resource {
s := map[string]*schema.Schema{
mkAliasName: {
Type: schema.TypeString,
Description: "Alias name",
Required: true,
},
mkAliasCIDR: {
Type: schema.TypeString,
Description: "IP/CIDR block",
Required: true,
},
mkAliasComment: {
Type: schema.TypeString,
Description: "Alias comment",
Optional: true,
Default: "",
},
}
structure.MergeSchema(s, selectorSchema())
return &schema.Resource{
Schema: s,
CreateContext: selectFirewallAPI(aliasCreate),
ReadContext: selectFirewallAPI(aliasRead),
UpdateContext: selectFirewallAPI(aliasUpdate),
DeleteContext: selectFirewallAPI(aliasDelete),
}
}
func aliasCreate(ctx context.Context, api firewall.API, d *schema.ResourceData) diag.Diagnostics {
comment := d.Get(mkAliasComment).(string)
name := d.Get(mkAliasName).(string)
cidr := d.Get(mkAliasCIDR).(string)
body := &firewall.AliasCreateRequestBody{
Comment: &comment,
Name: name,
CIDR: cidr,
}
err := api.CreateAlias(ctx, body)
if err != nil {
return diag.FromErr(err)
}
d.SetId(name)
return aliasRead(ctx, api, d)
}
func aliasRead(ctx context.Context, api firewall.API, d *schema.ResourceData) diag.Diagnostics {
name := d.Id()
alias, err := api.GetAlias(ctx, name)
if err != nil {
if strings.Contains(err.Error(), "no such alias") {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
aliasMap := map[string]interface{}{
mkAliasComment: alias.Comment,
mkAliasName: alias.Name,
mkAliasCIDR: alias.CIDR,
}
for key, val := range aliasMap {
err = d.Set(key, val)
if err != nil {
return diag.FromErr(err)
}
}
return nil
}
func aliasUpdate(ctx context.Context, api firewall.API, d *schema.ResourceData) diag.Diagnostics {
comment := d.Get(mkAliasComment).(string)
cidr := d.Get(mkAliasCIDR).(string)
newName := d.Get(mkAliasName).(string)
previousName := d.Id()
body := &firewall.AliasUpdateRequestBody{
ReName: newName,
CIDR: cidr,
Comment: &comment,
}
err := api.UpdateAlias(ctx, previousName, body)
if err != nil {
return diag.FromErr(err)
}
d.SetId(newName)
return aliasRead(ctx, api, d)
}
func aliasDelete(ctx context.Context, api firewall.API, d *schema.ResourceData) diag.Diagnostics {
name := d.Id()
err := api.DeleteAlias(ctx, name)
if err != nil {
if strings.Contains(err.Error(), "no such alias") {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
d.SetId("")
return nil
}