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 5 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
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(),
}
}
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
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)
}
176 changes: 176 additions & 0 deletions internal/resource_multi_region_backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//
// 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"
"fmt"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/require"

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

// TestAccResourceMultiRegionBackup verifies the Oasis Multi Region Backup resource is created along with the specified properties
func TestAccResourceMultiRegionBackup(t *testing.T) {
lakhansamani marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := os.LookupEnv("TF_ACC"); !ok {
t.Skip()
}
t.Parallel()

res := "terraform-multi-region-backup-" + acctest.RandString(10)
name := "multi-region-backup-" + acctest.RandString(10)
sourceBackupID := acctest.RandString((10))
regionID := "gcp-europe-west-4"

orgID, err := FetchOrganizationID()
require.NoError(t, err)
pid, err := FetchProjectID(context.Background(), orgID, testAccProvider)
require.NoError(t, err)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testProviderFactories,
CheckDestroy: testAccCheckDestroyBackup,
lakhansamani marked this conversation as resolved.
Show resolved Hide resolved
Steps: []resource.TestStep{
{
Config: testMultiRegionBackupConfigIncomplete(pid, res, name, sourceBackupID),
ExpectError: regexp.MustCompile("region ID missing"),
},
{
Config: testMultiRegionBackupConfig("", res, name, sourceBackupID, regionID),
ExpectError: regexp.MustCompile("Project ID missing"),
},
{
Config: testMultiRegionBackupConfig(pid, res, name, sourceBackupID, regionID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("oasis_deployment.my_oneshard_deployment", deplNameFieldName, "oasis_test_dep_tf"),
resource.TestCheckResourceAttr("oasis_deployment.my_oneshard_deployment", deplDiskPerformanceFieldName, "dp30"),
resource.TestCheckResourceAttr("oasis_deployment.my_oneshard_deployment", deplTAndCAcceptedFieldName, "true"),

resource.TestCheckResourceAttr("oasis_multi_region_backup."+res, backupSourceBackupIDFieldName, sourceBackupID),
resource.TestCheckResourceAttr("oasis_multi_region_backup."+res, backupRegionIDFieldName, regionID),
),
},
},
})
}

// testMultiRegionBackupConfig contains the Terraform resource definitions for testing usage
func testMultiRegionBackupConfig(project, res, name, sourceBackupID, regionID string) string {
return fmt.Sprintf(`resource "oasis_deployment" "my_oneshard_deployment" {
terms_and_conditions_accepted = "true"
project = "%s"
name = "%s"
location {
region = "gcp-europe-west4"
}
version {
db_version = "3.8.6"
}
security {
disable_foxx_authentication = false
}
disk_performance = "dp30"
configuration {
model = "oneshard"
node_size_id = "c4-a8"
node_disk_size = 20
maximum_node_disk_size = 40
}
notification_settings {
email_addresses = [
"[email protected]"
]
}
}

resource "oasis_multi_region_backup" "%s" {
source_backup_id = "%s"
region_id = "%s"
}
`, project, res, name, sourceBackupID, regionID)
}

// testMultiRegionBackupConfigIncomplete contains the incomplete Terraform resource definitions for regression testing (expected failure)
func testMultiRegionBackupConfigIncomplete(project, res, name, sourceBackupID string) string {
return fmt.Sprintf(`resource "oasis_deployment" "my_oneshard_deployment" {
terms_and_conditions_accepted = "true"
project = "%s"
name = "%s"
location {
region = "gcp-europe-west4"
}
version {
db_version = "3.8.6"
}
security {
disable_foxx_authentication = false
}
disk_performance = "dp30"
configuration {
model = "oneshard"
node_size_id = "c4-a8"
node_disk_size = 20
maximum_node_disk_size = 40
}
notification_settings {
email_addresses = [
"[email protected]"
]
}
}

resource "oasis_multi_region_backup" "%s" {
source_backup_id = "%s"
region_id = ""
}
`, project, res, name, sourceBackupID)
}

// testAccMultiRegionCheckDestroyBackup verifies the Terraform oasis_multi_region_backup resource cleanup.
func testAccMultiRegionCheckDestroyBackup(s *terraform.State) error {
client := testAccProvider.Meta().(*Client)
if err := client.Connect(); err != nil {
client.log.Error().Err(err).Msg("Failed to connect to api")
return err
}
backupc := backup.NewBackupServiceClient(client.conn)

for _, rs := range s.RootModule().Resources {
if rs.Type != "oasis_multi_region_backup" {
continue
}

if _, err := backupc.GetBackup(client.ctxWithToken, &common.IDOptions{Id: rs.Primary.ID}); !common.IsNotFound(err) {
return fmt.Errorf("backup still present")
}
}

return nil
}