|
| 1 | +// Copyright (c) 2017, 2021, 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-sdk/v2/helper/schema" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 15 | + "github.com/oracle/oci-go-sdk/v65/common" |
| 16 | + oci_golden_gate "github.com/oracle/oci-go-sdk/v65/goldengate" |
| 17 | + |
| 18 | + "github.com/oracle/terraform-provider-oci/httpreplay" |
| 19 | + "github.com/oracle/terraform-provider-oci/internal/acctest" |
| 20 | + tf_client "github.com/oracle/terraform-provider-oci/internal/client" |
| 21 | + "github.com/oracle/terraform-provider-oci/internal/tfresource" |
| 22 | + "github.com/oracle/terraform-provider-oci/internal/utils" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + GoldenGateConnectionAssignmentRequiredOnlyResource = acctest.GenerateResourceFromRepresentationMap("oci_golden_gate_connection_assignment", "test_connection_assignment", acctest.Optional, acctest.Create, GoldenGateConnectionAssignmentRepresentation) |
| 27 | + |
| 28 | + GoldenGateConnectionAssignmentResourceConfig = acctest.GenerateResourceFromRepresentationMap("oci_golden_gate_connection_assignment", "test_connection_assignment", acctest.Optional, acctest.Update, GoldenGateConnectionAssignmentRepresentation) |
| 29 | + |
| 30 | + GoldenGateGoldenGateConnectionAssignmentSingularDataSourceRepresentation = map[string]interface{}{ |
| 31 | + "connection_assignment_id": acctest.Representation{RepType: acctest.Required, Create: `${oci_golden_gate_connection_assignment.test_connection_assignment.id}`}, |
| 32 | + } |
| 33 | + |
| 34 | + GoldenGateGoldenGateConnectionAssignmentDataSourceRepresentation = map[string]interface{}{ |
| 35 | + "compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`}, |
| 36 | + "connection_id": acctest.Representation{RepType: acctest.Required, Create: `${var.test_connection_id}`}, |
| 37 | + "deployment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.test_deployment_id}`}, |
| 38 | + "name": acctest.Representation{RepType: acctest.Optional, Create: `name`}, |
| 39 | + "state": acctest.Representation{RepType: acctest.Optional, Create: `ACTIVE`}, |
| 40 | + "filter": acctest.RepresentationGroup{RepType: acctest.Required, Group: GoldenGateConnectionAssignmentDataSourceFilterRepresentation}} |
| 41 | + GoldenGateConnectionAssignmentDataSourceFilterRepresentation = map[string]interface{}{ |
| 42 | + "name": acctest.Representation{RepType: acctest.Required, Create: `name`}, |
| 43 | + "values": acctest.Representation{RepType: acctest.Required, Create: []string{`${oci_golden_gate_connection_assignment.test_connection_assignment.id}`}}, |
| 44 | + } |
| 45 | + |
| 46 | + GoldenGateConnectionAssignmentRepresentation = map[string]interface{}{ |
| 47 | + "connection_id": acctest.Representation{RepType: acctest.Required, Create: `${var.test_connection_id}`}, |
| 48 | + "deployment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.test_deployment_id}`}, |
| 49 | + } |
| 50 | +) |
| 51 | + |
| 52 | +// issue-routing-tag: golden_gate/default |
| 53 | +func TestGoldenGateConnectionAssignmentResource_basic(t *testing.T) { |
| 54 | + httpreplay.SetScenario("TestGoldenGateConnectionAssignmentResource_basic") |
| 55 | + defer httpreplay.SaveScenario() |
| 56 | + |
| 57 | + config := acctest.ProviderTestConfig() |
| 58 | + |
| 59 | + compartmentId := utils.GetEnvSettingWithBlankDefault("compartment_ocid") |
| 60 | + compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId) |
| 61 | + |
| 62 | + testConnectionId := utils.GetEnvSettingWithBlankDefault("connection_id") |
| 63 | + testConnectionIdVariableStr := fmt.Sprintf("variable \"test_connection_id\" { default = \"%s\" }\n", testConnectionId) |
| 64 | + |
| 65 | + testDeploymentId := utils.GetEnvSettingWithBlankDefault("deployment_ocid") |
| 66 | + testDeploymentIdVariableStr := fmt.Sprintf("variable \"test_deployment_id\" { default = \"%s\" }\n", testDeploymentId) |
| 67 | + |
| 68 | + subnetId := utils.GetEnvSettingWithBlankDefault("subnet_id") |
| 69 | + subnetIdVariableStr := fmt.Sprintf("variable \"test_subnet_id\" { default = \"%s\" }\n", subnetId) |
| 70 | + |
| 71 | + resourceName := "oci_golden_gate_connection_assignment.test_connection_assignment" |
| 72 | + datasourceName := "data.oci_golden_gate_connection_assignments.test_connection_assignments" |
| 73 | + singularDatasourceName := "data.oci_golden_gate_connection_assignment.test_connection_assignment" |
| 74 | + |
| 75 | + // Save TF content to Create resource with only required properties. This has to be exactly the same as the config part in the create step in the test. |
| 76 | + /*connectionResourceDependencies := acctest.GenerateResourceFromRepresentationMap("oci_golden_gate_connection", "test_connection", acctest.Required, acctest.Create, GoldenGateConnectionRepresentation) + |
| 77 | + acctest.GenerateResourceFromRepresentationMap("oci_golden_gate_deployment", "depl_test_ggs_deployment", acctest.Required, acctest.Create, goldenGateDeploymentRepresentation) |
| 78 | + */ |
| 79 | + acctest.SaveConfigContent(config+compartmentIdVariableStr+subnetIdVariableStr+testDeploymentIdVariableStr+testConnectionIdVariableStr, "goldengate", "connectionAssignment", t) |
| 80 | + |
| 81 | + acctest.ResourceTest(t, testAccCheckGoldenGateConnectionAssignmentDestroy, []resource.TestStep{ |
| 82 | + // verify Create |
| 83 | + { |
| 84 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + |
| 85 | + testConnectionIdVariableStr + testDeploymentIdVariableStr + |
| 86 | + acctest.GenerateResourceFromRepresentationMap("oci_golden_gate_connection_assignment", "test_connection_assignment", acctest.Required, acctest.Create, GoldenGateConnectionAssignmentRepresentation), |
| 87 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 88 | + resource.TestCheckResourceAttrSet(resourceName, "connection_id"), |
| 89 | + resource.TestCheckResourceAttrSet(resourceName, "deployment_id"), |
| 90 | + |
| 91 | + func(s *terraform.State) (err error) { |
| 92 | + _, err = acctest.FromInstanceState(s, resourceName, "id") |
| 93 | + return err |
| 94 | + }, |
| 95 | + ), |
| 96 | + }, |
| 97 | + |
| 98 | + // verify datasource |
| 99 | + { |
| 100 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + |
| 101 | + testDeploymentIdVariableStr + testConnectionIdVariableStr + |
| 102 | + acctest.GenerateDataSourceFromRepresentationMap("oci_golden_gate_connection_assignments", "test_connection_assignments", acctest.Optional, acctest.Update, GoldenGateGoldenGateConnectionAssignmentDataSourceRepresentation) + |
| 103 | + acctest.GenerateResourceFromRepresentationMap("oci_golden_gate_connection_assignment", "test_connection_assignment", acctest.Optional, acctest.Update, GoldenGateConnectionAssignmentRepresentation), |
| 104 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 105 | + resource.TestCheckResourceAttr(datasourceName, "compartment_id", compartmentId), |
| 106 | + resource.TestCheckResourceAttrSet(datasourceName, "connection_id"), |
| 107 | + resource.TestCheckResourceAttrSet(datasourceName, "deployment_id"), |
| 108 | + resource.TestCheckResourceAttr(datasourceName, "name", "name"), |
| 109 | + resource.TestCheckResourceAttr(datasourceName, "state", "ACTIVE"), |
| 110 | + |
| 111 | + resource.TestCheckResourceAttr(datasourceName, "connection_assignment_collection.#", "1"), |
| 112 | + resource.TestCheckResourceAttr(datasourceName, "connection_assignment_collection.0.items.#", "0"), |
| 113 | + ), |
| 114 | + }, |
| 115 | + // verify singular datasource |
| 116 | + { |
| 117 | + Config: config + compartmentIdVariableStr + subnetIdVariableStr + |
| 118 | + acctest.GenerateDataSourceFromRepresentationMap("oci_golden_gate_connection_assignment", "test_connection_assignment", acctest.Required, acctest.Create, GoldenGateGoldenGateConnectionAssignmentSingularDataSourceRepresentation) + |
| 119 | + //connectionResourceDependencies + |
| 120 | + testDeploymentIdVariableStr + testConnectionIdVariableStr + |
| 121 | + GoldenGateConnectionAssignmentResourceConfig, |
| 122 | + Check: acctest.ComposeAggregateTestCheckFuncWrapper( |
| 123 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "connection_assignment_id"), |
| 124 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "alias_name"), |
| 125 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "compartment_id"), |
| 126 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "id"), |
| 127 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "state"), |
| 128 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "time_created"), |
| 129 | + resource.TestCheckResourceAttrSet(singularDatasourceName, "time_updated"), |
| 130 | + ), |
| 131 | + }, |
| 132 | + // verify resource import |
| 133 | + { |
| 134 | + Config: config + GoldenGateConnectionAssignmentRequiredOnlyResource, |
| 135 | + ImportState: true, |
| 136 | + ImportStateVerify: true, |
| 137 | + ImportStateVerifyIgnore: []string{}, |
| 138 | + ResourceName: resourceName, |
| 139 | + }, |
| 140 | + }) |
| 141 | +} |
| 142 | + |
| 143 | +func testAccCheckGoldenGateConnectionAssignmentDestroy(s *terraform.State) error { |
| 144 | + noResourceFound := true |
| 145 | + client := acctest.TestAccProvider.Meta().(*tf_client.OracleClients).GoldenGateClient() |
| 146 | + for _, rs := range s.RootModule().Resources { |
| 147 | + if rs.Type == "oci_golden_gate_connection_assignment" { |
| 148 | + noResourceFound = false |
| 149 | + request := oci_golden_gate.GetConnectionAssignmentRequest{} |
| 150 | + |
| 151 | + tmp := rs.Primary.ID |
| 152 | + request.ConnectionAssignmentId = &tmp |
| 153 | + |
| 154 | + request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(true, "golden_gate") |
| 155 | + |
| 156 | + _, err := client.GetConnectionAssignment(context.Background(), request) |
| 157 | + |
| 158 | + if err == nil { |
| 159 | + return fmt.Errorf("resource still exists") |
| 160 | + } |
| 161 | + |
| 162 | + //Verify that exception is for '404 not found'. |
| 163 | + if failure, isServiceError := common.IsServiceError(err); !isServiceError || failure.GetHTTPStatusCode() != 404 { |
| 164 | + return err |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + if noResourceFound { |
| 169 | + return fmt.Errorf("at least one resource was expected from the state file, but could not be found") |
| 170 | + } |
| 171 | + |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +func init() { |
| 176 | + if acctest.DependencyGraph == nil { |
| 177 | + acctest.InitDependencyGraph() |
| 178 | + } |
| 179 | + if !acctest.InSweeperExcludeList("GoldenGateConnectionAssignment") { |
| 180 | + resource.AddTestSweepers("GoldenGateConnectionAssignment", &resource.Sweeper{ |
| 181 | + Name: "GoldenGateConnectionAssignment", |
| 182 | + Dependencies: acctest.DependencyGraph["connectionAssignment"], |
| 183 | + F: sweepGoldenGateConnectionAssignmentResource, |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func sweepGoldenGateConnectionAssignmentResource(compartment string) error { |
| 189 | + goldenGateClient := acctest.GetTestClients(&schema.ResourceData{}).GoldenGateClient() |
| 190 | + connectionAssignmentIds, err := getGoldenGateConnectionAssignmentIds(compartment) |
| 191 | + if err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + for _, connectionAssignmentId := range connectionAssignmentIds { |
| 195 | + if ok := acctest.SweeperDefaultResourceId[connectionAssignmentId]; !ok { |
| 196 | + deleteConnectionAssignmentRequest := oci_golden_gate.DeleteConnectionAssignmentRequest{} |
| 197 | + |
| 198 | + deleteConnectionAssignmentRequest.ConnectionAssignmentId = &connectionAssignmentId |
| 199 | + |
| 200 | + deleteConnectionAssignmentRequest.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(true, "golden_gate") |
| 201 | + _, error := goldenGateClient.DeleteConnectionAssignment(context.Background(), deleteConnectionAssignmentRequest) |
| 202 | + if error != nil { |
| 203 | + fmt.Printf("Error deleting ConnectionAssignment %s %s, It is possible that the resource is already deleted. Please verify manually \n", connectionAssignmentId, error) |
| 204 | + continue |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +func getGoldenGateConnectionAssignmentIds(compartment string) ([]string, error) { |
| 212 | + ids := acctest.GetResourceIdsToSweep(compartment, "ConnectionAssignmentId") |
| 213 | + if ids != nil { |
| 214 | + return ids, nil |
| 215 | + } |
| 216 | + var resourceIds []string |
| 217 | + compartmentId := compartment |
| 218 | + goldenGateClient := acctest.GetTestClients(&schema.ResourceData{}).GoldenGateClient() |
| 219 | + |
| 220 | + listConnectionAssignmentsRequest := oci_golden_gate.ListConnectionAssignmentsRequest{} |
| 221 | + listConnectionAssignmentsRequest.CompartmentId = &compartmentId |
| 222 | + listConnectionAssignmentsResponse, err := goldenGateClient.ListConnectionAssignments(context.Background(), listConnectionAssignmentsRequest) |
| 223 | + |
| 224 | + if err != nil { |
| 225 | + return resourceIds, fmt.Errorf("Error getting ConnectionAssignment list for compartment id : %s , %s \n", compartmentId, err) |
| 226 | + } |
| 227 | + for _, connectionAssignment := range listConnectionAssignmentsResponse.Items { |
| 228 | + id := *connectionAssignment.Id |
| 229 | + resourceIds = append(resourceIds, id) |
| 230 | + acctest.AddResourceIdToSweeperResourceIdMap(compartmentId, "ConnectionAssignmentId", id) |
| 231 | + } |
| 232 | + return resourceIds, nil |
| 233 | +} |
0 commit comments