Java Code Examples for com.microsoft.azure.PagedList#get()

The following examples show how to use com.microsoft.azure.PagedList#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AzureTests.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Tests basic generic resources retrieval.
 * @throws Exception
 */
@Test
public void testGenericResources() throws Exception {
    // Create some resources
    NetworkSecurityGroup nsg = azure.networkSecurityGroups().define(SdkContext.randomResourceName("nsg", 13))
        .withRegion(Region.US_EAST)
        .withNewResourceGroup()
        .create();
    azure.publicIPAddresses().define(SdkContext.randomResourceName("pip", 13))
        .withRegion(Region.US_EAST)
        .withExistingResourceGroup(nsg.resourceGroupName())
        .create();

    PagedList<GenericResource> resources = azure.genericResources().listByResourceGroup(nsg.resourceGroupName());
    Assert.assertEquals(2, resources.size());
    GenericResource firstResource = resources.get(0);

    GenericResource resourceById = azure.genericResources().getById(firstResource.id());
    GenericResource resourceByDetails = azure.genericResources().get(
            firstResource.resourceGroupName(),
            firstResource.resourceProviderNamespace(),
            firstResource.resourceType(),
            firstResource.name());
    Assert.assertTrue(resourceById.id().equalsIgnoreCase(resourceByDetails.id()));
    azure.resourceGroups().beginDeleteByName(nsg.resourceGroupName());
}
 
Example 2
Source File: RegistryTaskTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
@Ignore("Required to setup a github repo with proper file structure")
public void FileTaskRunRequestFromRegistry() {
    final String acrName = generateRandomResourceName("acr", 10);
    final String rgName = generateRandomResourceName("rgacr", 10);
    String sourceLocation = "URL of your source repository.";
    String taskFilePath = "Path to your file task that is relative to your source repository URL.";

    Registry registry = registryManager.containerRegistries().define(acrName)
            .withRegion(Region.US_WEST_CENTRAL)
            .withNewResourceGroup(rgName)
            .withPremiumSku()
            .withRegistryNameAsAdminUser()
            .withTag("tag1", "value1")
            .create();

    RegistryTaskRun registryTaskRun = registry.scheduleRun()
            .withWindows()
            .withFileTaskRunRequest()
                .defineFileTaskStep()
                    .withTaskPath(taskFilePath)
                    .attach()
                .withSourceLocation(sourceLocation)
            .withArchiveEnabled(true)
            .execute();

    registryTaskRun.refresh();
    Assert.assertEquals(registry.resourceGroupName(), registryTaskRun.resourceGroupName());
    Assert.assertEquals(acrName, registryTaskRun.registryName());
    Assert.assertTrue(registryTaskRun.isArchiveEnabled());
    Assert.assertEquals(OS.WINDOWS,registryTaskRun.platform().os());

    PagedList<RegistryTaskRun> registryTaskRuns = registryManager.registryTaskRuns().listByRegistry(rgName, acrName);
    RegistryTaskRun registryTaskRunFromList = registryTaskRuns.get(0);
    Assert.assertTrue(registryTaskRunFromList.status() != null);
    Assert.assertEquals("QuickRun", registryTaskRunFromList.runType().toString());
    Assert.assertTrue(registryTaskRunFromList.isArchiveEnabled());
    Assert.assertEquals(OS.WINDOWS, registryTaskRunFromList.platform().os());
    Assert.assertEquals("Succeeded", registryTaskRunFromList.provisioningState().toString());



}
 
Example 3
Source File: RegistryTaskTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
@Ignore("Required to setup a github repo with proper file structure")
public void EncodedTaskRunRequestFromRegistry() {
    final String acrName = generateRandomResourceName("acr", 10);
    final String rgName = generateRandomResourceName("rgacr", 10);
    String sourceLocation = "URL of your source repository.";
    String encodedTaskContent = "Base64 encoded task content.";

    Registry registry = registryManager.containerRegistries().define(acrName)
            .withRegion(Region.US_WEST_CENTRAL)
            .withNewResourceGroup(rgName)
            .withPremiumSku()
            .withRegistryNameAsAdminUser()
            .withTag("tag1", "value1")
            .create();

    RegistryTaskRun registryTaskRun = registry.scheduleRun()
            .withLinux()
            .withEncodedTaskRunRequest()
                .defineEncodedTaskStep()
                    .withBase64EncodedTaskContent(encodedTaskContent)
                    .attach()
                .withSourceLocation(sourceLocation)
            .withArchiveEnabled(true)
            .execute();


    registryTaskRun.refresh();

    Assert.assertEquals(registry.resourceGroupName(), registryTaskRun.resourceGroupName());
    Assert.assertEquals(acrName, registryTaskRun.registryName());
    Assert.assertTrue(registryTaskRun.isArchiveEnabled());
    Assert.assertEquals(OS.LINUX,registryTaskRun.platform().os());

    PagedList<RegistryTaskRun> registryTaskRuns = registryManager.registryTaskRuns().listByRegistry(rgName, acrName);
    RegistryTaskRun registryTaskRunFromList = registryTaskRuns.get(0);
    Assert.assertTrue(registryTaskRunFromList.status() != null);
    Assert.assertEquals("QuickRun", registryTaskRunFromList.runType().toString());
    Assert.assertTrue(registryTaskRunFromList.isArchiveEnabled());
    Assert.assertEquals(OS.LINUX, registryTaskRunFromList.platform().os());
    Assert.assertEquals("Succeeded", registryTaskRunFromList.provisioningState().toString());
}
 
Example 4
Source File: RegistryTaskTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
@Ignore("Required to setup a github repo with proper file structure")
public void DockerTaskRunRequestFromRegistry() {
    final String acrName = generateRandomResourceName("acr", 10);
    final String rgName = generateRandomResourceName("rgacr", 10);
    String dockerFilePath = "Replace with your docker file path relative to githubContext, eg: Dockerfile";
    String imageName = "Replace with the name of your image.";
    String sourceLocation = "URL of your source repository.";

    Registry registry = registryManager.containerRegistries().define(acrName)
            .withRegion(Region.US_WEST_CENTRAL)
            .withNewResourceGroup(rgName)
            .withPremiumSku()
            .withRegistryNameAsAdminUser()
            .withTag("tag1", "value1")
            .create();

    RegistryTaskRun registryTaskRun = registry.scheduleRun()
            .withLinux()
            .withDockerTaskRunRequest()
                .defineDockerTaskStep()
                    .withDockerFilePath(dockerFilePath)
                    .withImageNames(Arrays.asList(imageName))
                    .withCacheEnabled(true)
                    .withPushEnabled(true)
                    .attach()
                .withSourceLocation(sourceLocation)
            .withArchiveEnabled(true)
            .execute();

    registryTaskRun.refresh();
    Assert.assertEquals(registry.resourceGroupName(), registryTaskRun.resourceGroupName());
    Assert.assertEquals(acrName, registryTaskRun.registryName());
    Assert.assertTrue(registryTaskRun.isArchiveEnabled());
    Assert.assertEquals(OS.LINUX,registryTaskRun.platform().os());

    PagedList<RegistryTaskRun> registryTaskRuns = registryManager.registryTaskRuns().listByRegistry(rgName, acrName);
    RegistryTaskRun registryTaskRunFromList = registryTaskRuns.get(0);
    Assert.assertTrue(registryTaskRunFromList.status() != null);
    Assert.assertEquals("QuickRun", registryTaskRunFromList.runType().toString());
    Assert.assertTrue(registryTaskRunFromList.isArchiveEnabled());
    Assert.assertEquals(OS.LINUX, registryTaskRunFromList.platform().os());
    Assert.assertEquals("Succeeded", registryTaskRunFromList.provisioningState().toString());



}
 
Example 5
Source File: AlertsTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
public void canCRUDActivityLogAlerts() throws Exception {

    try {
        ActionGroup ag = monitorManager.actionGroups().define("simpleActionGroup")
                .withNewResourceGroup(RG_NAME, Region.US_EAST2)
                .defineReceiver("first")
                    .withPushNotification("[email protected]")
                    .withEmail("[email protected]")
                    .withSms("1", "4255655665")
                    .withVoice("1", "2062066050")
                    .withWebhook("https://www.rate.am")
                    .attach()
                .defineReceiver("second")
                    .withEmail("[email protected]")
                    .withWebhook("https://www.spyur.am")
                    .attach()
                .create();

        VirtualMachine justAvm = computeManager.virtualMachines().list().get(0);

        ActivityLogAlert ala = monitorManager.alertRules().activityLogAlerts().define("somename")
                .withExistingResourceGroup(RG_NAME)
                .withTargetSubscription(monitorManager.subscriptionId())
                .withDescription("AutoScale-VM-Creation-Failed")
                .withRuleEnabled()
                .withActionGroups(ag.id())
                .withEqualsCondition("category", "Administrative")
                .withEqualsCondition("resourceId", justAvm.id())
                .withEqualsCondition("operationName", "Microsoft.Compute/virtualMachines/delete")
                .create();

        Assert.assertNotNull(ala);
        Assert.assertEquals(1, ala.scopes().size());
        Assert.assertEquals("/subscriptions/" + monitorManager.subscriptionId(), ala.scopes().iterator().next());
        Assert.assertEquals("AutoScale-VM-Creation-Failed", ala.description());
        Assert.assertEquals(true, ala.enabled());
        Assert.assertEquals(1, ala.actionGroupIds().size());
        Assert.assertEquals(ag.id(), ala.actionGroupIds().iterator().next());
        Assert.assertEquals(3, ala.equalsConditions().size());
        Assert.assertEquals("Administrative", ala.equalsConditions().get("category"));
        Assert.assertEquals(justAvm.id(), ala.equalsConditions().get("resourceId"));
        Assert.assertEquals("Microsoft.Compute/virtualMachines/delete", ala.equalsConditions().get("operationName"));

        ActivityLogAlert alaFromGet = monitorManager.alertRules().activityLogAlerts().getById(ala.id());

        Assert.assertEquals(ala.scopes().size(), alaFromGet.scopes().size());
        Assert.assertEquals(ala.scopes().iterator().next(), alaFromGet.scopes().iterator().next());
        Assert.assertEquals(ala.description(), alaFromGet.description());
        Assert.assertEquals(ala.enabled(), alaFromGet.enabled());
        Assert.assertEquals(ala.actionGroupIds().size(), alaFromGet.actionGroupIds().size());
        Assert.assertEquals(ala.actionGroupIds().iterator().next(), alaFromGet.actionGroupIds().iterator().next());
        Assert.assertEquals(ala.equalsConditions().size(), alaFromGet.equalsConditions().size());
        Assert.assertEquals(ala.equalsConditions().get("category"), alaFromGet.equalsConditions().get("category"));
        Assert.assertEquals(ala.equalsConditions().get("resourceId"), alaFromGet.equalsConditions().get("resourceId"));
        Assert.assertEquals(ala.equalsConditions().get("operationName"), alaFromGet.equalsConditions().get("operationName"));

        ala.update()
                .withRuleDisabled()
                .withoutEqualsCondition("operationName")
                .withEqualsCondition("status", "Failed")
                .apply();

        Assert.assertEquals(1, ala.scopes().size());
        Assert.assertEquals("/subscriptions/" + monitorManager.subscriptionId(), ala.scopes().iterator().next());
        Assert.assertEquals("AutoScale-VM-Creation-Failed", ala.description());
        Assert.assertEquals(false, ala.enabled());
        Assert.assertEquals(1, ala.actionGroupIds().size());
        Assert.assertEquals(ag.id(), ala.actionGroupIds().iterator().next());
        Assert.assertEquals(3, ala.equalsConditions().size());
        Assert.assertEquals("Administrative", ala.equalsConditions().get("category"));
        Assert.assertEquals(justAvm.id(), ala.equalsConditions().get("resourceId"));
        Assert.assertEquals("Failed", ala.equalsConditions().get("status"));
        Assert.assertEquals(false, ala.equalsConditions().containsKey("operationName"));

        PagedList<ActivityLogAlert> alertsInRg = monitorManager.alertRules().activityLogAlerts().listByResourceGroup(RG_NAME);

        Assert.assertEquals(1, alertsInRg.size());
        alaFromGet = alertsInRg.get(0);;

        Assert.assertEquals(ala.scopes().size(), alaFromGet.scopes().size());
        Assert.assertEquals(ala.scopes().iterator().next(), alaFromGet.scopes().iterator().next());
        Assert.assertEquals(ala.description(), alaFromGet.description());
        Assert.assertEquals(ala.enabled(), alaFromGet.enabled());
        Assert.assertEquals(ala.actionGroupIds().size(), alaFromGet.actionGroupIds().size());
        Assert.assertEquals(ala.actionGroupIds().iterator().next(), alaFromGet.actionGroupIds().iterator().next());
        Assert.assertEquals(ala.equalsConditions().size(), alaFromGet.equalsConditions().size());
        Assert.assertEquals(ala.equalsConditions().get("category"), alaFromGet.equalsConditions().get("category"));
        Assert.assertEquals(ala.equalsConditions().get("resourceId"), alaFromGet.equalsConditions().get("resourceId"));
        Assert.assertEquals(ala.equalsConditions().get("status"), alaFromGet.equalsConditions().get("status"));
        Assert.assertEquals(ala.equalsConditions().containsKey("operationName"), alaFromGet.equalsConditions().containsKey("operationName"));

        monitorManager.alertRules().activityLogAlerts().deleteById(ala.id());
    }
    finally {
        resourceManager.resourceGroups().beginDeleteByName(RG_NAME);
    }
}
 
Example 6
Source File: DiagnosticSettingsTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
public void canCRUDDiagnosticSettings() throws Exception {

    VirtualMachine vm = computeManager.virtualMachines().list().get(0);

    // clean all diagnostic settings.
    PagedList<DiagnosticSetting> dsList = monitorManager.diagnosticSettings().listByResource(vm.id());
    for(DiagnosticSetting dsd : dsList) {
        monitorManager.diagnosticSettings().deleteById(dsd.id());
    }

    StorageAccount sa = storageManager.storageAccounts()
            .define(SA_NAME)
            // Storage Account should be in the same region as resource
            .withRegion(vm.region())
            .withNewResourceGroup(RG_NAME)
            .withTag("tag1", "value1")
            .create();

    EventHubNamespace namespace = eventHubManager.namespaces()
            .define(EH_NAME)
            // EventHub should be in the same region as resource
            .withRegion(vm.region())
            .withNewResourceGroup(RG_NAME)
            .withNewManageRule("mngRule1")
            .withNewSendRule("sndRule1")
            .create();

    EventHubNamespaceAuthorizationRule evenHubNsRule = namespace.listAuthorizationRules().get(0);

    List<DiagnosticSettingsCategory> categories = monitorManager.diagnosticSettings()
            .listCategoriesByResource(vm.id());

    Assert.assertNotNull(categories);
    Assert.assertFalse(categories.isEmpty());

    DiagnosticSetting setting = monitorManager.diagnosticSettings()
            .define(DS_NAME)
            .withResource(vm.id())
            .withStorageAccount(sa.id())
            .withEventHub(evenHubNsRule.id())
            .withLogsAndMetrics(categories, Period.minutes(5), 7)
            .create();

    Assert.assertTrue(vm.id().equalsIgnoreCase(setting.resourceId()));
    Assert.assertTrue(sa.id().equalsIgnoreCase(setting.storageAccountId()));
    Assert.assertTrue(evenHubNsRule.id().equalsIgnoreCase(setting.eventHubAuthorizationRuleId()));
    Assert.assertNull(setting.eventHubName());
    Assert.assertNull(setting.workspaceId());
    Assert.assertTrue(setting.logs().isEmpty());
    Assert.assertFalse(setting.metrics().isEmpty());

    setting.update()
            .withoutStorageAccount()
            .withoutLogs()
            .apply();

    Assert.assertTrue(vm.id().equalsIgnoreCase(setting.resourceId()));
    Assert.assertTrue(evenHubNsRule.id().equalsIgnoreCase(setting.eventHubAuthorizationRuleId()));
    Assert.assertNull(setting.storageAccountId());
    Assert.assertNull(setting.eventHubName());
    Assert.assertNull(setting.workspaceId());
    Assert.assertTrue(setting.logs().isEmpty());
    Assert.assertFalse(setting.metrics().isEmpty());

    DiagnosticSetting ds1 = monitorManager.diagnosticSettings().get(setting.resourceId(), setting.name());
    checkDiagnosticSettingValues(setting, ds1);

    DiagnosticSetting ds2 = monitorManager.diagnosticSettings().getById(setting.id());
    checkDiagnosticSettingValues(setting, ds2);

    dsList = monitorManager.diagnosticSettings().listByResource(vm.id());
    Assert.assertNotNull(dsList);
    Assert.assertEquals(1, dsList.size());
    DiagnosticSetting ds3 = dsList.get(0);
    checkDiagnosticSettingValues(setting, ds3);

    monitorManager.diagnosticSettings().deleteById(setting.id());

    dsList = monitorManager.diagnosticSettings().listByResource(vm.id());
    Assert.assertNotNull(dsList);
    Assert.assertTrue(dsList.isEmpty());
}
 
Example 7
Source File: VirtualMachineScaleSetOperationsTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
public void canCreateVirtualMachineScaleSetWithCustomScriptExtension() throws Exception {
    final String vmssName = generateRandomResourceName("vmss", 10);
    final String uname = "jvuser";
    final String password = "123OData!@#123";
    final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-libraries-for-net/master/Samples/Asset/install_apache.sh";
    final String installCommand = "bash install_apache.sh Abc.123x(";
    List<String> fileUris = new ArrayList<>();
    fileUris.add(apacheInstallScript);

    ResourceGroup resourceGroup = this.resourceManager.resourceGroups()
            .define(RG_NAME)
            .withRegion(REGION)
            .create();

    Network network = this.networkManager
            .networks()
            .define(generateRandomResourceName("vmssvnet", 15))
            .withRegion(REGION)
            .withExistingResourceGroup(resourceGroup)
            .withAddressSpace("10.0.0.0/28")
            .withSubnet("subnet1", "10.0.0.0/28")
            .create();

    LoadBalancer publicLoadBalancer = createHttpLoadBalancers(REGION, resourceGroup, "1");
    VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets().define(vmssName)
            .withRegion(REGION)
            .withExistingResourceGroup(resourceGroup)
            .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0)
            .withExistingPrimaryNetworkSubnet(network, "subnet1")
            .withExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer)
            .withoutPrimaryInternalLoadBalancer()
            .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
            .withRootUsername(uname)
            .withRootPassword(password)
            .withUnmanagedDisks()
            .withNewStorageAccount(generateRandomResourceName("stg", 15))
            .withNewStorageAccount(generateRandomResourceName("stg", 15))
            .defineNewExtension("CustomScriptForLinux")
            .withPublisher("Microsoft.OSTCExtensions")
            .withType("CustomScriptForLinux")
            .withVersion("1.4")
            .withMinorVersionAutoUpgrade()
            .withPublicSetting("fileUris",fileUris)
            .withPublicSetting("commandToExecute", installCommand)
            .attach()
            .withUpgradeMode(UpgradeMode.MANUAL)
            .create();

    checkVMInstances(virtualMachineScaleSet);

    List<String> publicIPAddressIds = virtualMachineScaleSet.primaryPublicIPAddressIds();
    PublicIPAddress publicIPAddress = this.networkManager.publicIPAddresses()
            .getById(publicIPAddressIds.get(0));

    String fqdn = publicIPAddress.fqdn();
    // Assert public load balancing connection
    if (!isPlaybackMode()) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://" + fqdn)
                .build();
        Response response = client.newCall(request).execute();
        Assert.assertEquals(response.code(), 200);
    }

    // Check SSH to VM instances via Nat rule
    //
    for (VirtualMachineScaleSetVM vm : virtualMachineScaleSet.virtualMachines().list()) {
        PagedList<VirtualMachineScaleSetNetworkInterface> networkInterfaces = vm.listNetworkInterfaces();
        Assert.assertEquals(networkInterfaces.size(), 1);
        VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.get(0);
        VirtualMachineScaleSetNicIPConfiguration primaryIpConfig = null;
        primaryIpConfig = networkInterface.primaryIPConfiguration();
        Assert.assertNotNull(primaryIpConfig);
        Integer sshFrontendPort = null;
        List<LoadBalancerInboundNatRule> natRules = primaryIpConfig.listAssociatedLoadBalancerInboundNatRules();
        for (LoadBalancerInboundNatRule natRule : natRules) {
            if (natRule.backendPort() == 22) {
                sshFrontendPort = natRule.frontendPort();
                break;
            }
        }
        Assert.assertNotNull(sshFrontendPort);

        this.sleep(1000 * 60); // Wait some time for VM to be available
        this.ensureCanDoSsh(fqdn, sshFrontendPort, uname, password);
    }
}