1 /* 2 * Copyright 2022 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 * https://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 17 package com.google.cloud.orgpolicy.v2; 18 19 import com.google.api.core.BetaApi; 20 import com.google.api.pathtemplate.PathTemplate; 21 import com.google.api.pathtemplate.ValidationException; 22 import com.google.api.resourcenames.ResourceName; 23 import com.google.common.base.Preconditions; 24 import com.google.common.collect.ImmutableMap; 25 import java.util.ArrayList; 26 import java.util.List; 27 import java.util.Map; 28 import java.util.Objects; 29 import javax.annotation.Generated; 30 31 // AUTO-GENERATED DOCUMENTATION AND CLASS. 32 @Generated("by gapic-generator-java") 33 public class PolicyName implements ResourceName { 34 private static final PathTemplate PROJECT_POLICY = 35 PathTemplate.createWithoutUrlEncoding("projects/{project}/policies/{policy}"); 36 private static final PathTemplate FOLDER_POLICY = 37 PathTemplate.createWithoutUrlEncoding("folders/{folder}/policies/{policy}"); 38 private static final PathTemplate ORGANIZATION_POLICY = 39 PathTemplate.createWithoutUrlEncoding("organizations/{organization}/policies/{policy}"); 40 private volatile Map<String, String> fieldValuesMap; 41 private PathTemplate pathTemplate; 42 private String fixedValue; 43 private final String project; 44 private final String policy; 45 private final String folder; 46 private final String organization; 47 48 @Deprecated PolicyName()49 protected PolicyName() { 50 project = null; 51 policy = null; 52 folder = null; 53 organization = null; 54 } 55 PolicyName(Builder builder)56 private PolicyName(Builder builder) { 57 project = Preconditions.checkNotNull(builder.getProject()); 58 policy = Preconditions.checkNotNull(builder.getPolicy()); 59 folder = null; 60 organization = null; 61 pathTemplate = PROJECT_POLICY; 62 } 63 PolicyName(FolderPolicyBuilder builder)64 private PolicyName(FolderPolicyBuilder builder) { 65 folder = Preconditions.checkNotNull(builder.getFolder()); 66 policy = Preconditions.checkNotNull(builder.getPolicy()); 67 project = null; 68 organization = null; 69 pathTemplate = FOLDER_POLICY; 70 } 71 PolicyName(OrganizationPolicyBuilder builder)72 private PolicyName(OrganizationPolicyBuilder builder) { 73 organization = Preconditions.checkNotNull(builder.getOrganization()); 74 policy = Preconditions.checkNotNull(builder.getPolicy()); 75 project = null; 76 folder = null; 77 pathTemplate = ORGANIZATION_POLICY; 78 } 79 getProject()80 public String getProject() { 81 return project; 82 } 83 getPolicy()84 public String getPolicy() { 85 return policy; 86 } 87 getFolder()88 public String getFolder() { 89 return folder; 90 } 91 getOrganization()92 public String getOrganization() { 93 return organization; 94 } 95 newBuilder()96 public static Builder newBuilder() { 97 return new Builder(); 98 } 99 100 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newProjectPolicyBuilder()101 public static Builder newProjectPolicyBuilder() { 102 return new Builder(); 103 } 104 105 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newFolderPolicyBuilder()106 public static FolderPolicyBuilder newFolderPolicyBuilder() { 107 return new FolderPolicyBuilder(); 108 } 109 110 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newOrganizationPolicyBuilder()111 public static OrganizationPolicyBuilder newOrganizationPolicyBuilder() { 112 return new OrganizationPolicyBuilder(); 113 } 114 toBuilder()115 public Builder toBuilder() { 116 return new Builder(this); 117 } 118 of(String project, String policy)119 public static PolicyName of(String project, String policy) { 120 return newBuilder().setProject(project).setPolicy(policy).build(); 121 } 122 123 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofProjectPolicyName(String project, String policy)124 public static PolicyName ofProjectPolicyName(String project, String policy) { 125 return newBuilder().setProject(project).setPolicy(policy).build(); 126 } 127 128 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofFolderPolicyName(String folder, String policy)129 public static PolicyName ofFolderPolicyName(String folder, String policy) { 130 return newFolderPolicyBuilder().setFolder(folder).setPolicy(policy).build(); 131 } 132 133 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofOrganizationPolicyName(String organization, String policy)134 public static PolicyName ofOrganizationPolicyName(String organization, String policy) { 135 return newOrganizationPolicyBuilder().setOrganization(organization).setPolicy(policy).build(); 136 } 137 format(String project, String policy)138 public static String format(String project, String policy) { 139 return newBuilder().setProject(project).setPolicy(policy).build().toString(); 140 } 141 142 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatProjectPolicyName(String project, String policy)143 public static String formatProjectPolicyName(String project, String policy) { 144 return newBuilder().setProject(project).setPolicy(policy).build().toString(); 145 } 146 147 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatFolderPolicyName(String folder, String policy)148 public static String formatFolderPolicyName(String folder, String policy) { 149 return newFolderPolicyBuilder().setFolder(folder).setPolicy(policy).build().toString(); 150 } 151 152 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatOrganizationPolicyName(String organization, String policy)153 public static String formatOrganizationPolicyName(String organization, String policy) { 154 return newOrganizationPolicyBuilder() 155 .setOrganization(organization) 156 .setPolicy(policy) 157 .build() 158 .toString(); 159 } 160 parse(String formattedString)161 public static PolicyName parse(String formattedString) { 162 if (formattedString.isEmpty()) { 163 return null; 164 } 165 if (PROJECT_POLICY.matches(formattedString)) { 166 Map<String, String> matchMap = PROJECT_POLICY.match(formattedString); 167 return ofProjectPolicyName(matchMap.get("project"), matchMap.get("policy")); 168 } else if (FOLDER_POLICY.matches(formattedString)) { 169 Map<String, String> matchMap = FOLDER_POLICY.match(formattedString); 170 return ofFolderPolicyName(matchMap.get("folder"), matchMap.get("policy")); 171 } else if (ORGANIZATION_POLICY.matches(formattedString)) { 172 Map<String, String> matchMap = ORGANIZATION_POLICY.match(formattedString); 173 return ofOrganizationPolicyName(matchMap.get("organization"), matchMap.get("policy")); 174 } 175 throw new ValidationException("PolicyName.parse: formattedString not in valid format"); 176 } 177 parseList(List<String> formattedStrings)178 public static List<PolicyName> parseList(List<String> formattedStrings) { 179 List<PolicyName> list = new ArrayList<>(formattedStrings.size()); 180 for (String formattedString : formattedStrings) { 181 list.add(parse(formattedString)); 182 } 183 return list; 184 } 185 toStringList(List<PolicyName> values)186 public static List<String> toStringList(List<PolicyName> values) { 187 List<String> list = new ArrayList<>(values.size()); 188 for (PolicyName value : values) { 189 if (value == null) { 190 list.add(""); 191 } else { 192 list.add(value.toString()); 193 } 194 } 195 return list; 196 } 197 isParsableFrom(String formattedString)198 public static boolean isParsableFrom(String formattedString) { 199 return PROJECT_POLICY.matches(formattedString) 200 || FOLDER_POLICY.matches(formattedString) 201 || ORGANIZATION_POLICY.matches(formattedString); 202 } 203 204 @Override getFieldValuesMap()205 public Map<String, String> getFieldValuesMap() { 206 if (fieldValuesMap == null) { 207 synchronized (this) { 208 if (fieldValuesMap == null) { 209 ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder(); 210 if (project != null) { 211 fieldMapBuilder.put("project", project); 212 } 213 if (policy != null) { 214 fieldMapBuilder.put("policy", policy); 215 } 216 if (folder != null) { 217 fieldMapBuilder.put("folder", folder); 218 } 219 if (organization != null) { 220 fieldMapBuilder.put("organization", organization); 221 } 222 fieldValuesMap = fieldMapBuilder.build(); 223 } 224 } 225 } 226 return fieldValuesMap; 227 } 228 getFieldValue(String fieldName)229 public String getFieldValue(String fieldName) { 230 return getFieldValuesMap().get(fieldName); 231 } 232 233 @Override toString()234 public String toString() { 235 return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap()); 236 } 237 238 @Override equals(Object o)239 public boolean equals(Object o) { 240 if (o == this) { 241 return true; 242 } 243 if (o != null || getClass() == o.getClass()) { 244 PolicyName that = ((PolicyName) o); 245 return Objects.equals(this.project, that.project) 246 && Objects.equals(this.policy, that.policy) 247 && Objects.equals(this.folder, that.folder) 248 && Objects.equals(this.organization, that.organization); 249 } 250 return false; 251 } 252 253 @Override hashCode()254 public int hashCode() { 255 int h = 1; 256 h *= 1000003; 257 h ^= Objects.hashCode(fixedValue); 258 h *= 1000003; 259 h ^= Objects.hashCode(project); 260 h *= 1000003; 261 h ^= Objects.hashCode(policy); 262 h *= 1000003; 263 h ^= Objects.hashCode(folder); 264 h *= 1000003; 265 h ^= Objects.hashCode(organization); 266 return h; 267 } 268 269 /** Builder for projects/{project}/policies/{policy}. */ 270 public static class Builder { 271 private String project; 272 private String policy; 273 Builder()274 protected Builder() {} 275 getProject()276 public String getProject() { 277 return project; 278 } 279 getPolicy()280 public String getPolicy() { 281 return policy; 282 } 283 setProject(String project)284 public Builder setProject(String project) { 285 this.project = project; 286 return this; 287 } 288 setPolicy(String policy)289 public Builder setPolicy(String policy) { 290 this.policy = policy; 291 return this; 292 } 293 Builder(PolicyName policyName)294 private Builder(PolicyName policyName) { 295 Preconditions.checkArgument( 296 Objects.equals(policyName.pathTemplate, PROJECT_POLICY), 297 "toBuilder is only supported when PolicyName has the pattern of projects/{project}/policies/{policy}"); 298 this.project = policyName.project; 299 this.policy = policyName.policy; 300 } 301 build()302 public PolicyName build() { 303 return new PolicyName(this); 304 } 305 } 306 307 /** Builder for folders/{folder}/policies/{policy}. */ 308 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") 309 public static class FolderPolicyBuilder { 310 private String folder; 311 private String policy; 312 FolderPolicyBuilder()313 protected FolderPolicyBuilder() {} 314 getFolder()315 public String getFolder() { 316 return folder; 317 } 318 getPolicy()319 public String getPolicy() { 320 return policy; 321 } 322 setFolder(String folder)323 public FolderPolicyBuilder setFolder(String folder) { 324 this.folder = folder; 325 return this; 326 } 327 setPolicy(String policy)328 public FolderPolicyBuilder setPolicy(String policy) { 329 this.policy = policy; 330 return this; 331 } 332 build()333 public PolicyName build() { 334 return new PolicyName(this); 335 } 336 } 337 338 /** Builder for organizations/{organization}/policies/{policy}. */ 339 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") 340 public static class OrganizationPolicyBuilder { 341 private String organization; 342 private String policy; 343 OrganizationPolicyBuilder()344 protected OrganizationPolicyBuilder() {} 345 getOrganization()346 public String getOrganization() { 347 return organization; 348 } 349 getPolicy()350 public String getPolicy() { 351 return policy; 352 } 353 setOrganization(String organization)354 public OrganizationPolicyBuilder setOrganization(String organization) { 355 this.organization = organization; 356 return this; 357 } 358 setPolicy(String policy)359 public OrganizationPolicyBuilder setPolicy(String policy) { 360 this.policy = policy; 361 return this; 362 } 363 build()364 public PolicyName build() { 365 return new PolicyName(this); 366 } 367 } 368 } 369