Skip to content

New fields for snapshot restore jobs #224

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

Merged
merged 12 commits into from
May 20, 2020
Merged
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/kr/pretty v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/mongodb/go-client-mongodb-atlas v0.2.0
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200515182955-c06f91978bf0
github.com/mwielbut/pointy v1.1.0
github.com/satori/uuid v1.2.0 // indirect
github.com/spf13/afero v1.2.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/I
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mongodb/go-client-mongodb-atlas v0.2.0 h1:UH02byrzAlboirAMgV0sMIJqXbEfmGXIBa5IniwUZNI=
github.com/mongodb/go-client-mongodb-atlas v0.2.0/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200515182955-c06f91978bf0 h1:c/VmNVstgZU/9qgqwCoQSRpHkwSgnD4DiTSrccOwCk0=
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200515182955-c06f91978bf0/go.mod h1:182DADO+2ZDWQyW25uIxzop3U0rrQdeNEzqvs6w9kTI=
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
Expand All @@ -401,6 +403,8 @@ github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.8.1 h1:C5Dqfs/LeauYDX0jJXIe2SWmwCbGzx9yF8C8xy3Lh34=
github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
github.com/openlyinc/pointy v1.1.2 h1:LywVV2BWC5Sp5v7FoP4bUD+2Yn5k0VNeRbU5vq9jUMY=
github.com/openlyinc/pointy v1.1.2/go.mod h1:w2Sytx+0FVuMKn37xpXIAyBNhFNBIJGR/v2m7ik1WtM=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/packer-community/winrmcp v0.0.0-20180102160824-81144009af58/go.mod h1:f6Izs6JvFTdnRbziASagjZ2vmf55NSIkC/weStxCHqk=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ func dataSourceMongoDBAtlasCloudProviderSnapshotRestoreJob() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"oplog_ts": {
Type: schema.TypeString,
Computed: true,
},
"point_in_time_utc_seconds": {
Type: schema.TypeInt,
Computed: true,
},
"oplog_inc": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -126,6 +138,15 @@ func dataSourceMongoDBAtlasCloudProviderSnapshotRestoreJobRead(d *schema.Resourc
if err = d.Set("timestamp", snapshotRes.Timestamp); err != nil {
return fmt.Errorf("error setting `timestamp` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err)
}
if err = d.Set("oplog_ts", snapshotRes.OplogTs); err != nil {
return fmt.Errorf("error setting `oplog_ts` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err)
}
if err = d.Set("point_in_time_utc_seconds", snapshotRes.PointInTimeUTCSeconds); err != nil {
return fmt.Errorf("error setting `point_in_time_utc_seconds` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err)
}
if err = d.Set("oplog_inc", snapshotRes.OplogInc); err != nil {
return fmt.Errorf("error setting `oplog_inc` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err)
}

d.SetId(snapshotRes.ID)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func dataSourceMongoDBAtlasCloudProviderSnapshotRestoreJobs() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"oplog_ts": {
Type: schema.TypeInt,
Computed: true,
},
"point_in_time_utc_seconds": {
Type: schema.TypeInt,
Computed: true,
},
"oplog_inc": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -120,18 +132,21 @@ func flattenCloudProviderSnapshotRestoreJobs(cloudProviderSnapshotRestoreJobs []

for k, cloudProviderSnapshotRestoreJob := range cloudProviderSnapshotRestoreJobs {
results[k] = map[string]interface{}{
"id": cloudProviderSnapshotRestoreJob.ID,
"cancelled": cloudProviderSnapshotRestoreJob.Cancelled,
"created_at": cloudProviderSnapshotRestoreJob.CreatedAt,
"delivery_type": cloudProviderSnapshotRestoreJob.DeliveryType,
"delivery_url": cloudProviderSnapshotRestoreJob.DeliveryURL,
"expired": cloudProviderSnapshotRestoreJob.Expired,
"expires_at": cloudProviderSnapshotRestoreJob.ExpiresAt,
"finished_at": cloudProviderSnapshotRestoreJob.FinishedAt,
"snapshot_id": cloudProviderSnapshotRestoreJob.SnapshotID,
"target_project_id": cloudProviderSnapshotRestoreJob.TargetGroupID,
"target_cluster_name": cloudProviderSnapshotRestoreJob.TargetClusterName,
"timestamp": cloudProviderSnapshotRestoreJob.Timestamp,
"id": cloudProviderSnapshotRestoreJob.ID,
"cancelled": cloudProviderSnapshotRestoreJob.Cancelled,
"created_at": cloudProviderSnapshotRestoreJob.CreatedAt,
"delivery_type": cloudProviderSnapshotRestoreJob.DeliveryType,
"delivery_url": cloudProviderSnapshotRestoreJob.DeliveryURL,
"expired": cloudProviderSnapshotRestoreJob.Expired,
"expires_at": cloudProviderSnapshotRestoreJob.ExpiresAt,
"finished_at": cloudProviderSnapshotRestoreJob.FinishedAt,
"snapshot_id": cloudProviderSnapshotRestoreJob.SnapshotID,
"target_project_id": cloudProviderSnapshotRestoreJob.TargetGroupID,
"target_cluster_name": cloudProviderSnapshotRestoreJob.TargetClusterName,
"timestamp": cloudProviderSnapshotRestoreJob.Timestamp,
"oplog_ts": cloudProviderSnapshotRestoreJob.OplogTs,
"point_in_time_utc_seconds": cloudProviderSnapshotRestoreJob.PointInTimeUTCSeconds,
"oplog_inc": cloudProviderSnapshotRestoreJob.OplogInc,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func dataSourceMongoDBAtlasProjects() *schema.Resource {
func dataSourceMongoDBAtlasProjectsRead(d *schema.ResourceData, meta interface{}) error {
//Get client connection.
conn := meta.(*matlas.Client)

projects, _, err := conn.Projects.GetAllProjects(context.Background())
options := &matlas.ListOptions{}
projects, _, err := conn.Projects.GetAllProjects(context.Background(), options)
if err != nil {
return fmt.Errorf("error getting projects information: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/spf13/cast"
"log"
"strings"

Expand Down Expand Up @@ -50,6 +51,10 @@ func resourceMongoDBAtlasCloudProviderSnapshotRestoreJob() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"point_in_time": {
Type: schema.TypeBool,
Optional: true,
},
"target_cluster_name": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -58,16 +63,29 @@ func resourceMongoDBAtlasCloudProviderSnapshotRestoreJob() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"oplog_ts": {
Type: schema.TypeInt,
Optional: true,
},
"point_in_time_utc_seconds": {
Type: schema.TypeInt,
Optional: true,
},
"oplog_inc": {
Type: schema.TypeInt,
Optional: true,
},
},
},
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(map[string]interface{})

_, automated := v["automated"]
_, download := v["download"]
_, pointInTime := v["point_in_time"]

if (v["automated"] == "true" && v["download"] == "true") || (v["automated"] == "false" && v["download"] == "false") || (!automated && !download) {
errs = append(errs, fmt.Errorf("%q you need to implement only one: automated and download delivery types", key))
if (v["automated"] == "true" && v["download"] == "true" && v["point_in_time"] == "true") || (v["automated"] == "false" && v["download"] == "false" && v["point_in_time"] == "false") || (!automated && !download && !pointInTime) {
errs = append(errs, fmt.Errorf("%q you can only submit one type of restore job: automated, download or point_in_time", key))
}
if v["automated"] == "true" && (v["download"] == "false" || v["download"] == "" || !download) {
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
Expand All @@ -77,14 +95,35 @@ func resourceMongoDBAtlasCloudProviderSnapshotRestoreJob() *schema.Resource {
errs = append(errs, fmt.Errorf("%q target_project_id must be set", key))
}
}
if v["download"] == "true" && (v["automated"] == "false" || v["automated"] == "" || !automated) {
if v["download"] == "true" && (v["automated"] == "false" || v["automated"] == "" || !automated) && (v["point_in_time"] == "false" || v["point_in_time"] == "" || !pointInTime) {
if targetClusterName, ok := v["target_cluster_name"]; ok || targetClusterName == "" {
errs = append(errs, fmt.Errorf("%q it's not necessary implement target_cluster_name when you are using download delivery type", key))
}
if targetGroupID, ok := v["target_project_id"]; ok || targetGroupID == "" {
errs = append(errs, fmt.Errorf("%q it's not necessary implement target_project_id when you are using download delivery type", key))
}
}
if v["point_in_time"] == "true" && (v["download"] == "false" || v["download"] == "" || !download) && (v["automated"] == "false" || v["automated"] == "" || !automated) {
_, oplogTs := v["oplog_ts"]
_, pointTimeUTC := v["point_in_time_utc_seconds"]
_, oplogInc := v["oplog_inc"]
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
errs = append(errs, fmt.Errorf("%q target_cluster_name must be set", key))
}
if targetGroupID, ok := v["target_project_id"]; !ok || targetGroupID == "" {
errs = append(errs, fmt.Errorf("%q target_project_id must be set", key))
}
if !pointTimeUTC && !oplogTs && !oplogInc {
errs = append(errs, fmt.Errorf("%q point_in_time_utc_seconds or oplog_ts and oplog_inc must be set", key))
}
if (oplogTs && !oplogInc) || (!oplogTs && oplogInc) {
errs = append(errs, fmt.Errorf("%q if oplog_ts or oplog_inc is provided, oplog_inc and oplog_ts must be set", key))

}
if pointTimeUTC && (oplogTs || oplogInc) {
errs = append(errs, fmt.Errorf("%q you can't use both point_in_time_utc_seconds and oplog_ts or oplog_inc", key))
}
}
return
},
},
Expand Down Expand Up @@ -140,14 +179,18 @@ func resourceMongoDBAtlasCloudProviderSnapshotRestoreJobCreate(d *schema.Resourc
if aut, _ := d.Get("delivery_type.automated").(string); aut != "true" {
deliveryType = "download"
}

if aut, _ := d.Get("delivery_type.point_in_time").(string); aut == "true" {
deliveryType = "pointInTime"
}
snapshotReq := &matlas.CloudProviderSnapshotRestoreJob{
SnapshotID: d.Get("snapshot_id").(string),
DeliveryType: deliveryType,
TargetClusterName: d.Get("delivery_type.target_cluster_name").(string),
TargetGroupID: d.Get("delivery_type.target_project_id").(string),
SnapshotID: d.Get("snapshot_id").(string),
DeliveryType: deliveryType,
TargetClusterName: d.Get("delivery_type.target_cluster_name").(string),
TargetGroupID: d.Get("delivery_type.target_project_id").(string),
OplogTs: cast.ToInt64(d.Get("delivery_type.oplog_ts")),
OplogInc: cast.ToInt64(d.Get("delivery_type.oplog_inc")),
PointInTimeUTCSeconds: cast.ToInt64(d.Get("delivery_type.point_in_time_utc_seconds")),
}

cloudProviderSnapshotRestoreJob, _, err := conn.CloudProviderSnapshotRestoreJobs.Create(context.Background(), requestParameters, snapshotReq)
if err != nil {
return fmt.Errorf("error restore a snapshot: %s", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package mongodbatlas
import (
"context"
"fmt"
"log"
"os"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
matlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
"log"
"os"
"testing"
)

func TestAccResourceMongoDBAtlasCloudProviderSnapshotRestoreJob_basic(t *testing.T) {
Expand Down Expand Up @@ -81,6 +80,28 @@ func TestAccResourceMongoDBAtlasCloudProviderSnapshotRestoreJob_importBasic(t *t
})
}

func TestAccResourceMongoDBAtlasCloudProviderSnapshotRestoreJobWithPointTime_basic(t *testing.T) {
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
clusterName := acctest.RandomWithPrefix("test-acc")
description := fmt.Sprintf("My description in %s", clusterName)
retentionInDays := "1"
targetClusterName := clusterName
targetGroupID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
timeUtc := int64(1589318358)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasCloudProviderSnapshotRestoreJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasCloudProviderSnapshotRestoreJobConfigPointInTime(projectID, clusterName, description, retentionInDays, targetClusterName, targetGroupID, timeUtc),
Check: resource.ComposeTestCheckFunc(),
},
},
})
}

func testAccCheckMongoDBAtlasCloudProviderSnapshotRestoreJobExists(resourceName string, cloudProviderSnapshotRestoreJob *matlas.CloudProviderSnapshotRestoreJob) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*matlas.Client)
Expand Down Expand Up @@ -222,3 +243,43 @@ func testAccMongoDBAtlasCloudProviderSnapshotRestoreJobConfigDownload(projectID,
}
`, projectID, clusterName, description, retentionInDays)
}

func testAccMongoDBAtlasCloudProviderSnapshotRestoreJobConfigPointInTime(projectID, clusterName, description, retentionInDays, targetClusterName, targetGroupID string, pointTimeUTC int64) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "my_cluster" {
project_id = "%s"
name = "%s"
disk_size_gb = 5

//Provider Settings "block"
provider_name = "AWS"
provider_region_name = "EU_WEST_2"
provider_instance_size_name = "M10"
provider_backup_enabled = true // enable cloud provider snapshots
provider_disk_iops = 100
provider_encrypt_ebs_volume = false
pit_enabled = true
}

resource "mongodbatlas_cloud_provider_snapshot" "test" {
project_id = mongodbatlas_cluster.my_cluster.project_id
cluster_name = mongodbatlas_cluster.my_cluster.name
description = "%s"
retention_in_days = %s
}

resource "mongodbatlas_cloud_provider_snapshot_restore_job" "test" {
project_id = mongodbatlas_cloud_provider_snapshot.test.project_id
cluster_name = mongodbatlas_cloud_provider_snapshot.test.cluster_name
snapshot_id = mongodbatlas_cloud_provider_snapshot.test.snapshot_id
delivery_type = {
point_in_time = true
target_cluster_name = "%s"
target_project_id = "%s"
oplog_ts = %v
oplog_inc = 1
}
depends_on = ["mongodbatlas_cloud_provider_snapshot.test"]
}
`, projectID, clusterName, description, retentionInDays, targetClusterName, targetGroupID, pointTimeUTC)
}
12 changes: 6 additions & 6 deletions mongodbatlas/resource_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ func resourceMongoDBAtlasClusterCreate(d *schema.ResourceData, meta interface{})
projectID := d.Get("project_id").(string)
providerName := d.Get("provider_name").(string)

autoScaling := matlas.AutoScaling{
autoScaling := &matlas.AutoScaling{
DiskGBEnabled: pointy.Bool(true),
}

if diskGBEnabled, ok := d.GetOkExists("auto_scaling_disk_gb_enabled"); ok {
autoScaling = matlas.AutoScaling{
autoScaling = &matlas.AutoScaling{
DiskGBEnabled: pointy.Bool(diskGBEnabled.(bool)),
}
}
Expand Down Expand Up @@ -416,7 +416,7 @@ func resourceMongoDBAtlasClusterCreate(d *schema.ResourceData, meta interface{})
if diskGBEnabled := d.Get("auto_scaling_disk_gb_enabled"); diskGBEnabled.(bool) {
return fmt.Errorf("`auto_scaling_disk_gb_enabled` cannot be true when provider name is TENANT")
}
autoScaling = matlas.AutoScaling{
autoScaling = &matlas.AutoScaling{
DiskGBEnabled: pointy.Bool(false),
}
}
Expand Down Expand Up @@ -817,7 +817,7 @@ func resourceMongoDBAtlasClusterImportState(d *schema.ResourceData, meta interfa
return []*schema.ResourceData{d}, nil
}

func expandBiConnector(d *schema.ResourceData) (matlas.BiConnector, error) {
func expandBiConnector(d *schema.ResourceData) (*matlas.BiConnector, error) {
var biConnector matlas.BiConnector

if v, ok := d.GetOk("bi_connector"); ok {
Expand All @@ -830,10 +830,10 @@ func expandBiConnector(d *schema.ResourceData) (matlas.BiConnector, error) {
ReadPreference: cast.ToString(biConnMap["read_preference"]),
}
}
return biConnector, nil
return &biConnector, nil
}

func flattenBiConnector(biConnector matlas.BiConnector) map[string]interface{} {
func flattenBiConnector(biConnector *matlas.BiConnector) map[string]interface{} {
biConnectorMap := make(map[string]interface{})

if biConnector.Enabled != nil {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading