use of com.microsoft.azure.management.appservice.FunctionApp in project azure-sdk-for-java by Azure.
the class ManageFunctionAppBasic method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
// New resources
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String rg1Name = SdkContext.randomResourceName("rg1NEMV_", 24);
final String rg2Name = SdkContext.randomResourceName("rg2NEMV_", 24);
try {
//============================================================
// Create a function app with a new app service plan
System.out.println("Creating function app " + app1Name + " in resource group " + rg1Name + "...");
FunctionApp app1 = azure.appServices().functionApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rg1Name).create();
System.out.println("Created function app " + app1.name());
Utils.print(app1);
//============================================================
// Create a second function app with the same app service plan
System.out.println("Creating another function app " + app2Name + " in resource group " + rg1Name + "...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
FunctionApp app2 = azure.appServices().functionApps().define(app2Name).withRegion(Region.US_WEST).withExistingResourceGroup(rg1Name).withNewAppServicePlan(PricingTier.BASIC_B1).create();
System.out.println("Created function app " + app2.name());
Utils.print(app2);
//============================================================
// Create a third function app with the same app service plan, but
// in a different resource group
System.out.println("Creating another function app " + app3Name + " in resource group " + rg2Name + "...");
FunctionApp app3 = azure.appServices().functionApps().define(app3Name).withExistingAppServicePlan(plan).withNewResourceGroup(rg2Name).create();
System.out.println("Created function app " + app3.name());
Utils.print(app3);
//============================================================
// stop and start app1, restart app 2
System.out.println("Stopping function app " + app1.name());
app1.stop();
System.out.println("Stopped function app " + app1.name());
Utils.print(app1);
System.out.println("Starting function app " + app1.name());
app1.start();
System.out.println("Started function app " + app1.name());
Utils.print(app1);
System.out.println("Restarting function app " + app2.name());
app2.restart();
System.out.println("Restarted function app " + app2.name());
Utils.print(app2);
//=============================================================
// List function apps
System.out.println("Printing list of function apps in resource group " + rg1Name + "...");
for (FunctionApp functionApp : azure.appServices().functionApps().listByResourceGroup(rg1Name)) {
Utils.print(functionApp);
}
System.out.println("Printing list of function apps in resource group " + rg2Name + "...");
for (FunctionApp functionApp : azure.appServices().functionApps().listByResourceGroup(rg2Name)) {
Utils.print(functionApp);
}
//=============================================================
// Delete a function app
System.out.println("Deleting function app " + app1Name + "...");
azure.appServices().functionApps().deleteByResourceGroup(rg1Name, app1Name);
System.out.println("Deleted function app " + app1Name + "...");
System.out.println("Printing list of function apps in resource group " + rg1Name + " again...");
for (FunctionApp functionApp : azure.appServices().functionApps().listByResourceGroup(rg1Name)) {
Utils.print(functionApp);
}
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rg1Name);
azure.resourceGroups().beginDeleteByName(rg1Name);
System.out.println("Deleted Resource Group: " + rg1Name);
System.out.println("Deleting Resource Group: " + rg2Name);
azure.resourceGroups().beginDeleteByName(rg2Name);
System.out.println("Deleted Resource Group: " + rg2Name);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.appservice.FunctionApp in project azure-sdk-for-java by Azure.
the class ManageFunctionAppWithAuthentication method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
// New resources
final String suffix = ".azurewebsites.net";
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app1Url = app1Name + suffix;
final String app2Url = app2Name + suffix;
final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
try {
//============================================================
// Create a function app with admin level auth
System.out.println("Creating function app " + app1Name + " in resource group " + rgName + " with admin level auth...");
FunctionApp app1 = azure.appServices().functionApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withLocalGitSourceControl().create();
System.out.println("Created function app " + app1.name());
Utils.print(app1);
//============================================================
// Create a second function app with function level auth
System.out.println("Creating another function app " + app2Name + " in resource group " + rgName + " with function level auth...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
FunctionApp app2 = azure.appServices().functionApps().define(app2Name).withExistingAppServicePlan(plan).withExistingResourceGroup(rgName).withExistingStorageAccount(app1.storageAccount()).withLocalGitSourceControl().create();
System.out.println("Created function app " + app2.name());
Utils.print(app2);
//============================================================
// Deploy to app 1 through Git
System.out.println("Deploying a local function app to " + app1Name + " through Git...");
PublishingProfile profile = app1.getPublishingProfile();
Git git = Git.init().setDirectory(new File(ManageFunctionAppWithAuthentication.class.getResource("/square-function-app-admin-auth/").getPath())).call();
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
PushCommand command = git.push();
command.setRemote(profile.gitUrl());
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(profile.gitUsername(), profile.gitPassword()));
command.setRefSpecs(new RefSpec("master:master"));
command.setForce(true);
command.call();
System.out.println("Deployment to function app " + app1.name() + " completed");
Utils.print(app1);
// warm up
System.out.println("Warming up " + app1Url + "/api/square...");
post("http://" + app1Url + "/api/square", "625");
Thread.sleep(5000);
System.out.println("CURLing " + app1Url + "/api/square...");
System.out.println("Square of 625 is " + post("http://" + app1Url + "/api/square?code=" + app1.getMasterKey(), "625"));
//============================================================
// Deploy to app 2 through Git
System.out.println("Deploying a local function app to " + app2Name + " through Git...");
profile = app2.getPublishingProfile();
git = Git.init().setDirectory(new File(ManageFunctionAppWithAuthentication.class.getResource("/square-function-app-function-auth/").getPath())).call();
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
command = git.push();
command.setRemote(profile.gitUrl());
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(profile.gitUsername(), profile.gitPassword()));
command.setRefSpecs(new RefSpec("master:master"));
command.setForce(true);
command.call();
System.out.println("Deployment to function app " + app2.name() + " completed");
Utils.print(app2);
String masterKey = app2.getMasterKey();
Map<String, String> functionsHeader = new HashMap<>();
functionsHeader.put("x-functions-key", masterKey);
String response = curl("http://" + app2Url + "/admin/functions/square/keys", functionsHeader);
Pattern pattern = Pattern.compile("\"name\":\"default\",\"value\":\"([\\w=/]+)\"");
Matcher matcher = pattern.matcher(response);
matcher.find();
String functionKey = matcher.group(1);
// warm up
System.out.println("Warming up " + app2Url + "/api/square...");
post("http://" + app2Url + "/api/square", "725");
Thread.sleep(5000);
System.out.println("CURLing " + app2Url + "/api/square...");
System.out.println("Square of 725 is " + post("http://" + app2Url + "/api/square?code=" + functionKey, "725"));
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.appservice.FunctionApp in project azure-sdk-for-java by Azure.
the class ManageFunctionAppWithDomainSsl method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
// New resources
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String rgName = SdkContext.randomResourceName("rgNEMV_", 24);
final String domainName = SdkContext.randomResourceName("jsdkdemo-", 20) + ".com";
final String certPassword = "StrongPass!12";
try {
//============================================================
// Create a function app with a new app service plan
System.out.println("Creating function app " + app1Name + "...");
FunctionApp app1 = azure.appServices().functionApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rgName).create();
System.out.println("Created function app " + app1.name());
Utils.print(app1);
//============================================================
// Create a second function app with the same app service plan
System.out.println("Creating another function app " + app2Name + "...");
FunctionApp app2 = azure.appServices().functionApps().define(app2Name).withRegion(Region.US_WEST).withExistingResourceGroup(rgName).create();
System.out.println("Created function app " + app2.name());
Utils.print(app2);
//============================================================
// Purchase a domain (will be canceled for a full refund)
System.out.println("Purchasing a domain " + domainName + "...");
AppServiceDomain domain = azure.appServices().domains().define(domainName).withExistingResourceGroup(rgName).defineRegistrantContact().withFirstName("Jon").withLastName("Doe").withEmail("jondoe@contoso.com").withAddressLine1("123 4th Ave").withCity("Redmond").withStateOrProvince("WA").withCountry(CountryIsoCode.UNITED_STATES).withPostalCode("98052").withPhoneCountryCode(CountryPhoneCode.UNITED_STATES).withPhoneNumber("4258828080").attach().withDomainPrivacyEnabled(true).withAutoRenewEnabled(false).create();
System.out.println("Purchased domain " + domain.name());
Utils.print(domain);
//============================================================
// Bind domain to function app 1
System.out.println("Binding http://" + app1Name + "." + domainName + " to function app " + app1Name + "...");
app1 = app1.update().defineHostnameBinding().withAzureManagedDomain(domain).withSubDomain(app1Name).withDnsRecordType(CustomHostNameDnsRecordType.CNAME).attach().apply();
System.out.println("Finished binding http://" + app1Name + "." + domainName + " to function app " + app1Name);
Utils.print(app1);
//============================================================
// Create a self-singed SSL certificate
String pfxPath = ManageFunctionAppWithDomainSsl.class.getResource("/").getPath() + app2Name + "." + domainName + ".pfx";
String cerPath = ManageFunctionAppWithDomainSsl.class.getResource("/").getPath() + app2Name + "." + domainName + ".cer";
System.out.println("Creating a self-signed certificate " + pfxPath + "...");
Utils.createCertificate(cerPath, pfxPath, domainName, certPassword, "*." + domainName);
System.out.println("Created self-signed certificate " + pfxPath);
//============================================================
// Bind domain to function app 2 and turn on wild card SSL for both
System.out.println("Binding https://" + app1Name + "." + domainName + " to function app " + app1Name + "...");
app1 = app1.update().withManagedHostnameBindings(domain, app1Name).defineSslBinding().forHostname(app1Name + "." + domainName).withPfxCertificateToUpload(new File(pfxPath), certPassword).withSniBasedSsl().attach().apply();
System.out.println("Finished binding http://" + app1Name + "." + domainName + " to function app " + app1Name);
Utils.print(app1);
System.out.println("Binding https://" + app2Name + "." + domainName + " to function app " + app2Name + "...");
app2 = app2.update().withManagedHostnameBindings(domain, app2Name).defineSslBinding().forHostname(app2Name + "." + domainName).withPfxCertificateToUpload(new File(pfxPath), certPassword).withSniBasedSsl().attach().apply();
System.out.println("Finished binding http://" + app2Name + "." + domainName + " to function app " + app2Name);
Utils.print(app2);
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.appservice.FunctionApp in project azure-gradle-plugins by lenala.
the class FTPArtifactHandlerImpl method publish.
@Override
public void publish() throws Exception {
final FTPUploader uploader = getUploader();
final FunctionApp app = functionsTask.getFunctionApp();
final PublishingProfile profile = app.getPublishingProfile();
final String serverUrl = profile.ftpUrl().split("/", 2)[0];
uploader.uploadDirectoryWithRetries(serverUrl, profile.ftpUsername(), profile.ftpPassword(), functionsTask.getDeploymentStageDirectory(), DEFAULT_FUNCTION_ROOT, DEFAULT_MAX_RETRY_TIMES);
app.syncTriggers();
}
use of com.microsoft.azure.management.appservice.FunctionApp in project azure-sdk-for-java by Azure.
the class ManageFunctionAppSourceControl method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
// New resources
final String suffix = ".azurewebsites.net";
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String app4Name = SdkContext.randomResourceName("webapp4-", 20);
final String app1Url = app1Name + suffix;
final String app2Url = app2Name + suffix;
final String app3Url = app3Name + suffix;
final String app4Url = app4Name + suffix;
final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
try {
//============================================================
// Create a function app with a new app service plan
System.out.println("Creating function app " + app1Name + " in resource group " + rgName + "...");
FunctionApp app1 = azure.appServices().functionApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rgName).create();
System.out.println("Created function app " + app1.name());
Utils.print(app1);
//============================================================
// Deploy to app 1 through FTP
System.out.println("Deploying a function app to " + app1Name + " through FTP...");
Utils.uploadFileToFtp(app1.getPublishingProfile(), "host.json", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/host.json"));
Utils.uploadFileToFtp(app1.getPublishingProfile(), "square/function.json", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/square/function.json"));
Utils.uploadFileToFtp(app1.getPublishingProfile(), "square/index.js", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/square/index.js"));
System.out.println("Deployment square app to function app " + app1.name() + " completed");
Utils.print(app1);
// warm up
System.out.println("Warming up " + app1Url + "/api/square...");
post("http://" + app1Url + "/api/square", "625");
Thread.sleep(5000);
System.out.println("CURLing " + app1Url + "/api/square...");
System.out.println("Square of 625 is " + post("http://" + app1Url + "/api/square", "625"));
//============================================================
// Create a second function app with local git source control
System.out.println("Creating another function app " + app2Name + " in resource group " + rgName + "...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
FunctionApp app2 = azure.appServices().functionApps().define(app2Name).withExistingAppServicePlan(plan).withExistingResourceGroup(rgName).withExistingStorageAccount(app1.storageAccount()).withLocalGitSourceControl().create();
System.out.println("Created function app " + app2.name());
Utils.print(app2);
//============================================================
// Deploy to app 2 through local Git
System.out.println("Deploying a local Tomcat source to " + app2Name + " through Git...");
PublishingProfile profile = app2.getPublishingProfile();
Git git = Git.init().setDirectory(new File(ManageFunctionAppSourceControl.class.getResource("/square-function-app/").getPath())).call();
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
PushCommand command = git.push();
command.setRemote(profile.gitUrl());
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(profile.gitUsername(), profile.gitPassword()));
command.setRefSpecs(new RefSpec("master:master"));
command.setForce(true);
command.call();
System.out.println("Deployment to function app " + app2.name() + " completed");
Utils.print(app2);
// warm up
System.out.println("Warming up " + app2Url + "/api/square...");
post("http://" + app2Url + "/api/square", "725");
Thread.sleep(5000);
System.out.println("CURLing " + app2Url + "/api/square...");
System.out.println("Square of 725 is " + post("http://" + app2Url + "/api/square", "725"));
//============================================================
// Create a 3rd function app with a public GitHub repo in Azure-Samples
System.out.println("Creating another function app " + app3Name + "...");
FunctionApp app3 = azure.appServices().functionApps().define(app3Name).withExistingAppServicePlan(plan).withNewResourceGroup(rgName).withExistingStorageAccount(app2.storageAccount()).defineSourceControl().withPublicGitRepository("https://github.com/jianghaolu/square-function-app-sample").withBranch("master").attach().create();
System.out.println("Created function app " + app3.name());
Utils.print(app3);
// warm up
System.out.println("Warming up " + app3Url + "/api/square...");
post("http://" + app3Url + "/api/square", "825");
Thread.sleep(5000);
System.out.println("CURLing " + app3Url + "/api/square...");
System.out.println("Square of 825 is " + post("http://" + app3Url + "/api/square", "825"));
//============================================================
// Create a 4th function app with a personal GitHub repo and turn on continuous integration
System.out.println("Creating another function app " + app4Name + "...");
FunctionApp app4 = azure.appServices().functionApps().define(app4Name).withExistingAppServicePlan(plan).withExistingResourceGroup(rgName).withExistingStorageAccount(app3.storageAccount()).create();
System.out.println("Created function app " + app4.name());
Utils.print(app4);
// warm up
System.out.println("Warming up " + app4Url + "...");
curl("http://" + app4Url);
Thread.sleep(5000);
System.out.println("CURLing " + app4Url + "...");
System.out.println(curl("http://" + app4Url));
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
Aggregations