Flatten the hosts flows

There's so little meat here that there's not much
reason to break this cl up any further

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133171754
This commit is contained in:
cgoldfeder 2016-07-10 14:10:42 -04:00 committed by Ben McIlwain
parent 00ea99960a
commit 516b5663a5
12 changed files with 336 additions and 210 deletions

View file

@ -18,23 +18,34 @@ import static google.registry.flows.host.HostFlowUtils.lookupSuperordinateDomain
import static google.registry.flows.host.HostFlowUtils.validateHostName;
import static google.registry.flows.host.HostFlowUtils.verifyDomainIsSameRegistrar;
import static google.registry.model.EppResourceUtils.createContactHostRoid;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.eppoutput.Result.Code.Success;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.CollectionUtils.isNullOrEmpty;
import static google.registry.util.CollectionUtils.union;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import google.registry.dns.DnsQueue;
import google.registry.flows.EppException;
import google.registry.flows.EppException.ParameterValueRangeErrorException;
import google.registry.flows.EppException.RequiredParameterMissingException;
import google.registry.flows.ResourceCreateFlow;
import google.registry.flows.FlowModule.ClientId;
import google.registry.flows.LoggedInFlow;
import google.registry.flows.TransactionalFlow;
import google.registry.flows.exceptions.ResourceAlreadyExistsException;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.metadata.MetadataExtension;
import google.registry.model.eppinput.ResourceCommand;
import google.registry.model.eppoutput.CreateData.HostCreateData;
import google.registry.model.eppoutput.EppOutput;
import google.registry.model.host.HostCommand.Create;
import google.registry.model.host.HostResource;
import google.registry.model.host.HostResource.Builder;
import google.registry.model.index.EppResourceIndex;
import google.registry.model.index.ForeignKeyIndex;
import google.registry.model.ofy.ObjectifyService;
import google.registry.model.reporting.HistoryEntry;
import javax.inject.Inject;
@ -43,7 +54,7 @@ import javax.inject.Inject;
* An EPP flow that creates a new host resource.
*
* @error {@link google.registry.flows.EppXmlTransformer.IpAddressVersionMismatchException}
* @error {@link google.registry.flows.ResourceCreateFlow.ResourceAlreadyExistsException}
* @error {@link google.registry.flows.exceptions.ResourceAlreadyExistsException}
* @error {@link HostFlowUtils.HostNameTooLongException}
* @error {@link HostFlowUtils.HostNameTooShallowException}
* @error {@link HostFlowUtils.InvalidHostNameException}
@ -51,37 +62,33 @@ import javax.inject.Inject;
* @error {@link SubordinateHostMustHaveIpException}
* @error {@link UnexpectedExternalHostIpException}
*/
public class HostCreateFlow extends ResourceCreateFlow<HostResource, Builder, Create> {
/**
* The superordinate domain of the host object if creating an in-bailiwick host, or null if
* creating an external host. This is looked up before we actually create the Host object so that
* we can detect error conditions earlier. By the time {@link #setCreateProperties} is called
* (where this reference is actually used), we no longer have the ability to return an
* {@link EppException}.
*
* <p>The general model of these classes is to do validation of parameters up front before we get
* to the actual object creation, which is why this class looks up and stores the superordinate
* domain ahead of time.
*/
private Optional<Key<DomainResource>> superordinateDomain;
public class HostCreateFlow extends LoggedInFlow implements TransactionalFlow {
@Inject ResourceCommand resourceCommand;
@Inject @ClientId String clientId;
@Inject HistoryEntry.Builder historyBuilder;
@Inject HostCreateFlow() {}
@Override
protected void initResourceCreateOrMutateFlow() throws EppException {
superordinateDomain = Optional.fromNullable(lookupSuperordinateDomain(
validateHostName(command.getFullyQualifiedHostName()), now));
@SuppressWarnings("unchecked")
protected final void initLoggedInFlow() throws EppException {
registerExtensions(MetadataExtension.class);
}
@Override
protected String createFlowRepoId() {
return createContactHostRoid(ObjectifyService.allocateId());
}
@Override
protected void verifyCreateIsAllowed() throws EppException {
verifyDomainIsSameRegistrar(superordinateDomain.orNull(), getClientId());
protected final EppOutput run() throws EppException {
Create command = (Create) resourceCommand;
String targetId = command.getTargetId();
HostResource existingResource = loadByUniqueId(HostResource.class, targetId, now);
if (existingResource != null) {
throw new ResourceAlreadyExistsException(targetId);
}
// The superordinate domain of the host object if creating an in-bailiwick host, or null if
// creating an external host. This is looked up before we actually create the Host object so
// we can detect error conditions earlier.
Optional<DomainResource> superordinateDomain = Optional.fromNullable(
lookupSuperordinateDomain(validateHostName(command.getFullyQualifiedHostName()), now));
verifyDomainIsSameRegistrar(superordinateDomain.orNull(), clientId);
boolean willBeSubordinate = superordinateDomain.isPresent();
boolean hasIpAddresses = !isNullOrEmpty(command.getInetAddresses());
if (willBeSubordinate != hasIpAddresses) {
@ -90,43 +97,36 @@ public class HostCreateFlow extends ResourceCreateFlow<HostResource, Builder, Cr
? new SubordinateHostMustHaveIpException()
: new UnexpectedExternalHostIpException();
}
}
@Override
protected void setCreateProperties(Builder builder) {
Builder builder = new Builder();
command.applyTo(builder);
HostResource newResource = builder
.setCreationClientId(clientId)
.setCurrentSponsorClientId(clientId)
.setRepoId(createContactHostRoid(ObjectifyService.allocateId()))
.setSuperordinateDomain(
superordinateDomain.isPresent() ? Key.create(superordinateDomain.get()) : null)
.build();
historyBuilder
.setType(HistoryEntry.Type.HOST_CREATE)
.setModificationTime(now)
.setParent(Key.create(newResource));
ImmutableSet<ImmutableObject> entitiesToSave = ImmutableSet.of(
newResource,
historyBuilder.build(),
ForeignKeyIndex.create(newResource, newResource.getDeletionTime()),
EppResourceIndex.create(Key.create(newResource)));
if (superordinateDomain.isPresent()) {
builder.setSuperordinateDomain(superordinateDomain.get());
entitiesToSave = union(
entitiesToSave,
superordinateDomain.get().asBuilder()
.addSubordinateHost(command.getFullyQualifiedHostName())
.build());
// Only update DNS if this is a subordinate host. External hosts have no glue to write, so
// they are only written as NS records from the referencing domain.
DnsQueue.create().addHostRefreshTask(targetId);
}
}
/** Modify any other resources that need to be informed of this create. */
@Override
protected void modifyCreateRelatedResources() {
if (superordinateDomain.isPresent()) {
ofy().save().entity(ofy().load().key(superordinateDomain.get()).now().asBuilder()
.addSubordinateHost(command.getFullyQualifiedHostName())
.build());
}
}
@Override
protected void enqueueTasks() {
// Only update DNS if this is a subordinate host. External hosts have no glue to write, so they
// are only written as NS records from the referencing domain.
if (superordinateDomain.isPresent()) {
DnsQueue.create().addHostRefreshTask(newResource.getFullyQualifiedHostName());
}
}
@Override
protected final HistoryEntry.Type getHistoryEntryType() {
return HistoryEntry.Type.HOST_CREATE;
}
@Override
protected EppOutput getOutput() {
return createOutput(Success,
HostCreateData.create(newResource.getFullyQualifiedHostName(), now));
ofy().save().entities(entitiesToSave);
return createOutput(Success, HostCreateData.create(targetId, now));
}
/** Subordinate hosts must have an ip address. */