aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/ZoneRegistryMock.java
blob: 53af74bf5426c8fc94a4305cd0632cb0324e15e2 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller;

import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneRegistry;

import java.net.URI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author mpolden
 */
public class ZoneRegistryMock extends AbstractComponent implements ZoneRegistry {

    private final Map<ZoneId, Duration> deploymentTimeToLive = new HashMap<>();
    private final Map<Environment, RegionName> defaultRegionForEnvironment = new HashMap<>();
    private List<ZoneId> zones = new ArrayList<>();
    private SystemName system = SystemName.main;

    @Inject
    public ZoneRegistryMock() {
        this.zones.add(ZoneId.from("prod", "corp-us-east-1"));
        this.zones.add(ZoneId.from("prod", "us-east-3"));
        this.zones.add(ZoneId.from("prod", "us-west-1"));
    }

    public ZoneRegistryMock setDeploymentTimeToLive(ZoneId zone, Duration duration) {
        deploymentTimeToLive.put(zone, duration);
        return this;
    }

    public ZoneRegistryMock setDefaultRegionForEnvironment(Environment environment, RegionName region) {
        defaultRegionForEnvironment.put(environment, region);
        return this;
    }

    public ZoneRegistryMock setZones(List<ZoneId> zones) {
        this.zones = zones;
        return this;
    }

    public ZoneRegistryMock setSystem(SystemName system) {
        this.system = system;
        return this;
    }

    @Override
    public SystemName system() {
        return system;
    }

    @Override
    public List<ZoneId> zones() {
        return Collections.unmodifiableList(zones);
    }

    @Override
    public Optional<ZoneId> getZone(Environment environment, RegionName region) {
        return zones().stream().filter(z -> z.environment().equals(environment) &&
                                            z.region().equals(region)).findFirst();
    }

    @Override
    public List<URI> getConfigServerUris(Environment environment, RegionName region) {
        return getZone(environment, region)
                .map(z -> URI.create(String.format("http://cfg.%s.%s.test", environment.value(), region.value())))
                .map(Collections::singletonList)
                .orElse(Collections.emptyList());
    }

    @Override
    public Optional<URI> getLogServerUri(Environment environment, RegionName region) {
        return getZone(environment, region)
                .map(z -> URI.create(String.format("http://log.%s.%s.test", environment.value(), region.value())));
    }

    @Override
    public Optional<Duration> getDeploymentTimeToLive(Environment environment, RegionName region) {
        return Optional.ofNullable(deploymentTimeToLive.get(ZoneId.from(environment, region)));
    }

    @Override
    public Optional<RegionName> getDefaultRegion(Environment environment) {
        return Optional.ofNullable(defaultRegionForEnvironment.get(environment));
    }

    @Override
    public URI getMonitoringSystemUri(Environment environment, RegionName name, ApplicationId application) {
        return URI.create("http://monitoring-system.test/?environment=" + environment.value() + "&region="
                                  + name.value() + "&application=" + application.toShortString());
    }

    @Override
    public URI getDashboardUri() {
        return URI.create("http://dashboard.test");
    }

}