Skip to content

Commit

Permalink
Communication - Added release phone number LRO (#16821)
Browse files Browse the repository at this point in the history
* Added release phone number LRO

* Made implemented functions private

* Refactor private methods and added readme

* Fixed style issues

* Fixed readme samples

* Addressed comments
  • Loading branch information
jbeauregardb committed Oct 29, 2020
1 parent 03f3897 commit 599ba5c
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 95 deletions.
16 changes: 16 additions & 0 deletions sdk/communication/azure-communication-administration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ SyncPoller<Void, Void> res =
res.waitForCompletion();
```

### Release Phone Numbers
<!-- embedme ./src/samples/java/com/azure/communication/administration/ReadmeSamples.java#L389-L399 -->
```java
Duration duration = Duration.ofSeconds(1);
PhoneNumber phoneNumber = new PhoneNumber("PHONE_NUMBER_TO_RELEASE");
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(phoneNumber);
PhoneNumberClient phoneNumberClient = createPhoneNumberClient();

SyncPoller<PhoneNumberRelease, PhoneNumberRelease> res =
phoneNumberClient.beginReleasePhoneNumbers(phoneNumbers, duration);
res.waitForCompletion();
PhoneNumberRelease result = res.getFinalResult();
System.out.println("Phone number release status: " + result.getStatus());
```

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a [Contributor License Agreement (CLA)][cla] declaring that you have the right to, and actually do, grant us the rights to use your contribution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.azure.communication.administration.models.PstnConfiguration;
import com.azure.communication.administration.models.ReleaseRequest;
import com.azure.communication.administration.models.ReleaseResponse;
import com.azure.communication.administration.models.ReleaseStatus;
import com.azure.communication.administration.models.UpdateNumberCapabilitiesResponse;
import com.azure.communication.administration.models.NumberConfiguration;
import com.azure.communication.administration.models.PhoneNumberSearch;
Expand Down Expand Up @@ -541,8 +542,7 @@ Mono<Response<PhoneNumberRelease>> getReleaseByIdWithResponse(String releaseId,
* @param phoneNumbers {@link List} of {@link PhoneNumber} objects with the phone numbers.
* @return A {@link Mono} containing a {@link ReleaseResponse} representing the release.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ReleaseResponse> releasePhoneNumbers(List<PhoneNumber> phoneNumbers) {
private Mono<ReleaseResponse> releasePhoneNumbers(List<PhoneNumber> phoneNumbers) {
return releasePhoneNumbersWithResponse(phoneNumbers).flatMap(FluxUtil::toMono);
}

Expand All @@ -553,12 +553,12 @@ public Mono<ReleaseResponse> releasePhoneNumbers(List<PhoneNumber> phoneNumbers)
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue()} value returns
* a {@link ReleaseResponse} representing the release.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<ReleaseResponse>> releasePhoneNumbersWithResponse(List<PhoneNumber> phoneNumbers) {
private Mono<Response<ReleaseResponse>> releasePhoneNumbersWithResponse(List<PhoneNumber> phoneNumbers) {
return releasePhoneNumbersWithResponse(phoneNumbers, null);
}

Mono<Response<ReleaseResponse>> releasePhoneNumbersWithResponse(List<PhoneNumber> phoneNumbers, Context context) {
private Mono<Response<ReleaseResponse>> releasePhoneNumbersWithResponse(
List<PhoneNumber> phoneNumbers, Context context) {
Objects.requireNonNull(phoneNumbers, "'phoneNumbers' cannot be null.");

List<String> phoneNumberStrings = phoneNumbers.stream().map(PhoneNumber::getValue).collect(Collectors.toList());
Expand Down Expand Up @@ -896,4 +896,70 @@ Mono<Void>> purchaseSearchFetchResultOperation() {
};

}

/**
* Releases the given phone numbers.
* This function returns a Long Running Operation poller that allows you to
* wait indefinitely until the operation is complete.
*
* @param phoneNumbers A list of {@link PhoneNumber} with the desired numbers to release
* @param pollInterval The time our long running operation will keep on polling
* until it gets a result from the server
* @return A {@link PollerFlux} object with the release entity
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PollerFlux<PhoneNumberRelease, PhoneNumberRelease>
beginReleasePhoneNumbers(List<PhoneNumber> phoneNumbers, Duration pollInterval) {
Objects.requireNonNull(phoneNumbers, "'phoneNumbers' cannot be null.");

if (pollInterval == null) {
pollInterval = defaultPollInterval;
}

return new PollerFlux<PhoneNumberRelease, PhoneNumberRelease>(pollInterval,
releaseNumbersActivationOperation(phoneNumbers),
releaseNumbersPollOperation(),
(activationResponse, pollingContext) ->
monoError(logger, new RuntimeException("Cancellation is not supported")),
releaseNumbersFetchResultOperation());
}

private Function<PollingContext<PhoneNumberRelease>, Mono<PhoneNumberRelease>>
releaseNumbersActivationOperation(List<PhoneNumber> phoneNumbers) {
return (pollingContext) -> {
Mono<PhoneNumberRelease> response = releasePhoneNumbers(phoneNumbers)
.flatMap(releaseNumberResponse -> {
String releaseId = releaseNumberResponse.getReleaseId();
Mono<PhoneNumberRelease> phoneNumberRelease = getReleaseById(releaseId);
return phoneNumberRelease;
});
return response;
};
}

private Function<PollingContext<PhoneNumberRelease>, Mono<PollResponse<PhoneNumberRelease>>>
releaseNumbersPollOperation() {
return pollingContext ->
getReleaseById(pollingContext.getLatestResponse().getValue().getReleaseId())
.flatMap(getReleaseResponse -> {
ReleaseStatus status = getReleaseResponse.getStatus();
if (status.equals(ReleaseStatus.COMPLETE)
|| status.equals(ReleaseStatus.EXPIRED)) {
return Mono.just(new PollResponse<>(
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getReleaseResponse));
}
if (status.equals(ReleaseStatus.FAILED)) {
return Mono.just(new PollResponse<>(
LongRunningOperationStatus.FAILED, getReleaseResponse));
}
return Mono.just(new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS, getReleaseResponse));
});
}

private Function<PollingContext<PhoneNumberRelease>,
Mono<PhoneNumberRelease>> releaseNumbersFetchResultOperation() {
return pollingContext -> {
return Mono.just(pollingContext.getLatestResponse().getValue());
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.azure.communication.administration.models.PhonePlan;
import com.azure.communication.administration.models.PhonePlanGroup;
import com.azure.communication.administration.models.PstnConfiguration;
import com.azure.communication.administration.models.ReleaseResponse;
import com.azure.communication.administration.models.UpdateNumberCapabilitiesResponse;
import com.azure.communication.administration.models.PhoneNumberSearch;
import com.azure.communication.administration.models.UpdatePhoneNumberCapabilitiesResponse;
Expand Down Expand Up @@ -367,30 +366,6 @@ public Response<PhoneNumberRelease> getReleaseByIdWithResponse(String releaseId,
return phoneNumberAsyncClient.getReleaseByIdWithResponse(releaseId, context).block();
}

/**
* Creates a release for the given phone numbers.
*
* @param phoneNumbers {@link List} of {@link PhoneNumber} objects with the phone numbers.
* @return A {@link ReleaseResponse} representing the release.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public ReleaseResponse releasePhoneNumbers(List<PhoneNumber> phoneNumbers) {
return phoneNumberAsyncClient.releasePhoneNumbers(phoneNumbers).block();
}

/**
* Creates a release for the given phone numbers.
*
* @param phoneNumbers {@link List} of {@link PhoneNumber} objects with the phone numbers.
* @param context A {@link Context} representing the request context.
* @return A {@link Response} whose {@link Response#getValue()} value returns
* a {@link ReleaseResponse} representing the release.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<ReleaseResponse> releasePhoneNumbersWithResponse(List<PhoneNumber> phoneNumbers, Context context) {
return phoneNumberAsyncClient.releasePhoneNumbersWithResponse(phoneNumbers, context).block();
}

/**
* Gets the list of all releases
*
Expand Down Expand Up @@ -532,4 +507,19 @@ public SyncPoller<Void, Void> beginPurchaseSearch(
String searchId, Duration pollInterval) {
return phoneNumberAsyncClient.beginPurchaseSearch(searchId, pollInterval).getSyncPoller();
}

/**
* Releases the given phone numbers.
* This function returns a Long Running Operation poller
*
* @param phoneNumbers A list of {@link PhoneNumber} with the desired numbers to release
* @param pollInterval The time our long running operation will keep on polling
* until it gets a result from the server
* @return A {@link SyncPoller} object with the search result
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public SyncPoller<PhoneNumberRelease, PhoneNumberRelease> beginReleasePhoneNumbers(
List<PhoneNumber> phoneNumbers, Duration pollInterval) {
return phoneNumberAsyncClient.beginReleasePhoneNumbers(phoneNumbers, pollInterval).getSyncPoller();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.azure.communication.administration.models.LocationOptionsDetails;
import com.azure.communication.administration.models.LocationOptionsQuery;
import com.azure.communication.administration.models.PhoneNumberCountry;
import com.azure.communication.administration.models.PhoneNumberRelease;
import com.azure.communication.administration.models.PhoneNumberSearch;
import com.azure.communication.administration.models.PhonePlan;
import com.azure.communication.administration.models.PhonePlanGroup;
Expand All @@ -27,7 +28,6 @@
import com.azure.core.util.polling.SyncPoller;

public class ReadmeSamples {

/**
* Sample code for creating a sync Communication Identity Client.
*
Expand Down Expand Up @@ -381,4 +381,21 @@ public void beginPurchaseSearch() {
phoneNumberClient.beginPurchaseSearch(phoneNumberSearchId, duration);
res.waitForCompletion();
}

/**
* Sample code to release a phone number as a long running operation
*/
public void beginReleasePhoneNumbers() {
Duration duration = Duration.ofSeconds(1);
PhoneNumber phoneNumber = new PhoneNumber("PHONE_NUMBER_TO_RELEASE");
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(phoneNumber);
PhoneNumberClient phoneNumberClient = createPhoneNumberClient();

SyncPoller<PhoneNumberRelease, PhoneNumberRelease> res =
phoneNumberClient.beginReleasePhoneNumbers(phoneNumbers, duration);
res.waitForCompletion();
PhoneNumberRelease result = res.getFinalResult();
System.out.println("Phone number release status: " + result.getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import com.azure.communication.administration.models.NumberUpdateCapabilities;
import com.azure.communication.administration.models.PhoneNumberCountry;
import com.azure.communication.administration.models.PhoneNumberEntity;
import com.azure.communication.administration.models.PhoneNumberRelease;
import com.azure.communication.administration.models.PhoneNumberSearch;
import com.azure.communication.administration.models.PhonePlan;
import com.azure.communication.administration.models.PhonePlanGroup;
import com.azure.communication.administration.models.PstnConfiguration;
import com.azure.communication.administration.models.ReleaseResponse;
import com.azure.communication.administration.models.ReleaseStatus;
import com.azure.communication.administration.models.SearchStatus;
import com.azure.communication.administration.models.UpdateNumberCapabilitiesResponse;
import com.azure.communication.administration.models.UpdatePhoneNumberCapabilitiesResponse;
Expand Down Expand Up @@ -450,38 +451,6 @@ public void unconfigureNumberWithResponse(HttpClient httpClient) {
.verifyComplete();
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void releasePhoneNumbers(HttpClient httpClient) {
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(new PhoneNumber(PHONENUMBER_TO_RELEASE));

Mono<ReleaseResponse> mono = this.getClient(httpClient).releasePhoneNumbers(phoneNumbers);

StepVerifier.create(mono)
.assertNext(item -> {
assertNotNull(item.getReleaseId());
})
.verifyComplete();
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void releasePhoneNumbersWithResponse(HttpClient httpClient) {
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(new PhoneNumber(PHONENUMBER_TO_RELEASE));

Mono<Response<ReleaseResponse>> mono =
this.getClient(httpClient).releasePhoneNumbersWithResponse(phoneNumbers, Context.NONE);

StepVerifier.create(mono)
.assertNext(item -> {
assertEquals(200, item.getStatusCode());
assertNotNull(item.getValue().getReleaseId());
})
.verifyComplete();
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void beginCreateSearch(HttpClient httpClient) {
Expand All @@ -500,12 +469,13 @@ public void beginCreateSearch(HttpClient httpClient) {
PhoneNumberAsyncClient client = this.getClient(httpClient);
PollerFlux<PhoneNumberSearch, PhoneNumberSearch> poller =
client.beginCreateSearch(createSearchOptions, duration);
AsyncPollResponse<PhoneNumberSearch, PhoneNumberSearch> asyncRes =
poller.takeUntil(apr -> apr.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED)
.blockLast();
PhoneNumberSearch testResult = asyncRes.getValue();
assertEquals(testResult.getPhoneNumbers().size(), 2);
assertNotNull(testResult.getSearchId());
Mono<AsyncPollResponse<PhoneNumberSearch, PhoneNumberSearch>> asyncRes = poller.last();
StepVerifier.create(asyncRes)
.assertNext(item -> {
assertEquals(item.getValue().getPhoneNumbers().size(), 2);
assertNotNull(item.getValue().getSearchId());
})
.verifyComplete();
}

@ParameterizedTest
Expand All @@ -525,6 +495,23 @@ public void beginPurchaseSearch(HttpClient httpClient) {
.verifyComplete();
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void beginReleasePhoneNumbers(HttpClient httpClient) {
PhoneNumber phoneNumber = new PhoneNumber(PHONENUMBER_TO_RELEASE);
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(phoneNumber);
Duration pollInterval = Duration.ofSeconds(1);
PollerFlux<PhoneNumberRelease, PhoneNumberRelease> poller =
this.getClient(httpClient).beginReleasePhoneNumbers(phoneNumbers, pollInterval);
Mono<AsyncPollResponse<PhoneNumberRelease, PhoneNumberRelease>> asyncRes = poller.last();
StepVerifier.create(asyncRes)
.assertNext(item -> {
assertEquals(ReleaseStatus.COMPLETE, item.getValue().getStatus());
})
.verifyComplete();
}

private PhoneNumberAsyncClient getClient(HttpClient httpClient) {
return super.getClientBuilder(httpClient).buildAsyncClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.azure.communication.administration.models.PhonePlan;
import com.azure.communication.administration.models.PhonePlanGroup;
import com.azure.communication.administration.models.PstnConfiguration;
import com.azure.communication.administration.models.ReleaseResponse;
import com.azure.communication.administration.models.UpdateNumberCapabilitiesResponse;
import com.azure.communication.administration.models.UpdatePhoneNumberCapabilitiesResponse;
import com.azure.communication.common.PhoneNumber;
Expand Down Expand Up @@ -331,30 +330,6 @@ public void unconfigureNumberWithResponse(HttpClient httpClient) {
assertEquals(200, response.getStatusCode());
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void releasePhoneNumbers(HttpClient httpClient) {
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(new PhoneNumber(PHONENUMBER_TO_RELEASE));

ReleaseResponse releaseResponse = this.getClient(httpClient).releasePhoneNumbers(phoneNumbers);

assertNotNull(releaseResponse.getReleaseId());
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void releasePhoneNumbersWithResponse(HttpClient httpClient) {
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(new PhoneNumber(PHONENUMBER_TO_RELEASE));

Response<ReleaseResponse> response =
this.getClient(httpClient).releasePhoneNumbersWithResponse(phoneNumbers, Context.NONE);

assertEquals(200, response.getStatusCode());
assertNotNull(response.getValue().getReleaseId());
}

private PhoneNumberClient getClient(HttpClient httpClient) {
return super.getClientBuilder(httpClient).buildClient();
}
Expand Down
Loading

0 comments on commit 599ba5c

Please sign in to comment.