Skip to content

Commit be41c30

Browse files
committed
Use string type for load balancer domain in resource and data source
1 parent eac6bcd commit be41c30

File tree

2 files changed

+49
-59
lines changed

2 files changed

+49
-59
lines changed

vultr/data_source_vultr_load_balancer.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ func dataSourceVultrLoadBalancer() *schema.Resource {
8282
Computed: true,
8383
Elem: &schema.Schema{Type: schema.TypeMap},
8484
},
85-
"auto_ssl": {
86-
Type: schema.TypeSet,
85+
"auto_ssl_domain": {
86+
Type: schema.TypeString,
8787
Computed: true,
88-
Elem: &schema.Schema{Type: schema.TypeMap},
8988
},
9089
},
9190
}
@@ -148,6 +147,9 @@ func dataSourceVultrLoadBalancerRead(ctx context.Context, d *schema.ResourceData
148147
if err := d.Set("ssl_redirect", lbList[0].GenericInfo.SSLRedirect); err != nil {
149148
return diag.Errorf("unable to set load_balancer `ssl_redirect` read value: %v", err)
150149
}
150+
if err := d.Set("auto_ssl_domain", lbList[0].AutoSSL.Domain); err != nil {
151+
return diag.Errorf("unable to set load_balancer `auto_ssl_domain` read value: %v", err)
152+
}
151153
if err := d.Set("proxy_protocol", lbList[0].GenericInfo.ProxyProtocol); err != nil {
152154
return diag.Errorf("unable to set load_balancer `proxy_protocol` read value: %v", err)
153155
}
@@ -217,17 +219,5 @@ func dataSourceVultrLoadBalancerRead(ctx context.Context, d *schema.ResourceData
217219
if err := d.Set("firewall_rules", fwrRules); err != nil {
218220
return diag.Errorf("unable to set load_balancer `firewall_rules` read value: %v", err)
219221
}
220-
221-
var autoSSL []map[string]interface{}
222-
autoSSLInfo := map[string]interface{}{
223-
"domain_zone": lbList[0].AutoSSL.DomainZone,
224-
"sub_domain": lbList[0].AutoSSL.DomainSub,
225-
}
226-
autoSSL = append(autoSSL, autoSSLInfo)
227-
228-
if err := d.Set("auto_ssl", autoSSL); err != nil {
229-
return diag.Errorf("unable to set load_balancer `auto_ssl` read value: %v", err)
230-
}
231-
232222
return nil
233223
}

vultr/resource_vultr_load_balancer.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"net/url"
78
"strings"
89
"time"
910

@@ -199,25 +200,10 @@ func resourceVultrLoadBalancer() *schema.Resource {
199200
Type: schema.TypeBool,
200201
Computed: true,
201202
},
202-
203-
"auto_ssl": {
204-
Type: schema.TypeSet,
203+
"auto_ssl_domain": {
204+
Type: schema.TypeString,
205205
Optional: true,
206-
MaxItems: 1,
207-
Elem: &schema.Resource{
208-
Schema: map[string]*schema.Schema{
209-
"domain_zone": {
210-
Type: schema.TypeString,
211-
Required: true,
212-
},
213-
"sub_domain": {
214-
Type: schema.TypeString,
215-
Optional: true,
216-
},
217-
},
218-
},
219206
},
220-
221207
"attached_instances": {
222208
Type: schema.TypeList,
223209
Optional: true,
@@ -275,8 +261,13 @@ func resourceVultrLoadBalancerCreate(ctx context.Context, d *schema.ResourceData
275261
}
276262

277263
var autoSSL *govultr.AutoSSL
278-
if autoSSLData, autoSSLOk := d.GetOk("auto_ssl"); autoSSLOk {
279-
autoSSL = generateAutoSSL(autoSSLData)
264+
if autoSSLDomainData, autoSSLDomainOk := d.GetOk("auto_ssl_domain"); autoSSLDomainOk {
265+
domain := autoSSLDomainData.(string)
266+
if autoSSLDomain, err := generateAutoSSL(domain); err != nil {
267+
return diag.Errorf("failed to parse auto SSL domain: %v", err)
268+
} else {
269+
autoSSL = autoSSLDomain
270+
}
280271
}
281272

282273
cookieName, cookieOk := d.GetOk("cookie_name")
@@ -382,19 +373,8 @@ func resourceVultrLoadBalancerRead(ctx context.Context, d *schema.ResourceData,
382373
"healthy_threshold": lb.HealthCheck.HealthyThreshold,
383374
}
384375
hc = append(hc, hcInfo)
385-
386-
var autoSSL interface{}
387-
if lb.AutoSSL != nil && lb.AutoSSL.DomainZone != "" {
388-
autoSSL = []map[string]interface{}{
389-
{
390-
"domain_zone": lb.AutoSSL.DomainZone,
391-
"sub_domain": lb.AutoSSL.DomainSub,
392-
},
393-
}
394-
}
395-
396-
if err := d.Set("auto_ssl", autoSSL); err != nil {
397-
return diag.Errorf("unable to set resource load_balancer `auto_ssl` read value: %v", err)
376+
if err := d.Set("auto_ssl_domain", lb.AutoSSL.Domain); err != nil {
377+
return diag.Errorf("unable to set resource load_balancer `auto_ssl_domain` read value: %v", err)
398378
}
399379
if err := d.Set("health_check", hc); err != nil {
400380
return diag.Errorf("unable to set resource load_balancer `health_check` read value: %v", err)
@@ -463,18 +443,20 @@ func resourceVultrLoadBalancerUpdate(ctx context.Context, d *schema.ResourceData
463443
req.SSL = nil
464444
}
465445
}
466-
467-
if d.HasChange("auto_ssl") {
468-
if autoSSLData, autoSSLOk := d.GetOk("auto_ssl"); autoSSLOk {
469-
req.AutoSSL = generateAutoSSL(autoSSLData)
446+
if d.HasChange("auto_ssl_domain") {
447+
if autoSSLData, ok := d.GetOk("auto_ssl_domain"); ok && autoSSLData.(string) != "" {
448+
autoSSL, err := generateAutoSSL(autoSSLData.(string))
449+
if err != nil {
450+
return diag.Errorf("failed to parse auto SSL domain: %v", err)
451+
}
452+
req.AutoSSL = autoSSL
470453
} else {
471-
log.Printf(`[INFO] Disabled load balancer auto SSL certificate (%v)`, d.Id())
454+
log.Printf("[INFO] Disabled load balancer auto SSL certificate (%v)", d.Id())
472455
if err := client.LoadBalancer.DeleteAutoSSL(ctx, d.Id()); err != nil {
473456
return diag.Errorf("error disabling load balancer auto SSL certificate (%v): %v", d.Id(), err)
474457
}
475458
}
476459
}
477-
478460
if d.HasChange("forwarding_rules") {
479461
_, newFR := d.GetChange("forwarding_rules")
480462

@@ -671,12 +653,30 @@ func generateSSL(sslData interface{}) *govultr.SSL {
671653
}
672654
}
673655

674-
func generateAutoSSL(autoSSLData interface{}) *govultr.AutoSSL {
675-
k := autoSSLData.(*schema.Set).List()
676-
config := k[0].(map[string]interface{})
656+
func generateAutoSSL(domain string) (*govultr.AutoSSL, error) {
657+
parsedDomain, err := url.Parse(domain)
658+
if err != nil || parsedDomain.Scheme != "" {
659+
return nil, fmt.Errorf("domain format must not include URL scheme (http:// or https://)")
660+
}
677661

678-
return &govultr.AutoSSL{
679-
DomainZone: config["domain_zone"].(string),
680-
DomainSub: config["sub_domain"].(string),
662+
hostname := parsedDomain.Hostname()
663+
if hostname == "" {
664+
hostname = domain
665+
}
666+
667+
subdomainParts := strings.Split(hostname, ".")
668+
if len(subdomainParts) <= 2 {
669+
return &govultr.AutoSSL{
670+
DomainZone: hostname,
671+
DomainSub: "",
672+
}, nil
681673
}
674+
675+
subDomain := subdomainParts[0]
676+
domainZone := strings.Join(subdomainParts[1:], ".")
677+
678+
return &govultr.AutoSSL{
679+
DomainZone: domainZone,
680+
DomainSub: subDomain,
681+
}, nil
682682
}

0 commit comments

Comments
 (0)