Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

feat: support conditional policies #110

Merged
merged 36 commits into from Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
67e52ce
Base implementation
frankyn Dec 17, 2019
84fcfea
Update with unit tests
frankyn Dec 20, 2019
145f7a0
lint
frankyn Dec 20, 2019
ac1fd6e
correct copyright date
frankyn Jan 7, 2020
2c8724b
lint
frankyn Jan 7, 2020
c90dbb0
Revert removal of helper functions
frankyn Jan 8, 2020
b0a617f
use auto-value
frankyn Jan 22, 2020
9dbfc2d
Merge branch 'master' into conditional-policy
frankyn Jan 22, 2020
54ad076
reformat Binding.java and Condition.java
frankyn Jan 22, 2020
ff49620
remove unnecessary dep
frankyn Jan 22, 2020
298b2be
code format
frankyn Jan 22, 2020
aaebba4
add dep on com.google.code.findbugs in google-cloud-core
frankyn Jan 22, 2020
a5f63ea
address comments
frankyn Feb 6, 2020
4873b73
Merge branch 'master' into conditional-policy
frankyn Feb 11, 2020
c853a84
Clean up
frankyn Feb 11, 2020
c64e55a
Merge branch 'conditional-policy' of github.com:frankyn/java-core int…
frankyn Feb 11, 2020
d2fab21
respond to comments
frankyn Feb 19, 2020
86cd863
Merge branch 'master' into conditional-policy
frankyn Feb 19, 2020
085959d
address comments
frankyn Feb 20, 2020
174e8c4
format
frankyn Feb 20, 2020
14e1aac
address feedback
frankyn Feb 21, 2020
fdb040a
remove unnecessary null check
frankyn Feb 21, 2020
42199d1
lint
frankyn Feb 21, 2020
bbb708a
address feedback
frankyn Feb 21, 2020
7f0e33e
remove ImmutableList from Binding AutoValue surface
frankyn Feb 21, 2020
108faec
address feedback
frankyn Feb 25, 2020
79126b5
split up unit test
frankyn Feb 25, 2020
505f9bc
use guava beta annotation
frankyn Feb 25, 2020
a89ef0c
surface ImmutableList<> for Binding class.
frankyn Feb 25, 2020
f0b5085
use BetaApi from api.core
frankyn Feb 25, 2020
9f5e600
return as expected
frankyn Feb 26, 2020
8580f5a
partial addressing of feedback
frankyn Feb 26, 2020
9fe4358
address feedback pt2
frankyn Feb 26, 2020
8f48a15
address remaining feedback
frankyn Feb 26, 2020
615ba06
Merge branch 'master' into conditional-policy
frankyn Feb 26, 2020
2b56641
address one last feedback
frankyn Feb 26, 2020
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
153 changes: 153 additions & 0 deletions google-cloud-core/src/main/java/com/google/cloud/Binding.java
@@ -0,0 +1,153 @@
/*
* Copyright 2020 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 com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Binding {
private String role;
private List<String> members;
private Condition condition;

public static class Builder {
Copy link
Contributor

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.

Copy link
Member Author

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.

private List<String> members = new ArrayList();
Copy link
Member

Choose a reason for hiding this comment

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

Did we end up deciding to use a List instead of a Set for the list of members?

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated it to List<> instead of Set<> to get the conversation moving. I can revert back to Set<>.

private String role;
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);
setMembers(binding.members);
setCondition(binding.condition);
}

public final Binding.Builder setRole(String role) {
Copy link
Member

Choose a reason for hiding this comment

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

is there a reason to use String instead of Role?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm trying to remove helpers from the new usage patterns. I could add them back in though.

String nullIdentityMessage = "The role cannot be null.";
checkNotNull(role, nullIdentityMessage);
this.role = role;
return this;
}

public final Binding.Builder setMembers(List<String> members) {
String nullIdentityMessage = "Null members are not permitted.";
checkNotNull(members, nullIdentityMessage);
this.members.clear();
for (String member : members) {
// Check member not null
this.members.add(member);
}
return this;
}

public final Binding.Builder removeMembers(String first, String... others) {
String nullIdentityMessage = "Null members are not permitted.";
checkNotNull(first, nullIdentityMessage);
checkNotNull(others, nullIdentityMessage);
this.members.remove(first);
for (String member : others) {
this.members.remove(member);
}
return this;
}

public final Binding.Builder addMembers(String first, String... others) {
String nullIdentityMessage = "Null identities are not permitted.";
checkNotNull(first, nullIdentityMessage);
checkNotNull(others, nullIdentityMessage);
this.members.add(first);
for (String member : others) {
this.members.add(member);
}
return this;
}

public final Binding.Builder setCondition(Condition condition) {
this.condition = condition;
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.members = ImmutableList.copyOf(builder.members);
this.condition = builder.condition;
}

public Binding.Builder toBuilder() {
return new Binding.Builder(this);
}

public String getRole() {
return this.role;
}

public List<String> getMembers() {
return this.members;
}

public Condition getCondition() {
return this.condition;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("role", role)
.add("members", members)
.add("condition", condition)
.toString();
}

@Override
public int hashCode() {
return Objects.hash(getClass(), role, members, 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(members, other.getMembers())
&& 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 google-cloud-core/src/main/java/com/google/cloud/Condition.java
@@ -0,0 +1,118 @@
/*
* Copyright 2020 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();
}
}