@Override
public DnsZone createZone(Project project, DnsZoneSpec spec) throws CloudException, DuplicateValueException {
String zoneName = spec.name;
zoneName = zoneName.toLowerCase();
DnsSuffixData suffixData = findMaximalSuffix(zoneName);
if (suffixData == null) {
throw new IllegalArgumentException("Unsupported suffix: " + zoneName);
}
if (suffixData.getTld() && !canCreateInTld) {
throw new IllegalArgumentException("Creation of domains blocked (under TLDs)");
}
String backendKey = null;
String zone;
// TODO: Is there any point in allowing a shared domain but then
// blocking creation under it?
// if (suffixData.getSharedDomain() && !canCreateInShared) {
// throw new
// IllegalArgumentException("Creation of domains blocked (under shared domains)");
// }
String suffix = suffixData.getKey();
String prefix = zoneName.substring(0, zoneName.length() - suffix.length());
prefix = CharMatcher.is('.').trimTrailingFrom(prefix);
if (prefix.isEmpty()) {
throw new IllegalArgumentException("Unsupported domain (TLD or marked as shared): " + zoneName);
} else {
int dotIndex = prefix.indexOf('.');
String prefixTail;
if (dotIndex != -1) {
prefixTail = prefix.substring(dotIndex + 1);
} else {
prefixTail = prefix;
}
// If this is a shared domain, try to assign it to the user
if (suffixData.getSharedDomain()) {
NamedItemCollection<DnsSuffixData> store = repository.getSharedSubdomains(suffixData.getKey());
DnsSuffixData allocation = store.find(prefixTail);
if (allocation == null) {
DnsSuffixData.Builder b = DnsSuffixData.newBuilder();
b.setKey(prefixTail);
b.setOwnerProject(project.getId());
allocation = store.create(b);
} else {
if (allocation.getOwnerProject() != project.getId()) {
// Already assigned to someone else
throw new DuplicateValueException();
}
}
}