|
| 1 | +// Copyright (c) 2017, 2025, 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-testing/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 12 | + |
| 13 | + "github.com/oracle/terraform-provider-oci/httpreplay" |
| 14 | + "github.com/oracle/terraform-provider-oci/internal/acctest" |
| 15 | + |
| 16 | + "github.com/oracle/terraform-provider-oci/internal/utils" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + NosqlConfigurationRequiredOnlyResource = acctest.GenerateResourceFromRepresentationMap("oci_nosql_configuration", "test_configuration", |
| 21 | + acctest.Optional, acctest.Create, NosqlConfigurationRepresentation) |
| 22 | + |
| 23 | + NosqlConfigurationResourceConfig = acctest.GenerateResourceFromRepresentationMap("oci_nosql_configuration", "test_configuration", |
| 24 | + acctest.Optional, acctest.Update, NosqlConfigurationRepresentation) |
| 25 | + |
| 26 | + NosqlConfigurationDataResourceConfig = acctest.GenerateDataSourceFromRepresentationMap("oci_nosql_configuration", "test_configuration", |
| 27 | + acctest.Required, acctest.Create, NosqlConfigurationSingularDataSourceRepresentation) |
| 28 | + |
| 29 | + UnassignKeyResourceConfig = acctest.GenerateResourceFromRepresentationMap("oci_nosql_configuration", "test_configuration", |
| 30 | + acctest.Required, acctest.Update, UnassignKeyRepresentation) |
| 31 | + |
| 32 | + NosqlConfigurationSingularDataSourceRepresentation = map[string]interface{}{ |
| 33 | + "compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`}, |
| 34 | + } |
| 35 | + |
| 36 | + NosqlConfigurationRepresentation = map[string]interface{}{ |
| 37 | + "compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`, Update: `${var.compartment_id}`}, |
| 38 | + "environment": acctest.Representation{RepType: acctest.Required, Create: `HOSTED`, Update: `HOSTED`}, |
| 39 | + "is_opc_dry_run": acctest.Representation{RepType: acctest.Optional, Create: `false`, Update: `false`}, |
| 40 | + "kms_key": acctest.RepresentationGroup{RepType: acctest.Optional, Group: NosqlConfigurationKmsKeyRepresentation}, |
| 41 | + } |
| 42 | + |
| 43 | + NosqlConfigurationKmsKeyRepresentation = map[string]interface{}{ |
| 44 | + "id": acctest.Representation{RepType: acctest.Optional, Create: `${var.key_id}`, Update: `${var.key_id2}`}, |
| 45 | + "kms_vault_id": acctest.Representation{RepType: acctest.Optional, Create: `${var.vault_id}`, Update: `${var.vault_id}`}, |
| 46 | + } |
| 47 | + |
| 48 | + UnassignKeyRepresentation = map[string]interface{}{ |
| 49 | + "compartment_id": acctest.Representation{RepType: acctest.Required, Update: `${var.compartment_id}`}, |
| 50 | + "environment": acctest.Representation{RepType: acctest.Required, Update: `HOSTED`}, |
| 51 | + "kms_key": acctest.RepresentationGroup{RepType: acctest.Required, Group: UnassignKeyKmsKeyRepresentation}, |
| 52 | + } |
| 53 | + |
| 54 | + UnassignKeyKmsKeyRepresentation = map[string]interface{}{ |
| 55 | + "id": acctest.Representation{RepType: acctest.Required, Update: nil}, |
| 56 | + "kms_vault_id": acctest.Representation{RepType: acctest.Required, Update: nil}, |
| 57 | + } |
| 58 | +) |
| 59 | + |
| 60 | +// issue-routing-tag: nosql/default |
| 61 | +func TestNosqlConfigurationResource_basic(t *testing.T) { |
| 62 | + httpreplay.SetScenario("TestNosqlConfigurationResource_basic") |
| 63 | + defer httpreplay.SaveScenario() |
| 64 | + |
| 65 | + config := acctest.ProviderTestConfig() |
| 66 | + |
| 67 | + // Compartment_id is used to identify the tenancy. |
| 68 | + compartmentId := utils.GetEnvSettingWithBlankDefault("tenancy_ocid") |
| 69 | + compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId) |
| 70 | + |
| 71 | + // Vault Id |
| 72 | + vaultId := utils.GetEnvSettingWithBlankDefault("vault_id") |
| 73 | + vaultIdVariableStr := fmt.Sprintf("variable \"vault_id\" { default = \"%s\" }\n", vaultId) |
| 74 | + |
| 75 | + // Key Id |
| 76 | + keyId := utils.GetEnvSettingWithBlankDefault("key_id") |
| 77 | + keyIdVariableStr := fmt.Sprintf("variable \"key_id\" { default = \"%s\" }\n", keyId) |
| 78 | + |
| 79 | + // Key Id for rotating key |
| 80 | + keyId2 := utils.GetEnvSettingWithBlankDefault("key_id2") |
| 81 | + keyId2VariableStr := fmt.Sprintf("variable \"key_id2\" { default = \"%s\" }\n", keyId2) |
| 82 | + |
| 83 | + nosqlConfigurationResourceDependencies := keyIdVariableStr + keyId2VariableStr + vaultIdVariableStr |
| 84 | + |
| 85 | + resourceName := "oci_nosql_configuration.test_configuration" |
| 86 | + |
| 87 | + singularDatasourceName := "data.oci_nosql_configuration.test_configuration" |
| 88 | + |
| 89 | + // 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. |
| 90 | + assignkey_config := config + compartmentIdVariableStr + nosqlConfigurationResourceDependencies + NosqlConfigurationRequiredOnlyResource |
| 91 | + updatekey_config := config + compartmentIdVariableStr + nosqlConfigurationResourceDependencies + NosqlConfigurationResourceConfig |
| 92 | + |
| 93 | + get_configuration_config := config + compartmentIdVariableStr + NosqlConfigurationDataResourceConfig |
| 94 | + get_configuration_after_updatekey_config := get_configuration_config + nosqlConfigurationResourceDependencies + NosqlConfigurationResourceConfig |
| 95 | + get_configuration_after_unassignkey_config := get_configuration_config + UnassignKeyResourceConfig |
| 96 | + |
| 97 | + acctest.SaveConfigContent(assignkey_config, "nosql", "configuration", t) |
| 98 | + |
| 99 | + acctest.ResourceTest( |
| 100 | + t, |
| 101 | + nil, |
| 102 | + []resource.TestStep{ |
| 103 | + // verify Assign global encryption key |
| 104 | + { |
| 105 | + Config: assignkey_config, |
| 106 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 107 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 108 | + resource.TestCheckResourceAttr(resourceName, "environment", "HOSTED"), |
| 109 | + resource.TestCheckResourceAttr(resourceName, "kms_key.#", "1"), |
| 110 | + resource.TestCheckResourceAttr(resourceName, "kms_key.0.id", keyId), |
| 111 | + resource.TestCheckResourceAttr(resourceName, "kms_key.0.kms_key_state", "ACTIVE"), |
| 112 | + resource.TestCheckResourceAttr(resourceName, "kms_key.0.kms_vault_id", vaultId), |
| 113 | + |
| 114 | + func(s *terraform.State) (err error) { |
| 115 | + _, err = acctest.FromInstanceState(s, resourceName, "id") |
| 116 | + return err |
| 117 | + }, |
| 118 | + ), |
| 119 | + }, |
| 120 | + |
| 121 | + // verify Update (rotate) global encryption key |
| 122 | + { |
| 123 | + Config: updatekey_config, |
| 124 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 125 | + resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId), |
| 126 | + resource.TestCheckResourceAttr(resourceName, "environment", "HOSTED"), |
| 127 | + resource.TestCheckResourceAttr(resourceName, "kms_key.#", "1"), |
| 128 | + resource.TestCheckResourceAttr(resourceName, "kms_key.0.id", keyId2), |
| 129 | + resource.TestCheckResourceAttr(resourceName, "kms_key.0.kms_key_state", "ACTIVE"), |
| 130 | + resource.TestCheckResourceAttr(resourceName, "kms_key.0.kms_vault_id", vaultId), |
| 131 | + |
| 132 | + func(s *terraform.State) (err error) { |
| 133 | + _, err = acctest.FromInstanceState(s, resourceName, "id") |
| 134 | + return err |
| 135 | + }, |
| 136 | + ), |
| 137 | + }, |
| 138 | + |
| 139 | + // verify singular datasource |
| 140 | + { |
| 141 | + Config: get_configuration_after_updatekey_config, |
| 142 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 143 | + resource.TestCheckResourceAttr(singularDatasourceName, "compartment_id", compartmentId), |
| 144 | + resource.TestCheckResourceAttr(singularDatasourceName, "environment", "HOSTED"), |
| 145 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.#", "1"), |
| 146 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.id", keyId2), |
| 147 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.kms_key_state", "ACTIVE"), |
| 148 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.kms_vault_id", vaultId), |
| 149 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "kms_key.0.time_created"), |
| 150 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "kms_key.0.time_updated"), |
| 151 | + ), |
| 152 | + }, |
| 153 | + |
| 154 | + // verify resource import |
| 155 | + { |
| 156 | + Config: updatekey_config, |
| 157 | + ImportState: true, |
| 158 | + ImportStateIdFunc: getConfigurationCompositeId(resourceName), |
| 159 | + ImportStateVerify: true, |
| 160 | + ImportStateVerifyIgnore: []string{"is_opc_dry_run"}, |
| 161 | + ResourceName: resourceName, |
| 162 | + }, |
| 163 | + |
| 164 | + // verify key unassignment |
| 165 | + { |
| 166 | + Config: config + compartmentIdVariableStr + UnassignKeyResourceConfig, |
| 167 | + }, |
| 168 | + |
| 169 | + // verify singular datasource after unassign key |
| 170 | + { |
| 171 | + Config: get_configuration_after_unassignkey_config, |
| 172 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 173 | + resource.TestCheckResourceAttr(singularDatasourceName, "compartment_id", compartmentId), |
| 174 | + resource.TestCheckResourceAttr(singularDatasourceName, "environment", "HOSTED"), |
| 175 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.#", "1"), |
| 176 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.id", ""), |
| 177 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.kms_vault_id", ""), |
| 178 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.kms_key_state", "ACTIVE"), |
| 179 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.time_created", ""), |
| 180 | + resource.TestCheckResourceAttr(singularDatasourceName, "kms_key.0.time_updated", ""), |
| 181 | + ), |
| 182 | + }, |
| 183 | + }) |
| 184 | +} |
| 185 | + |
| 186 | +// Gets the composite Id of the configuration resource |
| 187 | +func getConfigurationCompositeId(resourceName string) resource.ImportStateIdFunc { |
| 188 | + return func(s *terraform.State) (string, error) { |
| 189 | + rs, ok := s.RootModule().Resources[resourceName] |
| 190 | + if !ok { |
| 191 | + return "", fmt.Errorf("not found: %s", resourceName) |
| 192 | + } |
| 193 | + return fmt.Sprintf("configuration/compartmentId/%s", rs.Primary.Attributes["compartment_id"]), nil |
| 194 | + } |
| 195 | +} |
0 commit comments