aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/lb/LoadBalancerServiceMock.java
blob: 51f59bc3ea09f204dd252f7bb50035938f33ba05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.lb;

import com.google.common.collect.ImmutableSet;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeType;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author mpolden
 */
public class LoadBalancerServiceMock implements LoadBalancerService {

    private final Map<LoadBalancerId, LoadBalancerInstance> instances = new HashMap<>();
    private boolean throwOnCreate = false;
    private boolean supportsProvisioning = true;

    public Map<LoadBalancerId, LoadBalancerInstance> instances() {
        return Collections.unmodifiableMap(instances);
    }

    public LoadBalancerServiceMock throwOnCreate(boolean throwOnCreate) {
        this.throwOnCreate = throwOnCreate;
        return this;
    }

    public LoadBalancerServiceMock supportsProvisioning(boolean supportsProvisioning) {
        this.supportsProvisioning = supportsProvisioning;
        return this;
    }

    @Override
    public boolean supports(NodeType nodeType, ClusterSpec.Type clusterType) {
        if (!supportsProvisioning) return false;
        return (nodeType == NodeType.tenant && clusterType.isContainer()) ||
               nodeType.isConfigServerLike();
    }

    @Override
    public Protocol protocol() {
        return Protocol.ipv4;
    }

    @Override
    public LoadBalancerInstance create(LoadBalancerSpec spec, boolean force) {
        if (throwOnCreate) throw new IllegalStateException("Did not expect a new load balancer to be created");
        var id = new LoadBalancerId(spec.application(), spec.cluster());
        var oldInstance = instances.get(id);
        if (!force && oldInstance != null && !oldInstance.reals().isEmpty() && spec.reals().isEmpty()) {
            throw new IllegalArgumentException("Refusing to remove all reals from load balancer " + id);
        }
        var instance = new LoadBalancerInstance(
                HostName.from("lb-" + spec.application().toShortString() + "-" + spec.cluster().value()),
                Optional.of(new DnsZone("zone-id-1")),
                Collections.singleton(4443),
                ImmutableSet.of("10.2.3.0/24", "10.4.5.0/24"),
                spec.reals());
        instances.put(id, instance);
        return instance;
    }

    @Override
    public void remove(ApplicationId application, ClusterSpec.Id cluster) {
        instances.remove(new LoadBalancerId(application, cluster));
    }

}