Skip to content

Commit 7c347cd

Browse files
committed
feat(lb-frontend): add multi-certificates to resource
1 parent 3dc6acc commit 7c347cd

File tree

5 files changed

+1821
-26
lines changed

5 files changed

+1821
-26
lines changed

docs/resources/lb_frontend.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: |-
88

99
Creates and manages Scaleway Load-Balancer Frontends. For more information, see [the documentation](https://developers.scaleway.com/en/products/lb/zoned_api).
1010

11-
## Examples
11+
## Examples Usage
1212

1313
### Basic
1414

@@ -21,6 +21,40 @@ resource "scaleway_lb_frontend" "frontend01" {
2121
}
2222
```
2323

24+
## With Certificate
25+
26+
```hcl
27+
resource scaleway_lb_ip ip01 {}
28+
29+
resource scaleway_lb lb01 {
30+
ip_id = scaleway_lb_ip.ip01.id
31+
name = "test-lb"
32+
type = "lb-s"
33+
}
34+
35+
resource scaleway_lb_backend bkd01 {
36+
lb_id = scaleway_lb.lb01.id
37+
forward_protocol = "tcp"
38+
forward_port = 443
39+
proxy_protocol = "none"
40+
}
41+
42+
resource scaleway_lb_certificate cert01 {
43+
lb_id = scaleway_lb.lb01.id
44+
name = "test-cert-front-end"
45+
letsencrypt {
46+
common_name = "${replace(scaleway_lb_ip.ip01.ip_address,".", "-")}.lb.${scaleway_lb.lb01.region}.scw.cloud"
47+
}
48+
}
49+
50+
resource scaleway_lb_frontend frt01 {
51+
lb_id = scaleway_lb.lb01.id
52+
backend_id = scaleway_lb_backend.bkd01.id
53+
inbound_port = 443
54+
certificate_ids = [scaleway_lb_certificate.cert01.id]
55+
}
56+
```
57+
2458
## With ACLs
2559

2660
```hcl
@@ -93,8 +127,6 @@ The following arguments are supported:
93127
- `name` - (Optional) The name of the load-balancer frontend.
94128

95129
- `timeout_client` - (Optional) Maximum inactivity time on the client side. (e.g.: `1s`)
96-
97-
- `certificate_id` - (Deprecated) Certificate ID that should be used by the frontend.
98130

99131
- `certificate_ids` - (Optional) Collection of Certificate IDs that should be used by the frontend.
100132

@@ -125,6 +157,8 @@ The following arguments are supported:
125157
In addition to all arguments above, the following attributes are exported:
126158

127159
- `id` - The ID of the load-balancer frontend.
160+
- `certificate_id` - (Deprecated) first certificate ID used by the frontend.
161+
128162

129163
## Import
130164

scaleway/helpers.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,12 @@ func flattenSliceStringPtr(s []*string) interface{} {
451451
return res
452452
}
453453

454-
func flattenSliceIDsWithKey(certificates []string, key string, zone scw.Zone) interface{} {
455-
res := []map[string]interface{}(nil)
454+
func flattenSliceIDs(certificates []string, zone scw.Zone) interface{} {
455+
res := []interface{}(nil)
456456
for _, certificateID := range certificates {
457-
res = append(res, map[string]interface{}{
458-
key: newZonedIDString(zone, certificateID),
459-
})
457+
res = append(res, newZonedIDString(zone, certificateID))
460458
}
459+
461460
return res
462461
}
463462

scaleway/resource_lb_frontend.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,19 @@ func resourceScalewayLbFrontend() *schema.Resource {
6464
Description: "Set the maximum inactivity time on the client side",
6565
},
6666
"certificate_id": {
67-
Type: schema.TypeString,
68-
Optional: true,
69-
ValidateFunc: validationUUIDorUUIDWithLocality(),
70-
Description: "Certificate ID",
71-
Deprecated: "This field will no be longer supported. Please use certificate_ids",
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Certificate ID",
70+
Deprecated: "Please use certificate_ids",
7271
},
7372
"certificate_ids": {
74-
Type: schema.TypeSet,
75-
Optional: true,
76-
Description: "Collection of Certificate IDs",
77-
Elem: &schema.Resource{
78-
Schema: map[string]*schema.Schema{
79-
"certificate_id": {
80-
Type: schema.TypeString,
81-
ValidateFunc: validationUUID(),
82-
Required: true,
83-
Description: "Certificate ID",
84-
},
85-
},
73+
Type: schema.TypeList,
74+
Optional: true,
75+
Elem: &schema.Schema{
76+
Type: schema.TypeString,
77+
ValidateFunc: validationUUIDorUUIDWithLocality(),
8678
},
79+
Description: "Collection of Certificate IDs related to the load balancer and domain",
8780
},
8881
"acl": {
8982
Type: schema.TypeList,
@@ -259,7 +252,7 @@ func resourceScalewayLbFrontendRead(ctx context.Context, d *schema.ResourceData,
259252
}
260253

261254
if len(res.CertificateIDs) > 0 {
262-
_ = d.Set("certificate_ids", flattenSliceIDsWithKey(res.CertificateIDs, "certificate_id", zone))
255+
_ = d.Set("certificate_ids", flattenSliceIDs(res.CertificateIDs, zone))
263256
}
264257

265258
//read related acls.

scaleway/resource_lb_frontend_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,91 @@ func TestAccScalewayLbFrontend_Basic(t *testing.T) {
7878
})
7979
}
8080

81+
func TestAccScalewayLbFrontend_Certificate(t *testing.T) {
82+
tt := NewTestTools(t)
83+
defer tt.Cleanup()
84+
resource.ParallelTest(t, resource.TestCase{
85+
PreCheck: func() { testAccPreCheck(t) },
86+
ProviderFactories: tt.ProviderFactories,
87+
CheckDestroy: testAccCheckScalewayLbFrontendDestroy(tt),
88+
Steps: []resource.TestStep{
89+
{
90+
Config: `
91+
resource scaleway_lb_ip ip01 {}
92+
93+
resource scaleway_lb lb01 {
94+
ip_id = scaleway_lb_ip.ip01.id
95+
name = "test-lb"
96+
type = "lb-s"
97+
}
98+
99+
resource scaleway_lb_backend bkd01 {
100+
lb_id = scaleway_lb.lb01.id
101+
forward_protocol = "tcp"
102+
forward_port = 443
103+
proxy_protocol = "none"
104+
}
105+
106+
resource scaleway_lb_certificate cert01 {
107+
lb_id = scaleway_lb.lb01.id
108+
name = "test-cert-front-end"
109+
letsencrypt {
110+
common_name = "${replace(scaleway_lb_ip.ip01.ip_address,".", "-")}.lb.${scaleway_lb.lb01.region}.scw.cloud"
111+
}
112+
}
113+
114+
resource scaleway_lb_frontend frt01 {
115+
lb_id = scaleway_lb.lb01.id
116+
backend_id = scaleway_lb_backend.bkd01.id
117+
inbound_port = 443
118+
certificate_ids = [scaleway_lb_certificate.cert01.id]
119+
}
120+
`,
121+
Check: resource.ComposeTestCheckFunc(
122+
testAccCheckScalewayLbFrontendExists(tt, "scaleway_lb_frontend.frt01"),
123+
testAccCheckScalewayFrontendCertificateExist(tt, "scaleway_lb_frontend.frt01", "scaleway_lb_certificate.cert01"),
124+
resource.TestCheckResourceAttr("scaleway_lb_frontend.frt01",
125+
"certificate_ids.#", "1"),
126+
),
127+
},
128+
},
129+
})
130+
}
131+
func testAccCheckScalewayFrontendCertificateExist(tt *TestTools, f, c string) resource.TestCheckFunc {
132+
return func(s *terraform.State) error {
133+
rs, ok := s.RootModule().Resources[f]
134+
if !ok {
135+
return fmt.Errorf("resource not found: %s", f)
136+
}
137+
138+
cs, ok := s.RootModule().Resources[c]
139+
if !ok {
140+
return fmt.Errorf("resource not found: %s", c)
141+
}
142+
143+
lbAPI, zone, ID, err := lbAPIWithZoneAndID(tt.Meta, rs.Primary.ID)
144+
if err != nil {
145+
return err
146+
}
147+
148+
frEnd, err := lbAPI.GetFrontend(&lb.ZonedAPIGetFrontendRequest{
149+
FrontendID: ID,
150+
Zone: zone,
151+
})
152+
if err != nil {
153+
return err
154+
}
155+
156+
for _, id := range frEnd.CertificateIDs {
157+
if expandID(cs.Primary.ID) == id {
158+
return nil
159+
}
160+
}
161+
162+
return fmt.Errorf("certificate not found: %s", c)
163+
}
164+
}
165+
81166
func testAccCheckScalewayLbFrontendExists(tt *TestTools, n string) resource.TestCheckFunc {
82167
return func(s *terraform.State) error {
83168
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)