Skip to content

Commit

Permalink
Merge pull request #1211 from jianghaolu/apiversion
Browse files Browse the repository at this point in the history
Make default api version logic more robust
  • Loading branch information
Martin Sawicki authored Oct 20, 2016
2 parents c786da9 + 83dea26 commit 1fc4ca8
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public int capacity() {
@Override
public Network getPrimaryNetwork() throws IOException {
String subnetId = primaryNicDefaultIPConfiguration().subnet().id();
String virtualNetworkId = ResourceUtils.parentResourcePathFromResourceId(subnetId);
String virtualNetworkId = ResourceUtils.parentResourceIdFromResourceId(subnetId);
return this.networkManager
.networks()
.getById(virtualNetworkId);
Expand Down Expand Up @@ -374,12 +374,12 @@ public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer l
String lbNetworkId = null;
for (LoadBalancerFrontend frontEnd : loadBalancer.frontends().values()) {
if (frontEnd.inner().subnet().id() != null) {
lbNetworkId = ResourceUtils.parentResourcePathFromResourceId(frontEnd.inner().subnet().id());
lbNetworkId = ResourceUtils.parentResourceIdFromResourceId(frontEnd.inner().subnet().id());
}
}

if (isInCreateMode()) {
String vmNICNetworkId = ResourceUtils.parentResourcePathFromResourceId(this.existingPrimaryNetworkSubnetNameToAssociate);
String vmNICNetworkId = ResourceUtils.parentResourceIdFromResourceId(this.existingPrimaryNetworkSubnetNameToAssociate);
// Azure has a really wired BUG that - it throws exception when vnet of VMSS and LB are not same
// (code: NetworkInterfaceAndInternalLoadBalancerMustUseSameVnet) but at the same time Azure update
// the VMSS's network section to refer this invalid internal LB. This makes VMSS un-usable and portal
Expand All @@ -396,7 +396,7 @@ public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer l
associateLoadBalancerToIpConfiguration(this.primaryInternalLoadBalancer,
this.primaryNicDefaultIPConfiguration());
} else {
String vmNicVnetId = ResourceUtils.parentResourcePathFromResourceId(primaryNicDefaultIPConfiguration()
String vmNicVnetId = ResourceUtils.parentResourceIdFromResourceId(primaryNicDefaultIPConfiguration()
.subnet()
.id());
if (!vmNicVnetId.equalsIgnoreCase(lbNetworkId)) {
Expand Down Expand Up @@ -1109,12 +1109,12 @@ private void loadCurrentPrimaryLoadBalancersIfAvailable() throws IOException {
VirtualMachineScaleSetIPConfigurationInner ipConfig = primaryNicDefaultIPConfiguration();
if (!ipConfig.loadBalancerBackendAddressPools().isEmpty()) {
firstLoadBalancerId = ResourceUtils
.parentResourcePathFromResourceId(ipConfig.loadBalancerBackendAddressPools().get(0).id());
.parentResourceIdFromResourceId(ipConfig.loadBalancerBackendAddressPools().get(0).id());
}

if (firstLoadBalancerId == null && !ipConfig.loadBalancerInboundNatPools().isEmpty()) {
firstLoadBalancerId = ResourceUtils
.parentResourcePathFromResourceId(ipConfig.loadBalancerInboundNatPools().get(0).id());
.parentResourceIdFromResourceId(ipConfig.loadBalancerInboundNatPools().get(0).id());
}

if (firstLoadBalancerId == null) {
Expand All @@ -1134,7 +1134,7 @@ private void loadCurrentPrimaryLoadBalancersIfAvailable() throws IOException {
for (SubResource subResource: ipConfig.loadBalancerBackendAddressPools()) {
if (!subResource.id().toLowerCase().startsWith(firstLoadBalancerId.toLowerCase())) {
secondLoadBalancerId = ResourceUtils
.parentResourcePathFromResourceId(subResource.id());
.parentResourceIdFromResourceId(subResource.id());
break;
}
}
Expand All @@ -1143,7 +1143,7 @@ private void loadCurrentPrimaryLoadBalancersIfAvailable() throws IOException {
for (SubResource subResource: ipConfig.loadBalancerInboundNatPools()) {
if (!subResource.id().toLowerCase().startsWith(firstLoadBalancerId.toLowerCase())) {
secondLoadBalancerId = ResourceUtils
.parentResourcePathFromResourceId(subResource.id());
.parentResourceIdFromResourceId(subResource.id());
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String backendNetworkInterfaceId() {
if (this.inner().backendIPConfiguration() == null) {
return null;
} else {
return ResourceUtils.parentResourcePathFromResourceId(this.inner().backendIPConfiguration().id());
return ResourceUtils.parentResourceIdFromResourceId(this.inner().backendIPConfiguration().id());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Map<String, String> backendNicIpConfigurationNames() {
final Map<String, String> ipConfigNames = new TreeMap<>();
if (this.inner().backendIPConfigurations() != null) {
for (NetworkInterfaceIPConfigurationInner inner : this.inner().backendIPConfigurations()) {
String nicId = ResourceUtils.parentResourcePathFromResourceId(inner.id());
String nicId = ResourceUtils.parentResourceIdFromResourceId(inner.id());
String ipConfigName = ResourceUtils.nameFromResourceId(inner.id());
ipConfigNames.put(nicId, ipConfigName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LoadBalancerFrontendImpl
public String networkId() {
SubResource subnetRef = this.inner().subnet();
if (subnetRef != null) {
return ResourceUtils.parentResourcePathFromResourceId(subnetRef.id());
return ResourceUtils.parentResourceIdFromResourceId(subnetRef.id());
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ List<Subnet> listAssociatedSubnets(List<SubnetInner> subnetRefs) {

if (subnetRefs != null) {
for (SubnetInner subnetRef : subnetRefs) {
String networkId = ResourceUtils.parentResourcePathFromResourceId(subnetRef.id());
String networkId = ResourceUtils.parentResourceIdFromResourceId(subnetRef.id());
Network network = networks.get(networkId);
if (network == null) {
network = this.networks().getById(networkId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public String subnetName() {
@Override
public String networkId() {
SubResource subnetRef = this.inner().subnet();
return (subnetRef != null) ? ResourceUtils.parentResourcePathFromResourceId(subnetRef.id()) : null;
return (subnetRef != null) ? ResourceUtils.parentResourceIdFromResourceId(subnetRef.id()) : null;
}

@Override
Expand Down Expand Up @@ -393,7 +393,7 @@ public List<InboundNatRule> listAssociatedLoadBalancerInboundNatRules() {

if (refs != null) {
for (InboundNatRuleInner ref : refs) {
String loadBalancerId = ResourceUtils.parentResourcePathFromResourceId(ref.id());
String loadBalancerId = ResourceUtils.parentResourceIdFromResourceId(ref.id());
LoadBalancer loadBalancer = loadBalancers.get(loadBalancerId);
if (loadBalancer == null) {
loadBalancer = this.parent().manager().loadBalancers().getById(loadBalancerId);
Expand All @@ -416,7 +416,7 @@ public List<LoadBalancerBackend> listAssociatedLoadBalancerBackends() {

if (backendRefs != null) {
for (BackendAddressPoolInner backendRef : backendRefs) {
String loadBalancerId = ResourceUtils.parentResourcePathFromResourceId(backendRef.id());
String loadBalancerId = ResourceUtils.parentResourceIdFromResourceId(backendRef.id());
LoadBalancer loadBalancer = loadBalancers.get(loadBalancerId);
if (loadBalancer == null) {
loadBalancer = this.parent().manager().loadBalancers().getById(loadBalancerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public boolean hasAssignedLoadBalancer() {
public PublicFrontend getAssignedLoadBalancerFrontend() {
if (this.hasAssignedLoadBalancer()) {
final String refId = this.inner().ipConfiguration().id();
final String loadBalancerId = ResourceUtils.parentResourcePathFromResourceId(refId);
final String loadBalancerId = ResourceUtils.parentResourceIdFromResourceId(refId);
final LoadBalancer lb = this.myManager.loadBalancers().getById(loadBalancerId);
final String frontendName = ResourceUtils.nameFromResourceId(refId);
return (PublicFrontend) lb.frontends().get(frontendName);
Expand All @@ -199,7 +199,7 @@ public boolean hasAssignedNetworkInterface() {
public NicIpConfiguration getAssignedNetworkInterfaceIpConfiguration() {
if (this.hasAssignedNetworkInterface()) {
final String refId = this.inner().ipConfiguration().id();
final String parentId = ResourceUtils.parentResourcePathFromResourceId(refId);
final String parentId = ResourceUtils.parentResourceIdFromResourceId(refId);
final NetworkInterface nic = this.myManager.networkInterfaces().getById(parentId);
final String childName = ResourceUtils.nameFromResourceId(refId);
return nic.ipConfigurations().get(childName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface GenericResource extends
/**
* @return the id of the parent resource if this is a child resource
*/
String parentResourceId();
String parentResourcePath();

/**
* @return the type of the resource
Expand Down Expand Up @@ -124,14 +124,14 @@ interface WithPlan {
* @param promotionCode the promotion code, if any
* @return the next stage of the generic resource definition
*/
WithApiVersion withPlan(String name, String publisher, String product, String promotionCode);
WithCreate withPlan(String name, String publisher, String product, String promotionCode);

/**
* Specifies the plan of the resource.
*
* @return the next stage of the generic resource definition
*/
WithApiVersion withoutPlan();
WithCreate withoutPlan();
}

/**
Expand All @@ -157,7 +157,15 @@ interface WithParentResource {
* @param parentResourceId the parent resource id
* @return the next stage of the generic resource definition
*/
WithCreate withParentResource(String parentResourceId);
WithCreate withParentResourceId(String parentResourceId);

/**
* Specifies the parent resource relative path.
*
* @param parentResourcePath the relative path of parent resource
* @return the next stage of the generic resource definition
*/
WithCreate withParentResourcePath(String parentResourcePath);
}

/**
Expand All @@ -167,6 +175,7 @@ interface WithParentResource {
*/
interface WithCreate extends
WithParentResource,
WithApiVersion,
Creatable<GenericResource>,
Resource.DefinitionWithTags<WithCreate> {
/**
Expand Down Expand Up @@ -203,10 +212,18 @@ interface WithParentResource {
/**
* Specifies the parent resource.
*
* @param parentResourceId the parent resource ID
* @return the next stage of the generic resource definition
* @param parentResourceId the parent resource id
* @return the next stage of the generic resource update
*/
Update withParentResourceId(String parentResourceId);

/**
* Specifies the parent resource relative path.
*
* @param parentResourcePath the relative path of parent resource
* @return the next stage of the generic resource update
*/
Update withParentResource(String parentResourceId);
Update withParentResourcePath(String parentResourcePath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

package com.microsoft.azure.management.resources.fluentcore.arm;

import com.microsoft.azure.management.resources.Provider;
import com.microsoft.azure.management.resources.ProviderResourceType;

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

Expand Down Expand Up @@ -46,16 +49,47 @@ public static String resourceTypeFromResourceId(String id) {
return splits[splits.length - 2];
}

/**
* Extract parent resource ID from a resource ID string.
* E.g. subscriptions/s/resourcegroups/r/foos/foo/bars/bar will return
* subscriptions/s/resourcegroups/r/foos/foo.
*
* @param id the resource ID string
* @return the parent resource ID
*/
public static String parentResourceIdFromResourceId(String id) {
if (id == null) {
return null;
}
String parentId = id.replace("/" + resourceTypeFromResourceId(id) + "/" + nameFromResourceId(id), "");
Pattern pattern = Pattern.compile("[-\\w._/]+/providers/Microsoft.[\\w]+$");
Matcher matcher = pattern.matcher(id);
if (matcher.find()) {
return null;
} else {
return parentId;
}
}

/**
* Extract parent resource path from a resource ID string.
* E.g. subscriptions/s/resourcegroups/r/foos/foo/bars/bar will return foos/foo.
*
* @param id the resource ID string
* @return the parent resource path
* @return the parent resource ID
*/
public static String parentResourcePathFromResourceId(String id) {
public static String parentRelativePathFromResourceId(String id) {
if (id == null) {
return null;
}
return id.replace("/" + resourceTypeFromResourceId(id) + "/" + nameFromResourceId(id), "");
String parentId = id.replace("/" + resourceTypeFromResourceId(id) + "/" + nameFromResourceId(id), "");
Pattern pattern = Pattern.compile("[-\\w._/]+/providers/Microsoft.[\\w]+$");
Matcher matcher = pattern.matcher(id);
if (matcher.find()) {
return "";
} else {
return relativePathFromResourceId(parentId);
}
}

/**
Expand Down Expand Up @@ -89,7 +123,7 @@ public static String extractFromResourceId(String id, String identifier) {
if (id == null || identifier == null) {
return id;
}
Pattern pattern = Pattern.compile(identifier + "/[-\\w\\._]+");
Pattern pattern = Pattern.compile(identifier + "/[-\\w._]+");
Matcher matcher = pattern.matcher(id);
if (matcher.find()) {
return matcher.group().split("/")[1];
Expand All @@ -110,4 +144,69 @@ public static String nameFromResourceId(String id) {
String[] splits = id.split("/");
return splits[splits.length - 1];
}

/**
* Find out the default api version to make a REST request with from
* the resource provider.
*
* @param id the resource ID
* @param provider the resource provider
* @return the default api version to use
*/
public static String defaultApiVersion(String id, Provider provider) {
String resourceType = resourceTypeFromResourceId(id).toLowerCase();
// Exact match
for (ProviderResourceType prt : provider.resourceTypes()) {
if (prt.resourceType().equalsIgnoreCase(resourceType)) {
return prt.apiVersions().get(0);
}
}
// child resource, e.g. sites/config
for (ProviderResourceType prt : provider.resourceTypes()) {
if (prt.resourceType().toLowerCase().contains("/" + resourceType)) {
return prt.apiVersions().get(0);
}
}
// 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);
}
}

/**
* Creates a resource ID from information of a generic resource.
*
* @param subscriptionId the subscription UUID
* @param resourceGroupName the resource group name
* @param resourceProviderNamespace the resource provider namespace
* @param resourceType the type of the resource or nested resource
* @param resourceName name of the resource or nested resource
* @param parentResourcePath parent resource's relative path to the provider,
* if the resource is a generic resource
* @return the resource ID string
*/
public static String constructResourceId(
final String subscriptionId,
final String resourceGroupName,
final String resourceProviderNamespace,
final String resourceType,
final String resourceName,
final String parentResourcePath) {
String prefixedParentPath = parentResourcePath;
if (parentResourcePath != null && !parentResourcePath.isEmpty()) {
prefixedParentPath = "/" + parentResourcePath;
}
return String.format(
"/subscriptions/%s/resourcegroups/%s/providers/%s%s/%s/%s",
subscriptionId,
resourceGroupName,
resourceProviderNamespace,
prefixedParentPath,
resourceType,
resourceName);
}
}
Loading

0 comments on commit 1fc4ca8

Please sign in to comment.