Skip to content

Commit

Permalink
fix: Update ModifyListeners and ModifyRules for Selective Action Modi…
Browse files Browse the repository at this point in the history
…fication

Signed-off-by: moko-poi <[email protected]>
  • Loading branch information
moko-poi committed Nov 26, 2023
1 parent 38855ce commit 2650922
Showing 1 changed file with 54 additions and 67 deletions.
121 changes: 54 additions & 67 deletions pkg/app/piped/platformprovider/ecs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,24 +462,21 @@ func (c *client) ModifyListeners(ctx context.Context, listenerArns []string, rou
return fmt.Errorf("invalid listener configuration: requires 2 target groups")
}

// Describe the rule to get current actions
describelistenerOutput, err := c.elbClient.DescribeRules(ctx, &elasticloadbalancingv2.DescribeRulesInput{
RuleArns: listenerArns,
})
if err != nil {
return fmt.Errorf("error describing listener %v: %w", strings.Join(listenerArns, ", "), err)
}

// No rules found, nothing to modify
if len(describelistenerOutput.Rules) == 0 {
return fmt.Errorf("no listener found for ARN: %s", strings.Join(listenerArns, ", "))
}
for _, listenerArn := range listenerArns {
// Describe the listener to get the current actions
describeListenersOutput, err := c.elbClient.DescribeListeners(ctx, &elasticloadbalancingv2.DescribeListenersInput{
ListenerArns: []string{listenerArn},
})
if err != nil {
return fmt.Errorf("error describing listener %s: %w", listenerArn, err)
}

Check warning on line 472 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L465-L472

Added lines #L465 - L472 were not covered by tests

modifyListener := func(ctx context.Context, listenerArn string) error {
input := &elasticloadbalancingv2.ModifyListenerInput{
ListenerArn: aws.String(listenerArn),
DefaultActions: []elbtypes.Action{
{
// Prepare the actions to be modified
var modifiedActions []elbtypes.Action
for _, action := range describeListenersOutput.Listeners[0].DefaultActions {
if action.Type == elbtypes.ActionTypeEnumForward {
// Modify only the forward action (new logic)
modifiedAction := elbtypes.Action{

Check warning on line 479 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L475-L479

Added lines #L475 - L479 were not covered by tests
Type: elbtypes.ActionTypeEnumForward,
ForwardConfig: &elbtypes.ForwardActionConfig{
TargetGroups: []elbtypes.TargetGroupTuple{
Expand All @@ -493,25 +490,22 @@ func (c *client) ModifyListeners(ctx context.Context, listenerArns []string, rou
},
},
},
},
},
}
_, err := c.elbClient.ModifyListener(ctx, input)
return err
}

for _, listener := range listenerArns {
// Check if the current action type is forward
for _, rule := range describelistenerOutput.Rules {
for _, action := range rule.Actions {
if action.Type == elbtypes.ActionTypeEnumForward {
// Call modifyListenerRule only if action type is forward
if err := modifyListener(ctx, listener); err != nil {
return err
}
}
modifiedActions = append(modifiedActions, modifiedAction)
} else {
// Keep other actions unchanged (new logic)
modifiedActions = append(modifiedActions, action)
}

Check warning on line 498 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L493-L498

Added lines #L493 - L498 were not covered by tests
}

// Modify the listener
_, err = c.elbClient.ModifyListener(ctx, &elasticloadbalancingv2.ModifyListenerInput{
ListenerArn: aws.String(listenerArn),
DefaultActions: modifiedActions,
})
if err != nil {
return fmt.Errorf("error modifying listener %s: %w", listenerArn, err)
}

Check warning on line 508 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L502-L508

Added lines #L502 - L508 were not covered by tests
}
return nil

Check warning on line 510 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L510

Added line #L510 was not covered by tests
}
Expand All @@ -521,24 +515,21 @@ func (c *client) ModifyRules(ctx context.Context, listenerRuleArns []string, rou
return fmt.Errorf("invalid listener configuration: requires 2 target groups")

Check warning on line 515 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L513-L515

Added lines #L513 - L515 were not covered by tests
}

// Describe the rule to get current actions
describeRulesOutput, err := c.elbClient.DescribeRules(ctx, &elasticloadbalancingv2.DescribeRulesInput{
RuleArns: listenerRuleArns,
})
if err != nil {
return fmt.Errorf("error describing listener rule %v: %w", strings.Join(listenerRuleArns, ", "), err)
}

// No rules found, nothing to modify
if len(describeRulesOutput.Rules) == 0 {
return fmt.Errorf("no rules found for ARN: %s", strings.Join(listenerRuleArns, ", "))
}
for _, ruleArn := range listenerRuleArns {
// Describe the rule to get current actions
describeRulesOutput, err := c.elbClient.DescribeRules(ctx, &elasticloadbalancingv2.DescribeRulesInput{
RuleArns: []string{ruleArn},
})
if err != nil {
return fmt.Errorf("error describing listener rule %v: %w", strings.Join(listenerRuleArns, ", "), err)
}

Check warning on line 525 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L518-L525

Added lines #L518 - L525 were not covered by tests

modifyListenerRule := func(ctx context.Context, listenerRuleArn string) error {
input := &elasticloadbalancingv2.ModifyRuleInput{
RuleArn: aws.String(listenerRuleArn),
Actions: []elbtypes.Action{
{
// Prepare the actions to be modified
var modifiedActions []elbtypes.Action
for _, action := range describeRulesOutput.Rules[0].Actions {
if action.Type == elbtypes.ActionTypeEnumForward {
// Modify only the forward action (new logic)
modifiedAction := elbtypes.Action{
Type: elbtypes.ActionTypeEnumForward,
ForwardConfig: &elbtypes.ForwardActionConfig{
TargetGroups: []elbtypes.TargetGroupTuple{
Expand All @@ -552,27 +543,23 @@ func (c *client) ModifyRules(ctx context.Context, listenerRuleArns []string, rou
},
},
},
},
},
}
_, err := c.elbClient.ModifyRule(ctx, input)
return err
}

for _, ruleArn := range listenerRuleArns {
// Check if the current action type is forward
for _, rule := range describeRulesOutput.Rules {
for _, action := range rule.Actions {
if action.Type == elbtypes.ActionTypeEnumForward {
// Call modifyListenerRule only if action type is forward
if err := modifyListenerRule(ctx, ruleArn); err != nil {
return err
}
}
modifiedActions = append(modifiedActions, modifiedAction)
} else {
// Keep other actions unchanged (new logic)
modifiedActions = append(modifiedActions, action)
}

Check warning on line 551 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L528-L551

Added lines #L528 - L551 were not covered by tests
}
}

// Modify the rule with the new actions
_, err = c.elbClient.ModifyRule(ctx, &elasticloadbalancingv2.ModifyRuleInput{
RuleArn: aws.String(ruleArn),
Actions: modifiedActions,
})
if err != nil {
return fmt.Errorf("error modifying listener rule %s: %w", ruleArn, err)

Check warning on line 560 in pkg/app/piped/platformprovider/ecs/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/ecs/client.go#L555-L560

Added lines #L555 - L560 were not covered by tests
}
}
return nil
}

Expand Down

0 comments on commit 2650922

Please sign in to comment.