Skip to content

Commit 3b17ae5

Browse files
committed
feat(mongodb): add tsl certificate
1 parent 9662e6a commit 3b17ae5

File tree

4 files changed

+2267
-284
lines changed

4 files changed

+2267
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package mongodb
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
8+
"io"
79
"strings"
810
"time"
911

@@ -12,9 +14,11 @@ import (
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1416
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
17+
1518
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1619
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1"
1720
"github.com/scaleway/scaleway-sdk-go/scw"
21+
1822
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1923
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
2024
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
@@ -233,6 +237,11 @@ func ResourceInstance() *schema.Resource {
233237
// Common
234238
"region": regional.Schema(),
235239
"project_id": account.ProjectIDSchema(),
240+
"tls_certificate": {
241+
Type: schema.TypeString,
242+
Computed: true,
243+
Description: "PEM-encoded TLS certificate for MongoDB",
244+
},
236245
},
237246
CustomizeDiff: customdiff.All(
238247
func(ctx context.Context, d *schema.ResourceDiff, meta any) error {
@@ -460,6 +469,18 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m any) di
460469

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

472+
cert, err := mongodbAPI.GetInstanceCertificate(&mongodb.GetInstanceCertificateRequest{
473+
Region: region,
474+
InstanceID: ID,
475+
}, scw.WithContext(ctx))
476+
if err == nil && cert != nil {
477+
var buf bytes.Buffer
478+
_, copyErr := io.Copy(&buf, cert.Content)
479+
if copyErr == nil {
480+
_ = d.Set("tls_certificate", buf.String())
481+
}
482+
}
483+
463484
return diags
464485
}
465486

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)