Skip to content

Commit d679e30

Browse files
committed
feat(mongodb): delete private network
1 parent c66128d commit d679e30

File tree

3 files changed

+2
-149
lines changed

3 files changed

+2
-149
lines changed

internal/services/mongodb/helpers.go

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package mongodb
22

33
import (
4-
"bytes"
54
"context"
6-
"fmt"
7-
"sort"
85
"time"
96

107
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
118
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
129
"github.com/scaleway/scaleway-sdk-go/scw"
13-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1410
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1511
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1612
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
17-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1813
)
1914

2015
const (
@@ -31,7 +26,7 @@ func newAPI(m interface{}) *mongodb.API {
3126
return mongodb.NewAPI(meta.ExtractScwClient(m))
3227
}
3328

34-
// newAPIWithZone returns a new Redis API and the zone for a Create request
29+
// newAPIWithZone returns a new mongoDB API and the zone for a Create request
3530
func newAPIWithZone(d *schema.ResourceData, m interface{}) (*mongodb.API, scw.Zone, error) {
3631
zone, err := meta.ExtractZone(d, m)
3732
if err != nil {
@@ -60,7 +55,7 @@ func newAPIWithRegion(d *schema.ResourceData, m interface{}) (*mongodb.API, scw.
6055
return newAPI(m), region, nil
6156
}
6257

63-
// NewAPIWithZoneAndID returns a Redis API with zone and ID extracted from the state
58+
// NewAPIWithZoneAndID returns a mongoDB API with zone and ID extracted from the state
6459
func NewAPIWithZoneAndID(m interface{}, id string) (*mongodb.API, scw.Zone, string, error) {
6560
zone, ID, err := zonal.ParseID(id)
6661
if err != nil {
@@ -81,18 +76,6 @@ func NewAPIWithRegionAndID(m interface{}, id string) (*mongodb.API, scw.Region,
8176
return newAPI(m), region, ID, nil
8277
}
8378

84-
func NewAPIWithID(m interface{}, id string) (*mongodb.API, scw.Region, string, error) {
85-
zone, ID, err := zonal.ParseID(id)
86-
if err != nil {
87-
return nil, "", "", err
88-
}
89-
region, err := zone.Region()
90-
if err != nil {
91-
return nil, "", "", err
92-
}
93-
return newAPI(m), region, ID, nil
94-
}
95-
9679
func waitForInstance(ctx context.Context, api *mongodb.API, region scw.Region, id string, timeout time.Duration) (*mongodb.Instance, error) {
9780
retryInterval := defaultWaitMongodbInstanceRetryInterval
9881
if transport.DefaultWaitRetryInterval != nil {
@@ -120,26 +103,3 @@ func waitForSnapshot(ctx context.Context, api *mongodb.API, region scw.Region, i
120103
RetryInterval: &retryInterval,
121104
}, scw.WithContext(ctx))
122105
}
123-
124-
func privateNetworkSetHash(v interface{}) int {
125-
var buf bytes.Buffer
126-
127-
m := v.(map[string]interface{})
128-
if pnID, ok := m["id"]; ok {
129-
buf.WriteString(locality.ExpandID(pnID))
130-
}
131-
132-
if serviceIPs, ok := m["service_ips"]; ok {
133-
// Sort the service IPs before generating the hash.
134-
ips := serviceIPs.([]interface{})
135-
sort.Slice(ips, func(i, j int) bool {
136-
return ips[i].(string) < ips[j].(string)
137-
})
138-
139-
for i, item := range ips {
140-
buf.WriteString(fmt.Sprintf("%d-%s-", i, item.(string)))
141-
}
142-
}
143-
144-
return types.StringHashcode(buf.String())
145-
}

internal/services/mongodb/instance.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package mongodb
33
import (
44
"context"
55
"errors"
6-
"strings"
76
"time"
87

98
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -13,12 +12,10 @@ import (
1312
"github.com/scaleway/scaleway-sdk-go/scw"
1413
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1514
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
16-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1715
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1816
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1917
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
2018
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
21-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
2219
)
2320

2421
func ResourceInstance() *schema.Resource {
@@ -106,58 +103,6 @@ func ResourceInstance() *schema.Resource {
106103
"version",
107104
},
108105
},
109-
//endpoint
110-
"private_network": {
111-
Type: schema.TypeSet,
112-
Optional: true,
113-
Description: "Private network specs details",
114-
Set: privateNetworkSetHash,
115-
DiffSuppressFunc: func(k, oldValue, newValue string, _ *schema.ResourceData) bool {
116-
// Check if the key is for the 'id' attribute
117-
if strings.HasSuffix(k, "id") {
118-
return locality.ExpandID(oldValue) == locality.ExpandID(newValue)
119-
}
120-
// For all other attributes, don't suppress the diff
121-
return false
122-
},
123-
Elem: &schema.Resource{
124-
Schema: map[string]*schema.Schema{
125-
"id": {
126-
Type: schema.TypeString,
127-
Required: true,
128-
ValidateDiagFunc: verify.IsUUIDorUUIDWithLocality(),
129-
Description: "UUID of the private network to be connected to the cluster",
130-
},
131-
"ips": {
132-
Type: schema.TypeList,
133-
Optional: true,
134-
Computed: true,
135-
Elem: &schema.Schema{
136-
Type: schema.TypeString,
137-
ValidateFunc: validation.IsCIDR,
138-
},
139-
Description: "List of IPv4 addresses of the private network with a CIDR notation",
140-
},
141-
"port": {
142-
Type: schema.TypeInt,
143-
Computed: true,
144-
Description: "The port of your load balancer service",
145-
},
146-
"dns_records": {
147-
Type: schema.TypeString,
148-
Computed: true,
149-
Description: "The DNS record of your endpoint",
150-
},
151-
// computed
152-
"endpoint_id": {
153-
Type: schema.TypeString,
154-
Computed: true,
155-
Description: "UUID of the endpoint to be connected to the cluster",
156-
},
157-
"zone": zonal.ComputedSchema(),
158-
},
159-
},
160-
},
161106
// Computed
162107
"public_network": {
163108
Type: schema.TypeList,
@@ -272,19 +217,6 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
272217
createReq.Tags = types.ExpandStrings(tags)
273218
}
274219

275-
pn, pnExists := d.GetOk("private_network")
276-
if pnExists {
277-
pnSpecs, err := expandPrivateNetwork(pn.(*schema.Set).List())
278-
if err != nil {
279-
return diag.FromErr(err)
280-
}
281-
createReq.Endpoints = pnSpecs
282-
} else {
283-
epSpecs := make([]*mongodb.EndpointSpec, 0, 1)
284-
spec := &mongodb.EndpointSpecPublicDetails{}
285-
createReq.Endpoints = append(epSpecs, &mongodb.EndpointSpec{Public: spec})
286-
}
287-
288220
res, err = mongodbAPI.CreateInstance(createReq, scw.WithContext(ctx))
289221
if err != nil {
290222
return diag.FromErr(err)
@@ -333,10 +265,6 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
333265
_ = d.Set("volume_size_in_gb", int(instance.Volume.Size/scw.GB))
334266
}
335267

336-
privateNetworkEndpoints, privateNetworkExists := flattenPrivateNetwork(instance.Endpoints)
337-
if privateNetworkExists {
338-
_ = d.Set("private_network", privateNetworkEndpoints)
339-
}
340268
publicNetworkEndpoint, publicNetworkExists := flattenPublicNetwork(instance.Endpoints)
341269
if publicNetworkExists {
342270
_ = d.Set("public_network", publicNetworkEndpoint)

internal/services/mongodb/types.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,8 @@ package mongodb
22

33
import (
44
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
5-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
65
)
76

8-
func expandPrivateNetwork(data []interface{}) ([]*mongodb.EndpointSpec, error) {
9-
if data == nil {
10-
return nil, nil
11-
}
12-
epSpecs := make([]*mongodb.EndpointSpec, 0, len(data))
13-
for _, rawPN := range data {
14-
pn := rawPN.(map[string]interface{})
15-
pnID := locality.ExpandID(pn["id"].(string))
16-
17-
spec := &mongodb.EndpointSpecPrivateNetworkDetails{
18-
PrivateNetworkID: pnID,
19-
}
20-
epSpecs = append(epSpecs, &mongodb.EndpointSpec{PrivateNetwork: spec})
21-
}
22-
return epSpecs, nil
23-
}
24-
25-
func flattenPrivateNetwork(endpoints []*mongodb.Endpoint) (interface{}, bool) {
26-
pnFlat := []map[string]interface{}(nil)
27-
for _, endpoint := range endpoints {
28-
if endpoint.PrivateNetwork == nil {
29-
continue
30-
}
31-
pnFlat = append(pnFlat, map[string]interface{}{
32-
"endpoint_id": endpoint.ID,
33-
"id": endpoint.PrivateNetwork.PrivateNetworkID,
34-
"ips": endpoint.IPs,
35-
"port": endpoint.Port,
36-
"dns_records": endpoint.DNSRecords,
37-
})
38-
}
39-
return pnFlat, len(pnFlat) != 0
40-
}
41-
427
func flattenPublicNetwork(endpoints []*mongodb.Endpoint) (interface{}, bool) {
438
publicFlat := []map[string]interface{}(nil)
449
for _, endpoint := range endpoints {

0 commit comments

Comments
 (0)