Search in sources :

Example 1 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method createDeploymentWithAvailabilitySet.

@Test
public void createDeploymentWithAvailabilitySet() throws Exception {
    azureClient.resourceGroups().define(testEnv.azureResourceGroup).withRegion(testEnv.azureLocation).create();
    AvailabilitySet availabilitySet = azureClient.availabilitySets().define("test-av-set").withRegion(testEnv.azureLocation).withExistingResourceGroup(testEnv.azureResourceGroup).withFaultDomainCount(2).withUpdateDomainCount(4).withSku(AvailabilitySetSkuTypes.ALIGNED).create();
    Assert.assertNotNull("Failed to create availability set in resourceGroup " + testEnv.azureResourceGroup, availabilitySet);
    testEnv.availabilityType = AvailabilityType.AVAILABILITY_SET.getName();
    testEnv.availabilitySet = availabilitySet.name();
    AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
    when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
    AzureVMDeploymentInfo deploymentInfo;
    deploymentInfo = createDefaultDeployment(1, deploymentRegistrar);
    verify(deploymentRegistrar).registerDeployment("testCloud", testEnv.azureResourceGroup, deploymentInfo.getDeploymentName(), null);
    Network actualVNet = null;
    StorageAccount actualStorageAcc = null;
    try {
        actualVNet = azureClient.networks().getByResourceGroup(testEnv.azureResourceGroup, "jenkinsarm-vnet");
        actualStorageAcc = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, testEnv.azureStorageAccountName);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
    }
    Assert.assertNotNull("The deployed VNet doesn't exist: " + testEnv.azureResourceGroup, actualVNet);
    Assert.assertNotNull("The deployed Storage Account doesn't exist: " + testEnv.azureResourceGroup, actualStorageAcc);
    final String baseName = deploymentInfo.getVmBaseName() + "0";
    final String commonAssertMsg = testEnv.azureResourceGroup + ":" + baseName;
    VirtualMachine actualVM = null;
    NetworkInterface actualNetIface = null;
    PublicIpAddress actualIP = null;
    try {
        actualVM = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, baseName);
        actualNetIface = azureClient.networkInterfaces().getByResourceGroup(testEnv.azureResourceGroup, baseName + "NIC");
        actualIP = azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, baseName + "IPName");
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
    }
    Assert.assertNotNull("The deployed VM doesn't exist: " + commonAssertMsg, actualVM);
    Assert.assertNotNull("The deployed Network interface doesn't exist: " + commonAssertMsg, actualNetIface);
    Assert.assertNotNull("The deployed public IP doesn't exist: " + commonAssertMsg, actualIP);
    assertThat(availabilitySet.id(), is(equalToIgnoringCase(actualVM.availabilitySetId())));
}
Also used : AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) NetworkInterface(com.azure.resourcemanager.network.models.NetworkInterface) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) Network(com.azure.resourcemanager.network.models.Network) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) AvailabilitySet(com.azure.resourcemanager.compute.models.AvailabilitySet) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 2 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method createDeploymentTest.

@Test
public void createDeploymentTest() throws Exception {
    Random rand = new Random();
    final int numberOfAgents = rand.nextInt(4) + 1;
    AzureVMDeploymentInfo deploymentInfo = null;
    AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
    when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
    deploymentInfo = createDefaultDeployment(numberOfAgents, deploymentRegistrar);
    verify(deploymentRegistrar).registerDeployment("testCloud", testEnv.azureResourceGroup, deploymentInfo.getDeploymentName(), null);
    Network actualVNet = null;
    StorageAccount actualStorageAcc = null;
    try {
        actualVNet = azureClient.networks().getByResourceGroup(testEnv.azureResourceGroup, "jenkinsarm-vnet");
        actualStorageAcc = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, testEnv.azureStorageAccountName);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
    }
    Assert.assertNotNull("The deployed VNet doesn't exist: " + testEnv.azureResourceGroup, actualVNet);
    Assert.assertNotNull("The deployed Storage Account doesn't exist: " + testEnv.azureResourceGroup, actualStorageAcc);
    for (int i = 0; i < numberOfAgents; i++) {
        final String baseName = deploymentInfo.getVmBaseName() + i;
        final String commonAssertMsg = testEnv.azureResourceGroup + ":" + baseName;
        VirtualMachine actualVM = null;
        NetworkInterface actualNetIface = null;
        PublicIpAddress actualIP = null;
        try {
            actualVM = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, baseName);
            actualNetIface = azureClient.networkInterfaces().getByResourceGroup(testEnv.azureResourceGroup, baseName + "NIC");
            actualIP = azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, baseName + "IPName");
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, null, e);
        }
        Assert.assertNotNull("The deployed VM doesn't exist: " + commonAssertMsg, actualVM);
        Assert.assertNotNull("The deployed Network interface doesn't exist: " + commonAssertMsg, actualNetIface);
        Assert.assertNotNull("The deployed public IP doesn't exist: " + commonAssertMsg, actualIP);
        Assert.assertFalse(actualVM.isManagedServiceIdentityEnabled());
        Assert.assertEquals(getDefaultOsDiskSize(OS_TYPE), actualVM.osDiskSize());
        Assert.assertEquals("gavin", actualVM.tags().get("author"));
        Assert.assertEquals("test", actualVM.tags().get("env"));
        Assert.assertEquals("gavin", actualIP.tags().get("author"));
        Assert.assertEquals("test", actualIP.tags().get("env"));
        Assert.assertEquals("gavin", actualNetIface.tags().get("author"));
        Assert.assertEquals("test", actualNetIface.tags().get("env"));
        Assert.assertEquals("gavin", actualStorageAcc.tags().get("author"));
        Assert.assertEquals("test", actualStorageAcc.tags().get("env"));
        Assert.assertEquals("gavin", actualVNet.tags().get("author"));
        Assert.assertEquals("test", actualVNet.tags().get("env"));
    }
}
Also used : AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) NetworkInterface(com.azure.resourcemanager.network.models.NetworkInterface) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) Random(java.util.Random) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) Network(com.azure.resourcemanager.network.models.Network) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 3 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMManagementServiceDelegate method getCloudBlobContainer.

public BlobContainerClient getCloudBlobContainer(AzureResourceManager azureClient, String resourceGroupName, String targetStorageAccount, String blobContainerName) throws AzureCloudException {
    StorageAccount storageAccount;
    try {
        storageAccount = azureClient.storageAccounts().getByResourceGroup(resourceGroupName, targetStorageAccount);
    } catch (Exception e) {
        throw AzureCloudException.create(e);
    }
    BlobServiceClient account = getCloudStorageAccount(storageAccount);
    return getCloudBlobContainer(account, blobContainerName);
}
Also used : StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) BlobServiceClient(com.azure.storage.blob.BlobServiceClient) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) FormException(hudson.model.Descriptor.FormException) ManagementException(com.azure.core.management.exception.ManagementException) IOException(java.io.IOException)

Example 4 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method createDeploymentWithSpecificOsDiskSize.

@Test
public void createDeploymentWithSpecificOsDiskSize() throws Exception {
    int osDiskSize = 100;
    testEnv.osDiskSize = osDiskSize;
    AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
    when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
    AzureVMDeploymentInfo deploymentInfo;
    deploymentInfo = createDefaultDeployment(1, deploymentRegistrar);
    verify(deploymentRegistrar).registerDeployment("testCloud", testEnv.azureResourceGroup, deploymentInfo.getDeploymentName(), null);
    Network actualVNet = null;
    StorageAccount actualStorageAcc = null;
    try {
        actualVNet = azureClient.networks().getByResourceGroup(testEnv.azureResourceGroup, "jenkinsarm-vnet");
        actualStorageAcc = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, testEnv.azureStorageAccountName);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
    }
    Assert.assertNotNull("The deployed VNet doesn't exist: " + testEnv.azureResourceGroup, actualVNet);
    Assert.assertNotNull("The deployed Storage Account doesn't exist: " + testEnv.azureResourceGroup, actualStorageAcc);
    final String baseName = deploymentInfo.getVmBaseName() + "0";
    final String commonAssertMsg = testEnv.azureResourceGroup + ":" + baseName;
    VirtualMachine actualVM = null;
    NetworkInterface actualNetIface = null;
    PublicIpAddress actualIP = null;
    try {
        actualVM = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, baseName);
        actualNetIface = azureClient.networkInterfaces().getByResourceGroup(testEnv.azureResourceGroup, baseName + "NIC");
        actualIP = azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, baseName + "IPName");
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
    }
    Assert.assertNotNull("The deployed VM doesn't exist: " + commonAssertMsg, actualVM);
    Assert.assertNotNull("The deployed Network interface doesn't exist: " + commonAssertMsg, actualNetIface);
    Assert.assertNotNull("The deployed public IP doesn't exist: " + commonAssertMsg, actualIP);
    Assert.assertEquals(osDiskSize, actualVM.osDiskSize());
}
Also used : AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) NetworkInterface(com.azure.resourcemanager.network.models.NetworkInterface) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) Network(com.azure.resourcemanager.network.models.Network) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 5 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class IntegrationTest method containerExists.

protected boolean containerExists(String storageURI) {
    try {
        BlobUrlParts blobUrlParts = BlobUrlParts.parse(storageURI);
        String storageAccountName = blobUrlParts.getAccountName();
        final String containerName = blobUrlParts.getBlobContainerName();
        StorageAccount storageAccount = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, storageAccountName);
        List<StorageAccountKey> storageKeys = storageAccount.getKeys();
        if (storageKeys.isEmpty()) {
            return false;
        } else {
            String storageAccountKey = storageKeys.get(0).value();
            BlobServiceClient account = new BlobServiceClientBuilder().credential(new StorageSharedKeyCredential(storageAccountName, storageAccountKey)).endpoint(storageAccount.endPoints().primary().blob()).buildClient();
            BlobContainerClient blobClient = account.getBlobContainerClient(containerName);
            return blobClient.exists();
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        return false;
    }
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobUrlParts(com.azure.storage.blob.BlobUrlParts) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) StorageAccountKey(com.azure.resourcemanager.storage.models.StorageAccountKey) BlobServiceClient(com.azure.storage.blob.BlobServiceClient) BlobServiceClientBuilder(com.azure.storage.blob.BlobServiceClientBuilder) StorageSharedKeyCredential(com.azure.storage.common.StorageSharedKeyCredential) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) ManagementException(com.azure.core.management.exception.ManagementException) IOException(java.io.IOException)

Aggregations

StorageAccount (com.azure.resourcemanager.storage.models.StorageAccount)17 ManagementException (com.azure.core.management.exception.ManagementException)16 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)16 IOException (java.io.IOException)16 Test (org.junit.Test)11 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)7 Network (com.azure.resourcemanager.network.models.Network)7 NetworkInterface (com.azure.resourcemanager.network.models.NetworkInterface)7 PublicIpAddress (com.azure.resourcemanager.network.models.PublicIpAddress)7 AzureVMAgentCleanUpTask (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask)7 AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)7 AzureUtil (com.microsoft.azure.vmagent.util.AzureUtil)7 StorageAccountKey (com.azure.resourcemanager.storage.models.StorageAccountKey)4 BlobServiceClient (com.azure.storage.blob.BlobServiceClient)4 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)3 BlobServiceClientBuilder (com.azure.storage.blob.BlobServiceClientBuilder)3 StorageSharedKeyCredential (com.azure.storage.common.StorageSharedKeyCredential)3 FormException (hudson.model.Descriptor.FormException)3 URI (java.net.URI)3 BlobUrlParts (com.azure.storage.blob.BlobUrlParts)2