Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication - Added release phone number LRO #16821

Merged
merged 6 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,67 @@ 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.");
Objects.requireNonNull(pollInterval, "'pollInterval' cannot be null.");
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 Down Expand Up @@ -525,6 +494,22 @@ 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(5);
PollerFlux<PhoneNumberRelease, PhoneNumberRelease> poller =
this.getClient(httpClient).beginReleasePhoneNumbers(phoneNumbers, pollInterval);
AsyncPollResponse<PhoneNumberRelease, PhoneNumberRelease> asyncRes =
poller.takeUntil(apr -> apr.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED).
blockLast();
PhoneNumberRelease testResult = asyncRes.getValue();
assertEquals(ReleaseStatus.COMPLETE, testResult.getStatus());
}

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