mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(account): implement GetContactInformation and PutContactInformation (#8262)
This commit is contained in:
1 parent
16ff5a2c74
commit
5f03ff975d
15 files changed
+945
-4
No files matched your search
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.JsonErrorResponseUtils;
|
||||
import io.floci.aws.core.common.RequestContext;
|
||||
import io.floci.aws.services.account.model.AlternateContact;
|
||||
import io.floci.aws.services.account.model.ContactInformation;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.POST;
|
||||
@@ -20,6 +21,9 @@ import jakarta.ws.rs.core.Response;
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class AccountController {
|
||||
// Floci has no phone verification flow, so every account reports this status.
|
||||
private static final String VERIFICATION_STATUS_NOT_SUPPORTED = "NOT_SUPPORTED";
|
||||
|
||||
private final AccountService accountService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final RequestContext requestContext;
|
||||
@@ -54,13 +58,36 @@ public class AccountController {
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/putContactInformation")
|
||||
public Response putContactInformation(String body) {
|
||||
accountService.putContactInformation(requestContext.getAccountId(), readTree(body));
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/getContactInformation")
|
||||
public Response getContactInformation(String body) {
|
||||
ContactInformation information = accountService.getContactInformation(
|
||||
requestContext.getAccountId(), readTree(body));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("ContactInformation", objectMapper.valueToTree(information));
|
||||
response.put("VerificationStatus", VERIFICATION_STATUS_NOT_SUPPORTED);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private JsonNode readTree(String body) {
|
||||
JsonNode request;
|
||||
try {
|
||||
return objectMapper.reader()
|
||||
request = objectMapper.reader()
|
||||
.with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
|
||||
.readTree(body == null || body.isBlank() ? "{}" : body);
|
||||
} catch (Exception e) {
|
||||
throw new WebApplicationException(JsonErrorResponseUtils.createSerializationErrorResponse());
|
||||
}
|
||||
if (!request.isObject()) {
|
||||
throw new WebApplicationException(JsonErrorResponseUtils.createSerializationErrorResponse());
|
||||
}
|
||||
return request;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package io.floci.aws.services.account;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RequestConstraintCheck;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Checks the primary contact requests against the constraints generated by
|
||||
* {@code tools/aws/regen_account_shapes.py}. A member of the wrong JSON type answers
|
||||
* {@code SerializationException}, as a non-string {@code AccountId} does on the alternate contact
|
||||
* operations; every other violation is a {@code ValidationException} in the standard wording.
|
||||
*/
|
||||
final class AccountRequestShapes {
|
||||
|
||||
private static final RequestConstraintCheck.Messages VALIDATION =
|
||||
RequestConstraintCheck.standardMessages("ValidationException");
|
||||
|
||||
private static final RequestConstraintCheck CHECK = RequestConstraintCheck.load(
|
||||
"aws/account-request-shapes.json", "Account Management request shapes",
|
||||
new RequestConstraintCheck.Messages() {
|
||||
@Override
|
||||
public AwsException type(String path, String type) {
|
||||
String member = path.substring(path.indexOf('.') + 1);
|
||||
return new AwsException("SerializationException", member + " must be a " + type + ".", 400);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
return VALIDATION.bounds(path, type, min, max, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException pattern(String path, String pattern) {
|
||||
return VALIDATION.pattern(path, pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException enumValue(String path, List<String> values) {
|
||||
return VALIDATION.enumValue(path, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException required(String path) {
|
||||
return VALIDATION.required(path);
|
||||
}
|
||||
});
|
||||
|
||||
private AccountRequestShapes() {
|
||||
}
|
||||
|
||||
static void check(String operation, JsonNode request) {
|
||||
CHECK.check(operation, request);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.account.model.AlternateContact;
|
||||
import io.floci.aws.services.account.model.ContactInformation;
|
||||
import io.floci.aws.services.organizations.OrganizationsService;
|
||||
import io.floci.aws.services.organizations.model.Organization;
|
||||
import io.floci.aws.services.organizations.model.OrganizationAccount;
|
||||
@@ -17,6 +18,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -29,14 +31,20 @@ public class AccountService implements Resettable, StoredResourceArns {
|
||||
private static final Pattern EMAIL = Pattern.compile("\\s*[\\w+=.#|!&-]+@[\\w.-]+\\.[\\w]+\\s*");
|
||||
private static final Pattern PHONE = Pattern.compile("[\\s0-9()+-]+");
|
||||
private static final Set<String> CONTACT_TYPES = Set.of("BILLING", "OPERATIONS", "SECURITY");
|
||||
private static final Set<String> COUNTRIES_REQUIRING_STATE_OR_REGION =
|
||||
Set.of("US", "CA", "GB", "DE", "JP", "IN", "BR");
|
||||
private static final String PRIMARY_CONTACT_KEY = "primary";
|
||||
|
||||
private final AccountAwareStorageBackend<AlternateContact> contacts;
|
||||
private final AccountAwareStorageBackend<ContactInformation> contactInformation;
|
||||
private final OrganizationsService organizationsService;
|
||||
|
||||
@Inject
|
||||
public AccountService(StorageFactory storageFactory, OrganizationsService organizationsService) {
|
||||
this.contacts = storageFactory.create("account", "account-alternate-contacts.json",
|
||||
new TypeReference<Map<String, AlternateContact>>() {});
|
||||
this.contactInformation = storageFactory.create("account", "account-contact-information.json",
|
||||
new TypeReference<Map<String, ContactInformation>>() {});
|
||||
this.organizationsService = organizationsService;
|
||||
}
|
||||
|
||||
@@ -68,6 +76,39 @@ public class AccountService implements Resettable, StoredResourceArns {
|
||||
contacts.deleteForAccount(targetAccountId, type);
|
||||
}
|
||||
|
||||
public void putContactInformation(String callerAccountId, JsonNode request) {
|
||||
AccountRequestShapes.check("PutContactInformation", request);
|
||||
String targetAccountId = resolveTargetAccount(callerAccountId, request);
|
||||
JsonNode contact = request.get("ContactInformation");
|
||||
ContactInformation information = new ContactInformation();
|
||||
information.setFullName(text(contact, "FullName"));
|
||||
information.setAddressLine1(text(contact, "AddressLine1"));
|
||||
information.setAddressLine2(text(contact, "AddressLine2"));
|
||||
information.setAddressLine3(text(contact, "AddressLine3"));
|
||||
information.setCity(text(contact, "City"));
|
||||
information.setStateOrRegion(text(contact, "StateOrRegion"));
|
||||
information.setDistrictOrCounty(text(contact, "DistrictOrCounty"));
|
||||
information.setPostalCode(text(contact, "PostalCode"));
|
||||
information.setCountryCode(text(contact, "CountryCode"));
|
||||
information.setPhoneNumber(text(contact, "PhoneNumber"));
|
||||
information.setCompanyName(text(contact, "CompanyName"));
|
||||
information.setWebsiteUrl(text(contact, "WebsiteUrl"));
|
||||
// Only the API reference states this rule, so the vendored model cannot check it.
|
||||
if (information.getStateOrRegion() == null && COUNTRIES_REQUIRING_STATE_OR_REGION
|
||||
.contains(information.getCountryCode().toUpperCase(Locale.ROOT))) {
|
||||
throw validation("StateOrRegion is required when CountryCode is "
|
||||
+ information.getCountryCode() + ".");
|
||||
}
|
||||
contactInformation.putForAccount(targetAccountId, PRIMARY_CONTACT_KEY, information);
|
||||
}
|
||||
|
||||
public ContactInformation getContactInformation(String callerAccountId, JsonNode request) {
|
||||
AccountRequestShapes.check("GetContactInformation", request);
|
||||
String targetAccountId = resolveTargetAccount(callerAccountId, request);
|
||||
return contactInformation.getForAccount(targetAccountId, PRIMARY_CONTACT_KEY)
|
||||
.orElseThrow(AccountService::contactInformationNotFound);
|
||||
}
|
||||
|
||||
private String resolveTargetAccount(String callerAccountId, JsonNode request) {
|
||||
JsonNode accountIdNode = request == null ? null : request.get("AccountId");
|
||||
if (accountIdNode == null || accountIdNode.isNull()) {
|
||||
@@ -118,6 +159,7 @@ public class AccountService implements Resettable, StoredResourceArns {
|
||||
@Override
|
||||
public void clear() {
|
||||
contacts.clear();
|
||||
contactInformation.clear();
|
||||
}
|
||||
|
||||
/** The namespace of the resource types this service finds, as the request-resources table names it. */
|
||||
@@ -187,4 +229,9 @@ public class AccountService implements Resettable, StoredResourceArns {
|
||||
return new AwsException("ResourceNotFoundException",
|
||||
"The alternate contact does not exist for the specified account and contact type.", 404);
|
||||
}
|
||||
|
||||
private static AwsException contactInformationNotFound() {
|
||||
return new AwsException("ResourceNotFoundException",
|
||||
"The contact information does not exist for the specified account.", 404);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package io.floci.aws.services.account.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class ContactInformation {
|
||||
@JsonProperty("AddressLine1")
|
||||
private String addressLine1;
|
||||
@JsonProperty("AddressLine2")
|
||||
private String addressLine2;
|
||||
@JsonProperty("AddressLine3")
|
||||
private String addressLine3;
|
||||
@JsonProperty("City")
|
||||
private String city;
|
||||
@JsonProperty("CompanyName")
|
||||
private String companyName;
|
||||
@JsonProperty("CountryCode")
|
||||
private String countryCode;
|
||||
@JsonProperty("DistrictOrCounty")
|
||||
private String districtOrCounty;
|
||||
@JsonProperty("FullName")
|
||||
private String fullName;
|
||||
@JsonProperty("PhoneNumber")
|
||||
private String phoneNumber;
|
||||
@JsonProperty("PostalCode")
|
||||
private String postalCode;
|
||||
@JsonProperty("StateOrRegion")
|
||||
private String stateOrRegion;
|
||||
@JsonProperty("WebsiteUrl")
|
||||
private String websiteUrl;
|
||||
|
||||
public String getAddressLine1() {
|
||||
return addressLine1;
|
||||
}
|
||||
|
||||
public void setAddressLine1(String addressLine1) {
|
||||
this.addressLine1 = addressLine1;
|
||||
}
|
||||
|
||||
public String getAddressLine2() {
|
||||
return addressLine2;
|
||||
}
|
||||
|
||||
public void setAddressLine2(String addressLine2) {
|
||||
this.addressLine2 = addressLine2;
|
||||
}
|
||||
|
||||
public String getAddressLine3() {
|
||||
return addressLine3;
|
||||
}
|
||||
|
||||
public void setAddressLine3(String addressLine3) {
|
||||
this.addressLine3 = addressLine3;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getCompanyName() {
|
||||
return companyName;
|
||||
}
|
||||
|
||||
public void setCompanyName(String companyName) {
|
||||
this.companyName = companyName;
|
||||
}
|
||||
|
||||
public String getCountryCode() {
|
||||
return countryCode;
|
||||
}
|
||||
|
||||
public void setCountryCode(String countryCode) {
|
||||
this.countryCode = countryCode;
|
||||
}
|
||||
|
||||
public String getDistrictOrCounty() {
|
||||
return districtOrCounty;
|
||||
}
|
||||
|
||||
public void setDistrictOrCounty(String districtOrCounty) {
|
||||
this.districtOrCounty = districtOrCounty;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
public void setFullName(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(String phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String postalCode) {
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
public String getStateOrRegion() {
|
||||
return stateOrRegion;
|
||||
}
|
||||
|
||||
public void setStateOrRegion(String stateOrRegion) {
|
||||
this.stateOrRegion = stateOrRegion;
|
||||
}
|
||||
|
||||
public String getWebsiteUrl() {
|
||||
return websiteUrl;
|
||||
}
|
||||
|
||||
public void setWebsiteUrl(String websiteUrl) {
|
||||
this.websiteUrl = websiteUrl;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user