blob: 5f75d6105b59720e848c5904002fbc3e90f044c5 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
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 CLUSTER_PART_PATTERN = Pattern.compile("^([a-f][a-f0-9]{7}|\\*)$");
private static final Pattern APPLICATION_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", CLUSTER_PART_PATTERN);
Validation.requireMatch(applicationPart, "Application part", APPLICATION_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();
}
}
|