DeploymentPushSecurityGroupsStepCmd extends StepCommand { @Override protected void execute() throws ExternalException { List<DeploymentEntity> deploymentEntities = step.getTransientResourceEntities(null); checkArgument(deploymentEntities.size() == 1); DeploymentEntity deploymentEntity = deploymentEntities.get(0); logger.info("Propagating the security groups of deployment {}", deploymentEntity.getId()); ResourceList<Tenant> tenants = tenantBackend.filter(Optional.absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); ResourceList<Tenant> currentPage; List<String> deploymentSecurityGroups = deploymentEntity.getOauthSecurityGroups(); do { for (Tenant tenant : tenants.getItems()) { logger.info("Updating the security groups of tenant {} using the ones from deployment {}", tenant.getId(), deploymentEntity.getId()); List<SecurityGroup> currSecurityGroups = tenant.getSecurityGroups(); Pair<List<SecurityGroup>, List<String>> result = SecurityGroupUtils.mergeParentSecurityGroups(currSecurityGroups, deploymentSecurityGroups); tenantBackend.setSecurityGroups(tenant.getId(), result.getLeft()); if (result.getRight() != null && !result.getRight().isEmpty()) { step.addWarning(new SecurityGroupsAlreadyInheritedException(result.getRight())); } } currentPage = tenants; if (tenants.getNextPageLink() != null && !tenants.getNextPageLink().isEmpty()) { tenants = tenantBackend.getPage(tenants.getNextPageLink()); } } while (currentPage.getNextPageLink() != null && !currentPage.getNextPageLink().isEmpty()); } DeploymentPushSecurityGroupsStepCmd(TaskCommand taskCommand, StepBackend stepBackend,
                                             StepEntity stepEntity, TenantBackend tenantBackend);   }
@Test public void testExecuteSuccess() throws Exception { DeploymentEntity deploymentEntity = new DeploymentEntity(); deploymentEntity.setOauthSecurityGroups(ImmutableList.of("adminGroup1", "adminGroup2")); List<DeploymentEntity> deploymentEntities = ImmutableList.of(deploymentEntity); Tenant tenant = new Tenant(); tenant.setId("id"); tenant.setSecurityGroups(ImmutableList.of(new SecurityGroup("adminGroup3", false), new SecurityGroup("adminGroup4", false))); ResourceList<Tenant> tenants = new ResourceList<>(ImmutableList.of(tenant)); doReturn(deploymentEntities).when(stepEntity).getTransientResourceEntities(null); doReturn(tenants).when(tenantBackend).filter(Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); DeploymentPushSecurityGroupsStepCmd cmd = new DeploymentPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend); cmd.execute(); verify(tenantBackend, times(1)).setSecurityGroups(eq("id"), anyObject()); verify(stepEntity, times(0)).addWarning(isA(SecurityGroupsAlreadyInheritedException.class)); }
@Test public void testExecuteSuccessWithPaginatedTenants() throws Exception { DeploymentEntity deploymentEntity = new DeploymentEntity(); deploymentEntity.setOauthSecurityGroups(ImmutableList.of("adminGroup1", "adminGroup2")); List<DeploymentEntity> deploymentEntities = ImmutableList.of(deploymentEntity); int totalTenants = PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE + 5; List<Tenant> tenantList = new ArrayList<>(); for (int i = 0; i < totalTenants; i++) { Tenant tenant = new Tenant(); tenant.setId("id" + i); tenant.setSecurityGroups(ImmutableList.of(new SecurityGroup("adminGroup3", false), new SecurityGroup("adminGroup4", false))); tenantList.add(tenant); } ResourceList<Tenant> tenantsPage1 = new ResourceList<>(tenantList.subList(0, PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); tenantsPage1.setNextPageLink("nextLink"); ResourceList<Tenant> tenantsPage2 = new ResourceList<>(tenantList.subList( PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE, totalTenants)); doReturn(deploymentEntities).when(stepEntity).getTransientResourceEntities(null); doReturn(tenantsPage1).when(tenantBackend).filter(Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); doReturn(tenantsPage2).when(tenantBackend).getPage("nextLink"); DeploymentPushSecurityGroupsStepCmd cmd = new DeploymentPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend); cmd.execute(); verify(tenantBackend, times(totalTenants)).setSecurityGroups(anyString(), anyObject()); verify(stepEntity, times(0)).addWarning(isA(SecurityGroupsAlreadyInheritedException.class)); }
@Test public void testExecuteSuccessWithWarning() throws Exception { DeploymentEntity deploymentEntity = new DeploymentEntity(); deploymentEntity.setOauthSecurityGroups(ImmutableList.of("adminGroup1", "adminGroup2")); List<DeploymentEntity> deploymentEntities = ImmutableList.of(deploymentEntity); Tenant tenant = new Tenant(); tenant.setId("id"); tenant.setSecurityGroups(ImmutableList.of(new SecurityGroup("adminGroup2", false), new SecurityGroup("adminGroup3", false))); ResourceList<Tenant> tenants = new ResourceList<>(ImmutableList.of(tenant)); doReturn(deploymentEntities).when(stepEntity).getTransientResourceEntities(null); doReturn(tenants).when(tenantBackend).filter(Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); DeploymentPushSecurityGroupsStepCmd cmd = new DeploymentPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend); cmd.execute(); verify(tenantBackend, times(1)).setSecurityGroups(eq("id"), anyObject()); verify(stepEntity, times(1)).addWarning(isA(SecurityGroupsAlreadyInheritedException.class)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testCheckArgumentFailed() throws Exception { doReturn(new ArrayList<DeploymentEntity>()).when(stepEntity).getTransientResourceEntities(null); DeploymentPushSecurityGroupsStepCmd cmd = new DeploymentPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend); cmd.execute(); }
@Test public void testEmptyTenantList() throws Exception { List<DeploymentEntity> deploymentEntities = ImmutableList.of(new DeploymentEntity()); doReturn(deploymentEntities).when(stepEntity).getTransientResourceEntities(null); doReturn(new ResourceList<Tenant>(new ArrayList<>())).when(tenantBackend).filter(Optional.<String>absent(), Optional.of(PaginationConfig.DEFAULT_DEFAULT_PAGE_SIZE)); DeploymentPushSecurityGroupsStepCmd cmd = new DeploymentPushSecurityGroupsStepCmd(taskCommand, stepBackend, stepEntity, tenantBackend); cmd.execute(); verify(tenantBackend, times(0)).setSecurityGroups(anyString(), anyObject()); }