use of com.microsoft.azure.storage.CloudStorageAccount in project azure-sdk-for-java by Azure.
the class VirtualMachineScaleSetOperationsTests method canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings.
@Test
public void canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings() throws Exception {
final String vmssName = generateRandomResourceName("vmss", 10);
final String uname = "jvuser";
final String password = "123OData!@#123";
ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(RG_NAME).withRegion(REGION).create();
StorageAccount storageAccount = this.storageManager.storageAccounts().define(generateRandomResourceName("stg", 15)).withRegion(REGION).withExistingResourceGroup(resourceGroup).create();
List<StorageAccountKey> keys = storageAccount.getKeys();
Assert.assertNotNull(keys);
Assert.assertTrue(keys.size() > 0);
String storageAccountKey = keys.get(0).value();
final String storageConnectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", storageAccount.name(), storageAccountKey);
// Get the script to upload
//
InputStream scriptFileAsStream = VirtualMachineScaleSetOperationsTests.class.getResourceAsStream("/install_apache.sh");
// Get the size of the stream
//
int fileSize;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
int bytesRead;
while ((bytesRead = scriptFileAsStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileSize = outputStream.size();
outputStream.close();
// Upload the script file as block blob
//
URI fileUri;
if (IS_MOCKED) {
fileUri = new URI("http://nonexisting.blob.core.windows.net/scripts/install_apache.sh");
} else {
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient cloudBlobClient = account.createCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.getContainerReference("scripts");
container.createIfNotExists();
CloudBlockBlob blob = container.getBlockBlobReference("install_apache.sh");
blob.upload(scriptFileAsStream, fileSize);
fileUri = blob.getUri();
}
List<String> fileUris = new ArrayList<>();
fileUris.add(fileUri.toString());
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();
VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets().define(vmssName).withRegion(REGION).withExistingResourceGroup(resourceGroup).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0).withExistingPrimaryNetworkSubnet(network, "subnet1").withoutPrimaryInternetFacingLoadBalancer().withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(uname).withRootPassword(password).withUnmanagedDisks().withNewStorageAccount(generateRandomResourceName("stg", 15)).withExistingStorageAccount(storageAccount).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", fileUris).withProtectedSetting("commandToExecute", "bash install_apache.sh").withProtectedSetting("storageAccountName", storageAccount.name()).withProtectedSetting("storageAccountKey", storageAccountKey).attach().create();
// Validate extensions after create
//
Map<String, VirtualMachineScaleSetExtension> extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
VirtualMachineScaleSetExtension extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
// Retrieve scale set
VirtualMachineScaleSet scaleSet = this.computeManager.virtualMachineScaleSets().getById(virtualMachineScaleSet.id());
// Validate extensions after get
//
extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
// Update VMSS capacity
//
int newCapacity = (int) (scaleSet.capacity() + 1);
virtualMachineScaleSet.update().withCapacity(newCapacity).apply();
// Validate extensions after update
//
extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
}
use of com.microsoft.azure.storage.CloudStorageAccount in project azure-sdk-for-java by Azure.
the class ManageWebAppStorageAccountConnection method setUpStorageAccount.
private static CloudBlobContainer setUpStorageAccount(String connectionString, String containerName) {
try {
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
// Create a blob service client
CloudBlobClient blobClient = account.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(containerName);
container.createIfNotExists();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
// Set the permissions on the container
container.uploadPermissions(containerPermissions);
return container;
} catch (StorageException | URISyntaxException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
use of com.microsoft.azure.storage.CloudStorageAccount in project azure-sdk-for-java by Azure.
the class ManageLinuxWebAppStorageAccountConnection method setUpStorageAccount.
private static CloudBlobContainer setUpStorageAccount(String connectionString, String containerName) {
try {
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
// Create a blob service client
CloudBlobClient blobClient = account.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(containerName);
container.createIfNotExists();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
// Set the permissions on the container
container.uploadPermissions(containerPermissions);
return container;
} catch (StorageException | URISyntaxException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
use of com.microsoft.azure.storage.CloudStorageAccount in project azure-tools-for-java by Microsoft.
the class StorageAccoutUtils method getBlobSasUri.
public static String getBlobSasUri(String blobLink, String saKey) throws URISyntaxException, StorageException, InvalidKeyException, MalformedURLException {
CloudStorageAccount cloudStorageAccount = getCloudStorageAccount(blobLink, saKey);
// Create the blob client.
CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient();
// Get container and blob name from the link
String path = new URI(blobLink).getPath();
if (path == null) {
throw new IllegalArgumentException("Invalid blobLink: " + blobLink);
}
int containerNameEndIndex = path.indexOf("/", 1);
String containerName = path.substring(1, containerNameEndIndex);
if (containerName == null || containerName.isEmpty()) {
throw new IllegalArgumentException("Invalid blobLink, can't find container name: " + blobLink);
}
String blobName = path.substring(path.indexOf("/", containerNameEndIndex) + 1);
if (blobName == null || blobName.isEmpty()) {
throw new IllegalArgumentException("Invalid blobLink, can't find blob name: " + blobLink);
}
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.getContainerReference(containerName);
//CloudBlockBlob blob = container.getBlockBlobReference(blobName);
SharedAccessBlobPolicy sharedAccessBlobPolicy = new SharedAccessBlobPolicy();
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(new Date());
sharedAccessBlobPolicy.setSharedAccessStartTime(calendar.getTime());
calendar.add(Calendar.HOUR, 23);
sharedAccessBlobPolicy.setSharedAccessExpiryTime(calendar.getTime());
sharedAccessBlobPolicy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
container.uploadPermissions(containerPermissions);
String signature = container.generateSharedAccessSignature(sharedAccessBlobPolicy, null);
return blobLink + "?" + signature;
}
use of com.microsoft.azure.storage.CloudStorageAccount in project jackrabbit-oak by apache.
the class Utils method getBlobClient.
/**
* Create CloudBlobClient from properties.
*
* @param connectionString connectionString to configure @link {@link CloudBlobClient}
* @return {@link CloudBlobClient}
*/
public static CloudBlobClient getBlobClient(final String connectionString) throws URISyntaxException, InvalidKeyException {
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobClient client = account.createCloudBlobClient();
return client;
}
Aggregations