This repository was archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
feat: support conditional policies #110
Merged
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
67e52ce
Base implementation
84fcfea
Update with unit tests
145f7a0
lint
ac1fd6e
correct copyright date
2c8724b
lint
c90dbb0
Revert removal of helper functions
b0a617f
use auto-value
9dbfc2d
Merge branch 'master' into conditional-policy
54ad076
reformat Binding.java and Condition.java
ff49620
remove unnecessary dep
298b2be
code format
aaebba4
add dep on com.google.code.findbugs in google-cloud-core
a5f63ea
address comments
4873b73
Merge branch 'master' into conditional-policy
frankyn c853a84
Clean up
c64e55a
Merge branch 'conditional-policy' of github.com:frankyn/java-core int…
d2fab21
respond to comments
86cd863
Merge branch 'master' into conditional-policy
085959d
address comments
174e8c4
format
14e1aac
address feedback
fdb040a
remove unnecessary null check
42199d1
lint
bbb708a
address feedback
7f0e33e
remove ImmutableList from Binding AutoValue surface
108faec
address feedback
79126b5
split up unit test
505f9bc
use guava beta annotation
a89ef0c
surface ImmutableList<> for Binding class.
f0b5085
use BetaApi from api.core
9f5e600
return as expected
8580f5a
partial addressing of feedback
9fe4358
address feedback pt2
8f48a15
address remaining feedback
615ba06
Merge branch 'master' into conditional-policy
2b56641
address one last feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Base implementation
- Loading branch information
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
google-cloud-core/src/main/java/com/google/cloud/Binding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| * Copyright 2019 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.common.base.MoreObjects; | ||
| import java.util.*; | ||
|
|
||
| public class Binding { | ||
| private Role role; | ||
| private Set<Identity> identities; | ||
| private Condition condition; | ||
|
|
||
| public static class Builder { | ||
| private Role role; | ||
| private Set<Identity> identities; | ||
| private Condition condition; | ||
|
|
||
| @InternalApi("This class should only be extended within google-cloud-java") | ||
| protected Builder() {} | ||
|
|
||
| @InternalApi("This class should only be extended within google-cloud-java") | ||
| protected Builder(Binding binding) { | ||
| setRole(binding.role); | ||
| setIdentities(binding.identities); | ||
| setCondition(binding.condition); | ||
| } | ||
|
|
||
| public final Binding.Builder setRole(Role role) { | ||
| this.role = role; | ||
| return this; | ||
| } | ||
|
|
||
| public final Binding.Builder setIdentities(Set<Identity> identities) { | ||
| this.identities = identities; | ||
| return this; | ||
| } | ||
|
|
||
| public final Binding.Builder setCondition(Condition condition) { | ||
| this.condition = condition; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds one or more identities to the binding | ||
| * | ||
| * @throws NullPointerException if any of the identities is null. | ||
| */ | ||
| public final Builder addIdentity(Identity first, Identity... others) { | ||
| String nullIdentityMessage = "Null identities are not permitted."; | ||
| checkNotNull(first, nullIdentityMessage); | ||
| checkNotNull(others, nullIdentityMessage); | ||
| for (Identity identity : others) { | ||
| checkNotNull(identity, nullIdentityMessage); | ||
| } | ||
| Set<Identity> toAdd = new LinkedHashSet<>(); | ||
| toAdd.add(first); | ||
| toAdd.addAll(Arrays.asList(others)); | ||
| if (identities == null) { | ||
| identities = new HashSet<>(); | ||
| } | ||
| identities.addAll(toAdd); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Removes one or more identities from an existing binding. Does nothing if the binding | ||
| * associated with the provided role doesn't exist. | ||
| */ | ||
| public final Builder removeIdentity(Identity first, Identity... others) { | ||
| if (identities != null) { | ||
| identities.remove(first); | ||
| identities.removeAll(Arrays.asList(others)); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** Creates a {@code Policy} object. */ | ||
| public final Binding build() { | ||
| return new Binding(this); | ||
| } | ||
| } | ||
|
|
||
| private Binding(Binding.Builder builder) { | ||
| this.role = builder.role; | ||
| this.identities = builder.identities; | ||
| this.condition = builder.condition; | ||
| } | ||
|
|
||
| public Binding.Builder toBuilder() { | ||
| return new Binding.Builder(this); | ||
| } | ||
|
|
||
| public Role getRole() { | ||
| return this.role; | ||
| } | ||
|
|
||
| public Set<Identity> getIdentities() { | ||
| return this.identities; | ||
| } | ||
|
|
||
| public Condition getCondition() { | ||
| return this.condition; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("role", role) | ||
| .add("identities", identities) | ||
| .add("condition", condition) | ||
| .toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(getClass(), role, identities, condition); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == this) { | ||
| return true; | ||
| } | ||
| if (!(obj instanceof Binding)) { | ||
| return false; | ||
| } | ||
| Binding other = (Binding) obj; | ||
| return Objects.equals(role, other.getRole()) | ||
| && Objects.equals(identities, other.getIdentities()) | ||
| && Objects.equals(condition, other.getCondition()); | ||
| } | ||
|
|
||
| /** Returns a builder for {@code Policy} objects. */ | ||
| public static Binding.Builder newBuilder() { | ||
| return new Binding.Builder(); | ||
| } | ||
| } | ||
118 changes: 118 additions & 0 deletions
118
google-cloud-core/src/main/java/com/google/cloud/Condition.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright 2019 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.common.base.MoreObjects; | ||
| import java.util.Objects; | ||
|
|
||
| public class Condition { | ||
| private String title; | ||
| private String description; | ||
| private String expression; | ||
|
|
||
| public static class Builder { | ||
| private String title; | ||
| private String description; | ||
| private String expression; | ||
|
|
||
| @InternalApi("This class should only be extended within google-cloud-java") | ||
| protected Builder() {} | ||
|
|
||
| @InternalApi("This class should only be extended within google-cloud-java") | ||
| protected Builder(Condition condition) { | ||
| this.title = condition.title; | ||
| this.description = condition.description; | ||
| this.expression = condition.expression; | ||
| } | ||
|
|
||
| public final Condition.Builder setTitle(String title) { | ||
| this.title = title; | ||
| return this; | ||
| } | ||
|
|
||
| public final Condition.Builder setDescription(String description) { | ||
| this.description = description; | ||
| return this; | ||
| } | ||
|
|
||
| public final Condition.Builder setExpression(String expression) { | ||
| this.expression = expression; | ||
| return this; | ||
| } | ||
|
|
||
| /** Creates a {@code Condition} object. */ | ||
| public final Condition build() { | ||
| return new Condition(this); | ||
| } | ||
| } | ||
|
|
||
| private Condition(Condition.Builder builder) { | ||
| this.title = builder.title; | ||
| this.description = builder.description; | ||
| this.expression = builder.expression; | ||
| } | ||
|
|
||
| public Condition.Builder toBuilder() { | ||
| return new Condition.Builder(this); | ||
| } | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public String getExpression() { | ||
| return expression; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("title", title) | ||
| .add("description", description) | ||
| .add("expression", expression) | ||
| .toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(getClass(), title, description, expression); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == this) { | ||
| return true; | ||
| } | ||
| if (!(obj instanceof Condition)) { | ||
| return false; | ||
| } | ||
| Condition other = (Condition) obj; | ||
| return Objects.equals(title, other.getTitle()) | ||
| && Objects.equals(description, other.getDescription()) | ||
| && Objects.equals(expression, other.getExpression()); | ||
| } | ||
|
|
||
| /** Returns a builder for {@code Policy} objects. */ | ||
| public static Condition.Builder newBuilder() { | ||
| return new Condition.Builder(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be using auto-value for these classes and builders if they are meant to be value classes?
It might be tricky if we're trying to enforce that the list of members is immutable on the actual value, but it's probably worthwhile to have these classes fully immutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call I didn't think about using auto-value and I think you're right in that it can be helpful to be fully immutable reducing the number of builders.
Please correct me but I don't think I should update the Policy class but only Binding and Condition to use Auto-Value.