Search in sources :

Example 1 with Project

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());
}
Also used : TestIamPermissionsResponse(com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse) Project(com.google.api.services.cloudresourcemanager.model.Project) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 2 with Project

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;
}
Also used : Project(com.google.api.services.cloudresourcemanager.model.Project) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Set(java.util.Set) ThreadUtil(com.google.cloud.tools.intellij.util.ThreadUtil) IOException(java.io.IOException) GoogleApiClientFactory(com.google.cloud.tools.intellij.resources.GoogleApiClientFactory) FutureCallback(com.google.common.util.concurrent.FutureCallback) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) List(java.util.List) Strings(com.google.api.client.repackaged.com.google.common.base.Strings) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) Comparator(java.util.Comparator) ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse) Project(com.google.api.services.cloudresourcemanager.model.Project) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse)

Example 3 with Project

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);
    }
}
Also used : Project(com.google.api.services.cloudresourcemanager.model.Project) ValidationInfo(com.intellij.openapi.ui.ValidationInfo) List(java.util.List) JList(javax.swing.JList)

Example 4 with Project

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");
}
Also used : Project(com.google.api.services.cloudresourcemanager.model.Project) Before(org.junit.Before)

Example 5 with Project

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);
}
Also used : Project(com.google.api.services.cloudresourcemanager.model.Project) Test(org.junit.Test)

Aggregations

Project (com.google.api.services.cloudresourcemanager.model.Project)12 IOException (java.io.IOException)4 List (java.util.List)4 TestIamPermissionsResponse (com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 Before (org.junit.Before)3 Test (org.junit.Test)3 CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)2 ListProjectsResponse (com.google.api.services.cloudresourcemanager.model.ListProjectsResponse)2 CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)2 GoogleApiClientFactory (com.google.cloud.tools.intellij.resources.GoogleApiClientFactory)2 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 ArrayList (java.util.ArrayList)2 Answer (org.mockito.stubbing.Answer)2 Strings (com.google.api.client.repackaged.com.google.common.base.Strings)1 CloudToolsRule (com.google.cloud.tools.intellij.testing.CloudToolsRule)1 TestService (com.google.cloud.tools.intellij.testing.TestService)1 ThreadUtil (com.google.cloud.tools.intellij.util.ThreadUtil)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Futures (com.google.common.util.concurrent.Futures)1