Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAS-5111 | add resources for oasis multi region backup #73

Merged
merged 23 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ update-modules:
test -f go.mod || go mod init
go get \
$(shell zutano go mod latest \
github.com/arangodb-managed/apis \
github.com/arangodb-managed/apis@OAS-3929 \
lakhansamani marked this conversation as resolved.
Show resolved Hide resolved
)
go mod tidy
1 change: 1 addition & 0 deletions docs/resources/backup_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ resource "oasis_backup_policy" "my_backup_policy" {

### Optional

- `additional_region_ids` (List of String) Backup Policy Additional Region Identifiers where backup should be cloned
- `description` (String) Backup Policy Resource Backup Policy Description field
- `is_paused` (Boolean) Backup Policy Resource Backup Policy Is Paused field
- `locked` (Boolean) Backup Policy Resource Backup Policy Locked field
Expand Down
27 changes: 27 additions & 0 deletions docs/resources/multi_region_backup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "oasis_multi_region_backup Resource - terraform-provider-oasis"
subcategory: ""
description: |-
Oasis Multi Region Backup Resource
---

# oasis_multi_region_backup (Resource)

Oasis Multi Region Backup Resource



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `region_id` (String) Oasis cloud provider region identifier field
- `source_backup_id` (String) Oasis backup resource identifier field

### Read-Only

- `id` (String) The ID of this resource.


10 changes: 10 additions & 0 deletions docs/resources/private_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ resource "oasis_private_endpoint" "my_aws_private_endpoint" {
- `aws` (Block List, Max: 1) Private Endpoint Resource Private Endpoint AWS field (see [below for nested schema](#nestedblock--aws))
- `description` (String) Private Endpoint Resource Private Endpoint Description field
- `dns_names` (List of String) Private Endpoint Resource Private Endpoint DNS Names field (list of dns names)
- `gcp` (Block List, Max: 1) Private Endpoint Resource Private Endpoint GCP field (see [below for nested schema](#nestedblock--gcp))

### Read-Only

Expand Down Expand Up @@ -159,3 +160,12 @@ Optional:
- `user_names` (List of String) Private Endpoint Resource Private Endpoint AWS Principal User Names field



<a id="nestedblock--gcp"></a>
### Nested Schema for `gcp`

Required:

- `projects` (List of String) Private Endpoint Resource Private Endpoint GCP Projects field (list of project ids)


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/arangodb-managed/terraform-provider-oasis

require (
github.com/arangodb-managed/apis v0.74.13
github.com/arangodb-managed/apis v0.74.14-0.20220805090646-b72674786cfc
github.com/arangodb-managed/log-helper v0.2.5
github.com/gogo/protobuf v1.3.2
github.com/hashicorp/terraform-plugin-docs v0.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/arangodb-managed/apis v0.74.13 h1:p+XK1d3Nxb2JXDlpFGj3yTWKRmnQwR+0l8NK/wTf3NA=
github.com/arangodb-managed/apis v0.74.13/go.mod h1:ZlvA803MmUI3m6ijvaAYKKaWgLJq8bBZZuq8uyZo2PY=
github.com/arangodb-managed/apis v0.74.14-0.20220805090646-b72674786cfc h1:rTnaM1n+9Wt2PuZ8LGMTZ3BdhVsCsAvLvV6kDl5VaOg=
github.com/arangodb-managed/apis v0.74.14-0.20220805090646-b72674786cfc/go.mod h1:ZlvA803MmUI3m6ijvaAYKKaWgLJq8bBZZuq8uyZo2PY=
github.com/arangodb-managed/log-helper v0.2.5 h1:Kg3+0bDVFhEgyjMhIbCIj9hejgN2VaD4Cw/JQ4ARsd4=
github.com/arangodb-managed/log-helper v0.2.5/go.mod h1:G17ASSd3Edday3i1QREGefyLJ2TduHxxFsOaqoigurE=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 h1:BUAU3CGlLvorLI26FmByPp2eC2qla6E1Tw+scpcg/to=
Expand Down
1 change: 1 addition & 0 deletions internal/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Provider() *schema.Provider {
"oasis_ipallowlist": resourceIPAllowlist(),
"oasis_certificate": resourceCertificate(),
"oasis_backup": resourceBackup(),
"oasis_multi_region_backup": resourceMultiRegionBackup(),
"oasis_backup_policy": resourceBackupPolicy(),
"oasis_project": resourceProject(),
"oasis_example_dataset_installation": resourceExampleDatasetInstallation(),
Expand Down
1 change: 1 addition & 0 deletions internal/resource_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,6 @@ func flattenBackupResource(backup *backup.Backup) map[string]interface{} {
backupURLFieldName: backup.GetUrl(),
backupPolicyIDFieldName: backup.GetBackupPolicyId(),
backupDeploymentIDFieldName: backup.GetDeploymentId(),
backupRegionIDFieldName: backup.GetRegionId(),
}
}
41 changes: 41 additions & 0 deletions internal/resource_backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const (
hourlySchedule = "Hourly"
dailySchedule = "Daily"
monthlySchedule = "Monthly"

// Additional region identifiers where backup should be cloned
backupPolicyAdditionalRegionIDs = "additional_region_ids"
)

// resourceBackupPolicy defines a BackupPolicy oasis resource.
Expand Down Expand Up @@ -133,6 +136,13 @@ func resourceBackupPolicy() *schema.Resource {
Description: "Backup Policy Resource Backup Policy Email Notification field",
Required: true,
},
backupPolicyAdditionalRegionIDs: {
Type: schema.TypeList,
Description: "Backup Policy Additional Region Identifiers where backup should be cloned",
Optional: true,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
},
backupPolicyScheduleFieldName: {
Type: schema.TypeList,
Description: "Backup Policy Resource Backup Policy Schedule field",
Expand Down Expand Up @@ -320,6 +330,14 @@ func resourceBackupPolicyUpdate(ctx context.Context, d *schema.ResourceData, m i
if d.HasChange(backupPolicyScheduleFieldName) {
policy.Schedule = expandBackupPolicySchedule(d.Get(backupPolicyScheduleFieldName).([]interface{}))
}

// if v, ok := d.GetOk(backupPolicyAdditionalRegionIDs); ok {
// additionalRegionIDs, err := expandAdditionalRegionList(v.([]interface{}))
// if err != nil {
// return diag.FromErr(err)
// }
// policy.AdditionalRegionIds = additionalRegionIDs
// }
lakhansamani marked this conversation as resolved.
Show resolved Hide resolved
// Make sure we are sending the right schedule. The coming back schedule can contain invalid
// field items for different schedule. We make sure here that the right one is sent after an
// update. This check can be removed once terraform allows conflict checks for list items.
Expand Down Expand Up @@ -395,6 +413,7 @@ func flattenBackupPolicyResource(policy *backup.BackupPolicy) map[string]interfa
backupPolictEmailNotificationFieldName: policy.GetEmailNotification(),
backupPolicyScheduleFieldName: schedule,
backupPolicyLockedFieldName: policy.GetLocked(),
backupPolicyAdditionalRegionIDs: policy.GetAdditionalRegionIds(),
}
if policy.GetRetentionPeriod() != nil {
seconds := policy.GetRetentionPeriod().GetSeconds()
Expand Down Expand Up @@ -499,6 +518,13 @@ func expandBackupPolicyResource(d *schema.ResourceData) (*backup.BackupPolicy, e
if v, ok := d.GetOk(backupPolicyLockedFieldName); ok {
ret.Locked = v.(bool)
}
if v, ok := d.GetOk(backupPolicyAdditionalRegionIDs); ok {
additionalRegionIDs, err := expandAdditionalRegionList(v.([]interface{}))
if err != nil {
return ret, err
}
ret.AdditionalRegionIds = additionalRegionIDs
}
return ret, nil
}

Expand Down Expand Up @@ -669,3 +695,18 @@ func resourceBackupPolicyDelete(ctx context.Context, d *schema.ResourceData, m i
d.SetId("") // called automatically, but added to be explicit
return nil
}

// expandAdditionalRegionList creates a string list of items from an interface slice. It also
// verifies if a given string item is empty or not. In case it's empty, an error is thrown.
func expandAdditionalRegionList(list []interface{}) ([]string, error) {
additionalRegionIDs := []string{}
for _, v := range list {
if v, ok := v.(string); ok {
if v == "" {
continue
}
additionalRegionIDs = append(additionalRegionIDs, v)
}
}
return additionalRegionIDs, nil
}
36 changes: 20 additions & 16 deletions internal/resource_backup_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import (

func TestFlattenBackupPolicy(t *testing.T) {
policy := &backup.BackupPolicy{
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
AdditionalRegionIds: []string{"aks-westeurope"},
}

expected := map[string]interface{}{
Expand All @@ -52,6 +53,7 @@ func TestFlattenBackupPolicy(t *testing.T) {
backupPolicyRetentionPeriodFieldName: 200,
backupPolictEmailNotificationFieldName: "None",
backupPolicyLockedFieldName: true,
backupPolicyAdditionalRegionIDs: []string{"aks-westeurope"},
}

t.Run("with hourly schedule", func(tt *testing.T) {
Expand Down Expand Up @@ -168,16 +170,18 @@ func TestExpandBackupPolicy(t *testing.T) {
backupPolicyRetentionPeriodFieldName: 200,
backupPolictEmailNotificationFieldName: "None",
backupPolicyLockedFieldName: true,
backupPolicyAdditionalRegionIDs: []interface{}{"aks-westeurope"},
}
expected := &backup.BackupPolicy{
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
AdditionalRegionIds: []string{"aks-westeurope"},
}
t.Run("test hourly schedule", func(tt *testing.T) {
rawSchedule := []interface{}{
Expand Down
2 changes: 2 additions & 0 deletions internal/resource_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func TestFlattenBackup(t *testing.T) {
DeploymentId: "123456",
BackupPolicyId: "456123",
Url: "test-url",
RegionId: "gcp-europe-west-4",
}

expected := map[string]interface{}{
Expand All @@ -228,6 +229,7 @@ func TestFlattenBackup(t *testing.T) {
backupDeploymentIDFieldName: "123456",
backupPolicyIDFieldName: "456123",
backupURLFieldName: "test-url",
backupRegionIDFieldName: "gcp-europe-west-4",
}

flattened := flattenBackupResource(backup)
Expand Down
2 changes: 0 additions & 2 deletions internal/resource_iam_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func TestAccResourceIAMGroup(t *testing.T) {
orgID, err := FetchOrganizationID()
require.NoError(t, err)

fmt.Println(orgID)

name := acctest.RandString(5)

resource.Test(t, resource.TestCase{
Expand Down
94 changes: 94 additions & 0 deletions internal/resource_multi_region_backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// DISCLAIMER
//
// Copyright 2022 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

backup "github.com/arangodb-managed/apis/backup/v1"
)

const (
backupSourceBackupIDFieldName = "source_backup_id"
backupRegionIDFieldName = "region_id"
)

// resourceBackup defines a Multi Region Backup Oasis resource.
func resourceMultiRegionBackup() *schema.Resource {
return &schema.Resource{
Description: "Oasis Multi Region Backup Resource",
CreateContext: resourceCopyBackup,
ReadContext: resourceBackupRead,
UpdateContext: resourceBackupUpdate,
DeleteContext: resourceBackupDelete,
Schema: map[string]*schema.Schema{
backupSourceBackupIDFieldName: {
Type: schema.TypeString,
Description: "Oasis backup resource identifier field",
Required: true,
},
backupRegionIDFieldName: {
Type: schema.TypeString,
Description: "Oasis cloud provider region identifier field",
Required: true,
},
},
}
}

// resourceCopyBackup will copy backup resource to given region.
func resourceCopyBackup(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Client)
if err := client.Connect(); err != nil {
client.log.Error().Err(err).Msg("Failed to connect to api")
return diag.FromErr(err)
}
backupc := backup.NewBackupServiceClient(client.conn)
sourceBackupID := ""
if v, ok := d.GetOk(backupSourceBackupIDFieldName); ok {
sourceBackupID = v.(string)
} else {
return diag.Errorf("Source backup identifier required")
}

regionID := ""
if v, ok := d.GetOk(backupRegionIDFieldName); ok {
regionID = v.(string)
} else {
return diag.Errorf("Region identifier required")
}

bu, err := backupc.CopyBackup(client.ctxWithToken, &backup.CopyBackupRequest{
SourceBackupId: sourceBackupID,
RegionId: regionID,
})
if err != nil {
client.log.Error().Err(err).Msg("Failed to copy backup")
d.SetId("")
return diag.FromErr(err)
} else {
d.SetId(bu.GetId())
}
return resourceBackupRead(ctx, d, m)
}
Loading