Skip to content

Commit

Permalink
mgmt, provide API overload with apiVersion parameter (#1378)
Browse files Browse the repository at this point in the history
* use defaultApiVersion if available

* overload method for getById and deleteById API
  • Loading branch information
weidongxu-microsoft authored Mar 22, 2021
1 parent dc116e3 commit 044a7fe
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 580 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;
import rx.Completable;
import rx.Observable;

import java.util.List;

Expand All @@ -33,6 +34,80 @@ public interface GenericResources extends
SupportsCreating<GenericResource.DefinitionStages.Blank>,
SupportsDeletingById,
HasManager<ResourceManager> {
/**
* Deletes a resource from Azure, identifying it by its resource ID.
*
* For consistency across service versions, please use {@link #deleteById(String, String)} instead.
*
* @param id the resource ID of the resource to delete
*/
void deleteById(String id);

/**
* Asynchronously delete a resource from Azure, identifying it by its resource ID.
*
* For consistency across service versions, please use {@link #deleteByIdAsync(String, String)} instead.
*
* @param id the resource ID of the resource to delete
* @return a representation of the deferred computation of this call
*/
Completable deleteByIdAsync(String id);

/**
* Gets the information about a resource from Azure based on the resource id.
*
* For consistency across service versions, please use {@link #getById(String, String)} instead.
*
* @param id the id of the resource.
* @return an immutable representation of the resource
*/
GenericResource getById(String id);

/**
* Gets the information about a resource from Azure based on the resource id.
*
* For consistency across service versions, please use {@link #getByIdAsync(String, String)} instead.
*
* @param id the id of the resource.
* @return an immutable representation of the resource
*/
Observable<GenericResource> getByIdAsync(String id);

/**
* Deletes a resource from Azure, identifying it by its resource ID.
*
* @param id the resource ID of the resource to delete
* @param apiVersion the API version
*/
void deleteById(String id, String apiVersion);

/**
* Asynchronously delete a resource from Azure, identifying it by its resource ID.
*
* @param id the resource ID of the resource to delete
* @param apiVersion the API version
* @return a representation of the deferred computation of this call
*/
Completable deleteByIdAsync(String id, String apiVersion);

/**
* Gets the information about a resource from Azure based on the resource id.
*
* @param id the id of the resource.
* @param apiVersion the API version
* @return an immutable representation of the resource
*/
GenericResource getById(String id, String apiVersion);

/**
* Gets the information about a resource from Azure based on the resource id.
*
* @param id the id of the resource.
* @param apiVersion the API version
* @return an immutable representation of the resource
*/
Observable<GenericResource> getByIdAsync(String id, String apiVersion);

/**
* Checks if a resource exists in a resource group.
*
Expand All @@ -55,11 +130,22 @@ boolean checkExistence(
/**
* Checks if a resource exists.
*
* For consistency across service versions, please use {@link #checkExistenceById(String, String)} instead.
*
* @param id the ID of the resource.
* @return true if the resource exists; false otherwise
*/
boolean checkExistenceById(String id);

/**
* Checks if a resource exists.
*
* @param id the ID of the resource.
* @param apiVersion the API version
* @return true if the resource exists; false otherwise
*/
boolean checkExistenceById(String id, String apiVersion);

/**
* Returns a resource belonging to a resource group.
*
Expand All @@ -68,7 +154,7 @@ boolean checkExistence(
* @param parentResourcePath Resource identity.
* @param resourceType Resource identity.
* @param resourceName Resource identity.
* @param apiVersion the String value
* @param apiVersion the API version
* @return the generic resource
*/
GenericResource get(
Expand Down Expand Up @@ -133,7 +219,7 @@ GenericResource get(
* @param parentResourcePath Resource identity.
* @param resourceType Resource identity.
* @param resourceName Resource identity.
* @param apiVersion the String value
* @param apiVersion the API version
*/
void delete(String resourceGroupName, String resourceProviderNamespace, String parentResourcePath, String resourceType, String resourceName, String apiVersion);

Expand All @@ -146,7 +232,7 @@ GenericResource get(
* @param parentResourcePath Resource identity.
* @param resourceType Resource identity.
* @param resourceName Resource identity.
* @param apiVersion the String value
* @param apiVersion the API version
* @return a representation of the deferred computation of this call
*/
Completable deleteAsync(String resourceGroupName, String resourceProviderNamespace, String parentResourcePath, String resourceType, String resourceName, String apiVersion);
Expand Down Expand Up @@ -176,7 +262,7 @@ GenericResource get(
* @param parentResourcePath Resource identity.
* @param resourceType Resource identity.
* @param resourceName Resource identity.
* @param apiVersion the String value
* @param apiVersion the API version
* @param forceDeletion the force delete parameter
*/
void delete(String resourceGroupName, String resourceProviderNamespace, String parentResourcePath, String resourceType, String resourceName, String apiVersion, boolean forceDeletion);
Expand All @@ -191,7 +277,7 @@ GenericResource get(
* @param parentResourcePath Resource identity.
* @param resourceType Resource identity.
* @param resourceName Resource identity.
* @param apiVersion the String value
* @param apiVersion the API version
* @param forceDeletion the force delete parameter
* @return a representation of the deferred computation of this call
*/
Expand All @@ -207,7 +293,7 @@ GenericResource get(
* @param parentResourcePath Resource identity.
* @param resourceType Resource identity.
* @param resourceName Resource identity.
* @param apiVersion the String value
* @param apiVersion the API version
* @param forceDeletion the force delete parameter
* @param callback the callback to call on success or failure
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.microsoft.azure.management.resources.Provider;
import com.microsoft.azure.management.resources.ProviderResourceType;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -159,10 +160,23 @@ public static String defaultApiVersion(String id, Provider provider) {
// Exact match
for (ProviderResourceType prt : provider.resourceTypes()) {
if (prt.resourceType().equalsIgnoreCase(resourceType)) {
return prt.apiVersions().get(0);
return prt.defaultApiVersion() == null ? prt.apiVersions().get(0) : prt.defaultApiVersion();
}
}
return defaultApiVersion(parentResourceIdFromResourceId(id), provider);
// child resource, e.g. sites/config
for (ProviderResourceType prt : provider.resourceTypes()) {
if (prt.resourceType().toLowerCase(Locale.ROOT).contains("/" + resourceType)) {
return prt.defaultApiVersion() == null ? prt.apiVersions().get(0) : prt.defaultApiVersion();
}
}
// look for parent
String parentId = parentResourceIdFromResourceId(id);
if (parentId != null) {
return defaultApiVersion(parentId, provider);
} else {
// Fallback: use a random one, not guaranteed to work
return provider.resourceTypes().get(0).apiVersions().get(0);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public T getById(String id) {
}

@Override
public final Observable<T> getByIdAsync(String id) {
public Observable<T> getByIdAsync(String id) {
ResourceId resourceId = ResourceId.fromString(id);

if (resourceId == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,67 @@ public boolean checkExistence(String resourceGroupName, String resourceProviderN

@Override
public boolean checkExistenceById(String id) {
String apiVersion = getApiVersionFromId(id).toBlocking().single();
String apiVersion = getApiVersionFromIdAsync(id).toBlocking().single();
return this.checkExistenceById(id, apiVersion);
}

@Override
public boolean checkExistenceById(String id, String apiVersion) {
return this.inner().checkExistenceById(id, apiVersion);
}

@Override
public GenericResource getById(String id) {
Provider provider = this.manager().providers().getByName(ResourceUtils.resourceProviderFromResourceId(id));
String apiVersion = ResourceUtils.defaultApiVersion(id, provider);
GenericResourceImpl genericResource = wrapModel(this.inner().getById(id, apiVersion));
return genericResource == null ? null : genericResource.withApiVersion(apiVersion);
public Observable<GenericResource> getByIdAsync(final String id) {
final GenericResourcesImpl self = this;
return this.getApiVersionFromIdAsync(id)
.flatMap(new Func1<String, Observable<GenericResource>>() {
@Override
public Observable<GenericResource> call(String s) {
return self.getByIdAsync(id, s);
}
});
}

@Override
public Completable deleteByIdAsync(final String id) {
final ResourcesInner inner = this.inner();
return this.getApiVersionFromIdAsync(id)
.flatMap(new Func1<String, Observable<Void>>() {
@Override
public Observable<Void> call(String apiVersion) {
return inner.deleteByIdAsync(id, apiVersion);
}
}).toCompletable();
}

@Override
public void deleteById(String id, String apiVersion) {
this.deleteByIdAsync(id, apiVersion).await();
}

@Override
public Completable deleteByIdAsync(String id, String apiVersion) {
return this.inner().deleteByIdAsync(id, apiVersion).toCompletable();
}

@Override
public GenericResource getById(String id, String apiVersion) {
return this.getByIdAsync(id, apiVersion).toBlocking().last();
}

@Override
public Observable<GenericResource> getByIdAsync(String id, final String apiVersion) {
return this.inner().getByIdAsync(id, apiVersion)
.map(new Func1<GenericResourceInner, GenericResource>() {
@Override
public GenericResource call(GenericResourceInner genericResourceInner) {
GenericResourceImpl genericResource = wrapModel(genericResourceInner);
if (genericResource != null) {
genericResource.withApiVersion(apiVersion);
}
return genericResource;
}
});
}

@Override
Expand Down Expand Up @@ -241,19 +292,7 @@ protected Completable deleteInnerAsync(String resourceGroupName, String name) {
throw new UnsupportedOperationException("Delete just by resource group and name is not supported. Please use other overloads.");
}

@Override
public Completable deleteByIdAsync(final String id) {
final ResourcesInner inner = this.inner();
return getApiVersionFromId(id)
.flatMap(new Func1<String, Observable<Void>>() {
@Override
public Observable<Void> call(String apiVersion) {
return inner.deleteByIdAsync(id, apiVersion);
}
}).toCompletable();
}

private Observable<String> getApiVersionFromId(final String id) {
private Observable<String> getApiVersionFromIdAsync(final String id) {
return this.manager().providers().getByNameAsync(ResourceUtils.resourceProviderFromResourceId(id))
.map(new Func1<Provider, String>() {
@Override
Expand Down
Loading

0 comments on commit 044a7fe

Please sign in to comment.