use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class AWSLoadBalancerService method createSecurityGroupState.
private DeferredResult<AWSLoadBalancerContext> createSecurityGroupState(AWSLoadBalancerContext context) {
SecurityGroupState state = new SecurityGroupState();
state.authCredentialsLink = context.credentials.documentSelfLink;
state.endpointLink = context.loadBalancerStateExpanded.endpointLink;
if (state.endpointLinks == null) {
state.endpointLinks = new HashSet<>();
}
state.endpointLinks.add(context.loadBalancerStateExpanded.endpointLink);
state.instanceAdapterReference = UriUtils.buildUri(getHost(), AWSSecurityGroupService.SELF_LINK);
state.resourcePoolLink = context.loadBalancerStateExpanded.endpointState.resourcePoolLink;
state.customProperties = new HashMap<>(2);
state.customProperties.put(ComputeProperties.INFRASTRUCTURE_USE_PROP_NAME, Boolean.TRUE.toString());
state.customProperties.put(AWSConstants.AWS_LOAD_BALANCER_SECURITY_GROUP, Boolean.TRUE.toString());
state.tenantLinks = context.loadBalancerStateExpanded.tenantLinks;
state.regionId = context.loadBalancerStateExpanded.regionId;
state.name = context.loadBalancerStateExpanded.name + "_SG";
state.ingress = context.loadBalancerStateExpanded.routes.stream().map(routeConfiguration -> buildRule(routeConfiguration.port)).collect(Collectors.toList());
state.egress = context.loadBalancerStateExpanded.routes.stream().map(routeConfiguration -> buildRule(routeConfiguration.instancePort)).collect(Collectors.toList());
state.computeHostLink = context.loadBalancerStateExpanded.computeHostLink;
Operation operation = Operation.createPost(this, FACTORY_LINK).setBody(state);
return this.sendWithDeferredResult(operation, SecurityGroupState.class).thenApply(securityGroupState -> {
context.provisionedSecurityGroupState = securityGroupState;
return context;
});
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class TestProvisionAWSSecurityGroup method testDeleteAWSSecurityGroupWithDependency.
@Test
public void testDeleteAWSSecurityGroupWithDependency() throws Throwable {
// create credentials
Operation authResponse = new Operation();
TestUtils.postCredentials(this.host, authResponse, this.privateKey, this.privateKeyId);
AuthCredentialsServiceState creds = authResponse.getBody(AuthCredentialsServiceState.class);
// create resource pool
Operation poolResponse = new Operation();
TestUtils.postResourcePool(this.host, poolResponse);
ResourcePoolState pool = poolResponse.getBody(ResourcePoolState.class);
// create sg service
Operation securityGroupResponse = new Operation();
SecurityGroupState initialSecurityGroupState = buildSecurityGroupState(creds, pool);
TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, securityGroupResponse);
SecurityGroupState securityGroupState = securityGroupResponse.getBody(SecurityGroupState.class);
// set up security group task state
ProvisionSecurityGroupTaskState task = new ProvisionSecurityGroupTaskState();
task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.CREATE;
task.securityGroupDescriptionLinks = Stream.of(securityGroupState.documentSelfLink).collect(Collectors.toSet());
task.customProperties = new HashMap<>();
task.customProperties.put(NETWORK_STATE_ID_PROP_NAME, this.vpcId);
Operation provision = new Operation();
provisionSecurityGroup(task, provision);
ProvisionSecurityGroupTaskState ps = provision.getBody(ProvisionSecurityGroupTaskState.class);
waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
securityGroupState = getServiceSynchronously(securityGroupState.documentSelfLink, SecurityGroupState.class);
// provision machine on the newly created SG
String vm = provisionAWSVMWithEC2Client(this.host, this.ec2client, EC2_LINUX_AMI, this.subnetId, securityGroupState.id);
// reuse previous task, but switch to a delete
task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.DELETE;
Operation remove = new Operation();
provisionSecurityGroup(task, remove);
// delete the newly provisioned machine after a small delay
Runnable deleteMachine = () -> {
try {
Thread.sleep(2000);
deleteVMsUsingEC2Client(this.ec2client, this.host, Collections.singletonList(vm));
} catch (Throwable t) {
assertNotNull(t);
}
};
deleteMachine.run();
ProvisionSecurityGroupTaskState removeTask = remove.getBody(ProvisionSecurityGroupTaskState.class);
waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, removeTask.documentSelfLink));
// verify security group state is gone
try {
getSecurityGroupState(securityGroupState.documentSelfLink);
} catch (Exception ex) {
assertTrue(ex instanceof ServiceNotFoundException);
}
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class TestProvisionAWSSecurityGroup method validateAWSArtifacts.
private void validateAWSArtifacts(String securityGroupDescriptionLink, AuthCredentialsServiceState creds) throws Throwable {
SecurityGroupState securityGroup = getSecurityGroupState(securityGroupDescriptionLink);
AWSSecurityGroupClient client = new AWSSecurityGroupClient(AWSUtils.getAsyncClient(creds, this.region, getExecutor()));
// if any artifact is not present then an error will be thrown
SecurityGroup sg = client.getSecurityGroupById(securityGroup.customProperties.get(AWSSecurityGroupService.SECURITY_GROUP_ID));
assertNotNull(sg);
assertNotNull(sg.getIpPermissions());
assertTrue(sg.getIpPermissions().size() == 2);
// check that there is a rule that enables internal communication
assertTrue(isInternalRule(sg.getGroupId(), sg.getIpPermissions()));
assertNotNull(sg.getIpPermissionsEgress());
// there are two egress rules (one that was added as part of this test, and the default one)
assertTrue(sg.getIpPermissionsEgress().size() == 2);
// check that there is a rule that enables internal communication
assertTrue(isInternalRule(sg.getGroupId(), sg.getIpPermissionsEgress()));
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class TestProvisionAWSSecurityGroup method testProvisionAWSSecurityGroup.
@Test
public void testProvisionAWSSecurityGroup() throws Throwable {
// create credentials
Operation authResponse = new Operation();
TestUtils.postCredentials(this.host, authResponse, this.privateKey, this.privateKeyId);
AuthCredentialsServiceState creds = authResponse.getBody(AuthCredentialsServiceState.class);
// create resource pool
Operation poolResponse = new Operation();
TestUtils.postResourcePool(this.host, poolResponse);
ResourcePoolState pool = poolResponse.getBody(ResourcePoolState.class);
// create sg service
Operation securityGroupResponse = new Operation();
SecurityGroupState initialSecurityGroupState = buildSecurityGroupState(creds, pool);
TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, securityGroupResponse);
SecurityGroupState securityGroupState = securityGroupResponse.getBody(SecurityGroupState.class);
// set up security group task state
ProvisionSecurityGroupTaskState task = new ProvisionSecurityGroupTaskState();
task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.CREATE;
task.securityGroupDescriptionLinks = Stream.of(securityGroupState.documentSelfLink).collect(Collectors.toSet());
task.customProperties = new HashMap<>();
task.customProperties.put(NETWORK_STATE_ID_PROP_NAME, this.vpcId);
Operation provision = new Operation();
provisionSecurityGroup(task, provision);
ProvisionSecurityGroupTaskState ps = provision.getBody(ProvisionSecurityGroupTaskState.class);
waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
validateAWSArtifacts(securityGroupState.documentSelfLink, creds);
// reuse previous task, but switch to a delete
task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.DELETE;
Operation remove = new Operation();
provisionSecurityGroup(task, remove);
ProvisionSecurityGroupTaskState removeTask = remove.getBody(ProvisionSecurityGroupTaskState.class);
waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, removeTask.documentSelfLink));
// verify security group state is gone
try {
getSecurityGroupState(securityGroupState.documentSelfLink);
} catch (Exception ex) {
assertTrue(ex instanceof ServiceNotFoundException);
}
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class TestAWSEnumerationTask method validateSecurityGroupsInformation.
private void validateSecurityGroupsInformation(Set<String> securityGroupLinks) throws Throwable {
if (this.isAwsClientMock) {
return;
}
// Query all the SGs, enumerated in the system
Map<String, SecurityGroupState> allSecurityGroupStatesMap = ProvisioningUtils.<SecurityGroupState>getResourceStates(this.host, SecurityGroupService.FACTORY_LINK, SecurityGroupState.class);
// Assert that there are SGs enumerated in the system
assertNotNull(allSecurityGroupStatesMap);
if (securityGroupLinks == null) {
return;
}
validateSecurityGroupTagLinks(allSecurityGroupStatesMap);
List<URI> securityGroupURIs = new ArrayList<>();
for (String sgLink : securityGroupLinks) {
securityGroupURIs.add(UriUtils.buildUri(this.host, sgLink));
}
// Validate that the SecurityGroups for this VM are correctly described in SGStates
Map<URI, SecurityGroupState> sgStatesToLinksMap = this.host.getServiceState(null, SecurityGroupState.class, securityGroupURIs);
for (URI uri : securityGroupURIs) {
// Assert the SG State exist
assertNotNull(sgStatesToLinksMap.get(uri));
// Assert that the security group rules are correctly added to the SG State
// In the test setup there are both ingress and egress rules added
assertTrue(sgStatesToLinksMap.get(uri).ingress.size() > 0);
assertTrue(sgStatesToLinksMap.get(uri).egress.size() > 0);
assertFalse(StringUtil.isNullOrEmpty(sgStatesToLinksMap.get(uri).customProperties.get(AWS_VPC_ID)));
}
}
Aggregations