This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
/
Project.java
287 lines (254 loc) · 10 KB
/
Project.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2015 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.resourcemanager;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.cloud.Policy;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* A Google Cloud Resource Manager project object.
*
* <p>A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
* AppEngine Apps, VMs, and other Google Cloud Platform resources. This class' member variables are
* immutable. Methods that change or update the underlying Project information return a new Project
* instance. {@code Project} adds a layer of service-related functionality over {@link ProjectInfo}.
*
* @deprecated v3 GAPIC client of ResourceManager is now available
*/
@Deprecated
public class Project extends ProjectInfo {
private static final long serialVersionUID = 6767630161335155133L;
private final ResourceManagerOptions options;
private transient ResourceManager resourceManager;
/** Builder for {@code Project}. */
public static class Builder extends ProjectInfo.Builder {
private final ResourceManager resourceManager;
private final ProjectInfo.BuilderImpl infoBuilder;
Builder(Project project) {
this.resourceManager = project.resourceManager;
this.infoBuilder = new ProjectInfo.BuilderImpl(project);
}
@Override
public Builder setName(String name) {
infoBuilder.setName(name);
return this;
}
@Override
public Builder setProjectId(String projectId) {
infoBuilder.setProjectId(projectId);
return this;
}
@Override
public Builder addLabel(String key, String value) {
infoBuilder.addLabel(key, value);
return this;
}
@Override
public Builder removeLabel(String key) {
infoBuilder.removeLabel(key);
return this;
}
@Override
public Builder clearLabels() {
infoBuilder.clearLabels();
return this;
}
@Override
public Builder setLabels(Map<String, String> labels) {
infoBuilder.setLabels(labels);
return this;
}
@Override
Builder setProjectNumber(Long projectNumber) {
infoBuilder.setProjectNumber(projectNumber);
return this;
}
@Override
Builder setState(State state) {
infoBuilder.setState(state);
return this;
}
@Override
Builder setCreateTimeMillis(Long createTimeMillis) {
infoBuilder.setCreateTimeMillis(createTimeMillis);
return this;
}
@Override
public Builder setParent(ResourceId parent) {
infoBuilder.setParent(parent);
return this;
}
@Override
public Project build() {
return new Project(resourceManager, infoBuilder);
}
}
Project(ResourceManager resourceManager, ProjectInfo.BuilderImpl infoBuilder) {
super(infoBuilder);
this.resourceManager = checkNotNull(resourceManager);
this.options = resourceManager.getOptions();
}
/** Returns the {@link ResourceManager} service object associated with this Project. */
public ResourceManager getResourceManager() {
return resourceManager;
}
/**
* Fetches the project's latest information. Returns {@code null} if the project does not exist.
*
* @return Project containing the project's updated metadata or {@code null} if not found
* @throws ResourceManagerException upon failure
*/
public Project reload() {
return resourceManager.get(getProjectId());
}
/**
* Marks the project identified by the specified project ID for deletion.
*
* <p>This method will only affect the project if the following criteria are met:
*
* <ul>
* <li>The project does not have a billing account associated with it.
* <li>The project has a lifecycle state of {@link ProjectInfo.State#ACTIVE}.
* </ul>
*
* This method changes the project's lifecycle state from {@link ProjectInfo.State#ACTIVE} to
* {@link ProjectInfo.State#DELETE_REQUESTED}. The deletion starts at an unspecified time, at
* which point the lifecycle state changes to {@link ProjectInfo.State#DELETE_IN_PROGRESS}. Until
* the deletion completes, you can check the lifecycle state checked by retrieving the project
* with {@link ResourceManager#get}, and the project remains visible to {@link
* ResourceManager#list}. However, you cannot update the project. After the deletion completes,
* the project is not retrievable by the {@link ResourceManager#get} and {@link
* ResourceManager#list} methods. The caller must have modify permissions for this project.
*
* @throws ResourceManagerException upon failure
* @see <a href=
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete">Cloud
* Resource Manager delete</a>
*/
public void delete() {
resourceManager.delete(getProjectId());
}
/**
* Restores the project identified by the specified project ID.
*
* <p>You can only use this method for a project that has a lifecycle state of {@link
* ProjectInfo.State#DELETE_REQUESTED}. After deletion starts, as indicated by a lifecycle state
* of {@link ProjectInfo.State#DELETE_IN_PROGRESS}, the project cannot be restored. The caller
* must have modify permissions for this project.
*
* @throws ResourceManagerException upon failure (including when the project can't be restored)
* @see <a href=
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete">Cloud
* Resource Manager undelete</a>
*/
public void undelete() {
resourceManager.undelete(getProjectId());
}
/**
* Replaces the attributes of the project with the attributes of this project.
*
* <p>The caller must have modify permissions for this project.
*
* @return the Project representing the new project metadata
* @throws ResourceManagerException upon failure
* @see <a href=
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud
* Resource Manager update</a>
*/
public Project replace() {
return resourceManager.replace(this);
}
/**
* Returns the IAM access control policy for this project. Returns {@code null} if the resource
* does not exist or if you do not have adequate permission to view the project or get the policy.
*
* @return the IAM policy for the project
* @throws ResourceManagerException upon failure
* @see <a href=
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/getIamPolicy">
* Resource Manager getIamPolicy</a>
*/
public Policy getPolicy() {
return resourceManager.getPolicy(getProjectId());
}
/**
* Sets the IAM access control policy for this project. Replaces any existing policy. It is
* recommended that you use the read-modify-write pattern. See code samples and important details
* of replacing policies in the documentation for {@link ResourceManager#replacePolicy}.
*
* @return the newly set IAM policy for this project
* @throws ResourceManagerException upon failure
* @see <a href=
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/setIamPolicy">
* Resource Manager setIamPolicy</a>
*/
public Policy replacePolicy(Policy newPolicy) {
return resourceManager.replacePolicy(getProjectId(), newPolicy);
}
/**
* Returns the permissions that a caller has on this project. You typically don't call this method
* if you're using Google Cloud Platform directly to manage permissions. This method is intended
* for integration with your proprietary software, such as a customized graphical user interface.
* For example, the Cloud Platform Console tests IAM permissions internally to determine which UI
* should be available to the logged-in user. Each service that supports IAM lists the possible
* permissions; see the <i>Supported Cloud Platform services</i> page below for links to these
* lists.
*
* @return a list of booleans representing whether the caller has the permissions specified (in
* the order of the given permissions)
* @throws ResourceManagerException upon failure
* @see <a href=
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/testIamPermissions">
* Resource Manager testIamPermissions</a>
* @see <a href= "https://cloud.google.com/iam/#supported_cloud_platform_services">Supported Cloud
* Platform Services</a>
*/
List<Boolean> testPermissions(List<String> permissions) {
return resourceManager.testPermissions(getProjectId(), permissions);
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !obj.getClass().equals(Project.class)) {
return false;
}
Project other = (Project) obj;
return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options);
}
@Override
public final int hashCode() {
return Objects.hash(super.hashCode(), options);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.resourceManager = options.getService();
}
static Project fromPb(
ResourceManager resourceManager,
com.google.api.services.cloudresourcemanager.model.Project answer) {
ProjectInfo info = ProjectInfo.fromPb(answer);
return new Project(resourceManager, new ProjectInfo.BuilderImpl(info));
}
}