Skip to content

Commit

Permalink
Add replace method that takes no parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Jan 28, 2016
1 parent c7795a5 commit ce8ae4b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ import java.util.Iterator;
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
Project myProject = resourceManager.get("some-project-id"); // Use an existing project's ID
Project newProject = resourceManager.replace(myProject.toBuilder()
.addLabel("launch-status", "in-development").build());
Project newProject = myProject.toBuilder()
.addLabel("launch-status", "in-development")
.build()
.replace();
System.out.println("Updated the labels of project " + newProject.projectId()
+ " to be " + newProject.labels());
// List all the projects you have permission to view.
Expand Down
12 changes: 9 additions & 3 deletions gcloud-java-resourcemanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ To edit a project, create a new `ProjectInfo` object and pass it in to the `Proj
For example, to add a label for the newly created project to denote that it's launch status is "in development", add the following code:

```java
Project newProject = myProject.replace(myProject.toBuilder()
.addLabel("launch-status", "in-development").build());
Project newProject = myProject.toBuilder()
.addLabel("launch-status", "in-development")
.build()
.replace();
```

Note that the values of the project you pass in to `replace` overwrite the server's values for non-read-only fields, namely `projectName` and `labels`. For example, if you create a project with `projectName` "some-project-name" and subsequently call replace using a `ProjectInfo` object that didn't set the `projectName`, then the server will unset the project's name. The server ignores any attempted changes to the read-only fields `projectNumber`, `lifecycleState`, and `createTime`. The `projectId` cannot change.
Expand Down Expand Up @@ -160,7 +162,11 @@ public class GcloudJavaResourceManagerExample {
System.out.println("Got project " + projectFromServer.projectId() + " from the server.");

// Update a project
Project newProject = resourceManager.replace(myProject.toBuilder()
Project newProject = myProject.toBuilder()
.addLabel("launch-status", "in-development")
.build()
.replace();
Project newProject = myProject.replace(myProject.toBuilder()
.addLabel("launch-status", "in-development").build());
System.out.println("Updated the labels of project " + newProject.projectId()
+ " to be " + newProject.labels());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,36 @@ public void undelete() {
}

/**
* Replaces the attributes of the project.
* Replaces the attributes of the project with the attributes from the given {@code ProjectInfo}.
*
* <p>The caller must have modify permissions for this project.
*
* @see <a
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud
* Resource Manager update</a>
* @return the ProjectInfo representing the new project metadata
* @return the Project representing the new project metadata
* @throws ResourceManagerException upon failure
*/
public Project replace(ProjectInfo projectInfo) {
return resourceManager.replace(checkNotNull(projectInfo));
}

public static Builder builder(ResourceManager resourceManager, String projectId) {
/**
* Replaces the attributes of the project with the attributes of this project.
*
* <p>The caller must have modify permissions for this project.
*
* @see <a
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud
* Resource Manager update</a>
* @return the Project representing the new project metadata
* @throws ResourceManagerException upon failure
*/
public Project replace() {
return resourceManager.replace(this);
}

static Builder builder(ResourceManager resourceManager, String projectId) {
return new Builder(resourceManager).projectId(projectId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
* ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
* String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
* Project myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
* Project newProject = resourceManager.replace(myProject.toBuilder()
* .addLabel("launch-status", "in-development").build());
* Project newProject = myProject.toBuilder()
* .addLabel("launch-status", "in-development")
* .build()
* .replace();
* Iterator<Project> projectIterator = resourceManager.list().iterateAll();
* System.out.println("Projects I can view:");
* while (projectIterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.gcloud.resourcemanager;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
Expand Down Expand Up @@ -167,7 +168,7 @@ public void testUndelete() {
}

@Test
public void testReplace() {
public void testReplaceWithProjectInfo() {
initializeExpectedProject(2);
ProjectInfo newInfo = PROJECT_INFO.toBuilder().addLabel("k3", "v3").build();
Project expectedProject =
Expand All @@ -180,6 +181,20 @@ public void testReplace() {
assertEquals(expectedProject, newProject);
}

@Test
public void testReplaceWithoutProjectInfo() {
initializeExpectedProject(2);
ProjectInfo newInfo = PROJECT_INFO.toBuilder().addLabel("k3", "v3").build();
Project expectedProject =
new Project(serviceMockReturnsOptions, new ProjectInfo.BuilderImpl(newInfo));
expect(resourceManager.options()).andReturn(mockOptions);
expect(resourceManager.replace(anyObject(Project.class))).andReturn(expectedProject);
replay(resourceManager);
initializeProject();
Project newProject = project.replace(newInfo);
assertEquals(expectedProject, newProject);
}

private void compareProjects(Project expected, Project value) {
assertEquals(expected, value);
assertEquals(expected.projectId(), value.projectId());
Expand Down

0 comments on commit ce8ae4b

Please sign in to comment.