|
| 1 | +// Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package integrationtest |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 13 | + "github.com/oracle/oci-go-sdk/v65/common" |
| 14 | + oci_psql "github.com/oracle/oci-go-sdk/v65/psql" |
| 15 | + |
| 16 | + "github.com/oracle/terraform-provider-oci/httpreplay" |
| 17 | + "github.com/oracle/terraform-provider-oci/internal/acctest" |
| 18 | + tf_client "github.com/oracle/terraform-provider-oci/internal/client" |
| 19 | + "github.com/oracle/terraform-provider-oci/internal/tfresource" |
| 20 | + "github.com/oracle/terraform-provider-oci/internal/utils" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + PsqlBackupCopyRequiredOnlyResource = PsqlBackupResourceDependencies + acctest.GenerateResourceFromRepresentationMap("oci_psql_backup", "test_psql_backup_copy", acctest.Required, acctest.Create, PsqlBackupCopyRepresentation) |
| 25 | + |
| 26 | + PsqlBackupCopyRepresentation = map[string]interface{}{ |
| 27 | + "source_backup_details": acctest.RepresentationGroup{RepType: acctest.Required, Group: PsqlBackupCopySourceDetailsRepresentation}, |
| 28 | + "compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`}, |
| 29 | + "retention_period": acctest.Representation{RepType: acctest.Optional, Create: `10`, Update: `11`}, |
| 30 | + "lifecycle": acctest.RepresentationGroup{RepType: acctest.Required, Group: ignorePsqlBackupSystemTagsChangesRepresentation}, |
| 31 | + } |
| 32 | + |
| 33 | + PsqlBackupCopySourceDetailsRepresentation = map[string]interface{}{} |
| 34 | + |
| 35 | + ignorePsqlBackupSystemTagsChangesRepresentation = map[string]interface{}{ |
| 36 | + "ignore_changes": acctest.Representation{RepType: acctest.Required, Create: []string{`system_tags`}}, |
| 37 | + } |
| 38 | + |
| 39 | + PsqlBackupCopyResourceConfig = PsqlBackupResourceDependencies + |
| 40 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_backup", "test_psql_backup_copy", acctest.Optional, acctest.Update, PsqlBackupCopyRepresentation) |
| 41 | + |
| 42 | + PsqlBackupCopySingularDataSourceRepresentation = map[string]interface{}{ |
| 43 | + "backup_id": acctest.Representation{RepType: acctest.Required, Create: `${oci_psql_backup.test_psql_backup_copy.id}`}, |
| 44 | + } |
| 45 | +) |
| 46 | + |
| 47 | +// issue-routing-tag: psql/default |
| 48 | +func TestPsqlBackupCopyResource_basic(t *testing.T) { |
| 49 | + httpreplay.SetScenario("TestPsqlBackupCopyResource_basic") |
| 50 | + defer httpreplay.SaveScenario() |
| 51 | + |
| 52 | + config := acctest.ProviderTestConfig() |
| 53 | + |
| 54 | + compartmentId := utils.GetEnvSettingWithBlankDefault("compartment_id") |
| 55 | + compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId) |
| 56 | + |
| 57 | + sourceBackupId := utils.GetEnvSettingWithBlankDefault("source_backup_id") |
| 58 | + sourceBackupIdVariableStr := fmt.Sprintf("variable \"source_backup_id\" { default = \"%s\" }\n", sourceBackupId) |
| 59 | + |
| 60 | + sourceBackupRegion := utils.GetEnvSettingWithBlankDefault("source_region") |
| 61 | + sourceBackupRegionVariableStr := fmt.Sprintf("variable \"source_region\" { default = \"%s\" }\n", sourceBackupRegion) |
| 62 | + |
| 63 | + //compartmentIdU := utils.GetEnvSettingWithDefault("compartment_id_for_update", compartmentId) |
| 64 | + //compartmentIdUVariableStr := fmt.Sprintf("variable \"compartment_id_for_update\" { default = \"%s\" }\n", compartmentIdU) |
| 65 | + |
| 66 | + resourceName := "oci_psql_backup.test_psql_backup_copy" |
| 67 | + //singularDatasourceName := "data.oci_psql_backup.test_psql_backup_copy" |
| 68 | + |
| 69 | + PsqlBackupCopySourceDetailsRepresentation = map[string]interface{}{ |
| 70 | + "source_region": acctest.Representation{RepType: acctest.Required, Create: sourceBackupRegion}, |
| 71 | + "source_backup_id": acctest.Representation{RepType: acctest.Required, Create: sourceBackupId}, |
| 72 | + } |
| 73 | + |
| 74 | + PsqlBackupCopyRepresentation = acctest.GetUpdatedRepresentationCopy("source_backup_details", acctest.RepresentationGroup{RepType: acctest.Required, Group: PsqlBackupCopySourceDetailsRepresentation}, PsqlBackupCopyRepresentation) |
| 75 | + |
| 76 | + // Save TF content to Create resource with optional properties. This has to be exactly the same as the config part in the "create with optionals" step in the test. |
| 77 | + acctest.SaveConfigContent(config+compartmentIdVariableStr+ |
| 78 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_backup", "test_psql_backup_copy", acctest.Required, acctest.Create, PsqlBackupCopyRepresentation), "psql", "backup", t) |
| 79 | + |
| 80 | + acctest.ResourceTest(t, testAccCheckPsqlBackupCopyDestroy, []resource.TestStep{ |
| 81 | + // verify Create |
| 82 | + |
| 83 | + { |
| 84 | + Config: config + compartmentIdVariableStr + sourceBackupIdVariableStr + sourceBackupRegionVariableStr + |
| 85 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_backup", "test_psql_backup_copy", acctest.Required, acctest.Create, PsqlBackupCopyRepresentation), |
| 86 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 87 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 88 | + resource.TestCheckResourceAttrSet(resourceName, "db_system_id"), |
| 89 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.#", "1"), |
| 90 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.0.source_backup_id", sourceBackupId), |
| 91 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.0.source_region", sourceBackupRegion), |
| 92 | + resource.TestCheckResourceAttrSet(resourceName, "retention_period"), |
| 93 | + resource.TestCheckResourceAttrSet(resourceName, "source_type"), |
| 94 | + resource.TestCheckResourceAttrSet(resourceName, "state"), |
| 95 | + func(s *terraform.State) (err error) { |
| 96 | + _, err = acctest.FromInstanceState(s, resourceName, "id") |
| 97 | + return err |
| 98 | + }, |
| 99 | + ), |
| 100 | + }, |
| 101 | + |
| 102 | + // delete before next Create |
| 103 | + { |
| 104 | + Config: config + compartmentIdVariableStr + sourceBackupIdVariableStr + sourceBackupRegionVariableStr, |
| 105 | + }, |
| 106 | + |
| 107 | + { |
| 108 | + Config: config + compartmentIdVariableStr + sourceBackupIdVariableStr + sourceBackupRegionVariableStr + |
| 109 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_backup", "test_psql_backup_copy", acctest.Optional, acctest.Create, PsqlBackupCopyRepresentation), |
| 110 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 111 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 112 | + resource.TestCheckResourceAttrSet(resourceName, "db_system_id"), |
| 113 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.#", "1"), |
| 114 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.0.source_backup_id", sourceBackupId), |
| 115 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.0.source_region", sourceBackupRegion), |
| 116 | + resource.TestCheckResourceAttr(resourceName, "retention_period", "10"), |
| 117 | + resource.TestCheckResourceAttrSet(resourceName, "source_type"), |
| 118 | + resource.TestCheckResourceAttrSet(resourceName, "state"), |
| 119 | + func(s *terraform.State) (err error) { |
| 120 | + _, err = acctest.FromInstanceState(s, resourceName, "id") |
| 121 | + return err |
| 122 | + }, |
| 123 | + ), |
| 124 | + }, |
| 125 | + |
| 126 | + { |
| 127 | + Config: config + compartmentIdVariableStr + sourceBackupIdVariableStr + sourceBackupRegionVariableStr + |
| 128 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_backup", "test_psql_backup_copy", acctest.Optional, acctest.Update, PsqlBackupCopyRepresentation), |
| 129 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 130 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 131 | + resource.TestCheckResourceAttrSet(resourceName, "db_system_id"), |
| 132 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.#", "1"), |
| 133 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.0.source_backup_id", sourceBackupId), |
| 134 | + resource.TestCheckResourceAttr(resourceName, "source_backup_details.0.source_region", sourceBackupRegion), |
| 135 | + resource.TestCheckResourceAttr(resourceName, "retention_period", "11"), |
| 136 | + resource.TestCheckResourceAttrSet(resourceName, "source_type"), |
| 137 | + resource.TestCheckResourceAttrSet(resourceName, "state"), |
| 138 | + func(s *terraform.State) (err error) { |
| 139 | + _, err = acctest.FromInstanceState(s, resourceName, "id") |
| 140 | + return err |
| 141 | + }, |
| 142 | + ), |
| 143 | + }, |
| 144 | + |
| 145 | + /* |
| 146 | + // verify singular datasource |
| 147 | + { |
| 148 | + Config: config + compartmentIdVariableStr + sourceBackupIdVariableStr + sourceBackupRegionVariableStr + |
| 149 | + acctest.GenerateDataSourceFromRepresentationMap("oci_psql_backup", "test_psql_backup_copy", acctest.Optional, acctest.Create, PsqlBackupCopySingularDataSourceRepresentation) + |
| 150 | + PsqlBackupCopyResourceConfig, |
| 151 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 152 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "backup_id"), |
| 153 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "backup_size"), |
| 154 | + resource.TestCheckResourceAttr(singularDatasourceName, "compartment_id", compartmentId), |
| 155 | + resource.TestCheckResourceAttr(singularDatasourceName, "source_backup_details.#", "0"), |
| 156 | + resource.TestCheckResourceAttr(singularDatasourceName, "db_system_details.#", "1"), |
| 157 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "id"), |
| 158 | + resource.TestCheckResourceAttr(singularDatasourceName, "retention_period", "11"), |
| 159 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "source_type"), |
| 160 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "state"), |
| 161 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "time_created"), |
| 162 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "time_created_precise"), |
| 163 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "time_updated"), |
| 164 | + ), |
| 165 | + }, |
| 166 | +
|
| 167 | + */ |
| 168 | + |
| 169 | + // verify resource import |
| 170 | + { |
| 171 | + Config: config + compartmentIdVariableStr + sourceBackupIdVariableStr + sourceBackupRegionVariableStr + PsqlBackupCopyRequiredOnlyResource, |
| 172 | + ImportState: true, |
| 173 | + ImportStateVerify: true, |
| 174 | + ImportStateVerifyIgnore: []string{ |
| 175 | + "source_backup_details", |
| 176 | + "db_system_details", |
| 177 | + "retention_period", |
| 178 | + "last_accepted_request_token", |
| 179 | + "last_completed_request_token", |
| 180 | + }, |
| 181 | + ResourceName: resourceName, |
| 182 | + }, |
| 183 | + }) |
| 184 | +} |
| 185 | + |
| 186 | +func testAccCheckPsqlBackupCopyDestroy(s *terraform.State) error { |
| 187 | + noResourceFound := true |
| 188 | + client := acctest.TestAccProvider.Meta().(*tf_client.OracleClients).PostgresqlClient() |
| 189 | + for _, rs := range s.RootModule().Resources { |
| 190 | + if rs.Type == "oci_psql_backup" { |
| 191 | + noResourceFound = false |
| 192 | + request := oci_psql.GetBackupRequest{} |
| 193 | + |
| 194 | + tmp := rs.Primary.ID |
| 195 | + request.BackupId = &tmp |
| 196 | + |
| 197 | + request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(true, "psql") |
| 198 | + |
| 199 | + response, err := client.GetBackup(context.Background(), request) |
| 200 | + |
| 201 | + if err == nil { |
| 202 | + deletedLifecycleStates := map[string]bool{ |
| 203 | + string(oci_psql.BackupLifecycleStateDeleted): true, |
| 204 | + } |
| 205 | + if _, ok := deletedLifecycleStates[string(response.LifecycleState)]; !ok { |
| 206 | + //resource lifecycle state is not in expected deleted lifecycle states. |
| 207 | + return fmt.Errorf("resource lifecycle state: %s is not in expected deleted lifecycle states", response.LifecycleState) |
| 208 | + } |
| 209 | + //resource lifecycle state is in expected deleted lifecycle states. continue with next one. |
| 210 | + continue |
| 211 | + } |
| 212 | + |
| 213 | + //Verify that exception is for '404 not found'. |
| 214 | + if failure, isServiceError := common.IsServiceError(err); !isServiceError || failure.GetHTTPStatusCode() != 404 { |
| 215 | + return err |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + if noResourceFound { |
| 220 | + return fmt.Errorf("at least one resource was expected from the state file, but could not be found") |
| 221 | + } |
| 222 | + |
| 223 | + return nil |
| 224 | +} |
| 225 | + |
| 226 | +func init() { |
| 227 | + if acctest.DependencyGraph == nil { |
| 228 | + acctest.InitDependencyGraph() |
| 229 | + } |
| 230 | + if !acctest.InSweeperExcludeList("PsqlBackupCopy") { |
| 231 | + resource.AddTestSweepers("PsqlBackupCopy", &resource.Sweeper{ |
| 232 | + Name: "PsqlBackupCopy", |
| 233 | + Dependencies: acctest.DependencyGraph["backup"], |
| 234 | + F: sweepPsqlBackupResource, |
| 235 | + }) |
| 236 | + } |
| 237 | +} |
0 commit comments