Skip to content

Commit

Permalink
Supports using existing plan from another group
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Dec 3, 2016
1 parent be1f482 commit c22fc76
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public interface AppServicePlans extends
/**
* Gets the information about a resource from Azure based on the resource name and the name of its resource group.
*
* @param resourceGroupName the name of the resource group the resource is in
* @param name the name of the resource. (Note, this is not the ID)
* @param id the app service plan resource ID
* @return an immutable representation of the resource
*/
Observable<AppServicePlan> getByGroupAsync(String resourceGroupName, String name);
Observable<AppServicePlan> getByIdAsync(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,18 @@ interface WithAppServicePlan {
WithRegion withNewAppServicePlan(String name);

/**
* Uses an existing app service plan for the web app.
* Uses an existing app service plan in the same resource group for the web app.
* @param appServicePlanName the name of the existing app service plan
* @return the next stage of the web app definition
*/
WebAppBase.DefinitionStages.WithHostNameBinding<WebApp> withExistingAppServicePlan(String appServicePlanName);

/**
* Uses an existing app service plan for the web app.
* @param appServicePlan the existing app service plan
* @return the next stage of the web app definition
*/
WebAppBase.DefinitionStages.WithHostNameBinding<WebApp> withExistingAppServicePlan(AppServicePlan appServicePlan);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
package com.microsoft.azure.management.appservice.implementation;

import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.GroupableResourcesImpl;
import com.microsoft.azure.management.appservice.AppServicePlan;
import com.microsoft.azure.management.appservice.AppServicePlans;
import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.GroupableResourcesImpl;
import rx.Observable;
import rx.functions.Func1;

Expand All @@ -31,7 +32,7 @@ class AppServicePlansImpl

@Override
public AppServicePlan getByGroup(String groupName, String name) {
return getByGroupAsync(groupName, name).toBlocking().single();
return wrapModel(innerCollection.get(groupName, name));
}

@Override
Expand Down Expand Up @@ -69,8 +70,10 @@ public AppServicePlanImpl define(String name) {
}

@Override
public Observable<AppServicePlan> getByGroupAsync(String resourceGroupName, String name) {
return innerCollection.getAsync(resourceGroupName, name)
public Observable<AppServicePlan> getByIdAsync(String id) {
return innerCollection.getAsync(
ResourceUtils.groupFromResourceId(id),
ResourceUtils.nameFromResourceId(id))
.map(new Func1<AppServicePlanInner, AppServicePlan>() {
@Override
public AppServicePlan call(AppServicePlanInner appServicePlanInner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public Observable<FluentT> createResourceAsync() {
}
Observable<String> locationObs = Observable.just(inner().location());
if (inner().location() == null) {
locationObs = myManager.appServicePlans().getByGroupAsync(resourceGroupName(), inner().serverFarmId())
locationObs = myManager.appServicePlans().getByIdAsync(inner().serverFarmId())
.map(new Func1<AppServicePlan, String>() {
@Override
public String call(AppServicePlan appServicePlan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.microsoft.azure.management.appservice.AppServicePlan;
import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils;
import com.microsoft.azure.management.resources.fluentcore.model.Wrapper;
import com.microsoft.azure.management.resources.implementation.ResourceGroupInner;
import com.microsoft.azure.management.appservice.AppServicePricingTier;
Expand Down Expand Up @@ -199,7 +201,9 @@ public WebAppImpl refresh() {
@Override
public WebAppImpl withNewAppServicePlan(String name) {
appServicePlan = (AppServicePlanImpl) myManager.appServicePlans().define(name);
inner().withServerFarmId(name);
String id = ResourceUtils.constructResourceId(myManager.subscriptionId(),
resourceGroupName(), "Microsoft.Web", "serverFarms", name, "");
inner().withServerFarmId(id);
return this;
}

Expand All @@ -226,7 +230,19 @@ public WebAppImpl withPricingTier(AppServicePricingTier pricingTier) {

@Override
public WebAppImpl withExistingAppServicePlan(String appServicePlanName) {
inner().withServerFarmId(appServicePlanName);
String id = ResourceUtils.constructResourceId(myManager.subscriptionId(),
resourceGroupName(), "Microsoft.Web", "serverFarms", appServicePlanName, "");
inner().withServerFarmId(id);
return this;
}

@Override
@SuppressWarnings("unchecked")
public WebAppImpl withExistingAppServicePlan(AppServicePlan appServicePlan) {
inner().withServerFarmId(appServicePlan.id());
if (super.creatableGroup != null) {
((Wrapper<ResourceGroupInner>) super.creatableGroup).inner().withLocation(appServicePlan.regionName());
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void canCRUDWebApp() throws Exception {
.defineSslBinding()
.forHostname("pineapple.graph-webapp-319.com")
.withExistingAppServiceCertificateOrder(appServiceManager.certificateOrders().getByGroup(RG_NAME, "graphwildcert"))
// .withNewKeyVault("orangecertvault")
.withSniBasedSsl()
.attach()
.withRemoteDebuggingEnabled(RemoteVisualStudioVersion.VS2013)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
package com.microsoft.azure.management.appservice.samples;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.keyvault.Vault;
import com.microsoft.azure.management.resources.fluentcore.arm.CountryISOCode;
import com.microsoft.azure.management.resources.fluentcore.arm.CountryPhoneCode;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import com.microsoft.azure.management.samples.Utils;
import com.microsoft.azure.management.appservice.AppServiceCertificateOrder;
import com.microsoft.azure.management.appservice.AppServiceDomain;
import com.microsoft.azure.management.appservice.AppServicePricingTier;
import com.microsoft.azure.management.appservice.CustomHostNameDnsRecordType;
import com.microsoft.azure.management.appservice.DeploymentSlot;
import com.microsoft.azure.management.appservice.WebApp;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import com.microsoft.azure.management.samples.Utils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
Expand Down Expand Up @@ -56,15 +53,17 @@ public final class ManageAppService {
* @param args the parameters
*/
public static void main(String[] args) {
// Existing resources
final String domainName = "jsdk79877.com";
final String certName = "wild2crt8b42374211";
final String domainCertRg = "rgnemv24d683784f51d";

// New resources
final String app1Name = ResourceNamer.randomResourceName("webapp1", 20);
final String app2Name = ResourceNamer.randomResourceName("webapp2", 20);
final String slot1Name = ResourceNamer.randomResourceName("slot1", 20);
final String slot2Name = ResourceNamer.randomResourceName("slot2", 20);
final String planName = ResourceNamer.randomResourceName("jplan", 15);
final String domainName = ResourceNamer.randomResourceName("jsdk", 10) + ".com";
final String cert1Name = ResourceNamer.randomResourceName("std1crt", 20);
final String cert2Name = ResourceNamer.randomResourceName("wild2crt", 20);
final String vaultName = ResourceNamer.randomResourceName("demovault", 20);
final String rgName = ResourceNamer.randomResourceName("rgNEMV", 24);

try {
Expand All @@ -76,7 +75,7 @@ public static void main(String[] args) {

Azure azure = Azure
.configure()
.withLogLevel(HttpLoggingInterceptor.Level.BODY)
.withLogLevel(HttpLoggingInterceptor.Level.BASIC)
.authenticate(credFile)
.withDefaultSubscription();

Expand Down Expand Up @@ -121,92 +120,59 @@ public static void main(String[] args) {
System.out.println("Purchasing a domain " + domainName + "...");

AppServiceDomain domain = azure.appServices().domains()
.define(domainName)
.withExistingResourceGroup(rgName)
.defineRegistrantContact()
.withFirstName("Microsoft")
.withLastName("Azure")
.withEmail("[email protected]")
.withAddressLine1("1 Microsoft Way")
.withCity("Redmond")
.withStateOrProvince("Washington")
.withCountry(CountryISOCode.UNITED_STATES)
.withPostalCode("98052")
.withPhoneCountryCode(CountryPhoneCode.UNITED_STATES)
.withPhoneNumber("4258828080")
.attach()
.withDomainPrivacyEnabled(true)
.create();
.getByGroup(domainCertRg, domainName);

System.out.println("Purchased domain " + domain.name());
Utils.print(domain);

//============================================================
// Bind domain to web app 1

System.out.println("Binding http://app1." + domainName + " to web app " + app1Name + "...");
System.out.println("Binding http://" + app1Name + "." + domainName + " to web app " + app1Name + "...");

app1 = app1.update()
.defineHostnameBinding()
.withAzureManagedDomain(domain)
.withSubDomain("app1")
.withSubDomain(app1Name)
.withDnsRecordType(CustomHostNameDnsRecordType.CNAME)
.attach()
.apply();

System.out.println("Finish binding http://app1." + domainName + " to web app " + app1Name + "...");
System.out.println("Finish binding http://" + app1Name + "." + domainName + " to web app " + app1Name + "...");
Utils.print(app1);

System.out.println("CURLing http://app1." + domainName);
System.out.println(curl("http://app1." + domainName));
System.out.println("CURLing http://" + app1Name + "." + domainName);
System.out.println(curl("http://" + app1Name + "." + domainName));

//============================================================
// Purchase a wild card SSL certificate (will be canceled for a full refund)

System.out.println("Purchasing a wildcard SSL certificate " + cert2Name + "...");

Vault vault = azure.vaults().getByGroup("javatestrg", "javatestautovault");
AppServiceCertificateOrder certificateOrder = azure.appServices().certificateOrders()
.define(cert2Name)
.withExistingResourceGroup(rgName)
.withHostName("*." + domainName)
.withWildcardSku()
.withDomainVerification(domain)
.withExistingKeyVault(vault)
.withValidYears(1)
.create();
.getByGroup(domainCertRg, certName);

System.out.println("Wildcard Certificate " + cert2Name + " is ready to use.");
Utils.print(certificateOrder);

//============================================================
// Bind domain to web app 2 and turn on wild card SSL

System.out.println("Binding @/www/app2." + domainName + " to web app " + app2Name + "...");
System.out.println("Binding https://" + app2Name + "." + domainName + " to web app " + app2Name + "...");
app2 = app2.update()
.withManagedHostnameBindings(domain, "app2", "@", "www")
.defineSslBinding()
.forHostname(domainName)
.withExistingAppServiceCertificateOrder(certificateOrder)
.withSniBasedSsl()
.attach()
.withManagedHostnameBindings(domain, app2Name)
.defineSslBinding()
.forHostname("www." + domainName)
.withExistingAppServiceCertificateOrder(certificateOrder)
.withSniBasedSsl()
.attach()
.defineSslBinding()
.forHostname("app2." + domainName)
.forHostname(app2Name + "." + domainName)
.withExistingAppServiceCertificateOrder(certificateOrder)
.withSniBasedSsl()
.attach()
.apply();

System.out.println("Finished binding @/www/app2." + domainName + " to web app " + app2Name + "...");
System.out.println("Finished binding http://" + app2Name + "." + domainName + " to web app " + app2Name + "...");
Utils.print(app2);

System.out.println("CURLing https://www." + domainName);
System.out.println(curl("https://www." + domainName));
// Make a call to warm up with web app
curl("https://" + app2Name + "." + domainName);

System.out.println("CURLing https://" + app2Name + "." + domainName);
System.out.println(curl("https://" + app2Name + "." + domainName));

//============================================================
// Create 2 slots under web app 2
Expand All @@ -216,7 +182,7 @@ public static void main(String[] args) {

DeploymentSlot slot1 = app2.deploymentSlots().define(slot1Name)
.withBrandNewConfiguration()
.withManagedHostnameBindings(domain, "slot1")
.withManagedHostnameBindings(domain, slot1Name)
.withAutoSwapSlotName("production")
.create();

Expand All @@ -228,9 +194,9 @@ public static void main(String[] args) {

DeploymentSlot slot2 = app2.deploymentSlots().define(slot2Name)
.withConfigurationFromDeploymentSlot(slot1)
.withManagedHostnameBindings(domain, "slot2")
.withManagedHostnameBindings(domain, slot2Name)
.defineSslBinding()
.forHostname("slot2." + domainName)
.forHostname(slot2Name + "." + domainName)
.withExistingAppServiceCertificateOrder(certificateOrder)
.withSniBasedSsl()
.attach()
Expand All @@ -249,7 +215,7 @@ public static void main(String[] args) {
slot1 = slot1.update()
.withAutoSwapSlotName(null) // this will not affect slot 2
.defineSslBinding()
.forHostname("slot1." + domainName)
.forHostname(slot1Name + "." + domainName)
.withExistingAppServiceCertificateOrder(certificateOrder)
.withSniBasedSsl()
.attach()
Expand All @@ -274,8 +240,11 @@ public static void main(String[] args) {
System.out.println("Finished deploying public GitHub repo to slot " + slot2Name);
Utils.print(slot2);

System.out.println("CURLing https://www." + domainName + ". Should contain auto-swapped slot 2 content.");
System.out.println(curl("https://www." + domainName));
// Make a call to warm up with web app
curl("https://" + app2Name + "." + domainName);

System.out.println("CURLing https://" + app2Name + "." + domainName + ". Should contain auto-swapped slot 2 content.");
System.out.println(curl("https://" + app2Name + "." + domainName));

} catch (Exception e) {
System.err.println(e.getMessage());
Expand Down

0 comments on commit c22fc76

Please sign in to comment.