|
| 1 | +// Copyright (c) 2017, 2023, 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 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 12 | + |
| 13 | + "github.com/oracle/terraform-provider-oci/httpreplay" |
| 14 | + "github.com/oracle/terraform-provider-oci/internal/acctest" |
| 15 | + "github.com/oracle/terraform-provider-oci/internal/utils" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + PsqlDbSystemInsertPatchOperationsRepresentation = map[string]interface{}{ |
| 20 | + "operation": acctest.Representation{RepType: acctest.Required, Create: `INSERT`}, |
| 21 | + "selection": acctest.Representation{RepType: acctest.Required, Create: `instances`}, |
| 22 | + "value": acctest.Representation{RepType: acctest.Required, Create: map[string]string{"displayName": "patch-instance"}}, |
| 23 | + } |
| 24 | + |
| 25 | + PsqlPatchDbSystemDataSourceRepresentation = map[string]interface{}{ |
| 26 | + "compartment_id": acctest.Representation{RepType: acctest.Optional, Create: `${var.compartment_id}`}, |
| 27 | + "display_name": acctest.Representation{RepType: acctest.Optional, Create: `test-terraform`}, |
| 28 | + "state": acctest.Representation{RepType: acctest.Optional, Create: `ACTIVE`}, |
| 29 | + } |
| 30 | + |
| 31 | + PsqlPatchDbSystemSingularDataSourceRepresentation = map[string]interface{}{ |
| 32 | + "db_system_id": acctest.Representation{RepType: acctest.Required, Create: `${data.oci_psql_db_systems.test_db_systems.db_system_collection.0.items.0.id}`}, |
| 33 | + } |
| 34 | + |
| 35 | + PsqlDbSystemRemovePatchOperationsRepresentation = map[string]interface{}{ |
| 36 | + "operation": acctest.Representation{RepType: acctest.Required, Create: `REMOVE`}, |
| 37 | + "selection": acctest.Representation{RepType: acctest.Required, Create: `instances[?id == '${data.oci_psql_db_system.test_db_system.instances.1.id}']`}, |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +// issue-routing-tag: psql/default |
| 42 | +func TestPsqlDbSystemPatchResource_basic(t *testing.T) { |
| 43 | + httpreplay.SetScenario("TestPsqlDbSystemPatchResource_basic") |
| 44 | + defer httpreplay.SaveScenario() |
| 45 | + |
| 46 | + config := acctest.ProviderTestConfig() |
| 47 | + |
| 48 | + compartmentId := utils.GetEnvSettingWithBlankDefault("compartment_ocid") |
| 49 | + compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId) |
| 50 | + subnetId := utils.GetEnvSettingWithBlankDefault("subnet_ocid") |
| 51 | + subnetIdVariableStr := fmt.Sprintf("variable \"subnet_id\" { default = \"%s\" }\n", subnetId) |
| 52 | + |
| 53 | + resourceName := "oci_psql_db_system.test_db_system" |
| 54 | + |
| 55 | + var resId string |
| 56 | + var resId2 string |
| 57 | + // Save TF content to Create resource with required properties. |
| 58 | + acctest.SaveConfigContent(config+compartmentIdVariableStr+PsqlDbSystemResourceDependencies+ |
| 59 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_db_system", "test_db_system", acctest.Required, acctest.Create, PsqlDbSystemRepresentation), "psql", "dbSystem", t) |
| 60 | + |
| 61 | + acctest.ResourceTest(t, testAccCheckPsqlDbSystemDestroy, []resource.TestStep{ |
| 62 | + // verify Create |
| 63 | + { |
| 64 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + PsqlDbSystemResourceDependencies + |
| 65 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_db_system", "test_db_system", acctest.Required, acctest.Create, PsqlDbSystemRepresentation), |
| 66 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 67 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 68 | + resource.TestCheckResourceAttr(resourceName, "db_version", "14"), |
| 69 | + resource.TestCheckResourceAttr(resourceName, "display_name", "test-terraform"), |
| 70 | + resource.TestCheckResourceAttr(resourceName, "network_details.#", "1"), |
| 71 | + resource.TestCheckResourceAttrSet(resourceName, "network_details.0.subnet_id"), |
| 72 | + resource.TestCheckResourceAttr(resourceName, "shape", "PostgreSQL.VM.Standard.E4.Flex.2.32GB"), |
| 73 | + resource.TestCheckResourceAttr(resourceName, "storage_details.#", "1"), |
| 74 | + resource.TestCheckResourceAttr(resourceName, "instance_count", "1"), |
| 75 | + resource.TestCheckResourceAttrSet(resourceName, "storage_details.0.availability_domain"), |
| 76 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.is_regionally_durable", "false"), |
| 77 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.system_type", "OCI_OPTIMIZED_STORAGE"), |
| 78 | + |
| 79 | + func(s *terraform.State) (err error) { |
| 80 | + resId, err = acctest.FromInstanceState(s, resourceName, "id") |
| 81 | + return err |
| 82 | + }, |
| 83 | + ), |
| 84 | + }, |
| 85 | + |
| 86 | + // Verify adding read replica with Patch operation |
| 87 | + { |
| 88 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + PsqlDbSystemResourceDependencies + |
| 89 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_db_system", "test_db_system", acctest.Required, acctest.Create, |
| 90 | + acctest.RepresentationCopyWithNewProperties(PsqlDbSystemRepresentation, map[string]interface{}{ |
| 91 | + "patch_operations": acctest.RepresentationGroup{RepType: acctest.Required, Group: PsqlDbSystemInsertPatchOperationsRepresentation}, |
| 92 | + "instance_count": acctest.Representation{RepType: acctest.Required, Create: `2`}, |
| 93 | + })), |
| 94 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 95 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 96 | + resource.TestCheckResourceAttr(resourceName, "db_version", "14"), |
| 97 | + resource.TestCheckResourceAttr(resourceName, "display_name", "test-terraform"), |
| 98 | + resource.TestCheckResourceAttr(resourceName, "network_details.#", "1"), |
| 99 | + resource.TestCheckResourceAttrSet(resourceName, "network_details.0.subnet_id"), |
| 100 | + resource.TestCheckResourceAttr(resourceName, "instance_count", "2"), |
| 101 | + resource.TestCheckResourceAttr(resourceName, "shape", "PostgreSQL.VM.Standard.E4.Flex.2.32GB"), |
| 102 | + resource.TestCheckResourceAttr(resourceName, "storage_details.#", "1"), |
| 103 | + resource.TestCheckResourceAttrSet(resourceName, "storage_details.0.availability_domain"), |
| 104 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.is_regionally_durable", "false"), |
| 105 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.system_type", "OCI_OPTIMIZED_STORAGE"), |
| 106 | + |
| 107 | + func(s *terraform.State) (err error) { |
| 108 | + resId2, err = acctest.FromInstanceState(s, resourceName, "id") |
| 109 | + if resId != resId2 { |
| 110 | + return fmt.Errorf("resource recreated when it was supposed to be updated") |
| 111 | + } |
| 112 | + return err |
| 113 | + }, |
| 114 | + ), |
| 115 | + }, |
| 116 | + |
| 117 | + //Verify after patch operation is removed |
| 118 | + { |
| 119 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + PsqlDbSystemResourceDependencies + |
| 120 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_db_system", "test_db_system", acctest.Required, acctest.Create, |
| 121 | + acctest.RepresentationCopyWithNewProperties(PsqlDbSystemRepresentation, map[string]interface{}{ |
| 122 | + "instance_count": acctest.Representation{RepType: acctest.Required, Create: `2`}, |
| 123 | + })), |
| 124 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 125 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 126 | + resource.TestCheckResourceAttr(resourceName, "db_version", "14"), |
| 127 | + resource.TestCheckResourceAttr(resourceName, "display_name", "test-terraform"), |
| 128 | + resource.TestCheckResourceAttr(resourceName, "instance_count", "2"), |
| 129 | + resource.TestCheckResourceAttr(resourceName, "network_details.#", "1"), |
| 130 | + resource.TestCheckResourceAttrSet(resourceName, "network_details.0.subnet_id"), |
| 131 | + resource.TestCheckResourceAttr(resourceName, "shape", "PostgreSQL.VM.Standard.E4.Flex.2.32GB"), |
| 132 | + resource.TestCheckResourceAttr(resourceName, "storage_details.#", "1"), |
| 133 | + resource.TestCheckResourceAttrSet(resourceName, "storage_details.0.availability_domain"), |
| 134 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.is_regionally_durable", "false"), |
| 135 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.system_type", "OCI_OPTIMIZED_STORAGE"), |
| 136 | + |
| 137 | + func(s *terraform.State) (err error) { |
| 138 | + resId2, err = acctest.FromInstanceState(s, resourceName, "id") |
| 139 | + if resId != resId2 { |
| 140 | + return fmt.Errorf("resource recreated when it was supposed to be updated") |
| 141 | + } |
| 142 | + return err |
| 143 | + }, |
| 144 | + ), |
| 145 | + }, |
| 146 | + |
| 147 | + // Verify removing read replica with Patch operation |
| 148 | + { |
| 149 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + PsqlDbSystemResourceDependencies + |
| 150 | + acctest.GenerateDataSourceFromRepresentationMap("oci_psql_db_systems", "test_db_systems", acctest.Optional, acctest.Create, PsqlPatchDbSystemDataSourceRepresentation) + |
| 151 | + acctest.GenerateDataSourceFromRepresentationMap("oci_psql_db_system", "test_db_system", acctest.Optional, acctest.Create, PsqlPatchDbSystemSingularDataSourceRepresentation) + |
| 152 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_db_system", "test_db_system", acctest.Required, acctest.Create, |
| 153 | + acctest.RepresentationCopyWithNewProperties(PsqlDbSystemRepresentation, map[string]interface{}{ |
| 154 | + "patch_operations": acctest.RepresentationGroup{RepType: acctest.Required, Group: PsqlDbSystemRemovePatchOperationsRepresentation}, |
| 155 | + "instance_count": acctest.Representation{RepType: acctest.Required, Create: `1`}, |
| 156 | + })), |
| 157 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 158 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 159 | + resource.TestCheckResourceAttr(resourceName, "db_version", "14"), |
| 160 | + resource.TestCheckResourceAttr(resourceName, "display_name", "test-terraform"), |
| 161 | + resource.TestCheckResourceAttr(resourceName, "instance_count", "1"), |
| 162 | + resource.TestCheckResourceAttr(resourceName, "network_details.#", "1"), |
| 163 | + resource.TestCheckResourceAttrSet(resourceName, "network_details.0.subnet_id"), |
| 164 | + resource.TestCheckResourceAttr(resourceName, "shape", "PostgreSQL.VM.Standard.E4.Flex.2.32GB"), |
| 165 | + resource.TestCheckResourceAttr(resourceName, "storage_details.#", "1"), |
| 166 | + resource.TestCheckResourceAttrSet(resourceName, "storage_details.0.availability_domain"), |
| 167 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.is_regionally_durable", "false"), |
| 168 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.system_type", "OCI_OPTIMIZED_STORAGE"), |
| 169 | + |
| 170 | + func(s *terraform.State) (err error) { |
| 171 | + resId2, err = acctest.FromInstanceState(s, resourceName, "id") |
| 172 | + if resId != resId2 { |
| 173 | + return fmt.Errorf("resource recreated when it was supposed to be updated") |
| 174 | + } |
| 175 | + return err |
| 176 | + }, |
| 177 | + ), |
| 178 | + }, |
| 179 | + |
| 180 | + //Verify after patch operation is removed |
| 181 | + { |
| 182 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + PsqlDbSystemResourceDependencies + |
| 183 | + acctest.GenerateResourceFromRepresentationMap("oci_psql_db_system", "test_db_system", acctest.Required, acctest.Create, |
| 184 | + acctest.RepresentationCopyWithNewProperties(PsqlDbSystemRepresentation, map[string]interface{}{ |
| 185 | + "instance_count": acctest.Representation{RepType: acctest.Required, Create: `1`}, |
| 186 | + })), |
| 187 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 188 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 189 | + resource.TestCheckResourceAttr(resourceName, "db_version", "14"), |
| 190 | + resource.TestCheckResourceAttr(resourceName, "display_name", "test-terraform"), |
| 191 | + resource.TestCheckResourceAttr(resourceName, "network_details.#", "1"), |
| 192 | + resource.TestCheckResourceAttrSet(resourceName, "network_details.0.subnet_id"), |
| 193 | + resource.TestCheckResourceAttr(resourceName, "shape", "PostgreSQL.VM.Standard.E4.Flex.2.32GB"), |
| 194 | + resource.TestCheckResourceAttr(resourceName, "storage_details.#", "1"), |
| 195 | + resource.TestCheckResourceAttrSet(resourceName, "storage_details.0.availability_domain"), |
| 196 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.is_regionally_durable", "false"), |
| 197 | + resource.TestCheckResourceAttr(resourceName, "storage_details.0.system_type", "OCI_OPTIMIZED_STORAGE"), |
| 198 | + |
| 199 | + func(s *terraform.State) (err error) { |
| 200 | + resId2, err = acctest.FromInstanceState(s, resourceName, "id") |
| 201 | + if resId != resId2 { |
| 202 | + return fmt.Errorf("resource recreated when it was supposed to be updated") |
| 203 | + } |
| 204 | + return err |
| 205 | + }, |
| 206 | + ), |
| 207 | + }, |
| 208 | + }) |
| 209 | +} |
| 210 | + |
| 211 | +func init() { |
| 212 | + if acctest.DependencyGraph == nil { |
| 213 | + acctest.InitDependencyGraph() |
| 214 | + } |
| 215 | + if !acctest.InSweeperExcludeList("PsqlDbSystemPatch") { |
| 216 | + resource.AddTestSweepers("PsqlDbSystemPatch", &resource.Sweeper{ |
| 217 | + Name: "PsqlDbSystemPatch", |
| 218 | + Dependencies: acctest.DependencyGraph["dbSystem"], |
| 219 | + F: sweepPsqlDbSystemResource, |
| 220 | + }) |
| 221 | + } |
| 222 | +} |
0 commit comments