TenantPushSecurityGroupsStepCmd extends StepCommand { @Override protected void execute() throws ExternalException { List<TenantEntity> tenantList = step.getTransientResourceEntities(null); checkArgument(tenantList.size() > 0); for (TenantEntity tenantEntity : tenantList) { logger.info("Propagating the security groups of tenant {}", tenantEntity.getId()); tenantEntity = tenantBackend.findById(tenantEntity.getId()); List<Project> projects = new ArrayList<>(); ResourceList<Project> resourceList = projectBackend.filter(tenantEntity.getId(), Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); projects.addAll(resourceList.getItems()); while (StringUtils.isNotBlank(resourceList.getNextPageLink())) { resourceList = projectBackend.getProjectsPage(resourceList.getNextPageLink()); projects.addAll(resourceList.getItems()); } List<String> tenantSecurityGroups = tenantEntity.getSecurityGroups().stream() .map(g -> g.getName()).collect(Collectors.toList()); for (Project project : projects) { logger.info("Updating the security groups of project {} using the ones from tenant {}", project.getId(), tenantEntity.getId()); List<SecurityGroup> currSecurityGroups = project.getSecurityGroups(); Pair<List<SecurityGroup>, List<String>> result = SecurityGroupUtils.mergeParentSecurityGroups(currSecurityGroups, tenantSecurityGroups); projectBackend.replaceSecurityGroups(project.getId(), result.getLeft()); if (result.getRight() != null && !result.getRight().isEmpty()) { step.addWarning(new SecurityGroupsAlreadyInheritedException(result.getRight())); } } } } TenantPushSecurityGroupsStepCmd(TaskCommand taskCommand, StepBackend stepBackend,
                                         StepEntity stepEntity, TenantBackend tenantBackend,
                                         ProjectBackend projectBackend);   }
@Test public void testExecuteSuccess() throws Exception { List<SecurityGroupEntity> tenantSecurityGroups = new ArrayList<>(); tenantSecurityGroups.add(new SecurityGroupEntity("sg1", true)); tenantSecurityGroups.add(new SecurityGroupEntity("sg2", false)); TenantEntity tenantEntity = new TenantEntity(); tenantEntity.setId("tenant-id"); tenantEntity.setSecurityGroups(tenantSecurityGroups); List<TenantEntity> tenantEntityList = new ArrayList<>(); tenantEntityList.add(tenantEntity); List<SecurityGroup> projectSecurityGroups = new ArrayList<>(); projectSecurityGroups.add(new SecurityGroup("sg2", false)); projectSecurityGroups.add(new SecurityGroup("sg3", false)); Project project = new Project(); project.setId("project-id"); project.setSecurityGroups(projectSecurityGroups); List<Project> projects = new ArrayList<>(); projects.add(project); doReturn(tenantEntityList).when(stepEntity).getTransientResourceEntities(null); doReturn(tenantEntity).when(tenantBackend).findById("tenant-id"); String fakePageLink = UUID.randomUUID().toString(); doReturn(new ResourceList<Project>(new ArrayList<>(), fakePageLink, null)) .when(projectBackend) .filter("tenant-id", Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); doReturn(new ResourceList<>(projects)).when(projectBackend).getProjectsPage(fakePageLink); TenantPushSecurityGroupsStepCmd cmd = new TenantPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend, projectBackend); cmd.execute(); verify(projectBackend, times(1)).replaceSecurityGroups(eq("project-id"), anyListOf(SecurityGroup.class)); verify(stepEntity, times(1)).addWarning(anyObject()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testCheckArgumentFailed() throws Exception { doReturn(new ArrayList<TenantEntity>()).when(stepEntity).getTransientResourceEntities(null); TenantPushSecurityGroupsStepCmd cmd = new TenantPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend, projectBackend); cmd.execute(); }
@Test(expectedExceptions = ExternalException.class, expectedExceptionsMessageRegExp = "^Failed to list projects$") public void testFailedToGetProjects() throws Exception { TenantEntity tenantEntity = new TenantEntity(); tenantEntity.setId("id"); List<TenantEntity> tenantEntityList = new ArrayList<>(); tenantEntityList.add(tenantEntity); doReturn(tenantEntityList).when(stepEntity).getTransientResourceEntities(null); doReturn(tenantEntity).when(tenantBackend).findById("id"); doThrow(new ExternalException("Failed to list projects")) .when(projectBackend) .filter("id", Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); TenantPushSecurityGroupsStepCmd cmd = new TenantPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend, projectBackend); cmd.execute(); }
@Test public void testEmptyProjectsList() throws Exception { TenantEntity tenantEntity = new TenantEntity(); tenantEntity.setId("id"); List<TenantEntity> tenantEntityList = new ArrayList<>(); tenantEntityList.add(tenantEntity); doReturn(tenantEntityList).when(stepEntity).getTransientResourceEntities(null); doReturn(tenantEntity).when(tenantBackend).findById("id"); doReturn(new ResourceList<>(new ArrayList<>())).when(projectBackend).filter("id", Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); TenantPushSecurityGroupsStepCmd cmd = new TenantPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend, projectBackend); cmd.execute(); verify(projectBackend, times(0)).replaceSecurityGroups(anyString(), anyListOf(SecurityGroup.class)); }
@Test(expectedExceptions = ExternalException.class, expectedExceptionsMessageRegExp = "Failed to update project") public void testFailedToUpdate() throws Exception { List<SecurityGroupEntity> tenantSecurityGroups = new ArrayList<>(); tenantSecurityGroups.add(new SecurityGroupEntity("sg1", true)); tenantSecurityGroups.add(new SecurityGroupEntity("sg2", false)); TenantEntity tenantEntity = new TenantEntity(); tenantEntity.setId("tenant-id"); tenantEntity.setSecurityGroups(tenantSecurityGroups); List<TenantEntity> tenantEntityList = new ArrayList<>(); tenantEntityList.add(tenantEntity); List<SecurityGroup> projectSecurityGroups = new ArrayList<>(); projectSecurityGroups.add(new SecurityGroup("sg2", false)); projectSecurityGroups.add(new SecurityGroup("sg3", false)); Project project = new Project(); project.setId("project-id"); project.setSecurityGroups(projectSecurityGroups); List<Project> projects = new ArrayList<>(); projects.add(project); doReturn(tenantEntityList).when(stepEntity).getTransientResourceEntities(null); doReturn(tenantEntity).when(tenantBackend).findById("tenant-id"); String fakePageLink = UUID.randomUUID().toString(); doReturn(new ResourceList<Project>(new ArrayList<>(), fakePageLink, null)) .when(projectBackend) .filter("tenant-id", Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); doReturn(new ResourceList<>(projects)).when(projectBackend).getProjectsPage(fakePageLink); doThrow(new ExternalException("Failed to update project")) .when(projectBackend) .replaceSecurityGroups(eq("project-id"), anyListOf(SecurityGroup.class)); TenantPushSecurityGroupsStepCmd cmd = new TenantPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend, projectBackend); cmd.execute(); }