Skip to content

Commit c763c62

Browse files
authored
Merge pull request #72 from arangodb-managed/OAS-5419
OAS-5428 | Added support for private endpoints on GCP
2 parents a03749e + 840de54 commit c763c62

File tree

4 files changed

+108
-5
lines changed

4 files changed

+108
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/arangodb-managed/terraform-provider-oasis
22

33
require (
4-
github.com/arangodb-managed/apis v0.74.7
4+
github.com/arangodb-managed/apis v0.74.13
55
github.com/arangodb-managed/log-helper v0.2.5
66
github.com/gogo/protobuf v1.3.2
77
github.com/hashicorp/terraform-plugin-docs v0.8.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/
2828
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
2929
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
3030
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
31-
github.com/arangodb-managed/apis v0.74.7 h1:dIXemdYb1BjiyHLX/tYhOXwhA/KtL5HC61Yw9Vmtij4=
32-
github.com/arangodb-managed/apis v0.74.7/go.mod h1:ZlvA803MmUI3m6ijvaAYKKaWgLJq8bBZZuq8uyZo2PY=
31+
github.com/arangodb-managed/apis v0.74.13 h1:p+XK1d3Nxb2JXDlpFGj3yTWKRmnQwR+0l8NK/wTf3NA=
32+
github.com/arangodb-managed/apis v0.74.13/go.mod h1:ZlvA803MmUI3m6ijvaAYKKaWgLJq8bBZZuq8uyZo2PY=
3333
github.com/arangodb-managed/log-helper v0.2.5 h1:Kg3+0bDVFhEgyjMhIbCIj9hejgN2VaD4Cw/JQ4ARsd4=
3434
github.com/arangodb-managed/log-helper v0.2.5/go.mod h1:G17ASSd3Edday3i1QREGefyLJ2TduHxxFsOaqoigurE=
3535
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 h1:BUAU3CGlLvorLI26FmByPp2eC2qla6E1Tw+scpcg/to=

internal/resource_private_endpoint.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package provider
2323
import (
2424
"context"
2525
"fmt"
26+
2627
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
2728
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2829

@@ -47,6 +48,10 @@ const (
4748
privateEndpointAWSPrincipalAccountIdFieldName = "account_id"
4849
privateEndpointAWSPrincipalUserNamesFieldName = "user_names"
4950
privateEndpointAWSPrincipalRoleNamesFieldName = "role_names"
51+
52+
// GCP field names
53+
privateEndpointGCPFieldName = "gcp"
54+
privateEndpointGCPProjectsFieldName = "projects"
5055
)
5156

5257
// resourcePrivateEndpoint defines a Private Endpoint Oasis resource.
@@ -143,6 +148,26 @@ func resourcePrivateEndpoint() *schema.Resource {
143148
},
144149
},
145150
},
151+
privateEndpointGCPFieldName: {
152+
Type: schema.TypeList,
153+
Description: "Private Endpoint Resource Private Endpoint GCP field",
154+
Optional: true,
155+
MaxItems: 1,
156+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
157+
return old == "1" && new == "0"
158+
},
159+
Elem: &schema.Resource{
160+
Schema: map[string]*schema.Schema{
161+
privateEndpointGCPProjectsFieldName: {
162+
Type: schema.TypeList,
163+
Description: "Private Endpoint Resource Private Endpoint GCP Projects field (list of project ids)",
164+
Required: true,
165+
MaxItems: 1,
166+
Elem: &schema.Schema{Type: schema.TypeString},
167+
},
168+
},
169+
},
170+
},
146171
},
147172
}
148173
}
@@ -181,6 +206,7 @@ func flattenPrivateEndpointResource(privateEndpoint *network.PrivateEndpointServ
181206
privateEndpointDNSNamesFieldName: privateEndpoint.GetAlternateDnsNames(),
182207
privateEndpointAKSFieldName: flattenAKSResource(privateEndpoint.GetAks()),
183208
privateEndpointAWSFieldName: flattenAWSResource(privateEndpoint.GetAws()),
209+
privateEndpointGCPFieldName: flattenGCPResource(privateEndpoint.GetGcp()),
184210
}
185211
}
186212

@@ -202,6 +228,15 @@ func flattenAWSResource(privateEndpointAWS *network.PrivateEndpointService_Aws)
202228
}
203229
}
204230

231+
// flattenGCPResource will take an GCP Resource part of a Private Endpoint and create a sub map for terraform schema.
232+
func flattenGCPResource(privateEndpointGCP *network.PrivateEndpointService_Gcp) []interface{} {
233+
return []interface{}{
234+
map[string]interface{}{
235+
privateEndpointGCPProjectsFieldName: privateEndpointGCP.GetProjects(),
236+
},
237+
}
238+
}
239+
205240
// flattenAWSPrincipals will take an AWS Principal Resource part of a Private Endpoint and create a sub map for terraform schema.
206241
func flattenAWSPrincipals(privateEndpointAWSPrincipals []*network.PrivateEndpointService_AwsPrincipals) []interface{} {
207242
var principals = make(map[string]interface{})
@@ -281,11 +316,11 @@ func expandPrivateEndpointResource(d *schema.ResourceData) (*network.PrivateEndp
281316
ret.AlternateDnsNames = dnsNames
282317
}
283318
if v, ok := d.GetOk(privateEndpointAKSFieldName); ok {
284-
subscriptionIds, err := expandAKSResource(v.([]interface{}))
319+
aksResource, err := expandAKSResource(v.([]interface{}))
285320
if err != nil {
286321
return nil, err
287322
}
288-
ret.Aks = subscriptionIds
323+
ret.Aks = aksResource
289324
}
290325
if v, ok := d.GetOk(privateEndpointAWSFieldName); ok {
291326
awsResource, err := expandAWSResource(v.([]interface{}))
@@ -294,6 +329,13 @@ func expandPrivateEndpointResource(d *schema.ResourceData) (*network.PrivateEndp
294329
}
295330
ret.Aws = awsResource
296331
}
332+
if v, ok := d.GetOk(privateEndpointGCPFieldName); ok {
333+
gcpResource, err := expandGCPResource(v.([]interface{}))
334+
if err != nil {
335+
return nil, err
336+
}
337+
ret.Gcp = gcpResource
338+
}
297339
return ret, nil
298340
}
299341

@@ -365,6 +407,26 @@ func expandAWSPrincipal(s []interface{}) ([]*network.PrivateEndpointService_AwsP
365407
return principals, nil
366408
}
367409

410+
// expandGCPResource gathers GCP Resource data from the terraform store
411+
func expandGCPResource(s []interface{}) (gcpResource *network.PrivateEndpointService_Gcp, err error) {
412+
for _, v := range s {
413+
item := v.(map[string]interface{})
414+
if projects, ok := item[privateEndpointGCPProjectsFieldName]; ok {
415+
projects, ok := projects.([]interface{})
416+
if !ok {
417+
return nil, fmt.Errorf("failed to parse field %s", privateEndpointGCPProjectsFieldName)
418+
}
419+
if gcpResource == nil {
420+
gcpResource = &network.PrivateEndpointService_Gcp{}
421+
}
422+
for _, addr := range projects {
423+
gcpResource.Projects = append(gcpResource.Projects, addr.(string))
424+
}
425+
}
426+
}
427+
return
428+
}
429+
368430
// resourcePrivateEndpointDelete will delete the Terraform PrivateEndpoint resource
369431
func resourcePrivateEndpointDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
370432
d.SetId("")
@@ -414,6 +476,13 @@ func resourcePrivateEndpointUpdate(ctx context.Context, d *schema.ResourceData,
414476
}
415477
privateEndpoint.Aws = awsResource
416478
}
479+
if d.HasChange(privateEndpointGCPFieldName) {
480+
gcpResource, err := expandGCPResource(d.Get(privateEndpointGCPFieldName).([]interface{}))
481+
if err != nil {
482+
diag.FromErr(err)
483+
}
484+
privateEndpoint.Gcp = gcpResource
485+
}
417486

418487
_, err = nwc.UpdatePrivateEndpointService(client.ctxWithToken, privateEndpoint)
419488
if err != nil {

internal/resource_private_endpoint_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ func TestFlattenPrivateEndpoint(t *testing.T) {
114114
map[string]interface{}{},
115115
},
116116
}}
117+
var projects []string
118+
expected[privateEndpointGCPFieldName] = []interface{}{map[string]interface{}{
119+
privateEndpointGCPProjectsFieldName: projects,
120+
}}
117121

118122
rawAks := &network.PrivateEndpointService_Aks{
119123
ClientSubscriptionIds: []string{"ba3f371b-a5e3-47bf-b097-dc3bb0a052a5"},
@@ -142,6 +146,10 @@ func TestFlattenPrivateEndpoint(t *testing.T) {
142146
privateEndpointAKSClientSubscriptionIdsFieldName: subIDs,
143147
},
144148
}
149+
var projects []string
150+
expected[privateEndpointGCPFieldName] = []interface{}{map[string]interface{}{
151+
privateEndpointGCPProjectsFieldName: projects,
152+
}}
145153

146154
rawAws := &network.PrivateEndpointService_Aws{
147155
AwsPrincipals: []*network.PrivateEndpointService_AwsPrincipals{
@@ -156,8 +164,34 @@ func TestFlattenPrivateEndpoint(t *testing.T) {
156164

157165
flattened := flattenPrivateEndpointResource(privateEndpoint)
158166
assert.Equal(tt, expected, flattened)
167+
privateEndpoint.Aws = nil
159168
})
160169

170+
t.Run("flattening with gcp field", func(tt *testing.T) {
171+
expectedGcp := []interface{}{
172+
map[string]interface{}{
173+
privateEndpointGCPProjectsFieldName: []string{"project1"},
174+
},
175+
}
176+
expected[privateEndpointGCPFieldName] = expectedGcp
177+
expected[privateEndpointAWSFieldName] = []interface{}{map[string]interface{}{
178+
privateEndpointAWSPrincipalFieldName: []interface{}{
179+
map[string]interface{}{},
180+
},
181+
}}
182+
var subIDs []string
183+
expected[privateEndpointAKSFieldName] = []interface{}{map[string]interface{}{
184+
privateEndpointAKSClientSubscriptionIdsFieldName: subIDs,
185+
}}
186+
rawGcp := &network.PrivateEndpointService_Gcp{
187+
Projects: []string{"project1"},
188+
}
189+
privateEndpoint.Gcp = rawGcp
190+
191+
flattened := flattenPrivateEndpointResource(privateEndpoint)
192+
assert.Equal(tt, expected, flattened)
193+
privateEndpoint.Gcp = nil
194+
})
161195
}
162196

163197
// TestExpandPrivateEndpoint tests the Oasis Private Endpoint expansion for Terraform schema compatibility.

0 commit comments

Comments
 (0)