Skip to content

Commit 84ee657

Browse files
Rakhi Narayanjotruon
authored andcommitted
Addressed PR comments
1 parent eca5fa8 commit 84ee657

4 files changed

+176
-5
lines changed

oci/core_drg_route_distribution_statement_resource.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,24 @@ func (s *CoreDrgRouteDistributionStatementResourceCrud) Create() error {
167167
}
168168

169169
if len(response.Items) > 0 {
170-
s.Res = &response.Items[0]
170+
for _, responseStatement := range response.Items {
171+
if responseStatement.Action != oci_core.DrgRouteDistributionStatementActionEnum(statement.Action) {
172+
continue
173+
}
174+
if len(responseStatement.MatchCriteria) == len(statement.MatchCriteria) {
175+
if !isDrgRouteDistributionMatchCriteriaEqual(responseStatement.MatchCriteria, statement.MatchCriteria) {
176+
continue
177+
}
178+
}
179+
if *responseStatement.Priority != *statement.Priority {
180+
continue
181+
}
182+
s.Res = &responseStatement
183+
break
184+
}
171185
} else {
172186
return fmt.Errorf("distribution statement missing in response")
173187
}
174-
175188
return nil
176189
}
177190

@@ -261,7 +274,15 @@ func (s *CoreDrgRouteDistributionStatementResourceCrud) Update() error {
261274
return fmt.Errorf("failed to update distribution statements, error: %v", err)
262275
}
263276
if response.Items != nil && len(response.Items) > 0 {
264-
s.Res = &response.Items[0]
277+
_, statementId, err := parseDrgRouteDistributionStatementCompositeId(s.D.Id())
278+
for _, distributionStatement := range response.Items {
279+
if *distributionStatement.Id == statementId {
280+
s.Res = &distributionStatement
281+
}
282+
}
283+
if err != nil {
284+
return fmt.Errorf("failed to update distribution statements, error: %v", err)
285+
}
265286
}
266287

267288
return nil
@@ -387,3 +408,20 @@ func DrgRouteDistributionMatchCriteriaToMap(obj oci_core.DrgRouteDistributionMat
387408
}
388409
return result
389410
}
411+
412+
func isDrgRouteDistributionMatchCriteriaEqual(criteria1, criteria2 oci_core.DrgRouteDistributionMatchCriteria) bool {
413+
mapCriteria1 := DrgRouteDistributionMatchCriteriaToMap(criteria1)
414+
mapCriteria2 := DrgRouteDistributionMatchCriteriaToMap(criteria2)
415+
416+
for key, value := range mapCriteria1 {
417+
if val2, ok := mapCriteria2[key]; ok {
418+
if val2 != value {
419+
return false
420+
}
421+
} else {
422+
return false
423+
}
424+
}
425+
426+
return true
427+
}

oci/core_drg_route_distribution_statement_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,40 @@ var (
2929
"priority": Representation{repType: Required, create: `10`, update: `15`},
3030
}
3131

32+
drgRouteDistributionStatementRepresentation2 = map[string]interface{}{
33+
"drg_route_distribution_id": Representation{repType: Required, create: `${oci_core_drg_route_distribution.test_drg_route_distribution.id}`},
34+
"action": Representation{repType: Required, create: `ACCEPT`},
35+
"match_criteria": RepresentationGroup{Required, drgRouteDistributionStatementStatementsMatchCriteriaRepresentation2},
36+
"priority": Representation{repType: Required, create: `20`, update: `25`},
37+
}
38+
39+
drgRouteDistributionStatementRepresentation3 = map[string]interface{}{
40+
"drg_route_distribution_id": Representation{repType: Required, create: `${oci_core_drg_route_distribution.test_drg_route_distribution.id}`},
41+
"action": Representation{repType: Required, create: `ACCEPT`},
42+
"match_criteria": RepresentationGroup{Required, drgRouteDistributionStatementStatementsMatchCriteriaRepresentation3},
43+
"priority": Representation{repType: Required, create: `30`, update: `35`},
44+
}
45+
3246
drgRouteDistributionStatementStatementsMatchCriteriaRepresentation = map[string]interface{}{
3347
"match_type": Representation{repType: Required, create: `DRG_ATTACHMENT_TYPE`},
3448
"attachment_type": Representation{repType: Required, create: `VCN`, update: `VIRTUAL_CIRCUIT`},
3549
}
3650

51+
drgRouteDistributionStatementStatementsMatchCriteriaRepresentation2 = map[string]interface{}{
52+
"match_type": Representation{repType: Required, create: `DRG_ATTACHMENT_TYPE`},
53+
"attachment_type": Representation{repType: Required, create: `REMOTE_PEERING_CONNECTION`},
54+
}
55+
56+
drgRouteDistributionStatementStatementsMatchCriteriaRepresentation3 = map[string]interface{}{
57+
"match_type": Representation{repType: Required, create: `DRG_ATTACHMENT_ID`},
58+
"drg_attachment_id": Representation{repType: Required, create: `${oci_core_drg_attachment.test_drg_attachment2.id}`},
59+
}
60+
3761
DrgRouteDistributionStatementResourceDependencies = DefinedTagsDependencies +
62+
generateResourceFromRepresentationMap("oci_core_drg_attachment", "test_drg_attachment2", Required, Create, drgAttachmentRepresentation) +
3863
generateResourceFromRepresentationMap("oci_core_drg_route_distribution", "test_drg_route_distribution", Required, Create, drgRouteDistributionRepresentation) +
64+
generateResourceFromRepresentationMap("oci_core_vcn", "test_vcn", Required, Create, vcnRepresentation) +
65+
generateResourceFromRepresentationMap("oci_core_route_table", "test_route_table", Required, Create, routeTableRepresentation) +
3966
generateResourceFromRepresentationMap("oci_core_drg", "test_drg", Required, Create, drgRepresentation)
4067
)
4168

@@ -51,6 +78,9 @@ func TestCoreDrgRouteDistributionStatementResource_basic(t *testing.T) {
5178

5279
resourceName := "oci_core_drg_route_distribution_statement.test_drg_route_distribution_statement"
5380
datasourceName := "data.oci_core_drg_route_distribution_statements.test_drg_route_distribution_statements"
81+
resourceName1 := "oci_core_drg_route_distribution_statement.test_drg_route_distribution_statement2"
82+
resourceName2 := "oci_core_drg_route_distribution_statement.test_drg_route_distribution_statement3"
83+
resourceName3 := "oci_core_drg_route_distribution_statement.test_drg_route_distribution_statement4"
5484

5585
var resId, resId2 string
5686
// 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.
@@ -135,6 +165,47 @@ func TestCoreDrgRouteDistributionStatementResource_basic(t *testing.T) {
135165
ImportStateVerifyIgnore: []string{},
136166
ResourceName: resourceName,
137167
},
168+
// delete before next create
169+
{
170+
Config: config + compartmentIdVariableStr + DrgRouteDistributionStatementResourceDependencies,
171+
},
172+
// verify create
173+
{
174+
Config: config + compartmentIdVariableStr + DrgRouteDistributionStatementResourceDependencies +
175+
generateResourceFromRepresentationMap("oci_core_drg_route_distribution_statement", "test_drg_route_distribution_statement2", Optional, Create, drgRouteDistributionStatementRepresentation) +
176+
generateResourceFromRepresentationMap("oci_core_drg_route_distribution_statement", "test_drg_route_distribution_statement3", Required, Create, drgRouteDistributionStatementRepresentation2) +
177+
generateResourceFromRepresentationMap("oci_core_drg_route_distribution_statement", "test_drg_route_distribution_statement4", Required, Create, drgRouteDistributionStatementRepresentation3),
178+
Check: resource.ComposeAggregateTestCheckFunc(
179+
//check first resource
180+
resource.TestCheckResourceAttrSet(resourceName1, "drg_route_distribution_id"),
181+
resource.TestCheckResourceAttr(resourceName1, "action", "ACCEPT"),
182+
resource.TestCheckResourceAttr(resourceName1, "match_criteria.#", "1"),
183+
resource.TestCheckResourceAttr(resourceName1, "match_criteria.0.attachment_type", "VCN"),
184+
resource.TestCheckResourceAttr(resourceName1, "match_criteria.0.match_type", "DRG_ATTACHMENT_TYPE"),
185+
resource.TestCheckResourceAttr(resourceName1, "priority", "10"),
186+
resource.TestCheckResourceAttrSet(resourceName1, "id"),
187+
//check second resource
188+
resource.TestCheckResourceAttrSet(resourceName2, "drg_route_distribution_id"),
189+
resource.TestCheckResourceAttr(resourceName2, "action", "ACCEPT"),
190+
resource.TestCheckResourceAttr(resourceName2, "match_criteria.#", "1"),
191+
resource.TestCheckResourceAttr(resourceName2, "match_criteria.0.attachment_type", "REMOTE_PEERING_CONNECTION"),
192+
resource.TestCheckResourceAttr(resourceName2, "match_criteria.0.match_type", "DRG_ATTACHMENT_TYPE"),
193+
resource.TestCheckResourceAttr(resourceName2, "priority", "20"),
194+
resource.TestCheckResourceAttrSet(resourceName2, "id"),
195+
// check third resource
196+
resource.TestCheckResourceAttrSet(resourceName3, "drg_route_distribution_id"),
197+
resource.TestCheckResourceAttr(resourceName3, "action", "ACCEPT"),
198+
resource.TestCheckResourceAttr(resourceName3, "match_criteria.#", "1"),
199+
resource.TestCheckResourceAttrSet(resourceName3, "match_criteria.0.drg_attachment_id"),
200+
resource.TestCheckResourceAttr(resourceName3, "match_criteria.0.match_type", "DRG_ATTACHMENT_ID"),
201+
resource.TestCheckResourceAttr(resourceName3, "priority", "30"),
202+
resource.TestCheckResourceAttrSet(resourceName3, "id"),
203+
),
204+
},
205+
// delete
206+
{
207+
Config: config + compartmentIdVariableStr + DrgRouteDistributionStatementResourceDependencies,
208+
},
138209
},
139210
})
140211
}

oci/core_drg_route_table_route_rule_resource.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,19 @@ func (s *CoreDrgRouteTableRouteRuleResourceCrud) Create() error {
145145
}
146146

147147
if len(response.Items) > 0 {
148-
s.Res = &response.Items[0]
148+
for _, responseRouteRule := range response.Items {
149+
if responseRouteRule.DestinationType != oci_core.DrgRouteRuleDestinationTypeEnum(addDrgRouteRuleDetails.DestinationType) {
150+
continue
151+
}
152+
if *addDrgRouteRuleDetails.Destination != *responseRouteRule.Destination {
153+
continue
154+
}
155+
if *addDrgRouteRuleDetails.NextHopDrgAttachmentId != *responseRouteRule.NextHopDrgAttachmentId {
156+
continue
157+
}
158+
s.Res = &responseRouteRule
159+
break
160+
}
149161
} else {
150162
return fmt.Errorf("route rule missing in response")
151163
}
@@ -289,7 +301,15 @@ func (s *CoreDrgRouteTableRouteRuleResourceCrud) Update() error {
289301
return fmt.Errorf("failed to update route rules, error: %v", err)
290302
}
291303
if response.Items != nil && len(response.Items) > 0 {
292-
s.Res = &response.Items[0]
304+
_, drgRouteRuleId, err := parseDrgRouteTableRouteRuleCompositeId(s.D.Id())
305+
for _, routeRule := range response.Items {
306+
if *routeRule.Id == drgRouteRuleId {
307+
s.Res = &routeRule
308+
}
309+
}
310+
if err != nil {
311+
return fmt.Errorf("failed to update route rules, error: %v", err)
312+
}
293313
}
294314

295315
return nil

oci/core_drg_route_table_route_rule_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,22 @@ var (
2929
"next_hop_drg_attachment_id": Representation{repType: Required, create: `${oci_core_drg_attachment.test_drg_attachment.id}`},
3030
}
3131

32+
drgRouteTableRouteRuleRepresentation2 = map[string]interface{}{
33+
"drg_route_table_id": Representation{repType: Required, create: `${oci_core_drg_route_table.test_drg_route_table.id}`},
34+
"destination_type": Representation{repType: Required, create: `CIDR_BLOCK`},
35+
"destination": Representation{repType: Required, create: `1.1.1.0/24`, update: `1.1.11.0/24`},
36+
"next_hop_drg_attachment_id": Representation{repType: Required, create: `${oci_core_drg_attachment.test_drg_attachment.id}`},
37+
}
38+
39+
drgRouteTableRouteRuleRepresentation3 = map[string]interface{}{
40+
"drg_route_table_id": Representation{repType: Required, create: `${oci_core_drg_route_table.test_drg_route_table.id}`},
41+
"destination_type": Representation{repType: Required, create: `CIDR_BLOCK`},
42+
"destination": Representation{repType: Required, create: `1.1.2.0/24`, update: `1.1.12.0/24`},
43+
"next_hop_drg_attachment_id": Representation{repType: Required, create: `${oci_core_drg_attachment.test_drg_attachment.id}`},
44+
}
45+
3246
DrgRouteTableRouteRuleResourceDependencies = generateResourceFromRepresentationMap("oci_core_drg_attachment", "test_drg_attachment", Required, Create, drgAttachmentRepresentation) +
47+
generateResourceFromRepresentationMap("oci_core_drg_attachment", "test_drg_attachment", Required, Create, drgAttachmentRepresentation) +
3348
generateResourceFromRepresentationMap("oci_core_drg_route_table", "test_drg_route_table", Required, Create, drgRouteTableRepresentation) +
3449
generateResourceFromRepresentationMap("oci_core_drg", "test_drg", Required, Create, drgRepresentation) +
3550
generateResourceFromRepresentationMap("oci_core_vcn", "test_vcn", Required, Create, vcnRepresentation) +
@@ -49,6 +64,9 @@ func TestCoreDrgRouteTableRouteRuleResource_basic(t *testing.T) {
4964
resourceName := "oci_core_drg_route_table_route_rule.test_drg_route_table_route_rule"
5065
datasourceName := "data.oci_core_drg_route_table_route_rules.test_drg_route_table_route_rules"
5166

67+
resourceName2 := "oci_core_drg_route_table_route_rule.test_drg_route_table_route_rule2"
68+
resourceName3 := "oci_core_drg_route_table_route_rule.test_drg_route_table_route_rule3"
69+
5270
var resId, resId2 string
5371
// 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.
5472
saveConfigContent(config+compartmentIdVariableStr+DrgRouteTableRouteRuleResourceDependencies+
@@ -136,6 +154,30 @@ func TestCoreDrgRouteTableRouteRuleResource_basic(t *testing.T) {
136154
ImportStateVerifyIgnore: []string{},
137155
ResourceName: resourceName,
138156
},
157+
// delete before next create
158+
{
159+
Config: config + compartmentIdVariableStr + DrgRouteTableRouteRuleResourceDependencies,
160+
},
161+
// verify create
162+
{
163+
Config: config + compartmentIdVariableStr + DrgRouteTableRouteRuleResourceDependencies +
164+
generateResourceFromRepresentationMap("oci_core_drg_route_table_route_rule", "test_drg_route_table_route_rule2", Optional, Create, drgRouteTableRouteRuleRepresentation2) +
165+
generateResourceFromRepresentationMap("oci_core_drg_route_table_route_rule", "test_drg_route_table_route_rule3", Required, Create, drgRouteTableRouteRuleRepresentation3),
166+
Check: resource.ComposeAggregateTestCheckFunc(
167+
//check first resource
168+
resource.TestCheckResourceAttrSet(resourceName2, "destination"),
169+
resource.TestCheckResourceAttrSet(resourceName2, "destination_type"),
170+
resource.TestCheckResourceAttrSet(resourceName2, "drg_route_table_id"),
171+
resource.TestCheckResourceAttrSet(resourceName2, "id"),
172+
resource.TestCheckResourceAttrSet(resourceName2, "next_hop_drg_attachment_id"),
173+
//check second resource
174+
resource.TestCheckResourceAttrSet(resourceName3, "destination"),
175+
resource.TestCheckResourceAttrSet(resourceName3, "destination_type"),
176+
resource.TestCheckResourceAttrSet(resourceName3, "drg_route_table_id"),
177+
resource.TestCheckResourceAttrSet(resourceName3, "id"),
178+
resource.TestCheckResourceAttrSet(resourceName3, "next_hop_drg_attachment_id"),
179+
),
180+
},
139181
},
140182
})
141183
}

0 commit comments

Comments
 (0)