1
0
mirror of https://github.com/google/nomulus synced 2026-06-09 00:13:02 +00:00

Add mutating commands for PackagePromotion (#1769)

* Add mutating commands for PackagePromotion

* Add checkAllocationToken methods

* Remove abstract methods

* Add better comments

* Small fixes

* Remove unneccesary init method

* Only assert in transaction in helper method
This commit is contained in:
sarahcaseybot
2022-09-21 12:38:09 -04:00
committed by GitHub
parent 82a3a49268
commit 1d3738da27
7 changed files with 641 additions and 1 deletions

View File

@@ -15,6 +15,7 @@
package google.registry.model.domain.token;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
@@ -94,6 +95,21 @@ public class PackagePromotion extends ImmutableObject implements Buildable {
return Optional.ofNullable(lastNotificationSent);
}
/** Loads and returns a PackagePromotion entity by its token string directly from Cloud SQL. */
public static Optional<PackagePromotion> loadByTokenString(String tokenString) {
jpaTm().assertInTransaction();
return jpaTm()
.query("FROM PackagePromotion WHERE token = :token", PackagePromotion.class)
.setParameter("token", VKey.createSql(AllocationToken.class, tokenString))
.getResultStream()
.findFirst();
}
@Override
public VKey<PackagePromotion> createVKey() {
return VKey.createSql(PackagePromotion.class, packagePromotionId);
}
@Override
public Builder asBuilder() {
return new Builder(clone(this));
@@ -143,7 +159,7 @@ public class PackagePromotion extends ImmutableObject implements Buildable {
return this;
}
public Builder setNextBillingDate(@Nullable DateTime nextBillingDate) {
public Builder setNextBillingDate(DateTime nextBillingDate) {
checkArgumentNotNull(nextBillingDate, "Next billing date must not be null");
getInstance().nextBillingDate = nextBillingDate;
return this;

View File

@@ -0,0 +1,120 @@
// Copyright 2022 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import com.beust.jcommander.Parameter;
import google.registry.model.domain.token.AllocationToken;
import google.registry.model.domain.token.AllocationToken.TokenType;
import google.registry.model.domain.token.PackagePromotion;
import google.registry.persistence.VKey;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.joda.money.Money;
import org.joda.time.DateTime;
/** Shared base class for commands to create or update a PackagePromotion object. */
abstract class CreateOrUpdatePackagePromotionCommand extends MutatingCommand {
@Parameter(description = "Allocation token String of the package token", required = true)
List<String> mainParameters;
@Nullable
@Parameter(
names = {"-d", "--max_domains"},
description = "Maximum concurrent active domains allowed in the package")
Integer maxDomains;
@Nullable
@Parameter(
names = {"-c", "--max_creates"},
description = "Maximum domain creations allowed in the package each year")
Integer maxCreates;
@Nullable
@Parameter(
names = {"-p", "--price"},
description = "Annual price of the package")
Money price;
@Nullable
@Parameter(
names = "--next_billing_date",
description = "The next date that the package should be billed for its annual fee")
Date nextBillingDate;
/** Returns the existing PackagePromotion or null if it does not exist. */
@Nullable
abstract PackagePromotion getOldPackagePromotion(String token);
/** Returns the allocation token object. */
AllocationToken getAndCheckAllocationToken(String token) {
Optional<AllocationToken> allocationToken =
tm().transact(() -> tm().loadByKeyIfPresent(VKey.createSql(AllocationToken.class, token)));
checkArgument(
allocationToken.isPresent(),
"An allocation token with the token String %s does not exist. The package token must be"
+ " created first before it can be used to create a PackagePromotion",
token);
checkArgument(
allocationToken.get().getTokenType().equals(TokenType.PACKAGE),
"The allocation token must be of the PACKAGE token type");
return allocationToken.get();
}
/** Does not clear the lastNotificationSent field. Subclasses can override this. */
boolean clearLastNotificationSent() {
return false;
}
@Override
protected final void init() throws Exception {
for (String token : mainParameters) {
jpaTm()
.transact(
() -> {
PackagePromotion oldPackage = getOldPackagePromotion(token);
checkArgument(
oldPackage != null || price != null,
"PackagePrice is required when creating a new package");
AllocationToken allocationToken = getAndCheckAllocationToken(token);
PackagePromotion.Builder builder =
(oldPackage == null)
? new PackagePromotion.Builder().setToken(allocationToken)
: oldPackage.asBuilder();
Optional.ofNullable(maxDomains).ifPresent(builder::setMaxDomains);
Optional.ofNullable(maxCreates).ifPresent(builder::setMaxCreates);
Optional.ofNullable(price).ifPresent(builder::setPackagePrice);
Optional.ofNullable(nextBillingDate)
.ifPresent(
nextBillingDate ->
builder.setNextBillingDate(new DateTime(nextBillingDate)));
if (clearLastNotificationSent()) {
builder.setLastNotificationSent(null);
}
PackagePromotion newPackage = builder.build();
stageEntityChange(oldPackage, newPackage);
});
}
}
}

View File

@@ -0,0 +1,39 @@
// Copyright 2022 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import com.beust.jcommander.Parameters;
import google.registry.model.domain.token.PackagePromotion;
import org.jetbrains.annotations.Nullable;
/** Command to create a PackagePromotion */
@Parameters(
separators = " =",
commandDescription =
"Create new package promotion object(s) for registrars to register multiple names under"
+ " one contractual annual package price using a package allocation token")
public final class CreatePackagePromotionCommand extends CreateOrUpdatePackagePromotionCommand {
@Nullable
@Override
PackagePromotion getOldPackagePromotion(String tokenString) {
checkArgument(
!PackagePromotion.loadByTokenString(tokenString).isPresent(),
"PackagePromotion with token %s already exists",
tokenString);
return null;
}
}

View File

@@ -42,6 +42,7 @@ public final class RegistryTool {
.put("create_contact", CreateContactCommand.class)
.put("create_domain", CreateDomainCommand.class)
.put("create_host", CreateHostCommand.class)
.put("create_package_promotion", CreatePackagePromotionCommand.class)
.put("create_premium_list", CreatePremiumListCommand.class)
.put("create_registrar", CreateRegistrarCommand.class)
.put("create_registrar_groups", CreateRegistrarGroupsCommand.class)
@@ -106,6 +107,7 @@ public final class RegistryTool {
.put("update_cursors", UpdateCursorsCommand.class)
.put("update_domain", UpdateDomainCommand.class)
.put("update_keyring_secret", UpdateKeyringSecretCommand.class)
.put("update_package_promotion", UpdatePackagePromotionCommand.class)
.put("update_premium_list", UpdatePremiumListCommand.class)
.put("update_registrar", UpdateRegistrarCommand.class)
.put("update_reserved_list", UpdateReservedListCommand.class)

View File

@@ -0,0 +1,45 @@
// Copyright 2022 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import google.registry.model.domain.token.PackagePromotion;
import java.util.Optional;
/** Command to update a PackagePromotion */
@Parameters(separators = " =", commandDescription = "Update package promotion object(s)")
public final class UpdatePackagePromotionCommand extends CreateOrUpdatePackagePromotionCommand {
@Parameter(
names = "--clear_last_notification_sent",
description =
"Clear the date last max-domain non-compliance notification was sent? (This should be"
+ " cleared whenever a tier is upgraded)")
boolean clearLastNotificationSent;
@Override
PackagePromotion getOldPackagePromotion(String token) {
Optional<PackagePromotion> oldPackage = PackagePromotion.loadByTokenString(token);
checkArgument(oldPackage.isPresent(), "PackagePromotion with token %s does not exist", token);
return oldPackage.get();
}
@Override
boolean clearLastNotificationSent() {
return clearLastNotificationSent;
}
}