aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/GeneratedEndpoint.java
blob: 28f9963f24c5bd336e6631fe17f0d49f0b9b4088 (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
package com.yahoo.vespa.hosted.controller.application;

import ai.vespa.validation.Validation;
import com.yahoo.config.provision.zone.AuthMethod;

import java.util.Objects;
import java.util.Optional;
import java.util.random.RandomGenerator;
import java.util.regex.Pattern;

/**
 * A system-generated endpoint, where the cluster and application parts are randomly generated. These become the
 * first and second part of an endpoint name. See {@link Endpoint}.
 *
 * @author mpolden
 */
public record GeneratedEndpoint(String clusterPart, String applicationPart, AuthMethod authMethod, Optional<EndpointId> endpoint) {

    private static final Pattern PART_PATTERN = Pattern.compile("^[a-f][a-f0-9]{7}$");

    public GeneratedEndpoint {
        Objects.requireNonNull(clusterPart);
        Objects.requireNonNull(applicationPart);
        Objects.requireNonNull(authMethod);
        Objects.requireNonNull(endpoint);

        Validation.requireMatch(clusterPart, "Cluster part", PART_PATTERN);
        Validation.requireMatch(applicationPart, "Application part", PART_PATTERN);
    }

    /** Returns whether this was generated for an endpoint declared in {@link com.yahoo.config.application.api.DeploymentSpec} */
    public boolean declared() {
        return endpoint.isPresent();
    }

    /** Returns whether this was generated for a cluster declared in {@link com.yahoo.vespa.hosted.controller.application.pkg.BasicServicesXml} */
    public boolean cluster() {
        return !declared();
    }

    /** Returns a copy of this with cluster part set to given value */
    public GeneratedEndpoint withClusterPart(String clusterPart) {
        return new GeneratedEndpoint(clusterPart, applicationPart, authMethod, endpoint);
    }

    /** Create a new endpoint part, using random as a source of randomness */
    public static String createPart(RandomGenerator random) {
        String alphabet = "abcdef0123456789";
        StringBuilder sb = new StringBuilder();
        sb.append(alphabet.charAt(random.nextInt(6))); // Start with letter
        for (int i = 0; i < 7; i++) {
            sb.append(alphabet.charAt(random.nextInt(alphabet.length())));
        }
        return sb.toString();
    }

}