Skip to content

Commit

Permalink
Merge pull request #101 from perfectsense/feature/bucket-iam-policy
Browse files Browse the repository at this point in the history
Bucket IAM Policy implementation
  • Loading branch information
Jeremy Collins authored Feb 4, 2020
2 parents 2d9b248 + 1f74401 commit f52747c
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 5 deletions.
105 changes: 105 additions & 0 deletions src/main/java/gyro/google/storage/BucketIamPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* 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 gyro.google.storage;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.google.api.services.storage.model.Policy;
import gyro.core.resource.Diffable;
import gyro.core.resource.Output;
import gyro.core.resource.Updatable;
import gyro.google.Copyable;

/*
The Bucket's IAM policy configuration.
*/
public class BucketIamPolicy extends Diffable implements Copyable<Policy> {

private List<BucketIamPolicyBinding> bindings;

// Read-only
private Integer version;
private String resourceId;

/**
* The association between the policies' role and members who may assume that role.
*
* @subresource gyro.google.storage.BucketIamPolicyBinding
*/
@Updatable
public List<BucketIamPolicyBinding> getBindings() {
if (bindings == null) {
bindings = new ArrayList<>();
}
return bindings;
}

public void setBindings(List<BucketIamPolicyBinding> bindings) {
this.bindings = bindings;
}

/**
* The ID of the resource to which this policy belongs.
*/
@Output
public String getResourceId() {
return resourceId;
}

public void setResourceId(String resourceId) {
this.resourceId = resourceId;
}

/**
* The version of this policy. See also `Understanding Policies <https://cloud.google.com/iam/docs/policies#versions>`_.
*/
@Output
public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

@Override
public String primaryKey() {
return "";
}

@Override
public void copyFrom(Policy model) {
setVersion(model.getVersion());
setResourceId(model.getResourceId());
getBindings().clear();
if (model.getBindings() != null) {
setBindings(model.getBindings().stream().map(binding -> {
BucketIamPolicyBinding iamBinding = newSubresource(BucketIamPolicyBinding.class);
iamBinding.copyFrom(binding);
return iamBinding;
}).collect(Collectors.toList())
);
}
}

public Policy toPolicy() {
return new Policy().setVersion(3).setResourceId(getResourceId())
.setBindings(getBindings().stream().map(BucketIamPolicyBinding::toBinding).collect(Collectors.toList()));
}
}
116 changes: 116 additions & 0 deletions src/main/java/gyro/google/storage/BucketIamPolicyBinding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* 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 gyro.google.storage;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.google.api.services.storage.model.Expr;
import com.google.api.services.storage.model.Policy;
import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Required;
import gyro.google.Copyable;

/*
* The Bucket's IAM policy binding configuration.
*/
public class BucketIamPolicyBinding extends Diffable implements Copyable<Policy.Bindings> {

private String role;
private List<String> members;
private BucketIamPolicyBindingCondition condition;

/**
* The role associated with this binding. (Required)
*/
@Required
public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

/**
* A list of identifiers for members who may assume the provided role. (Required)
*/
@Required
public List<String> getMembers() {
if (members == null) {
members = new ArrayList<>();
}
return members.stream().sorted().collect(Collectors.toList());
}

public void setMembers(List<String> members) {
this.members = members;
}

/**
* The condition object associated with this binding.
*
* @subresource gyro.google.storage.BucketIamPolicyBindingConfiguration
*/
@Updatable
public BucketIamPolicyBindingCondition getCondition() {
return condition;
}

public void setCondition(BucketIamPolicyBindingCondition condition) {
this.condition = condition;
}

@Override
public String primaryKey() {
return String.format(
"with role '%s', members [ '%s' ] and condition '%s'",
getRole(),
String.join("','", getMembers()),
(getCondition() == null) ? "" : getCondition().primaryKey());
}

@Override
public void copyFrom(Policy.Bindings model) {
setRole(model.getRole());
setCondition(null);
if (model.getCondition() != null) {
Expr condition = model.getCondition();
BucketIamPolicyBindingCondition iamCondition = newSubresource(BucketIamPolicyBindingCondition.class);
iamCondition.copyFrom(condition);
setCondition(iamCondition);
}

getMembers().clear();
if (model.getMembers() != null) {
setMembers(model.getMembers());
}
}

public Policy.Bindings toBinding() {
Policy.Bindings policyBinding = new Policy.Bindings();
policyBinding.setMembers(getMembers());
policyBinding.setRole(getRole());
if (getCondition() != null) {
policyBinding.setCondition(getCondition().toCondition());
}

return policyBinding;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* 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 gyro.google.storage;

import com.google.api.services.storage.model.Expr;
import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Required;
import gyro.google.Copyable;

/*
* The Bucket's IAM policy binding condition configuration.
*/
public class BucketIamPolicyBindingCondition extends Diffable implements Copyable<Expr> {

private String description;
private String expression;
private String title;

/**
* The description of the condition.
*/
@Updatable
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

/**
* The attribute-based logic expression. See also `Conditions Overview <https://cloud.google.com/iam/docs/conditions-overview#attributes>`_. (Required)
*/
@Required
public String getExpression() {
return expression;
}

public void setExpression(String expression) {
this.expression = expression;
}

/**
* The title of the condition. (Required)
*/
@Required
public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String primaryKey() {
return String.format("with title '%s' and expression of '%s'", getTitle(), getExpression());
}

@Override
public void copyFrom(Expr model) {
setDescription(model.getDescription());
setExpression(model.getExpression());
setTitle(model.getTitle());
}

public Expr toCondition() {
return new Expr().setDescription(getDescription()).setExpression(getExpression()).setTitle(getTitle());
}
}
Loading

0 comments on commit f52747c

Please sign in to comment.