Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove all client side validation for OLM, allow nonspecific lif… #1160

Merged
merged 7 commits into from Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions google-cloud-storage/clirr-ignored-differences.xml
Expand Up @@ -16,6 +16,11 @@
<className>com/google/cloud/storage/spi/v1/StorageRpc</className>
<method>*.StorageObject writeWithResponse(*.String, byte[], int, long, int, boolean)</method>
</difference>
<difference>
<className>com/google/cloud/storage/BucketInfo$LifecycleRule$LifecycleAction</className>
<method>BucketInfo$LifecycleRule$LifecycleAction()</method>
<differenceType>7004</differenceType>
</difference>
<difference>
<className>com/google/cloud/storage/BucketInfo$Builder</className>
<method>com.google.cloud.storage.BucketInfo$Builder deleteLifecycleRules()</method>
Expand Down
Expand Up @@ -548,8 +548,13 @@ static LifecycleRule fromPb(Rule rule) {
StorageClass.valueOf(action.getStorageClass()));
break;
default:
throw new UnsupportedOperationException(
"The specified lifecycle action " + action.getType() + " is not currently supported");
log.warning(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests to ensure unknown types no longer throw exceptions?

"The lifecycle action "
+ action.getType()
+ " is not supported by this version of the library. "
+ "Attempting to update with this rule may cause errors. Please "
+ "update to the latest version of google-cloud-storage.");
lifecycleAction = LifecycleAction.newLifecycleAction("Unknown action");
}

Rule.Condition condition = rule.getCondition();
Expand Down Expand Up @@ -799,13 +804,21 @@ public LifecycleCondition build() {
}

/**
* Base class for the Action to take when a Lifecycle Condition is met. Specific Actions are
* Base class for the Action to take when a Lifecycle Condition is met. Supported Actions are
* expressed as subclasses of this class, accessed by static factory methods.
*/
public abstract static class LifecycleAction implements Serializable {
public static class LifecycleAction implements Serializable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to remove the modifier abstract in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abstract classes can't be instantiated so we do if we want to allow for unsupported action types to be created in this way.

The alternative would be to make an implementation of LifecycleAction for that purpose, like "CustomLifecycleAction" or something, but I'm worried that doing that would be confusing, or imply that this is the correct way to use action types that aren't in the library (when in actuality users should upgrade to the latest version of the library)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, totally missed that. Thanks Jesse, I would confirm what happens when a say setStorageClass doesn't have storageClass in the rule (the case an action type is added that requires additional parameters) and see how GCS handles the request on a PATCH request.

I suspect it will fail; so we may want to clarify if we want the library not to create an instance for that unknown action type.

private static final long serialVersionUID = 5801228724709173284L;

public abstract String getActionType();
private final String actionType;

public LifecycleAction(String actionType) {
this.actionType = actionType;
}

public String getActionType() {
return actionType;
}

@Override
public String toString() {
Expand All @@ -830,17 +843,24 @@ public static SetStorageClassLifecycleAction newSetStorageClassAction(
StorageClass storageClass) {
return new SetStorageClassLifecycleAction(storageClass);
}

/**
* Creates a new {@code LifecycleAction , with no specific supported action associated with it. This
* is only intended as a "backup" for when the library doesn't recognize the type, and should
* generally not be used, instead use the supported actions, and upgrade the library if necessary
* to get new supported actions.
*/
public static LifecycleAction newLifecycleAction(String actionType) {
return new LifecycleAction(actionType);
}
}

public static class DeleteLifecycleAction extends LifecycleAction {
public static final String TYPE = "Delete";
private static final long serialVersionUID = -2050986302222644873L;

private DeleteLifecycleAction() {}

@Override
public String getActionType() {
return TYPE;
private DeleteLifecycleAction() {
super(TYPE);
}
}

Expand All @@ -851,14 +871,10 @@ public static class SetStorageClassLifecycleAction extends LifecycleAction {
private final StorageClass storageClass;

private SetStorageClassLifecycleAction(StorageClass storageClass) {
super(TYPE);
this.storageClass = storageClass;
}

@Override
public String getActionType() {
return TYPE;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down Expand Up @@ -1007,7 +1023,11 @@ static class RawDeleteRule extends DeleteRule {

@Override
void populateCondition(Rule.Condition condition) {
throw new UnsupportedOperationException();
log.warning(
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
"The lifecycle condition "
+ condition
+ " is not currently supported. Please update to the latest version of google-cloud-java."
+ " Also, use LifecycleRule rather than the deprecated DeleteRule.");
}

private void writeObject(ObjectOutputStream out) throws IOException {
Expand Down
Expand Up @@ -314,6 +314,10 @@ public void testDeleteRules() {
for (DeleteRule delRule : rules) {
assertEquals(delRule, DeleteRule.fromPb(delRule.toPb()));
}
Rule unsupportedRule =
new Rule().setAction(new Rule.Action().setType("This action doesn't exist"));
DeleteRule.fromPb(
unsupportedRule); // if this doesn't throw an exception, unsupported rules work
}

@Test
Expand Down Expand Up @@ -363,6 +367,17 @@ public void testLifecycleRules() {
assertEquals(StorageClass.COLDLINE.toString(), lifecycleRule.getAction().getStorageClass());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue());
assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore());

Rule unsupportedRule =
new LifecycleRule(
LifecycleAction.newLifecycleAction("This action type doesn't exist"),
LifecycleCondition.newBuilder().setAge(10).build())
.toPb();
unsupportedRule.setAction(
unsupportedRule.getAction().setType("This action type also doesn't exist"));

LifecycleRule.fromPb(
unsupportedRule); // If this doesn't throw an exception, unsupported rules are working
}

@Test
Expand Down