Skip to content

Commit cf06dc7

Browse files
committed
fix for routeRule update after having a serviceGateway Rule as part of the rules
1 parent 0ca62b9 commit cf06dc7

File tree

2 files changed

+98
-9
lines changed

2 files changed

+98
-9
lines changed

provider/core_route_table_resource.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ func (s *RouteTableResourceCrud) mapToRouteRule(hashcode int) oci_core.RouteRule
357357
// @CODEGEN We need this change because the service will return both cidr_block and destination.
358358
// Without this change on update operations terraform will send both paremeters since they are both in the statefile.
359359
// The service will complain if both parameters are not the same on the update operation so we need to make sure only the relevant one in sent to the service.
360+
destinationType, destinationTypePresent := s.D.GetOkExists(fmt.Sprintf("route_rules.%d.destination_type", hashcode))
361+
if destinationTypePresent {
362+
tmp := oci_core.RouteRuleDestinationTypeEnum(destinationType.(string))
363+
result.DestinationType = tmp
364+
}
365+
360366
cidrBlockChanged := false
361367
cidrBlock, cidrBlockPresent := s.D.GetOkExists(fmt.Sprintf("route_rules.%d.cidr_block", hashcode))
362368
if cidrBlockPresent && s.D.HasChange(fmt.Sprintf("route_rules.%d.cidr_block", hashcode)) {
@@ -371,20 +377,16 @@ func (s *RouteTableResourceCrud) mapToRouteRule(hashcode int) oci_core.RouteRule
371377
destinationChanged = true
372378
}
373379

374-
if !destinationChanged && !cidrBlockChanged {
380+
if destinationType == string(oci_core.RouteRuleDestinationTypeServiceCidrBlock) || (!destinationChanged && !cidrBlockChanged) {
375381
tmp := destination.(string)
376382
result.Destination = &tmp
377383
}
378-
if !destinationChanged && cidrBlockPresent {
384+
385+
if !destinationChanged && cidrBlockPresent && cidrBlock != "" && destinationType != string(oci_core.RouteRuleDestinationTypeServiceCidrBlock) {
379386
tmp := cidrBlock.(string)
380387
result.CidrBlock = &tmp
381388
}
382389

383-
if destinationType, ok := s.D.GetOkExists(fmt.Sprintf("route_rules.%d.destination_type", hashcode)); ok {
384-
tmp := oci_core.RouteRuleDestinationTypeEnum(destinationType.(string))
385-
result.DestinationType = tmp
386-
}
387-
388390
if networkEntityId, ok := s.D.GetOkExists(fmt.Sprintf("route_rules.%d.network_entity_id", hashcode)); ok {
389391
tmp := networkEntityId.(string)
390392
result.NetworkEntityId = &tmp

provider/core_route_table_test.go

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ resource "oci_core_route_table" "test_route_table" {
5555
freeform_tags = "${var.route_table_freeform_tags}"
5656
}
5757
`
58-
5958
RouteTableResourceConfigWithServiceCidr = RouteTableResourceDependencies + `
6059
resource "oci_core_route_table" "test_route_table" {
6160
#Required
@@ -68,6 +67,49 @@ resource "oci_core_route_table" "test_route_table" {
6867
destination = "${lookup(data.oci_core_services.test_services.services[0], "cidr_block")}"
6968
destination_type = "${var.route_table_route_rules_destination_type}"
7069
}
70+
route_rules {
71+
#Required
72+
destination = "${var.route_table_route_rules_destination}"
73+
network_entity_id = "${oci_core_drg.test_drg.id}"
74+
}
75+
vcn_id = "${oci_core_vcn.test_vcn.id}"
76+
77+
#Optional
78+
defined_tags = "${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "${var.route_table_defined_tags_value}")}"
79+
display_name = "${var.route_table_display_name}"
80+
freeform_tags = "${var.route_table_freeform_tags}"
81+
}
82+
83+
resource "oci_core_service_gateway" "test_service_gateway" {
84+
#Required
85+
compartment_id = "${var.compartment_id}"
86+
services {
87+
service_id = "${lookup(data.oci_core_services.test_services.services[0], "id")}"
88+
}
89+
vcn_id = "${oci_core_vcn.test_vcn.id}"
90+
}
91+
92+
data "oci_core_services" "test_services" {
93+
}
94+
`
95+
RouteTableResourceConfigWithServiceCidrAddingCidrBlock = RouteTableResourceDependencies + `
96+
resource "oci_core_route_table" "test_route_table" {
97+
#Required
98+
compartment_id = "${var.compartment_id}"
99+
route_rules {
100+
#Required
101+
network_entity_id = "${oci_core_service_gateway.test_service_gateway.id}"
102+
103+
#Optional
104+
cidr_block = "${lookup(data.oci_core_services.test_services.services[0], "cidr_block")}"
105+
destination = "${lookup(data.oci_core_services.test_services.services[0], "cidr_block")}"
106+
destination_type = "${var.route_table_route_rules_destination_type}"
107+
}
108+
route_rules {
109+
#Required
110+
destination = "${var.route_table_route_rules_destination}"
111+
network_entity_id = "${oci_core_drg.test_drg.id}"
112+
}
71113
vcn_id = "${oci_core_vcn.test_vcn.id}"
72114
73115
#Optional
@@ -240,8 +282,53 @@ variable "route_table_state" { default = "AVAILABLE" }
240282
Check: resource.ComposeAggregateTestCheckFunc(
241283
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
242284
resource.TestCheckResourceAttrSet(resourceName, "id"),
243-
resource.TestCheckResourceAttr(resourceName, "route_rules.#", "1"),
285+
resource.TestCheckResourceAttr(resourceName, "route_rules.#", "2"),
286+
CheckResourceSetContainsElementWithProperties(resourceName, "route_rules", map[string]string{"destination_type": "SERVICE_CIDR_BLOCK"}, []string{"network_entity_id", "destination"}),
287+
CheckResourceSetContainsElementWithProperties(resourceName, "route_rules", map[string]string{"destination_type": "CIDR_BLOCK", "destination": "0.0.0.0/0"}, []string{"network_entity_id"}),
288+
resource.TestCheckResourceAttrSet(resourceName, "state"),
289+
resource.TestCheckResourceAttrSet(resourceName, "vcn_id"),
290+
),
291+
},
292+
// verify update after having a destination_type rule
293+
{
294+
Config: config + `
295+
variable "route_table_defined_tags_value" { default = "value" }
296+
variable "route_table_display_name" { default = "MyRouteTable" }
297+
variable "route_table_freeform_tags" { default = {"Department"= "Finance"} }
298+
variable "route_table_route_rules_cidr_block" { default = "10.0.0.0/8" }
299+
variable "route_table_route_rules_destination" { default = "0.0.0.0/1" }
300+
variable "route_table_route_rules_destination_type" { default = "SERVICE_CIDR_BLOCK" }
301+
variable "route_table_state" { default = "AVAILABLE" }
302+
303+
` + compartmentIdVariableStr + RouteTableResourceConfigWithServiceCidr,
304+
Check: resource.ComposeAggregateTestCheckFunc(
305+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
306+
resource.TestCheckResourceAttrSet(resourceName, "id"),
307+
resource.TestCheckResourceAttr(resourceName, "route_rules.#", "2"),
308+
CheckResourceSetContainsElementWithProperties(resourceName, "route_rules", map[string]string{"destination_type": "SERVICE_CIDR_BLOCK"}, []string{"network_entity_id", "destination"}),
309+
CheckResourceSetContainsElementWithProperties(resourceName, "route_rules", map[string]string{"destination_type": "CIDR_BLOCK", "destination": "0.0.0.0/1"}, []string{"network_entity_id"}),
310+
resource.TestCheckResourceAttrSet(resourceName, "state"),
311+
resource.TestCheckResourceAttrSet(resourceName, "vcn_id"),
312+
),
313+
},
314+
// verify adding cidr_block to a rule that has destination already
315+
{
316+
Config: config + `
317+
variable "route_table_defined_tags_value" { default = "value" }
318+
variable "route_table_display_name" { default = "MyRouteTable" }
319+
variable "route_table_freeform_tags" { default = {"Department"= "Finance"} }
320+
variable "route_table_route_rules_cidr_block" { default = "10.0.0.0/8" }
321+
variable "route_table_route_rules_destination" { default = "0.0.0.0/1" }
322+
variable "route_table_route_rules_destination_type" { default = "SERVICE_CIDR_BLOCK" }
323+
variable "route_table_state" { default = "AVAILABLE" }
324+
325+
` + compartmentIdVariableStr + RouteTableResourceConfigWithServiceCidrAddingCidrBlock,
326+
Check: resource.ComposeAggregateTestCheckFunc(
327+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
328+
resource.TestCheckResourceAttrSet(resourceName, "id"),
329+
resource.TestCheckResourceAttr(resourceName, "route_rules.#", "2"),
244330
CheckResourceSetContainsElementWithProperties(resourceName, "route_rules", map[string]string{"destination_type": "SERVICE_CIDR_BLOCK"}, []string{"network_entity_id", "destination"}),
331+
CheckResourceSetContainsElementWithProperties(resourceName, "route_rules", map[string]string{"destination_type": "CIDR_BLOCK", "destination": "0.0.0.0/1"}, []string{"network_entity_id"}),
245332
resource.TestCheckResourceAttrSet(resourceName, "state"),
246333
resource.TestCheckResourceAttrSet(resourceName, "vcn_id"),
247334
),

0 commit comments

Comments
 (0)