Skip to content

Commit

Permalink
Handle errors while adding domains to a communication service
Browse files Browse the repository at this point in the history
  • Loading branch information
harjain99 committed Aug 1, 2024
1 parent 78b6327 commit 224df8c
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/gyro/azure/communication/DomainResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<VerificationStatusRecord> {

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 "";
}
}
135 changes: 135 additions & 0 deletions src/main/java/gyro/azure/communication/VerificationStates.java
Original file line number Diff line number Diff line change
@@ -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<DomainPropertiesVerificationStates> {

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 "";
}
}

0 comments on commit 224df8c

Please sign in to comment.