/
resource_aviatrix_fqdn_tag_rule.go
162 lines (139 loc) · 4.01 KB
/
resource_aviatrix_fqdn_tag_rule.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package aviatrix
import (
"fmt"
"log"
"strings"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v2/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceAviatrixFQDNTagRule() *schema.Resource {
return &schema.Resource{
Create: resourceAviatrixFQDNTagRuleCreate,
Read: resourceAviatrixFQDNTagRuleRead,
Delete: resourceAviatrixFQDNTagRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"fqdn_tag_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "FQDN Filter Tag Name to attach this domain.",
},
"fqdn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "FQDN.",
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Protocol.",
},
"port": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Port.",
},
"action": {
Type: schema.TypeString,
Optional: true,
Default: "Base Policy",
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"Base Policy", "Allow", "Deny"}, false),
Description: "What action should happen to matching requests. " +
"Possible values are: 'Base Policy', 'Allow' or 'Deny'. Defaults to 'Base Policy' if no value is provided.",
},
},
}
}
func marshalFQDNTagRuleInput(d *schema.ResourceData) *goaviatrix.FQDN {
return &goaviatrix.FQDN{
FQDNTag: d.Get("fqdn_tag_name").(string),
DomainList: []*goaviatrix.Filters{
{
FQDN: d.Get("fqdn").(string),
Protocol: d.Get("protocol").(string),
Port: d.Get("port").(string),
Verdict: d.Get("action").(string),
},
},
}
}
func getFQDNTagRuleID(fqdn *goaviatrix.FQDN) string {
return fmt.Sprintf("%s~%s~%s~%s~%s",
fqdn.FQDNTag, fqdn.DomainList[0].FQDN, fqdn.DomainList[0].Protocol, fqdn.DomainList[0].Port, fqdn.DomainList[0].Verdict)
}
func resourceAviatrixFQDNTagRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
fqdn := marshalFQDNTagRuleInput(d)
if err := client.AddFQDNTagRule(fqdn); err != nil {
return err
}
id := getFQDNTagRuleID(fqdn)
d.SetId(id)
return nil
}
func resourceAviatrixFQDNTagRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
fqdnTag := d.Get("fqdn_tag_name").(string)
fqdnDomain := d.Get("fqdn").(string)
protocol := d.Get("protocol").(string)
port := d.Get("port").(string)
action := d.Get("action").(string)
if fqdnTag == "" {
id := d.Id()
log.Printf("[DEBUG] Looks like an import, no id received. Import Id is %s", id)
parts := strings.Split(id, "~")
if len(parts) != 5 {
return fmt.Errorf("invalid fqdn_tag_rule import id: %q, "+
"import id must be in the form fqdn_tag_name~fqdn~protocol~port~action", id)
}
d.SetId(id)
fqdnTag, fqdnDomain, protocol, port, action = parts[0], parts[1], parts[2], parts[3], parts[4]
}
fqdn := &goaviatrix.FQDN{
FQDNTag: fqdnTag,
DomainList: []*goaviatrix.Filters{
{
FQDN: fqdnDomain,
Protocol: protocol,
Port: port,
Verdict: action,
},
},
}
fqdn, err := client.GetFQDNTagRule(fqdn)
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("could not find fqdn_tag_rule %s: %v", fqdnDomain, err)
}
d.Set("fqdn_tag_name", fqdnTag)
d.Set("fqdn", fqdnDomain)
d.Set("protocol", protocol)
d.Set("port", port)
d.Set("action", action)
id := getFQDNTagRuleID(fqdn)
d.SetId(id)
return nil
}
func resourceAviatrixFQDNTagRuleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
fqdn := marshalFQDNTagRuleInput(d)
err := client.DeleteFQDNTagRule(fqdn)
if err == goaviatrix.ErrNotFound {
return nil
}
if err != nil {
return err
}
return nil
}