I have created a lifecycle policy for azure storage account with the help of following piece of code.
StorageManager manager = StorageManager.authenticate(credential, subId); //credentilas and subscription iD
manager.managementPolicies()
.define("testprefix")
.withExistingStorageAccount(resourceGroup, storageAcc)
.defineRule("testprefixrulenew")
.withLifecycleRuleType()
.withBlobTypeToFilterFor(BlobTypes.BLOCK_BLOB)
.withPrefixToFilterFor("prefix1")
.withDeleteActionOnBaseBlob(1)
.attach()
.create();
when I run this, all the existing lifecycle rules for the storage account get deleted and only the newly created one is present .
1) How to avoid this deletion of existing lifecycle rules for azure storage account using java?
also , when I try to get the existing lifecycle policies by using following code snippet.
ManagementPolicyInner managementPolicy1 =
manager2
.inner()
.managementPolicies()
.getAsync(resourceGroup, storageAcc)
.toBlocking()
.last();
I am able to get only the last modified/created policy. and if we remove "last()", it gives Observable value, so.
2) How to get the existing policies, iterate them , and update one of it if required using java???
How to avoid this deletion of existing lifecycle rules for azure storage account using java?
Regarding the issue, we can use the class com.azure.resourcemanager.storage.implementation.StorageManagementClientImpl
to implement it. For more details, please refer to here
for example
String clientId="<the service principal client id>";
String clientSecret="<the service principal client secret>"";
String tenant="";
String subId="";
AzureProfile profile = new AzureProfile(tenant,subId,AzureEnvironment.AZURE);
TokenCredential credential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret)
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.tenantId(tenant)
.build();
StorageManagementClientImpl storageManagementClient = new StorageManagementClientBuilder()
.pipeline(HttpPipelineProvider.buildHttpPipeline(credential,profile))
.endpoint(profile.getEnvironment().getResourceManagerEndpoint())
.subscriptionId(profile.getSubscriptionId())
.buildClient();
ManagementPolicyInner policyInner= storageManagementClient.getManagementPolicies()
.get("andywin7","andyprivate",ManagementPolicyName.DEFAULT);
ManagementPolicyDefinition definition = new ManagementPolicyDefinition()
.withActions( new ManagementPolicyAction().withBaseBlob(
new ManagementPolicyBaseBlob().withTierToCool(
new DateAfterModification().withDaysAfterModificationGreaterThan(180))))
.withFilters(new ManagementPolicyFilter().withBlobTypes(Arrays.asList("blockBlob")));
ManagementPolicyRule newRule = new ManagementPolicyRule()
.withName("changeTier")
.withEnabled(true)
.withType(RuleType.LIFECYCLE)
.withDefinition(definition);
policyInner.policy().rules().add(newRule);
Response<ManagementPolicyInner> res =storageManagementClient.getManagementPolicies().createOrUpdateWithResponse(
"andywin7","andyprivate",ManagementPolicyName.DEFAULT,
policyInner.policy(), Context.NONE);
How to get the existing policies, iterate them, and update one of it if required using java???
Please refer to the following code list
String clientId="<the service principal client id>";
String clientSecret="<the service principal client secret>"";
String tenant="";
String subId="";
AzureProfile profile = new AzureProfile(tenant,subId,AzureEnvironment.AZURE);
TokenCredential credential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret)
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.tenantId(tenant)
.build();
StorageManager manager = StorageManager.authenticate(credential,profile);
ManagementPolicy managementPolicy = manager.managementPolicies()
.getAsync("andywin7","andyprivate")
.block();
ObjectMapper mapper = new ObjectMapper();
for ( ManagementPolicyRule rule : managementPolicy.policy().rules()){
System.out.println(mapper.writeValueAsString(rule));
}
Update
String clientId="<the service principal client id>";
String clientSecret="<the service principal client secret>"";
String tenant="";
String subId="";
AzureProfile profile = new AzureProfile(tenant,subId,AzureEnvironment.AZURE);
TokenCredential credential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret)
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.tenantId(tenant)
.build();
StorageManager manager = StorageManager.authenticate(credential,profile);
ManagementPolicy managementPolicy = manager.managementPolicies()
.getAsync("andywin7","andyprivate")
.block();
managementPolicy.update()
.updateRule("<the rule name you want to update>")
... the action you want to update
.parent()
.withoutRule("<the rule name you do not want to update>")
.apply();
For more details, please refer to here
Update
Regarding how to update the existing rule, please refer to the fowwing code
StorageManager manager = StorageManager.authenticate(credential,profile);
ManagementPolicy managementPolicy = manager.managementPolicies()
.getAsync("andywin7","andyprivate")
.block();
ObjectMapper mapper = new ObjectMapper();
for ( ManagementPolicyRule rule : managementPolicy.policy().rules()){
System.out.println(mapper.writeValueAsString(rule));
if(rule.name().equals("deleteAppend")){
int i= managementPolicy.policy().rules().indexOf(rule);
ManagementPolicyRule newRule= managementPolicy.policy().rules().get(i);
managementPolicy.policy().rules().remove(rule);
newRule.definition()
.actions()
.withBaseBlob(new ManagementPolicyBaseBlob().withDelete(new DateAfterModification().withDaysAfterModificationGreaterThan(18) ));
managementPolicy.policy().rules().add(newRule);
}
}
managementPolicy.update()
.withPolicy(managementPolicy.policy())
.apply();