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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Identifies an account in a public cloud, such as {@link CloudName#AWS} or {@link CloudName#GCP}.
*
* @author mpolden
*/
public class CloudAccount implements Comparable<CloudAccount> {
private record CloudMeta(String accountType, Pattern pattern) {
private boolean matches(String account) { return pattern.matcher(account).matches(); }
}
private static final Map<String, CloudMeta> META_BY_CLOUD = Map.of(
"aws", new CloudMeta("Account ID", Pattern.compile("[0-9]{12}")),
"gcp", new CloudMeta("Project ID", Pattern.compile("[a-z][a-z0-9-]{4,28}[a-z0-9]")));
/** Empty value. When this is used, either implicitly or explicitly, the zone will use its default account */
public static final CloudAccount empty = new CloudAccount("", CloudName.DEFAULT);
private final String account;
private final CloudName cloudName;
private CloudAccount(String account, CloudName cloudName) {
this.account = account;
this.cloudName = cloudName;
}
public String account() { return account; }
public CloudName cloudName() { return cloudName; }
/** Returns the serialized value of this account that can be deserialized with {@link CloudAccount#from} */
public final String value() {
if (isUnspecified()) return account;
return cloudName.value() + ':' + account;
}
public boolean isUnspecified() {
return this.equals(empty);
}
/** Returns true if this is an exclave account. */
public boolean isExclave(Zone zone) {
return !isUnspecified() &&
zone.system().isPublic() &&
!equals(zone.cloud().account());
}
@Override
public String toString() {
return isUnspecified() ? "unspecified account" : "account '" + account + "' in " + cloudName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CloudAccount that = (CloudAccount) o;
return account.equals(that.account) && cloudName.equals(that.cloudName);
}
@Override
public int hashCode() {
return Objects.hash(account, cloudName);
}
@Override
public int compareTo(CloudAccount o) {
return this.value().compareTo(o.value());
}
public static CloudAccount from(String cloudAccount) {
int index = cloudAccount.indexOf(':');
if (index < 0) {
// Tenants are allowed to specify "default" in services.xml.
if (cloudAccount.isEmpty() || cloudAccount.equals("default"))
return empty;
if (META_BY_CLOUD.get("aws").matches(cloudAccount))
return new CloudAccount(cloudAccount, CloudName.AWS);
throw illegal(cloudAccount, "Must be on format '<cloud-name>:<account>' or 'default'");
}
String cloud = cloudAccount.substring(0, index);
String account = cloudAccount.substring(index + 1);
CloudMeta cloudMeta = META_BY_CLOUD.get(cloud);
if (cloudMeta == null)
throw illegal(cloudAccount, "Cloud name must be one of: " + META_BY_CLOUD.keySet().stream().sorted().collect(Collectors.joining(", ")));
if (!cloudMeta.matches(account))
throw illegal(cloudAccount, cloudMeta.accountType + " must match '" + cloudMeta.pattern.pattern() + "'");
return new CloudAccount(account, CloudName.from(cloud));
}
private static IllegalArgumentException illegal(String cloudAccount, String details) {
return new IllegalArgumentException("Invalid cloud account '" + cloudAccount + "': " + details);
}
}
|