Skip to content

Commit 90f2f01

Browse files
authored
feat(mongodb): add TLS certificate (#3228)
* feat(mongodb): add tsl certificate * use io.ReadAll
1 parent d3cd20a commit 90f2f01

File tree

4 files changed

+2270
-284
lines changed

4 files changed

+2270
-284
lines changed

docs/resources/mongodb_instance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ In addition to all arguments above, the following attributes are exported:
126126
- `id` - The ID of the endpoint.
127127
- `port` - TCP port of the endpoint.
128128
- `dns_records` - List of DNS records for your endpoint.
129+
- `tls_certificate` - The PEM-encoded TLS certificate for the MongoDB® instance, if available.
129130

130131
## Import
131132

internal/services/mongodb/instance.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78
"strings"
89
"time"
910

@@ -233,6 +234,11 @@ func ResourceInstance() *schema.Resource {
233234
// Common
234235
"region": regional.Schema(),
235236
"project_id": account.ProjectIDSchema(),
237+
"tls_certificate": {
238+
Type: schema.TypeString,
239+
Computed: true,
240+
Description: "PEM-encoded TLS certificate for MongoDB",
241+
},
236242
},
237243
CustomizeDiff: customdiff.All(
238244
func(ctx context.Context, d *schema.ResourceDiff, meta any) error {
@@ -460,6 +466,24 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m any) di
460466

461467
_ = d.Set("settings", map[string]string{})
462468

469+
cert, err := mongodbAPI.GetInstanceCertificate(&mongodb.GetInstanceCertificateRequest{
470+
Region: region,
471+
InstanceID: ID,
472+
}, scw.WithContext(ctx))
473+
474+
if err == nil && cert != nil {
475+
certBytes, readErr := io.ReadAll(cert.Content)
476+
if readErr == nil {
477+
_ = d.Set("tls_certificate", string(certBytes))
478+
} else {
479+
diags = append(diags, diag.Diagnostic{
480+
Severity: diag.Warning,
481+
Summary: "Failed to read MongoDB TLS certificate content",
482+
Detail: readErr.Error(),
483+
})
484+
}
485+
}
486+
463487
return diags
464488
}
465489

internal/services/mongodb/instance_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestAccMongoDBInstance_Basic(t *testing.T) {
4141
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "node_number", "1"),
4242
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "user_name", "my_initial_user"),
4343
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "password", "thiZ_is_v&ry_s3cret"),
44+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "tls_certificate"),
4445
),
4546
},
4647
},

0 commit comments

Comments
 (0)