implemented rule action modification

This commit is contained in:
Niklas Opiela
2022-12-08 15:35:16 +01:00
parent b90b065946
commit 871182bd81
4 changed files with 47 additions and 0 deletions
@@ -148,4 +148,6 @@ public interface ApplicationLoadBalancer<ShardingKey> extends Named {
RuleCondition createHostHeaderRuleCondition(String hostname);
java.util.Collection<Rule> getRulesForTargetGroups(Iterable<TargetGroup<ShardingKey>> targetGroups);
Iterable<Rule> replaceTargetGroupInForwardRules(TargetGroup<ShardingKey> oldTargetGroup, TargetGroup<ShardingKey> newTargetGroup );
}
@@ -517,6 +517,8 @@ public interface AwsLandscape<ShardingKey> extends Landscape<ShardingKey> {
Iterable<Rule> getLoadBalancerListenerRules(Listener loadBalancerListener, Region region);
public Iterable<Rule> modifyRuleConditions(Region region, Rule rule);
public Iterable<Rule> modifyRuleActions(Region region, Rule rule);
/**
* Use {@link Rule.Builder} to create {@link Rule} objects you'd like to set for the {@link Listener} passed as parameter.
@@ -31,6 +31,7 @@ import software.amazon.awssdk.services.elasticloadbalancingv2.model.RedirectActi
import software.amazon.awssdk.services.elasticloadbalancingv2.model.Rule;
import software.amazon.awssdk.services.elasticloadbalancingv2.model.RuleCondition;
import software.amazon.awssdk.services.elasticloadbalancingv2.model.RulePriorityPair;
import software.amazon.awssdk.services.elasticloadbalancingv2.model.TargetGroupTuple;
public class ApplicationLoadBalancerImpl<ShardingKey>
implements ApplicationLoadBalancer<ShardingKey> {
@@ -395,6 +396,41 @@ implements ApplicationLoadBalancer<ShardingKey> {
}
return ret;
}
/**
* Goes through all rules in this load balancer and replaces every target group in a forward-action that has the
* same ARN as the {@code oldTargetgroup} with the {@code newTargetgroup}
*/
@Override
public Iterable<Rule> replaceTargetGroupInForwardRules(TargetGroup<ShardingKey> oldTargetGroup,
TargetGroup<ShardingKey> newTargetGroup) {
Iterable<Rule> rules = getRules();
Collection<Rule> modifiedRules = new ArrayList<>();
for (Rule r : rules) {
int i = 0;
boolean modified = false;
Action[] newActions = new Action[r.actions().size()];
for (Action a : r.actions()) {
if (a.forwardConfig() != null && a.targetGroupArn().equals(oldTargetGroup.getTargetGroupArn())) {
newActions[i++] = createForwardToTargetGroupAction(newTargetGroup);
modified = true;
} else {
newActions[i++] = a;
}
}
if(modified) {
landscape.modifyRuleActions(region, Rule.builder().actions(newActions).ruleArn(r.ruleArn()).build())
.forEach(t -> modifiedRules.add(t));
}
}
return modifiedRules;
}
private Action createForwardToTargetGroupAction(TargetGroup<ShardingKey> targetGroup) {
return Action.builder().type(ActionTypeEnum.FORWARD).forwardConfig(fc -> fc
.targetGroups(TargetGroupTuple.builder().targetGroupArn(targetGroup.getTargetGroupArn()).build()))
.build();
}
}
@@ -2039,4 +2039,11 @@ public class AwsLandscapeImpl<ShardingKey> implements AwsLandscape<ShardingKey>
public long getDNSTTLInSeconds() {
return DEFAULT_DNS_TTL_SECONDS;
}
@Override
public Iterable<Rule> modifyRuleActions(com.sap.sse.landscape.Region region, Rule rule) {
ModifyRuleResponse res = getLoadBalancingClient(getRegion(region))
.modifyRule(t -> t.actions(rule.actions()).ruleArn(rule.ruleArn()).build());
return res.rules();
}
}