com.google.api.services.cloudresourcemanager.CloudResourceManager.Projects Java Examples

The following examples show how to use com.google.api.services.cloudresourcemanager.CloudResourceManager.Projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GCPProject.java    From policyscanner with Apache License 2.0 6 votes vote down vote up
/**
 * Return the Projects api object used for accessing the Cloud Resource Manager Projects API.
 * @return Projects api object used for accessing the Cloud Resource Manager Projects API
 * @throws GeneralSecurityException Thrown if there's a permissions error.
 * @throws IOException Thrown if there's an IO error initializing the API object.
 */
public static synchronized Projects getProjectsApiStub()
    throws GeneralSecurityException, IOException {
  if (projectApiStub != null) {
    return projectApiStub;
  }
  HttpTransport transport;
  GoogleCredential credential;
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  transport = GoogleNetHttpTransport.newTrustedTransport();
  credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
  if (credential.createScopedRequired()) {
    Collection<String> scopes = CloudResourceManagerScopes.all();
    credential = credential.createScoped(scopes);
  }
  projectApiStub = new CloudResourceManager
      .Builder(transport, jsonFactory, credential)
      .build()
      .projects();
  return projectApiStub;
}
 
Example #2
Source File: RunOptionsDefaultsComponentTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
private void mockProjectList(Credential credential, GcpProject... gcpProjects)
    throws IOException {
  Projects projectsApi = mock(Projects.class);
  Projects.List listApi = mock(Projects.List.class);
  List<Project> projectsList = new ArrayList<>();
  for (GcpProject gcpProject : gcpProjects) {
    Project project = new Project(); // cannot mock final classes
    project.setName(gcpProject.getName());
    project.setProjectId(gcpProject.getId());
    projectsList.add(project);
  }
  ListProjectsResponse response = new ListProjectsResponse(); // cannot mock final classes
  response.setProjects(projectsList);
  doReturn(projectsApi).when(apiFactory).newProjectsApi(credential);
  doReturn(listApi).when(listApi).setPageSize(anyInt());
  doReturn(listApi).when(projectsApi).list();
  doReturn(response).when(listApi).execute();
}
 
Example #3
Source File: MiniSelectorTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
private void mockProjectsList(Credential credential, GcpProject... gcpProjects) {
  Projects projectsApi = mock(Projects.class);
  Projects.List listApi = mock(Projects.List.class);
  List<Project> projectsList = new ArrayList<>();
  for (GcpProject gcpProject : gcpProjects) {
    Project project = new Project();
    project.setName(gcpProject.getName());
    project.setProjectId(gcpProject.getId());
    projectsList.add(project);
  }
  ListProjectsResponse response = new ListProjectsResponse();
  response.setProjects(projectsList);
  try {
    doReturn(projectsApi).when(apiFactory).newProjectsApi(credential);
    doReturn(listApi).when(listApi).setPageSize(any(Integer.class));
    doReturn(listApi).when(projectsApi).list();
    doReturn(response).when(listApi).execute();
  } catch (IOException ex) {
    fail(ex.toString());
  }
}
 
Example #4
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetProjects_pagination() throws IOException, ProjectRepositoryException {
  Projects.List list = initializeListRequest();
  ListProjectsResponse response1 = new ListProjectsResponse();
  response1.setProjects(Collections.singletonList(project));
  response1.setNextPageToken("a token");
  
  ListProjectsResponse response2 = new ListProjectsResponse();
  Project project2 = new Project();
  project2.setName("project 2").setProjectId("project_2");
  response2.setProjects(Collections.singletonList(project2));
  
  when(list.execute()).thenReturn(response1, response2);

  List<GcpProject> gcpProjects = repository.getProjects(mock(Credential.class));

  assertThat(gcpProjects.size(), is(2));
  GcpProject gcpProject = gcpProjects.get(0);
  assertThat(gcpProject.getName(), is("projectName"));
  assertThat(gcpProject.getId(), is("projectId"));
  GcpProject gcpProject2 = gcpProjects.get(1);
  assertThat(gcpProject2.getName(), is("project 2"));
  assertThat(gcpProject2.getId(), is("project_2"));
}
 
Example #5
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test(expected = ProjectRepositoryException.class)
public void testGetProject_exceptionInRequest() throws IOException, ProjectRepositoryException {
  Projects projects = mock(Projects.class);
  when(apiFactory.newProjectsApi(any(Credential.class))).thenReturn(projects);
  Get get = mock(Get.class);
  when(projects.get(anyString())).thenReturn(get);
  when(get.execute()).thenThrow(new IOException("test exception"));

  repository.getProject(mock(Credential.class), "projectId");
}
 
Example #6
Source File: GoogleApiFactory.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Override
public Projects newProjectsApi(Credential credential) {
  Preconditions.checkNotNull(transportCache, "transportCache is null");
  HttpTransport transport = transportCache.getUnchecked(GoogleApi.CLOUDRESOURCE_MANAGER_API);
  Preconditions.checkNotNull(transport, "transport is null");
  Preconditions.checkNotNull(jsonFactory, "jsonFactory is null");

  CloudResourceManager resourceManager =
      new CloudResourceManager.Builder(transport, jsonFactory, credential)
          .setApplicationName(CloudToolsInfo.USER_AGENT).build();
  return resourceManager.projects();
}
 
Example #7
Source File: ProjectRepository.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * @return all active projects the account identified by {@code credential} has access to
 * @throws ProjectRepositoryException if an error happens while communicating with the backend
 */
public List<GcpProject> getProjects(Credential credential) throws ProjectRepositoryException {
  Preconditions.checkNotNull(credential);
  // TODO cache results https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/1374
  try {
    Projects projects = apiFactory.newProjectsApi(credential);
    
    String token = null;
    List<Project> projectList = new ArrayList<>();
    do {
      Projects.List listRequest = projects.list().setPageSize(PROJECT_LIST_PAGESIZE);
      if (token != null) {
        listRequest = listRequest.setPageToken(token); 
      }
      ListProjectsResponse response = listRequest.execute();
      List<Project> responseProjects = response.getProjects();
      if (responseProjects != null) {
        projectList.addAll(responseProjects);
      }
      token = response.getNextPageToken();
    } while (token != null);
    List<GcpProject> gcpProjects = convertToGcpProjects(projectList);
    return gcpProjects;
  } catch (IOException ex) {
    throw new ProjectRepositoryException(ex);
  }
}
 
Example #8
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private Projects.List initializeListRequest() throws IOException {
  Projects projects = mock(Projects.class);
  when(apiFactory.newProjectsApi(any(Credential.class))).thenReturn(projects);
  Projects.List list = mock(Projects.List.class);
  when(projects.list()).thenReturn(list);
  when(list.setPageSize(anyInt())).thenReturn(list);
  when(list.setPageToken(anyString())).thenReturn(list);
  return list;
}
 
Example #9
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProject_successful() throws IOException, ProjectRepositoryException {
  Projects projects = mock(Projects.class);
  when(apiFactory.newProjectsApi(any(Credential.class))).thenReturn(projects);
  Get get = mock(Get.class);
  when(projects.get(anyString())).thenReturn(get);
  when(get.execute()).thenReturn(project);

  GcpProject gcpProject = repository.getProject(mock(Credential.class), "projectId");

  assertNotNull(gcpProject);
  assertThat(gcpProject.getName(), is("projectName"));
  assertThat(gcpProject.getId(), is("projectId"));
}
 
Example #10
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProjects_onlyDeletedProjectsReturned()
    throws IOException, ProjectRepositoryException {
  Projects.List list = initializeListRequest();
  ListProjectsResponse response = new ListProjectsResponse();
  response.setProjects(Collections.singletonList(project));
  project.setLifecycleState("DELETE_REQUESTED");
  when(list.execute()).thenReturn(response);

  List<GcpProject> gcpProjects = repository.getProjects(mock(Credential.class));

  assertNotNull(gcpProjects);
  assertTrue(gcpProjects.isEmpty());
}
 
Example #11
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProjects_null() throws IOException, ProjectRepositoryException {
  Projects.List list = initializeListRequest();
  ListProjectsResponse response = new ListProjectsResponse();
  response.setProjects(null);
  when(list.execute()).thenReturn(response);

  List<GcpProject> gcpProjects = repository.getProjects(mock(Credential.class));

  assertNotNull(gcpProjects);
}
 
Example #12
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProjects_successful() throws IOException, ProjectRepositoryException {
  Projects.List list = initializeListRequest();
  ListProjectsResponse response = new ListProjectsResponse();
  response.setProjects(Collections.singletonList(project));
  when(list.execute()).thenReturn(response);

  List<GcpProject> gcpProjects = repository.getProjects(mock(Credential.class));

  assertNotNull(gcpProjects);
  assertThat(gcpProjects.size(), is(1));
  GcpProject gcpProject = gcpProjects.get(0);
  assertThat(gcpProject.getName(), is("projectName"));
  assertThat(gcpProject.getId(), is("projectId"));
}
 
Example #13
Source File: ProjectRepositoryTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test(expected = ProjectRepositoryException.class)
public void testGetProjects_exceptionInRequest() throws IOException, ProjectRepositoryException {
  Projects.List list = initializeListRequest();
  when(list.execute()).thenThrow(new IOException("test exception"));

  repository.getProjects(mock(Credential.class));
}
 
Example #14
Source File: ListServiceAccountsTest.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  GCPProject.setProjectsApiStub(this.projectsApiObject);
  this.projectsApiObject = mock(Projects.class);
  this.getIamPolicy = mock(GetIamPolicy.class);
  this.tester = DoFnTester.of(new ExtractState());
  when(this.projectsApiObject.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(this.getIamPolicy);
}
 
Example #15
Source File: ExtractStateTest.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  this.projectsApiObject = mock(Projects.class);
  GCPProject.setProjectsApiStub(this.projectsApiObject);

  this.getIamPolicy = mock(Projects.GetIamPolicy.class);
  this.tester = DoFnTester.of(new ExtractState());
  when(this.projectsApiObject.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(this.getIamPolicy);
}
 
Example #16
Source File: LiveProjectSource.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
private boolean refreshProjects(String nextPageToken) throws IOException {
  ListProjectsResponse projectListResponse;
  Projects.List projectsList;
  try {
    projectsList = GCPProject.getProjectsApiStub().list();
    if (nextPageToken != null) {
      projectsList = projectsList.setPageToken(nextPageToken);
    }
    if (source.getOrgId() != null) {
        projectsList = projectsList
            .setFilter("parent.type:organization parent.id:" + source.getOrgId());
    }
    projectListResponse = projectsList.execute();
  } catch (GeneralSecurityException gse) {
    throw new IOException("Cannot get projects. Access denied");
  }
  List<Project> projects = projectListResponse.getProjects();

  for (Project project : projects) {
    String orgId = null;
    if (project.getParent() != null) {
      orgId = project.getParent().getId();
    }
    if (project.getLifecycleState() == null
        || project.getLifecycleState().startsWith(DELETE_PREFIX)) {
      continue;
    }
    this.projects.add(new GCPProject(project.getProjectId(), orgId, project.getName()));
  }
  this.nextPageToken = projectListResponse.getNextPageToken();

  return !this.projects.isEmpty();
}
 
Example #17
Source File: GoogleApiFactoryWithProxyServerTest.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Test
public void testNewProjectsApi_userAgentIsSet() throws IOException {
  Projects api = googleApiFactory.newProjectsApi(mock(Credential.class));
  assertThat(api.get("").getRequestHeaders().getUserAgent(),
             containsString(CloudToolsInfo.USER_AGENT));
}
 
Example #18
Source File: GoogleApiFactoryTest.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Test
public void testNewProjectsApi() {
  Projects projects = googleApiFactory.newProjectsApi(mock(Credential.class));
  assertNotNull(projects);
}
 
Example #19
Source File: GoogleApiFactoryTest.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Test
public void testNewProjectsApi_userAgentIsSet() throws IOException {
  Projects api = googleApiFactory.newProjectsApi(mock(Credential.class));
  assertThat(api.get("").getRequestHeaders().getUserAgent(),
             containsString(CloudToolsInfo.USER_AGENT));
}
 
Example #20
Source File: GCPProject.java    From policyscanner with Apache License 2.0 2 votes vote down vote up
/**
 * Setter for the CRM Projects API stub.
 * @param project The CRM Projects API stub.
 */
public static synchronized void setProjectsApiStub(Projects project) {
  GCPProject.projectApiStub = project;
}
 
Example #21
Source File: IGoogleApiFactory.java    From google-cloud-eclipse with Apache License 2.0 2 votes vote down vote up
/**
 * @return a CloudResourceManager/Projects API client
 */
Projects newProjectsApi(Credential credential);