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();