diff --git a/src/main/java/gyro/azure/communication/CommunicationServiceResource.java b/src/main/java/gyro/azure/communication/CommunicationServiceResource.java index abbc54b2..cf33038c 100644 --- a/src/main/java/gyro/azure/communication/CommunicationServiceResource.java +++ b/src/main/java/gyro/azure/communication/CommunicationServiceResource.java @@ -23,6 +23,7 @@ import java.util.Set; import java.util.stream.Collectors; +import com.azure.core.management.exception.ManagementException; import com.azure.resourcemanager.communication.CommunicationManager; import com.azure.resourcemanager.communication.fluent.models.CommunicationServiceResourceInner; import gyro.azure.AzureResource; @@ -250,13 +251,25 @@ public void create(GyroUI ui, State state) throws Exception { state.save(); // Add domains in the update call - // If domains are not verified but are added to the create call, the api errors out at the service is not saved to the state + // If domains are not verified but are added to the create call, the api errors out and the service is not saved to the state if (!getDomains().isEmpty()) { service.withLinkedDomains(getDomains().stream().map(DomainResource::getId).collect(Collectors.toList())); } - client.serviceClient().getCommunicationServices() - .createOrUpdate(getResourceGroup().getName(), getName(), service); + try { + client.serviceClient().getCommunicationServices() + .createOrUpdate(getResourceGroup().getName(), getName(), service); + + } catch (ManagementException ex) { + if (ex.getMessage() != null && ex.getMessage().contains("The specified domain is unable to be linked.")) { + // Domains should be added but the API will error out in case the domains were not verified. + // The refresh will only keep added domains in the state + refresh(); + + } else { + throw ex; + } + } } @Override diff --git a/src/main/java/gyro/azure/communication/DomainResource.java b/src/main/java/gyro/azure/communication/DomainResource.java index dcb0dfb0..da1c04c6 100644 --- a/src/main/java/gyro/azure/communication/DomainResource.java +++ b/src/main/java/gyro/azure/communication/DomainResource.java @@ -69,6 +69,7 @@ public class DomainResource extends AzureResource private String dataLocation; private String id; private VerificationRecords verificationRecords; + private VerificationStates verificationStates; /** * The resource group in which to build the service @@ -172,6 +173,18 @@ public void setVerificationRecords(VerificationRecords verificationRecords) { this.verificationRecords = verificationRecords; } + /** + * The verification status records for this domain + */ + @Output + public VerificationStates getVerificationStates() { + return verificationStates; + } + + public void setVerificationStates(VerificationStates verificationStates) { + this.verificationStates = verificationStates; + } + @Override public void copyFrom(com.azure.resourcemanager.communication.models.DomainResource model) { setDomainManagement(model.domainManagement().toString()); @@ -190,6 +203,13 @@ public void copyFrom(com.azure.resourcemanager.communication.models.DomainResour records.copyFrom(model.verificationRecords()); setVerificationRecords(records); } + + setVerificationStates(null); + if (model.verificationStates() != null) { + VerificationStates states = newSubresource(VerificationStates.class); + states.copyFrom(model.verificationStates()); + setVerificationStates(states); + } } @Override @@ -222,8 +242,12 @@ public void create(GyroUI ui, State state) throws Exception { service.withDomainManagement(DomainManagement.fromString(getDomainManagement())); } - setId(client.serviceClient().getDomains() - .createOrUpdate(getResourceGroup().getName(), getEmailService().getName(), getName(), service).id()); + service = client.serviceClient().getDomains() + .createOrUpdate(getResourceGroup().getName(), getEmailService().getName(), getName(), service); + + setId(service.id()); + + refresh(); } @Override diff --git a/src/main/java/gyro/azure/communication/DomainVerificationStatusRecord.java b/src/main/java/gyro/azure/communication/DomainVerificationStatusRecord.java new file mode 100644 index 00000000..fa8dfcac --- /dev/null +++ b/src/main/java/gyro/azure/communication/DomainVerificationStatusRecord.java @@ -0,0 +1,66 @@ +/* + * Copyright 2024, Brightspot, Inc. + * + * 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 gyro.azure.communication; + +import com.azure.resourcemanager.communication.models.VerificationStatusRecord; +import gyro.azure.Copyable; +import gyro.core.resource.Diffable; +import gyro.core.resource.Output; +import gyro.core.validation.ValidStrings; + +public class DomainVerificationStatusRecord extends Diffable implements Copyable { + + private String status; + private String errorCode; + + @ValidStrings({ + "NotStarted", + "VerificationRequested", + "VerificationInProgress", + "VerificationFailed", + "Verified", + "CancellationRequested" + }) + @Output + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + @Output + public String getErrorCode() { + return errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + @Override + public void copyFrom(VerificationStatusRecord model) { + setStatus(model.status().toString()); + setErrorCode(model.errorCode()); + } + + @Override + public String primaryKey() { + return ""; + } +} diff --git a/src/main/java/gyro/azure/communication/VerificationStates.java b/src/main/java/gyro/azure/communication/VerificationStates.java new file mode 100644 index 00000000..96bbb573 --- /dev/null +++ b/src/main/java/gyro/azure/communication/VerificationStates.java @@ -0,0 +1,135 @@ +/* + * Copyright 2024, Brightspot, Inc. + * + * 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 gyro.azure.communication; + +import com.azure.resourcemanager.communication.models.DomainPropertiesVerificationStates; +import gyro.azure.Copyable; +import gyro.core.resource.Diffable; +import gyro.core.resource.Output; + +public class VerificationStates extends Diffable implements Copyable { + + private DomainVerificationStatusRecord dkim; + private DomainVerificationStatusRecord spf; + private DomainVerificationStatusRecord dkim2; + private DomainVerificationStatusRecord dmarc; + private DomainVerificationStatusRecord domain; + + /** + * The dkim verification state record. + */ + @Output + public DomainVerificationStatusRecord getDkim() { + return dkim; + } + + public void setDkim(DomainVerificationStatusRecord dkim) { + this.dkim = dkim; + } + + /** + * The spf verification state record. + */ + @Output + public DomainVerificationStatusRecord getSpf() { + return spf; + } + + public void setSpf(DomainVerificationStatusRecord spf) { + this.spf = spf; + } + + /** + * The dkim2 verification state record. + */ + @Output + public DomainVerificationStatusRecord getDkim2() { + return dkim2; + } + + public void setDkim2(DomainVerificationStatusRecord dkim2) { + this.dkim2 = dkim2; + } + + /** + * The dmarc verification state record. + */ + @Output + public DomainVerificationStatusRecord getDmarc() { + return dmarc; + } + + public void setDmarc(DomainVerificationStatusRecord dmarc) { + this.dmarc = dmarc; + } + + /** + * The domain verification state record. + */ + @Output + public DomainVerificationStatusRecord getDomain() { + return domain; + } + + public void setDomain(DomainVerificationStatusRecord domain) { + this.domain = domain; + } + + @Override + public void copyFrom(DomainPropertiesVerificationStates model) { + setDkim(null); + if (model.dkim() != null) { + DomainVerificationStatusRecord dkimRecord = new DomainVerificationStatusRecord(); + dkimRecord.copyFrom(model.dkim()); + setDkim(dkimRecord); + } + + setSpf(null); + if (model.spf() != null) { + DomainVerificationStatusRecord spfRecord = new DomainVerificationStatusRecord(); + spfRecord.copyFrom(model.spf()); + setSpf(spfRecord); + } + + setDkim2(null); + if (model.dkim2() != null) { + DomainVerificationStatusRecord dkim2Record = new DomainVerificationStatusRecord(); + dkim2Record.copyFrom(model.dkim2()); + setDkim2(dkim2Record); + } + + setDmarc(null); + if (model.dmarc() != null) { + DomainVerificationStatusRecord dmarcRecord = new DomainVerificationStatusRecord(); + dmarcRecord.copyFrom(model.dmarc()); + setDmarc(dmarcRecord); + } + + setDomain(null); + if (model.domain() != null) { + DomainVerificationStatusRecord domainRecord = new DomainVerificationStatusRecord(); + domainRecord.copyFrom(model.domain()); + setDomain(domainRecord); + } + + } + + @Override + public String primaryKey() { + return ""; + } +}