-
Notifications
You must be signed in to change notification settings - Fork 39
feat: support conditional policies #110
Changes from 6 commits
67e52ce
84fcfea
145f7a0
ac1fd6e
2c8724b
c90dbb0
b0a617f
9dbfc2d
54ad076
ff49620
298b2be
aaebba4
a5f63ea
4873b73
c853a84
c64e55a
d2fab21
86cd863
085959d
174e8c4
14e1aac
fdb040a
42199d1
bbb708a
7f0e33e
108faec
79126b5
505f9bc
a89ef0c
f0b5085
9f5e600
8580f5a
9fe4358
8f48a15
615ba06
2b56641
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
private List<String> members = new ArrayList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we end up deciding to use a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason to use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
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.