/
resource_vcd_lb_app_rule.go
216 lines (185 loc) · 7.07 KB
/
resource_vcd_lb_app_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package vcd
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
func resourceVcdLBAppRule() *schema.Resource {
return &schema.Resource{
Create: resourceVcdLBAppRuleCreate,
Read: resourceVcdLBAppRuleRead,
Update: resourceVcdLBAppRuleUpdate,
Delete: resourceVcdLBAppRuleDelete,
Importer: &schema.ResourceImporter{
State: resourceVcdLBAppRuleImport,
},
Schema: map[string]*schema.Schema{
"org": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of organization to use, optional if defined at provider " +
"level. Useful when connected as sysadmin working across different organizations",
},
"vdc": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of VDC to use, optional if defined at provider level",
},
"edge_gateway": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Edge gateway name in which the LB Application Rule is located",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Unique LB Application Rule name",
},
"script": {
Type: schema.TypeString,
Required: true,
Description: "The script for the application rule. Note - you may find HEREDOC " +
"useful to pass multiline strings",
},
},
}
}
func resourceVcdLBAppRuleCreate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
defer vcdClient.unLockParentEdgeGtw(d)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
LBRule, err := getLBAppRuleType(d)
if err != nil {
return fmt.Errorf("unable to create load balancer application rule type: %s", err)
}
createdPool, err := edgeGateway.CreateLbAppRule(LBRule)
if err != nil {
return fmt.Errorf("error creating new load balancer application rule: %s", err)
}
err = setLBAppRuleData(d, createdPool)
if err != nil {
return err
}
d.SetId(createdPool.ID)
return resourceVcdLBAppRuleRead(d, meta)
}
func resourceVcdLBAppRuleRead(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
readLBRule, err := edgeGateway.GetLbAppRuleById(d.Id())
if err != nil {
d.SetId("")
return fmt.Errorf("unable to find load balancer application rule with ID %s: %s", d.Id(), err)
}
return setLBAppRuleData(d, readLBRule)
}
func resourceVcdLBAppRuleUpdate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
defer vcdClient.unLockParentEdgeGtw(d)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
updateLBRuleConfig, err := getLBAppRuleType(d)
updateLBRuleConfig.ID = d.Id() // We already know an ID for update and it allows to change name
if err != nil {
return fmt.Errorf("could not create load balancer application rule type for update: %s", err)
}
updatedLBRule, err := edgeGateway.UpdateLbAppRule(updateLBRuleConfig)
if err != nil {
return fmt.Errorf("unable to update load balancer application rule with ID %s: %s", d.Id(), err)
}
if err := setLBAppRuleData(d, updatedLBRule); err != nil {
return err
}
return nil
}
func resourceVcdLBAppRuleDelete(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
defer vcdClient.unLockParentEdgeGtw(d)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
err = edgeGateway.DeleteLbAppRuleById(d.Id())
if err != nil {
return fmt.Errorf("error deleting load balancer application rule: %s", err)
}
d.SetId("")
return nil
}
// resourceVcdLBAppRuleImport is responsible for importing the resource.
// The following steps happen as part of import
// 1. The user supplies `terraform import _resource_name_ _the_id_string_` command
// 2. `_the_id_string_` contains a dot formatted path to resource as in the example below
// 3. The functions splits the dot-formatted path and tries to lookup the object
// 4. If the lookup succeeds it sets the ID field for `_resource_name_` resource in statefile
// (the resource must be already defined in .tf config otherwise `terraform import` will complain)
// 5. `terraform refresh` is being implicitly launched. The Read method looks up all other fields
// based on the known ID of object.
//
// Example resource name (_resource_name_): vcd_lb_app_rule.my-test-app-rule
// Example import path (_the_id_string_): org.vdc.edge-gw.existing-app-rule
// Note: the separator can be changed using Provider.import_separator or variable VCD_IMPORT_SEPARATOR
func resourceVcdLBAppRuleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
resourceURI := strings.Split(d.Id(), ImportSeparator)
if len(resourceURI) != 4 {
return nil, fmt.Errorf("resource name must be specified in such way org.vdc.edge-gw.existing-app-rule")
}
orgName, vdcName, edgeName, appRuleName := resourceURI[0], resourceURI[1], resourceURI[2], resourceURI[3]
vcdClient := meta.(*VCDClient)
edgeGateway, err := vcdClient.GetEdgeGateway(orgName, vdcName, edgeName)
if err != nil {
return nil, fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
readLBRule, err := edgeGateway.GetLbAppRuleByName(appRuleName)
if err != nil {
return []*schema.ResourceData{}, fmt.Errorf("unable to find load balancer application rule with name %s: %s",
d.Id(), err)
}
dSet(d, "org", orgName)
dSet(d, "vdc", vdcName)
dSet(d, "edge_gateway", edgeName)
dSet(d, "name", appRuleName)
d.SetId(readLBRule.ID)
return []*schema.ResourceData{d}, nil
}
// getLBAppRuleType converts Terraform resource data into types.LbAppRule type for API request.
func getLBAppRuleType(d *schema.ResourceData) (*types.LbAppRule, error) {
lbAppRule := &types.LbAppRule{
Name: d.Get("name").(string),
Script: d.Get("script").(string),
}
return lbAppRule, nil
}
// setLBAppRuleData sets name and script API fields. API output returns a single string terminated
// by newline ("\n") for each line of script. If a user wants to set multiline script Terraform's
// HEREDOC syntax is helpful.
// This terraform configuration
// script = <<-EOT
//
// acl vmware_page url_beg / vmware redirect location https://www.vmware.com/ ifvmware_page
// acl other_page2 url_beg / other2 redirect location https://www.other2.com/ ifother_page2
// acl hello payload(0,6) -m bin 48656c6c6f0a
// EOT
//
// is rendered as such API call
// <script>acl en req.fhdr(accept-language),language(es;fr;en) -m str en\nuse_backend english if en</script>
func setLBAppRuleData(d *schema.ResourceData, LBRule *types.LbAppRule) error {
dSet(d, "script", LBRule.Script)
dSet(d, "name", LBRule.Name)
return nil
}