if (networkMap.findHostGroupByKey(key) != null) {
throw new IllegalArgumentException("Host group already exists");
}
HostGroupData parent = null;
if (parentKey != null) {
parent = networkMap.findHostGroupByKey(parentKey);
if (parent == null) {
throw new IllegalArgumentException("Specified parent not found");
}
}
HostGroupData.Builder b = HostGroupData.newBuilder();
if (label != null) {
b.setLabel(label);
}
b.setKey(key);
type = type.toLowerCase().trim();
if (type.equals("ec2")) {
b.setHostGroupType(HostGroupType.HOST_GROUP_TYPE_AMAZON_EC2);
} else if (type.equals("bare")) {
b.setHostGroupType(HostGroupType.HOST_GROUP_TYPE_RAW);
} else {
throw new IllegalArgumentException("Expected type to be 'bare' or 'ec2'");
}
{
HostGroupSecretData.Builder sb = HostGroupSecretData.newBuilder();
// We're going to rely on IAM ... so much easier + more secure
// if (username != null) {
// sb.setUsername(username);
// }
//
// if (password != null) {
// sb.setPassword(password);
// }
SecretData secretData = computeSecrets.encrypt(sb.build());
b.setSecretData(secretData);
}
if (parent != null) {
b.setParent(parent.getId());
// IpRange parentRange = IpRange.parse(parent.getCidr());
// if (!containsStrict(parentRange, range)) {
// throw new
// IllegalArgumentException("Child CIDR must be a sub-range of the parent range");
// }
}
HostGroupData created = networkMap.createHostGroup(b);
return created;
}