Skip to content

Commit 15aadbd

Browse files
authored
Merge pull request #32 from arangodb-managed/OAS-3140
2 parents 0a23517 + 9710682 commit 15aadbd

File tree

5 files changed

+168
-28
lines changed

5 files changed

+168
-28
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ resource "oasis_deployment" "my_oneshard_deployment" {
7777
node_size_id = "a4"
7878
node_disk_size = 20
7979
}
80+
81+
notification_settings {
82+
email_addresses = "[email protected]" // this will set email addresses used for notifications regarding depoyment
83+
}
8084
}
8185
8286
// Example of oneshard deployment without node specification
@@ -124,6 +128,7 @@ resource "oasis_deployment" "my_sharded_deployment" {
124128
node_disk_size = 20
125129
node_count = 5
126130
}
131+
127132
}
128133
```
129134

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.70.8
4+
github.com/arangodb-managed/apis v0.71.2
55
github.com/arangodb-managed/log-helper v0.2.0
66
github.com/gogo/protobuf v1.3.0
77
github.com/hashicorp/hcl v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0 h1:MzVXffFU
4545
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
4646
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
4747
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
48-
github.com/arangodb-managed/apis v0.70.8 h1:d6NqlgBdmt3l30kFc5NxyAJULD8wRtZGcrhcuqYFJiI=
49-
github.com/arangodb-managed/apis v0.70.8/go.mod h1:dSEV+DTPdZNH06qVqFWA+F0OcaL2ePGEo+odyMaU72Y=
48+
github.com/arangodb-managed/apis v0.71.2 h1:I5SAcZzAzzOJG5GvFwXBYG57btSx/YNEDo4IBW33ZJ4=
49+
github.com/arangodb-managed/apis v0.71.2/go.mod h1:dSEV+DTPdZNH06qVqFWA+F0OcaL2ePGEo+odyMaU72Y=
5050
github.com/arangodb-managed/log-helper v0.2.0 h1:QK85i0a+mGM++wK625Oe1z4HuXhvaN3vR/Nunwa1qAA=
5151
github.com/arangodb-managed/log-helper v0.2.0/go.mod h1:WJogNCCXWM5OQx/ZYvtRo/1zwm/IpKj+f4QVtM8hNJw=
5252
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=

pkg/resource_deployment.go

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,25 @@ import (
3838
)
3939

4040
const (
41-
deplTAndCAcceptedFieldName = "terms_and_conditions_accepted"
42-
deplProjectFieldName = "project"
43-
deplNameFieldName = "name"
44-
deplDescriptionFieldName = "description"
45-
deplLocationFieldName = "location"
46-
deplLocationRegionFieldName = "region"
47-
deplVersionFieldName = "version"
48-
deplVersionDbVersionFieldName = "db_version"
49-
deplSecurityFieldName = "security"
50-
deplSecurityCaCertificateFieldName = "ca_certificate"
51-
deplSecurityIpAllowlistFieldName = "ip_allowlist"
52-
deplSecurityDisableFoxxAuthenticationFieldName = "disable_foxx_authentication"
53-
deplConfigurationFieldName = "configuration"
54-
deplConfigurationModelFieldName = "model"
55-
deplConfigurationNodeSizeIdFieldName = "node_size_id"
56-
deplConfigurationNodeCountFieldName = "node_count"
57-
deplConfigurationNodeDiskSizeFieldName = "node_disk_size"
41+
deplTAndCAcceptedFieldName = "terms_and_conditions_accepted"
42+
deplProjectFieldName = "project"
43+
deplNameFieldName = "name"
44+
deplDescriptionFieldName = "description"
45+
deplLocationFieldName = "location"
46+
deplLocationRegionFieldName = "region"
47+
deplVersionFieldName = "version"
48+
deplVersionDbVersionFieldName = "db_version"
49+
deplSecurityFieldName = "security"
50+
deplSecurityCaCertificateFieldName = "ca_certificate"
51+
deplSecurityIpAllowlistFieldName = "ip_allowlist"
52+
deplSecurityDisableFoxxAuthenticationFieldName = "disable_foxx_authentication"
53+
deplConfigurationFieldName = "configuration"
54+
deplConfigurationModelFieldName = "model"
55+
deplConfigurationNodeSizeIdFieldName = "node_size_id"
56+
deplConfigurationNodeCountFieldName = "node_count"
57+
deplConfigurationNodeDiskSizeFieldName = "node_disk_size"
58+
deplNotificationConfigurationFieldName = "notification_settings"
59+
deplNotificationConfigurationEmailAddressesFieldName = "email_addresses"
5860
)
5961

6062
func resourceDeployment() *schema.Resource {
@@ -180,6 +182,25 @@ func resourceDeployment() *schema.Resource {
180182
},
181183
},
182184
},
185+
deplNotificationConfigurationFieldName: {
186+
Type: schema.TypeList,
187+
Optional: true,
188+
MaxItems: 1,
189+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
190+
return new == ""
191+
},
192+
Elem: &schema.Resource{
193+
Schema: map[string]*schema.Schema{
194+
deplNotificationConfigurationEmailAddressesFieldName: {
195+
Type: schema.TypeList,
196+
Optional: true,
197+
Elem: &schema.Schema{
198+
Type: schema.TypeString,
199+
},
200+
},
201+
},
202+
},
203+
},
183204
},
184205
}
185206
}
@@ -330,13 +351,14 @@ type configuration struct {
330351
func expandDeploymentResource(d *schema.ResourceData, defaultProject string) (*data.Deployment, error) {
331352
project := defaultProject
332353
var (
333-
name string
334-
description string
335-
ver version
336-
loc location
337-
conf configuration
338-
sec securityFields
339-
err error
354+
name string
355+
description string
356+
ver version
357+
loc location
358+
conf configuration
359+
sec securityFields
360+
err error
361+
notificationSetting *data.Deployment_NotificationSettings
340362
)
341363
if v, ok := d.GetOk(deplNameFieldName); ok {
342364
name = v.(string)
@@ -372,6 +394,12 @@ func expandDeploymentResource(d *schema.ResourceData, defaultProject string) (*d
372394
return nil, fmt.Errorf("unable to find parse field %s", deplConfigurationFieldName)
373395
}
374396

397+
if v, ok := d.GetOk(deplNotificationConfigurationFieldName); ok {
398+
if notificationSetting, err = expandNotificationSettings(v.([]interface{})); err != nil {
399+
return nil, err
400+
}
401+
}
402+
375403
return &data.Deployment{
376404
Name: name,
377405
Description: description,
@@ -387,6 +415,7 @@ func expandDeploymentResource(d *schema.ResourceData, defaultProject string) (*d
387415
NodeDiskSize: int32(conf.nodeDiskSize),
388416
NodeSizeId: conf.nodeSizeId,
389417
},
418+
NotificationSettings: notificationSetting,
390419
}, nil
391420
}
392421

@@ -455,6 +484,26 @@ func expandConfiguration(s []interface{}) (conf configuration, err error) {
455484
return
456485
}
457486

487+
// expandNotificationSettings gathers notification settings data set in terraform schema
488+
func expandNotificationSettings(s []interface{}) (settings *data.Deployment_NotificationSettings, err error) {
489+
for _, v := range s {
490+
item := v.(map[string]interface{})
491+
if emailAddresses, ok := item[deplNotificationConfigurationEmailAddressesFieldName]; ok {
492+
emailAddresses, ok := emailAddresses.([]interface{})
493+
if !ok {
494+
return nil, fmt.Errorf("failed to parse field %s", deplNotificationConfigurationEmailAddressesFieldName)
495+
}
496+
if settings == nil {
497+
settings = &data.Deployment_NotificationSettings{}
498+
}
499+
for _, addr := range emailAddresses {
500+
settings.EmailAddresses = append(settings.EmailAddresses, addr.(string))
501+
}
502+
}
503+
}
504+
return
505+
}
506+
458507
// resourceDeploymentRead retrieves deployment information from terraform stores.
459508
func resourceDeploymentRead(d *schema.ResourceData, m interface{}) error {
460509
client := m.(*Client)
@@ -486,8 +535,9 @@ func flattenDeployment(depl *data.Deployment) map[string]interface{} {
486535
loc := flattenLocationData(depl)
487536
ver := flattenVersion(depl)
488537
sec := flattenSecurity(depl)
538+
notificationSetting := flattenNotificationSettings(depl)
489539

490-
return map[string]interface{}{
540+
result := map[string]interface{}{
491541
deplNameFieldName: depl.GetName(),
492542
deplProjectFieldName: depl.GetProjectId(),
493543
deplDescriptionFieldName: depl.GetDescription(),
@@ -496,6 +546,10 @@ func flattenDeployment(depl *data.Deployment) map[string]interface{} {
496546
deplVersionFieldName: ver,
497547
deplSecurityFieldName: sec,
498548
}
549+
if notificationSetting != nil {
550+
result[deplNotificationConfigurationFieldName] = notificationSetting
551+
}
552+
return result
499553
}
500554

501555
// flattenVersion takes the version part of a deployment and creates a sub map for terraform schema.
@@ -539,6 +593,18 @@ func flattenConfigurationData(depl *data.Deployment) []interface{} {
539593
}
540594
}
541595

596+
// flattenNotificationSettings takes the notification settings part of a deployment and creates a sub map for terraform schema.
597+
func flattenNotificationSettings(depl *data.Deployment) []interface{} {
598+
if depl.GetNotificationSettings() == nil {
599+
return nil
600+
}
601+
return []interface{}{
602+
map[string]interface{}{
603+
deplNotificationConfigurationEmailAddressesFieldName: depl.GetNotificationSettings().GetEmailAddresses(),
604+
},
605+
}
606+
}
607+
542608
// resourceDeploymentUpdate checks fields for differences and updates a deployment if necessary.
543609
func resourceDeploymentUpdate(d *schema.ResourceData, m interface{}) error {
544610
client := m.(*Client)
@@ -597,6 +663,14 @@ func resourceDeploymentUpdate(d *schema.ResourceData, m interface{}) error {
597663
depl.Model.NodeCount = int32(conf.nodeCount)
598664
}
599665
}
666+
// if we have change on NotificationSettings apply it
667+
if d.HasChange(deplNotificationConfigurationFieldName) {
668+
settings, err := expandNotificationSettings(d.Get(deplNotificationConfigurationFieldName).([]interface{}))
669+
if err != nil {
670+
return err
671+
}
672+
depl.NotificationSettings = settings
673+
}
600674

601675
if res, err := datac.UpdateDeployment(client.ctxWithToken, depl); err != nil {
602676
client.log.Error().Err(err).Msg("Failed to update deployment")

pkg/resource_deployment_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,67 @@ func TestFlattenDeploymentResourceDisableFoxxAuth(t *testing.T) {
172172
assert.Equal(t, expected, flattened)
173173
}
174174

175+
func TestFlattenDeploymentResourceNotificationSettings(t *testing.T) {
176+
depl := &data.Deployment{
177+
Name: "test-name",
178+
Description: "test-desc",
179+
ProjectId: "123456789",
180+
RegionId: "gcp-europe-west4",
181+
Version: "3.6.0",
182+
Certificates: &data.Deployment_CertificateSpec{
183+
CaCertificateId: "certificate-id",
184+
},
185+
IpallowlistId: "ip-allowlist",
186+
DisableFoxxAuthentication: true,
187+
Model: &data.Deployment_ModelSpec{
188+
Model: "oneshard",
189+
NodeSizeId: "a8",
190+
NodeCount: 3,
191+
NodeDiskSize: 32,
192+
},
193+
NotificationSettings: &data.Deployment_NotificationSettings{
194+
EmailAddresses: []string{"[email protected]"},
195+
},
196+
}
197+
flattened := flattenDeployment(depl)
198+
expected := map[string]interface{}{
199+
deplProjectFieldName: "123456789",
200+
deplNameFieldName: "test-name",
201+
deplDescriptionFieldName: "test-desc",
202+
deplLocationFieldName: []interface{}{
203+
map[string]interface{}{
204+
deplLocationRegionFieldName: "gcp-europe-west4",
205+
},
206+
},
207+
deplVersionFieldName: []interface{}{
208+
map[string]interface{}{
209+
deplVersionDbVersionFieldName: "3.6.0",
210+
},
211+
},
212+
deplSecurityFieldName: []interface{}{
213+
map[string]interface{}{
214+
deplSecurityCaCertificateFieldName: "certificate-id",
215+
deplSecurityIpAllowlistFieldName: "ip-allowlist",
216+
deplSecurityDisableFoxxAuthenticationFieldName: true,
217+
},
218+
},
219+
deplConfigurationFieldName: []interface{}{
220+
map[string]interface{}{
221+
deplConfigurationModelFieldName: "oneshard",
222+
deplConfigurationNodeSizeIdFieldName: "a8",
223+
deplConfigurationNodeCountFieldName: 3,
224+
deplConfigurationNodeDiskSizeFieldName: 32,
225+
},
226+
},
227+
deplNotificationConfigurationFieldName: []interface{}{
228+
map[string]interface{}{
229+
deplNotificationConfigurationEmailAddressesFieldName: []string{"[email protected]"},
230+
},
231+
},
232+
}
233+
assert.Equal(t, expected, flattened)
234+
}
235+
175236
func TestExpandingDeploymentResource(t *testing.T) {
176237
depl := &data.Deployment{
177238
Name: "test-name",

0 commit comments

Comments
 (0)