use of com.google.api.services.cloudresourcemanager.model.Project in project google-cloud-java by GoogleCloudPlatform.
the class LocalResourceManagerHelper method list.
Response list(Map<String, Object> options) {
List<String> projectsSerialized = new ArrayList<>();
String[] filters = (String[]) options.get("filter");
if (filters != null && !isValidFilter(filters)) {
return Error.INVALID_ARGUMENT.response("Could not parse the filter.");
}
String[] projectFields = (String[]) options.get("projectFields");
int count = 0;
String pageToken = (String) options.get("pageToken");
Integer pageSize = (Integer) options.get("pageSize");
String nextPageToken = null;
Map<String, Project> projectsToScan = projects;
if (pageToken != null) {
projectsToScan = projects.tailMap(pageToken);
}
for (Project p : projectsToScan.values()) {
if (pageSize != null && count >= pageSize) {
nextPageToken = p.getProjectId();
break;
}
boolean includeProject = includeProject(p, filters);
if (includeProject) {
count++;
try {
projectsSerialized.add(jsonFactory.toString(extractFields(p, projectFields)));
} catch (IOException e) {
return Error.INTERNAL_ERROR.response("Error when serializing project " + p.getProjectId());
}
}
}
String[] listFields = (String[]) options.get("listFields");
StringBuilder responseBody = new StringBuilder();
responseBody.append('{');
// If fields parameter is set but no project field is selected we must return no projects.
if (!(projectFields != null && projectFields.length == 0)) {
responseBody.append("\"projects\": [");
Joiner.on(",").appendTo(responseBody, projectsSerialized);
responseBody.append(']');
}
if (nextPageToken != null && (listFields == null || ImmutableSet.copyOf(listFields).contains("nextPageToken"))) {
if (responseBody.length() > 1) {
responseBody.append(',');
}
responseBody.append("\"nextPageToken\": \"");
responseBody.append(nextPageToken);
responseBody.append('"');
}
responseBody.append('}');
return new Response(HTTP_OK, responseBody.toString());
}
use of com.google.api.services.cloudresourcemanager.model.Project in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectLoader method loadUserProjects.
private List<Project> loadUserProjects(CredentialedUser user) throws IOException {
CloudResourceManager cloudResourceManagerClient = GoogleApiClientFactory.getInstance().getCloudResourceManagerClient(user.getCredential());
final List<Project> result = new ArrayList<>();
ListProjectsResponse response = cloudResourceManagerClient.projects().list().setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
if (response != null && response.getProjects() != null) {
// Create a sorted set to sort the projects list by project name.
Set<Project> allProjects = new TreeSet<>(Comparator.comparing(project -> project.getName().toLowerCase()));
allProjects.addAll(response.getProjects());
while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
response = cloudResourceManagerClient.projects().list().setPageToken(response.getNextPageToken()).setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
allProjects.addAll(response.getProjects());
}
allProjects.stream().filter((project) -> !PROJECT_DELETE_REQUESTED.equals(project.getLifecycleState())).filter((project) -> !Strings.isNullOrEmpty(project.getProjectId())).forEach(result::add);
}
return result;
}
use of com.google.api.services.cloudresourcemanager.model.Project in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectSelectionDialog method loadProjectList.
/**
* Check if cached, if not then check if already loading, and finally start loading if not.
*/
private void loadProjectList(CredentialedUser user) {
if (!cachedProjectList.containsKey(user) && !runningProjectLoaderJobs.containsKey(user)) {
// not cached and not loading - start loading project list here and keep the future reference.
ListenableFuture<List<Project>> futureResult = projectLoader.loadUserProjectsInBackground(user);
addProjectListFutureCallback(futureResult, new FutureCallback<List<Project>>() {
@Override
public void onSuccess(List<Project> projects) {
SwingUtilities.invokeLater(() -> {
// record job result and clear future reference.
runningProjectLoaderJobs.remove(user);
cachedProjectList.put(user, projects);
refreshProjectListUi(user);
});
}
@Override
public void onFailure(Throwable throwable) {
SwingUtilities.invokeLater(() -> {
runningProjectLoaderJobs.remove(user);
// validation message should not be null, use exception name if needed.
String errorMessage = Optional.ofNullable(throwable.getMessage()).orElse(throwable.getClass().getName());
dialogWrapper.setErrorInfoAll(Collections.singletonList(new ValidationInfo(errorMessage)));
refreshProjectListUi(user);
});
}
});
runningProjectLoaderJobs.put(user, futureResult);
}
}
use of com.google.api.services.cloudresourcemanager.model.Project in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectListTableModelTest method setUp.
@Before
public void setUp() {
model = new ProjectListTableModel();
testProject1 = new Project();
testProject1.setName("test-project");
testProject1.setProjectId("test-project-ID");
testProject2 = new Project();
testProject2.setName("test-project-2");
testProject2.setProjectId("test-project-2-ID");
}
use of com.google.api.services.cloudresourcemanager.model.Project in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectSelectionDialogTest method getCloudProject_returns_selectedProject.
@Test
public void getCloudProject_returns_selectedProject() {
prepareOneTestUserOneTestProjectDialog(testUiProject);
Project secondProject = new Project();
secondProject.setName("project-2");
secondProject.setProjectId("project-2-id");
mockUserProjects(mockTestUser, Arrays.asList(testGoogleProject, secondProject));
cleanLoadUsersAndProjects();
projectSelectionDialog.showProjectInList(secondProject.getName());
CloudProject selectedProject = projectSelectionDialog.getSelectedProject();
CloudProject expected = CloudProject.create(secondProject.getName(), secondProject.getProjectId(), mockTestUser.getEmail());
assertThat(selectedProject).isEqualTo(expected);
}
Aggregations