Search in sources :

Example 16 with StorageAccount

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

the class IntegrationTest method downloadFromAzure.

protected String downloadFromAzure(String resourceGroup, String storageAccountName, String containerName, String fileName) throws IOException {
    StorageAccount storageAccount = azureClient.storageAccounts().getByResourceGroup(resourceGroup, storageAccountName);
    List<StorageAccountKey> storageKeys = storageAccount.getKeys();
    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);
    if (!blobClient.exists()) {
        blobClient.create();
    }
    BlobClient blob = blobClient.getBlobClient(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    blob.download(byteArrayOutputStream);
    return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name());
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StorageSharedKeyCredential(com.azure.storage.common.StorageSharedKeyCredential)

Example 17 with StorageAccount

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

the class IntegrationTest method blobExists.

protected boolean blobExists(String storageURI) {
    try {
        BlobUrlParts blobUrlParts = BlobUrlParts.parse(storageURI);
        String storageAccountName = blobUrlParts.getAccountName();
        final String containerName = blobUrlParts.getBlobContainerName();
        final String blobName = blobUrlParts.getBlobName();
        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.getBlobClient(blobName).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