Skip to content

Commit 8945e8f

Browse files
committed
Support for creating an Oracle Autonomous Database by cloning from a backup of an existing Autonomous Database.
1 parent d8d1670 commit 8945e8f

14 files changed

+428
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## 3.58.1 (Unreleased)
2+
### Added
3+
- Support for creating `oci_database_autonomous_database` resource by cloning from a backup of an existing Autonomous Database.
4+
25
## 3.58.0 (January 15, 2020)
36

47
### Added

examples/database/adb/autonomous_database.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,39 @@ resource "oci_database_autonomous_database" "autonomous_database" {
2626
whitelisted_ips = ["1.1.1.1/28"]
2727
}
2828

29+
resource "oci_database_autonomous_database_backup" "autonomous_database_backup" {
30+
#Required
31+
autonomous_database_id = "${oci_database_autonomous_database.autonomous_database.id}"
32+
display_name = "${var.autonomous_database_backup_display_name}"
33+
}
34+
35+
resource "oci_database_autonomous_database" "autonomous_database_from_backup_id" {
36+
#Required
37+
admin_password = "${random_string.autonomous_database_admin_password.result}"
38+
compartment_id = "${var.compartment_ocid}"
39+
cpu_core_count = "1"
40+
data_storage_size_in_tbs = "1"
41+
db_name = "adbdb2"
42+
43+
clone_type = "FULL"
44+
source = "BACKUP_FROM_ID"
45+
autonomous_database_backup_id = "${oci_database_autonomous_database_backup.autonomous_database_backup.id}"
46+
}
47+
48+
resource "oci_database_autonomous_database" "autonomous_database_from_backup_timestamp" {
49+
#Required
50+
admin_password = "${random_string.autonomous_database_admin_password.result}"
51+
compartment_id = "${var.compartment_ocid}"
52+
cpu_core_count = "1"
53+
data_storage_size_in_tbs = "1"
54+
db_name = "adbdb3"
55+
56+
clone_type = "FULL"
57+
source = "BACKUP_FROM_TIMESTAMP"
58+
autonomous_database_id = "${oci_database_autonomous_database_backup.autonomous_database_backup.autonomous_database_id}"
59+
timestamp = "${oci_database_autonomous_database_backup.autonomous_database_backup.time_ended}"
60+
}
61+
2962
data "oci_database_autonomous_databases" "autonomous_databases" {
3063
#Required
3164
compartment_id = "${var.compartment_ocid}"

examples/database/adb/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ variable "private_key_path" {}
77
variable "region" {}
88
variable "compartment_ocid" {}
99

10+
variable "autonomous_database_backup_display_name" {
11+
default = "Monthly Backup"
12+
}
13+
1014
variable "autonomous_database_db_workload" {
1115
default = "OLTP"
1216
}

oci/database_autonomous_database_backup_data_source.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (s *DatabaseAutonomousDatabaseBackupDataSourceCrud) SetData() error {
8282
s.D.Set("is_automatic", *s.Res.IsAutomatic)
8383
}
8484

85+
if s.Res.IsRestorable != nil {
86+
s.D.Set("is_restorable", *s.Res.IsRestorable)
87+
}
88+
8589
if s.Res.LifecycleDetails != nil {
8690
s.D.Set("lifecycle_details", *s.Res.LifecycleDetails)
8791
}

oci/database_autonomous_database_backup_resource.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package oci
44

55
import (
66
"context"
7+
"time"
78

89
"github.com/hashicorp/terraform/helper/schema"
910

@@ -47,6 +48,10 @@ func DatabaseAutonomousDatabaseBackupResource() *schema.Resource {
4748
Type: schema.TypeBool,
4849
Computed: true,
4950
},
51+
"is_restorable": {
52+
Type: schema.TypeBool,
53+
Computed: true,
54+
},
5055
"lifecycle_details": {
5156
Type: schema.TypeString,
5257
Computed: true,
@@ -188,18 +193,22 @@ func (s *DatabaseAutonomousDatabaseBackupResourceCrud) SetData() error {
188193
s.D.Set("is_automatic", *s.Res.IsAutomatic)
189194
}
190195

196+
if s.Res.IsRestorable != nil {
197+
s.D.Set("is_restorable", *s.Res.IsRestorable)
198+
}
199+
191200
if s.Res.LifecycleDetails != nil {
192201
s.D.Set("lifecycle_details", *s.Res.LifecycleDetails)
193202
}
194203

195204
s.D.Set("state", s.Res.LifecycleState)
196205

197206
if s.Res.TimeEnded != nil {
198-
s.D.Set("time_ended", s.Res.TimeEnded.String())
207+
s.D.Set("time_ended", s.Res.TimeEnded.Format(time.RFC3339Nano))
199208
}
200209

201210
if s.Res.TimeStarted != nil {
202-
s.D.Set("time_started", s.Res.TimeStarted.String())
211+
s.D.Set("time_started", s.Res.TimeStarted.Format(time.RFC3339Nano))
203212
}
204213

205214
s.D.Set("type", s.Res.Type)

oci/database_autonomous_database_backup_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func TestDatabaseAutonomousDatabaseBackupResource_basic(t *testing.T) {
8484
resource.TestCheckResourceAttr(datasourceName, "autonomous_database_backups.0.display_name", "Monthly Backup"),
8585
resource.TestCheckResourceAttrSet(datasourceName, "autonomous_database_backups.0.id"),
8686
resource.TestCheckResourceAttrSet(datasourceName, "autonomous_database_backups.0.is_automatic"),
87+
resource.TestCheckResourceAttrSet(datasourceName, "autonomous_database_backups.0.is_restorable"),
8788
resource.TestCheckResourceAttrSet(datasourceName, "autonomous_database_backups.0.state"),
8889
resource.TestCheckResourceAttrSet(datasourceName, "autonomous_database_backups.0.time_ended"),
8990
resource.TestCheckResourceAttrSet(datasourceName, "autonomous_database_backups.0.time_started"),

oci/database_autonomous_database_backups_data_source.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ func (s *DatabaseAutonomousDatabaseBackupsDataSourceCrud) SetData() error {
137137
autonomousDatabaseBackup["is_automatic"] = *r.IsAutomatic
138138
}
139139

140+
if r.IsRestorable != nil {
141+
autonomousDatabaseBackup["is_restorable"] = *r.IsRestorable
142+
}
143+
140144
if r.LifecycleDetails != nil {
141145
autonomousDatabaseBackup["lifecycle_details"] = *r.LifecycleDetails
142146
}

oci/database_autonomous_database_resource.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"context"
77
"fmt"
88
"strings"
9+
"time"
910

1011
"github.com/hashicorp/terraform/helper/schema"
1112
"github.com/hashicorp/terraform/helper/validation"
1213

14+
oci_common "github.com/oracle/oci-go-sdk/common"
1315
oci_database "github.com/oracle/oci-go-sdk/database"
1416
oci_work_requests "github.com/oracle/oci-go-sdk/workrequests"
1517
)
@@ -60,6 +62,18 @@ func DatabaseAutonomousDatabaseResource() *schema.Resource {
6062
Computed: true,
6163
ForceNew: true,
6264
},
65+
"autonomous_database_backup_id": {
66+
Type: schema.TypeString,
67+
Optional: true,
68+
Computed: true,
69+
ForceNew: true,
70+
},
71+
"autonomous_database_id": {
72+
Type: schema.TypeString,
73+
Optional: true,
74+
Computed: true,
75+
ForceNew: true,
76+
},
6377
"clone_type": {
6478
Type: schema.TypeString,
6579
Optional: true,
@@ -134,6 +148,8 @@ func DatabaseAutonomousDatabaseResource() *schema.Resource {
134148
ForceNew: true,
135149
DiffSuppressFunc: EqualIgnoreCaseSuppressDiff,
136150
ValidateFunc: validation.StringInSlice([]string{
151+
"BACKUP_FROM_ID",
152+
"BACKUP_FROM_TIMESTAMP",
137153
"DATABASE",
138154
"NONE",
139155
}, true),
@@ -144,6 +160,13 @@ func DatabaseAutonomousDatabaseResource() *schema.Resource {
144160
Computed: true,
145161
ForceNew: true,
146162
},
163+
"timestamp": {
164+
Type: schema.TypeString,
165+
Optional: true,
166+
Computed: true,
167+
ForceNew: true,
168+
DiffSuppressFunc: timeDiffSuppressFunction,
169+
},
147170
"whitelisted_ips": {
148171
Type: schema.TypeList,
149172
Optional: true,
@@ -687,6 +710,177 @@ func (s *DatabaseAutonomousDatabaseResourceCrud) populateTopLevelPolymorphicCrea
687710
source = "NONE" // default value
688711
}
689712
switch strings.ToLower(source) {
713+
case strings.ToLower("BACKUP_FROM_ID"):
714+
details := oci_database.CreateAutonomousDatabaseFromBackupDetails{}
715+
if autonomousDatabaseBackupId, ok := s.D.GetOkExists("autonomous_database_backup_id"); ok {
716+
tmp := autonomousDatabaseBackupId.(string)
717+
details.AutonomousDatabaseBackupId = &tmp
718+
}
719+
if cloneType, ok := s.D.GetOkExists("clone_type"); ok {
720+
details.CloneType = oci_database.CreateAutonomousDatabaseFromBackupDetailsCloneTypeEnum(cloneType.(string))
721+
}
722+
if adminPassword, ok := s.D.GetOkExists("admin_password"); ok {
723+
tmp := adminPassword.(string)
724+
details.AdminPassword = &tmp
725+
}
726+
if autonomousContainerDatabaseId, ok := s.D.GetOkExists("autonomous_container_database_id"); ok {
727+
tmp := autonomousContainerDatabaseId.(string)
728+
details.AutonomousContainerDatabaseId = &tmp
729+
}
730+
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
731+
tmp := compartmentId.(string)
732+
details.CompartmentId = &tmp
733+
}
734+
if cpuCoreCount, ok := s.D.GetOkExists("cpu_core_count"); ok {
735+
tmp := cpuCoreCount.(int)
736+
details.CpuCoreCount = &tmp
737+
}
738+
if dataStorageSizeInTBs, ok := s.D.GetOkExists("data_storage_size_in_tbs"); ok {
739+
tmp := dataStorageSizeInTBs.(int)
740+
details.DataStorageSizeInTBs = &tmp
741+
}
742+
if dbName, ok := s.D.GetOkExists("db_name"); ok {
743+
tmp := dbName.(string)
744+
details.DbName = &tmp
745+
}
746+
if dbWorkload, ok := s.D.GetOkExists("db_workload"); ok {
747+
details.DbWorkload = oci_database.CreateAutonomousDatabaseBaseDbWorkloadEnum(dbWorkload.(string))
748+
}
749+
if definedTags, ok := s.D.GetOkExists("defined_tags"); ok {
750+
convertedDefinedTags, err := mapToDefinedTags(definedTags.(map[string]interface{}))
751+
if err != nil {
752+
return err
753+
}
754+
details.DefinedTags = convertedDefinedTags
755+
}
756+
if displayName, ok := s.D.GetOkExists("display_name"); ok {
757+
tmp := displayName.(string)
758+
details.DisplayName = &tmp
759+
}
760+
if freeformTags, ok := s.D.GetOkExists("freeform_tags"); ok {
761+
details.FreeformTags = objectMapToStringMap(freeformTags.(map[string]interface{}))
762+
}
763+
if isAutoScalingEnabled, ok := s.D.GetOkExists("is_auto_scaling_enabled"); ok {
764+
tmp := isAutoScalingEnabled.(bool)
765+
details.IsAutoScalingEnabled = &tmp
766+
}
767+
if isDedicated, ok := s.D.GetOkExists("is_dedicated"); ok {
768+
tmp := isDedicated.(bool)
769+
details.IsDedicated = &tmp
770+
}
771+
if isFreeTier, ok := s.D.GetOkExists("is_free_tier"); ok {
772+
tmp := isFreeTier.(bool)
773+
details.IsFreeTier = &tmp
774+
}
775+
if isPreviewVersionWithServiceTermsAccepted, ok := s.D.GetOkExists("is_preview_version_with_service_terms_accepted"); ok {
776+
tmp := isPreviewVersionWithServiceTermsAccepted.(bool)
777+
details.IsPreviewVersionWithServiceTermsAccepted = &tmp
778+
}
779+
if licenseModel, ok := s.D.GetOkExists("license_model"); ok {
780+
details.LicenseModel = oci_database.CreateAutonomousDatabaseBaseLicenseModelEnum(licenseModel.(string))
781+
}
782+
if whitelistedIps, ok := s.D.GetOkExists("whitelisted_ips"); ok {
783+
interfaces := whitelistedIps.([]interface{})
784+
tmp := make([]string, len(interfaces))
785+
for i := range interfaces {
786+
if interfaces[i] != nil {
787+
tmp[i] = interfaces[i].(string)
788+
}
789+
}
790+
if len(tmp) != 0 || s.D.HasChange("whitelisted_ips") {
791+
details.WhitelistedIps = tmp
792+
}
793+
}
794+
request.CreateAutonomousDatabaseDetails = details
795+
case strings.ToLower("BACKUP_FROM_TIMESTAMP"):
796+
details := oci_database.CreateAutonomousDatabaseFromBackupTimestampDetails{}
797+
if autonomousDatabaseId, ok := s.D.GetOkExists("autonomous_database_id"); ok {
798+
tmp := autonomousDatabaseId.(string)
799+
details.AutonomousDatabaseId = &tmp
800+
}
801+
if cloneType, ok := s.D.GetOkExists("clone_type"); ok {
802+
details.CloneType = oci_database.CreateAutonomousDatabaseFromBackupTimestampDetailsCloneTypeEnum(cloneType.(string))
803+
}
804+
if timestamp, ok := s.D.GetOkExists("timestamp"); ok {
805+
tmp, err := time.Parse(time.RFC3339, timestamp.(string))
806+
if err != nil {
807+
return err
808+
}
809+
details.Timestamp = &oci_common.SDKTime{Time: tmp}
810+
}
811+
if adminPassword, ok := s.D.GetOkExists("admin_password"); ok {
812+
tmp := adminPassword.(string)
813+
details.AdminPassword = &tmp
814+
}
815+
if autonomousContainerDatabaseId, ok := s.D.GetOkExists("autonomous_container_database_id"); ok {
816+
tmp := autonomousContainerDatabaseId.(string)
817+
details.AutonomousContainerDatabaseId = &tmp
818+
}
819+
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
820+
tmp := compartmentId.(string)
821+
details.CompartmentId = &tmp
822+
}
823+
if cpuCoreCount, ok := s.D.GetOkExists("cpu_core_count"); ok {
824+
tmp := cpuCoreCount.(int)
825+
details.CpuCoreCount = &tmp
826+
}
827+
if dataStorageSizeInTBs, ok := s.D.GetOkExists("data_storage_size_in_tbs"); ok {
828+
tmp := dataStorageSizeInTBs.(int)
829+
details.DataStorageSizeInTBs = &tmp
830+
}
831+
if dbName, ok := s.D.GetOkExists("db_name"); ok {
832+
tmp := dbName.(string)
833+
details.DbName = &tmp
834+
}
835+
if dbWorkload, ok := s.D.GetOkExists("db_workload"); ok {
836+
details.DbWorkload = oci_database.CreateAutonomousDatabaseBaseDbWorkloadEnum(dbWorkload.(string))
837+
}
838+
if definedTags, ok := s.D.GetOkExists("defined_tags"); ok {
839+
convertedDefinedTags, err := mapToDefinedTags(definedTags.(map[string]interface{}))
840+
if err != nil {
841+
return err
842+
}
843+
details.DefinedTags = convertedDefinedTags
844+
}
845+
if displayName, ok := s.D.GetOkExists("display_name"); ok {
846+
tmp := displayName.(string)
847+
details.DisplayName = &tmp
848+
}
849+
if freeformTags, ok := s.D.GetOkExists("freeform_tags"); ok {
850+
details.FreeformTags = objectMapToStringMap(freeformTags.(map[string]interface{}))
851+
}
852+
if isAutoScalingEnabled, ok := s.D.GetOkExists("is_auto_scaling_enabled"); ok {
853+
tmp := isAutoScalingEnabled.(bool)
854+
details.IsAutoScalingEnabled = &tmp
855+
}
856+
if isDedicated, ok := s.D.GetOkExists("is_dedicated"); ok {
857+
tmp := isDedicated.(bool)
858+
details.IsDedicated = &tmp
859+
}
860+
if isFreeTier, ok := s.D.GetOkExists("is_free_tier"); ok {
861+
tmp := isFreeTier.(bool)
862+
details.IsFreeTier = &tmp
863+
}
864+
if isPreviewVersionWithServiceTermsAccepted, ok := s.D.GetOkExists("is_preview_version_with_service_terms_accepted"); ok {
865+
tmp := isPreviewVersionWithServiceTermsAccepted.(bool)
866+
details.IsPreviewVersionWithServiceTermsAccepted = &tmp
867+
}
868+
if licenseModel, ok := s.D.GetOkExists("license_model"); ok {
869+
details.LicenseModel = oci_database.CreateAutonomousDatabaseBaseLicenseModelEnum(licenseModel.(string))
870+
}
871+
if whitelistedIps, ok := s.D.GetOkExists("whitelisted_ips"); ok {
872+
interfaces := whitelistedIps.([]interface{})
873+
tmp := make([]string, len(interfaces))
874+
for i := range interfaces {
875+
if interfaces[i] != nil {
876+
tmp[i] = interfaces[i].(string)
877+
}
878+
}
879+
if len(tmp) != 0 || s.D.HasChange("whitelisted_ips") {
880+
details.WhitelistedIps = tmp
881+
}
882+
}
883+
request.CreateAutonomousDatabaseDetails = details
690884
case strings.ToLower("DATABASE"):
691885
details := oci_database.CreateAutonomousDatabaseCloneDetails{}
692886
if cloneType, ok := s.D.GetOkExists("clone_type"); ok {

0 commit comments

Comments
 (0)