|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.resourcemanager; |
| 17 | + |
| 18 | +import com.google.api.services.cloudresourcemanager.model.BooleanConstraint; |
| 19 | +import com.google.api.services.cloudresourcemanager.model.Constraint; |
| 20 | +import com.google.api.services.cloudresourcemanager.model.ListConstraint; |
| 21 | +import com.google.common.base.Function; |
| 22 | +import com.google.common.base.MoreObjects; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +/** |
| 26 | + * A Google Cloud Resource Manager constraint metadata object. |
| 27 | + * |
| 28 | + * @see <a |
| 29 | + * href="https://cloud.google.com/resource-manager/reference/rest/v1/ListAvailableOrgPolicyConstraintsResponse#Constraint">Constraint</a> |
| 30 | + */ |
| 31 | +public class ConstraintInfo { |
| 32 | + |
| 33 | + static final Function<Constraint, ConstraintInfo> FROM_PROTOBUF_FUNCTION = |
| 34 | + new Function<Constraint, ConstraintInfo>() { |
| 35 | + @Override |
| 36 | + public ConstraintInfo apply(Constraint protobuf) { |
| 37 | + return ConstraintInfo.fromProtobuf(protobuf); |
| 38 | + } |
| 39 | + }; |
| 40 | + static final Function<ConstraintInfo, Constraint> TO_PROTOBUF_FUNCTION = |
| 41 | + new Function<ConstraintInfo, Constraint>() { |
| 42 | + @Override |
| 43 | + public Constraint apply(ConstraintInfo constraintInfo) { |
| 44 | + return constraintInfo.toProtobuf(); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + private BooleanConstraint booleanConstraint; |
| 49 | + private String constraintDefault; |
| 50 | + private String description; |
| 51 | + private String displayName; |
| 52 | + private Constraints constraints; |
| 53 | + private String name; |
| 54 | + private Integer version; |
| 55 | + |
| 56 | + /** |
| 57 | + * A Constraint that allows or disallows a list of string values, which are configured by an |
| 58 | + * Organization's policy administrator with a Policy. |
| 59 | + */ |
| 60 | + static class Constraints { |
| 61 | + |
| 62 | + private final String suggestedValue; |
| 63 | + private final Boolean supportsUnder; |
| 64 | + |
| 65 | + Constraints(String suggestedValue, Boolean supportsUnder) { |
| 66 | + this.suggestedValue = suggestedValue; |
| 67 | + this.supportsUnder = supportsUnder; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * The Google Cloud Console tries to default to a configuration that matches the value specified |
| 72 | + * in this Constraint. |
| 73 | + */ |
| 74 | + String getSuggestedValue() { |
| 75 | + return suggestedValue; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Indicates whether subtrees of Cloud Resource Manager resource hierarchy can be used in |
| 80 | + * Policy.allowed_values and Policy.denied_values. |
| 81 | + */ |
| 82 | + Boolean getSupportsUnder() { |
| 83 | + return supportsUnder; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String toString() { |
| 88 | + return MoreObjects.toStringHelper(this) |
| 89 | + .add("suggestedValue", getSuggestedValue()) |
| 90 | + .add("supportsUnder", getSupportsUnder()) |
| 91 | + .toString(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public int hashCode() { |
| 96 | + return Objects.hash(suggestedValue, supportsUnder); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean equals(Object o) { |
| 101 | + if (this == o) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + if (o == null || getClass() != o.getClass()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + Constraints that = (Constraints) o; |
| 108 | + return Objects.equals(suggestedValue, that.suggestedValue) |
| 109 | + && Objects.equals(supportsUnder, that.supportsUnder); |
| 110 | + } |
| 111 | + |
| 112 | + ListConstraint toProtobuf() { |
| 113 | + return new ListConstraint().setSuggestedValue(suggestedValue).setSupportsUnder(supportsUnder); |
| 114 | + } |
| 115 | + |
| 116 | + static Constraints fromProtobuf(ListConstraint listConstraint) { |
| 117 | + return new Constraints(listConstraint.getSuggestedValue(), listConstraint.getSupportsUnder()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** Builder for {@code ConstraintInfo}. */ |
| 122 | + static class Builder { |
| 123 | + private BooleanConstraint booleanConstraint; |
| 124 | + private String constraintDefault; |
| 125 | + private String description; |
| 126 | + private String displayName; |
| 127 | + private Constraints constraints; |
| 128 | + private String name; |
| 129 | + private Integer version; |
| 130 | + |
| 131 | + Builder(String name) { |
| 132 | + this.name = name; |
| 133 | + } |
| 134 | + |
| 135 | + Builder(ConstraintInfo info) { |
| 136 | + this.booleanConstraint = info.booleanConstraint; |
| 137 | + this.constraintDefault = info.constraintDefault; |
| 138 | + this.description = info.description; |
| 139 | + this.displayName = info.displayName; |
| 140 | + this.constraints = info.constraints; |
| 141 | + this.name = info.name; |
| 142 | + this.version = info.version; |
| 143 | + } |
| 144 | + |
| 145 | + Builder setBooleanConstraint(BooleanConstraint booleanConstraint) { |
| 146 | + this.booleanConstraint = booleanConstraint; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + Builder setConstraintDefault(String constraintDefault) { |
| 151 | + this.constraintDefault = constraintDefault; |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + Builder setDescription(String description) { |
| 156 | + this.description = description; |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + Builder setDisplayName(String displayName) { |
| 161 | + this.displayName = displayName; |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + Builder setConstraints(Constraints constraints) { |
| 166 | + this.constraints = constraints; |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
| 170 | + Builder setName(String name) { |
| 171 | + this.name = name; |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + Builder setVersion(Integer version) { |
| 176 | + this.version = version; |
| 177 | + return this; |
| 178 | + } |
| 179 | + |
| 180 | + ConstraintInfo build() { |
| 181 | + return new ConstraintInfo(this); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + ConstraintInfo(Builder builder) { |
| 186 | + this.booleanConstraint = builder.booleanConstraint; |
| 187 | + this.constraintDefault = builder.constraintDefault; |
| 188 | + this.description = builder.description; |
| 189 | + this.displayName = builder.displayName; |
| 190 | + this.constraints = builder.constraints; |
| 191 | + this.name = builder.name; |
| 192 | + this.version = builder.version; |
| 193 | + } |
| 194 | + |
| 195 | + /** Returns the boolean constraint to check whether the constraint is enforced or not. */ |
| 196 | + public BooleanConstraint getBooleanConstraint() { |
| 197 | + return booleanConstraint; |
| 198 | + } |
| 199 | + |
| 200 | + /** Returns the default behavior of the constraint. */ |
| 201 | + public String getConstraintDefault() { |
| 202 | + return constraintDefault; |
| 203 | + } |
| 204 | + |
| 205 | + /** Returns the detailed description of the constraint. */ |
| 206 | + public String getDescription() { |
| 207 | + return description; |
| 208 | + } |
| 209 | + |
| 210 | + /** Returns the human readable name of the constraint. */ |
| 211 | + public String getDisplayName() { |
| 212 | + return displayName; |
| 213 | + } |
| 214 | + |
| 215 | + /** Returns the listConstraintInfo. */ |
| 216 | + public Constraints getConstraints() { |
| 217 | + return constraints; |
| 218 | + } |
| 219 | + |
| 220 | + /** Returns the globally unique name of the constraint. */ |
| 221 | + public String getName() { |
| 222 | + return name; |
| 223 | + } |
| 224 | + |
| 225 | + /** Returns the version of the Constraint. Default version is 0. */ |
| 226 | + public Integer getVersion() { |
| 227 | + return version; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public boolean equals(Object o) { |
| 232 | + if (this == o) { |
| 233 | + return true; |
| 234 | + } |
| 235 | + if (o == null || getClass() != o.getClass()) { |
| 236 | + return false; |
| 237 | + } |
| 238 | + ConstraintInfo that = (ConstraintInfo) o; |
| 239 | + return Objects.equals(booleanConstraint, that.booleanConstraint) |
| 240 | + && Objects.equals(constraintDefault, that.constraintDefault) |
| 241 | + && Objects.equals(description, that.description) |
| 242 | + && Objects.equals(displayName, that.displayName) |
| 243 | + && Objects.equals(constraints, that.constraints) |
| 244 | + && Objects.equals(name, that.name) |
| 245 | + && Objects.equals(version, that.version); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public int hashCode() { |
| 250 | + return Objects.hash( |
| 251 | + booleanConstraint, constraintDefault, description, displayName, constraints, name, version); |
| 252 | + } |
| 253 | + |
| 254 | + /** Returns a builder for the {@link ConstraintInfo} object. */ |
| 255 | + public static Builder newBuilder(String name) { |
| 256 | + return new Builder(name); |
| 257 | + } |
| 258 | + |
| 259 | + /** Returns a builder for the {@link ConstraintInfo} object. */ |
| 260 | + public Builder toBuilder() { |
| 261 | + return new Builder(this); |
| 262 | + } |
| 263 | + |
| 264 | + Constraint toProtobuf() { |
| 265 | + Constraint constraintProto = new Constraint(); |
| 266 | + constraintProto.setBooleanConstraint(booleanConstraint); |
| 267 | + constraintProto.setConstraintDefault(constraintDefault); |
| 268 | + constraintProto.setDescription(description); |
| 269 | + constraintProto.setDisplayName(displayName); |
| 270 | + if (constraints != null) { |
| 271 | + constraintProto.setListConstraint(constraints.toProtobuf()); |
| 272 | + } |
| 273 | + constraintProto.setName(name); |
| 274 | + constraintProto.setVersion(version); |
| 275 | + return constraintProto; |
| 276 | + } |
| 277 | + |
| 278 | + static ConstraintInfo fromProtobuf(Constraint constraintProtobuf) { |
| 279 | + Builder builder = newBuilder(constraintProtobuf.getName()); |
| 280 | + builder.setBooleanConstraint(constraintProtobuf.getBooleanConstraint()); |
| 281 | + builder.setConstraintDefault(constraintProtobuf.getConstraintDefault()); |
| 282 | + builder.setDescription(constraintProtobuf.getDescription()); |
| 283 | + builder.setDisplayName(constraintProtobuf.getDisplayName()); |
| 284 | + if (constraintProtobuf.getListConstraint() != null) { |
| 285 | + builder.setConstraints(Constraints.fromProtobuf(constraintProtobuf.getListConstraint())); |
| 286 | + } |
| 287 | + if (constraintProtobuf.getName() != null && !constraintProtobuf.getName().equals("Unnamed")) { |
| 288 | + builder.setName(constraintProtobuf.getName()); |
| 289 | + } |
| 290 | + builder.setVersion(constraintProtobuf.getVersion()); |
| 291 | + return builder.build(); |
| 292 | + } |
| 293 | +} |
0 commit comments