Skip to content

Commit

Permalink
storage lifecycle refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
deepanjan90 committed Jul 12, 2022
1 parent 98ae2fa commit cbf2fa5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/main/java/gyro/azure/storage/PolicyFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
import gyro.azure.Copyable;
import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.ValidStrings;

public class PolicyFilter extends Diffable implements Copyable<ManagementPolicyFilter> {

private Set<String> blobTypes;
private Set<String> prefixMatches;

/**
* Allowed blob types for the filter. Currently only supported value is ``blockBlob``. Defaults to ``blockBlob``.
* Allowed blob types for the filter. Defaults to ``blockBlob``.
*/
@ValidStrings({"blockBlob", "appendBlob"})
public Set<String> getBlobTypes() {
if (blobTypes == null) {
blobTypes = new HashSet<>();
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/gyro/azure/storage/PolicyRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Required;
import gyro.core.validation.ValidStrings;

public class PolicyRule extends Diffable implements Copyable<ManagementPolicyRule> {

private String name;
private RuleType type;
private String type;
private Boolean enabled;
private PolicyDefinition definition;

Expand All @@ -43,17 +44,18 @@ public void setName(String name) {
}

/**
* Type of rule. Currently only supported value is ``Lifecycle``. Defaults to ``Lifecycle``.
* Type of rule.
*/
public RuleType getType() {
@ValidStrings("Lifecycle")
public String getType() {
if (type == null) {
type = RuleType.LIFECYCLE;
type = RuleType.LIFECYCLE.toString();
}

return type;
}

public void setType(RuleType type) {
public void setType(String type) {
this.type = type;
}

Expand All @@ -76,7 +78,7 @@ public void setEnabled(Boolean enabled) {
/**
* The rule details.
*
* @sunresource gyro.azure.storage.PolicyDefinition
* @subresource gyro.azure.storage.PolicyDefinition
*/
@Required
@Updatable
Expand All @@ -96,7 +98,7 @@ public String primaryKey() {
@Override
public void copyFrom(ManagementPolicyRule rule) {
setName(rule.name());
setType(rule.type());
setType(rule.type().toString());
setEnabled(rule.enabled());
PolicyDefinition policyDefinition = newSubresource(PolicyDefinition.class);
policyDefinition.copyFrom(rule.definition());
Expand All @@ -105,11 +107,10 @@ public void copyFrom(ManagementPolicyRule rule) {

ManagementPolicyRule toManagementPolicyRule() {
ManagementPolicyRule rule = new ManagementPolicyRule();
rule = rule.withName(getName())
.withType(getType())

return rule.withName(getName())
.withType(RuleType.fromString(getType()))
.withEnabled(getEnabled())
.withDefinition(getDefinition().toManagementPolicyDefinition());

return rule;
}
}
10 changes: 6 additions & 4 deletions src/main/java/gyro/azure/storage/StorageLifeCycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.azure.resourcemanager.storage.models.ManagementPolicy;
import com.azure.resourcemanager.storage.models.ManagementPolicyRule;
import com.azure.resourcemanager.storage.models.ManagementPolicySchema;
import com.azure.resourcemanager.storage.models.PolicyRule.DefinitionStages.WithBlobTypesToFilterFor;
import com.azure.resourcemanager.storage.models.PolicyRule.DefinitionStages.WithPolicyRuleAttachable;
import com.azure.resourcemanager.storage.models.StorageAccount;
import gyro.azure.AzureResource;
import gyro.azure.Copyable;
Expand All @@ -47,7 +49,7 @@ public class StorageLifeCycle extends AzureResource implements Copyable<Manageme
private Set<PolicyRule> rule;

/**
* The name of the lifecycle policy. Currently only supported value is ``DefaultManagementPolicy``. Defaults to ``DefaultManagementPolicy``.
* The name of the lifecycle policy.
*/
@ValidStrings("DefaultManagementPolicy")
public String getName() {
Expand Down Expand Up @@ -150,12 +152,12 @@ public void create(GyroUI ui, State state) throws Exception {
ManagementPolicy.DefinitionStages.WithCreate create = null;

for (PolicyRule rule : getRule()) {
com.azure.resourcemanager.storage.models.PolicyRule.DefinitionStages.WithBlobTypesToFilterFor withBlobTypesToFilterFor =
WithBlobTypesToFilterFor withBlobTypesToFilterFor =
create == null
? withRule.defineRule(rule.getName()).withLifecycleRuleType()
: create.defineRule(rule.getName()).withLifecycleRuleType();

com.azure.resourcemanager.storage.models.PolicyRule.DefinitionStages.WithPolicyRuleAttachable withPolicyRuleAttachable = withBlobTypesToFilterFor
WithPolicyRuleAttachable withPolicyRuleAttachable = withBlobTypesToFilterFor
.withBlobTypesToFilterFor(rule.getDefinition()
.getFilter()
.getBlobTypes()
Expand All @@ -180,7 +182,7 @@ public void create(GyroUI ui, State state) throws Exception {
state.save();

// Api does not allow creating one or more disabled rule when creating a policy.
// If one or more rules are are configured to be disabled then an update is required.
// If one or more rules are configured to be disabled then an update is required.
if (getRule().stream().anyMatch(o -> !o.getEnabled())) {
update(ui, state, this, new HashSet<>());
} else {
Expand Down

0 comments on commit cbf2fa5

Please sign in to comment.