use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class ResourceGroomerTaskService method populateEndpointLinksByDocumentLinks.
/**
* Helper for creating corresponding object and parsing the response for given documentKind
* to store selfLink and endpointLink.
*/
private static void populateEndpointLinksByDocumentLinks(Map<String, Object> documents, Map<String, Set<String>> endpointLinksByDocumentLinks, Map<String, String> endpointLinkByDocumentLinks) {
for (Object document : documents.values()) {
ServiceDocument doc = Utils.fromJson(document, ServiceDocument.class);
Set<String> endpointLinks = new HashSet<>();
if (doc.documentKind.equals(COMPUTE_STATE_DOCUMENT_KIND)) {
ComputeState state = Utils.fromJson(document, ComputeState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(DISK_STATE_DOCUMENT_KIND)) {
DiskState state = Utils.fromJson(document, DiskState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(COMPUTE_DESCRIPTION_DOCUMENT_KIND)) {
ComputeDescription state = Utils.fromJson(document, ComputeDescription.class);
// only deleting discovered resources
if (!(state.customProperties != null && (ResourceEnumerationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK)) || EndpointAllocationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK))))) {
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_STATE_DOCUMENT_KIND)) {
NetworkState state = Utils.fromJson(document, NetworkState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_INTERFACE_STATE_DOCUMENT_KIND)) {
NetworkInterfaceState state = Utils.fromJson(document, NetworkInterfaceState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SECURITY_GROUP_STATE_DOCUMENT_KIND)) {
SecurityGroupState state = Utils.fromJson(document, SecurityGroupState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SUBNET_STATE_DOCUMENT_KIND)) {
SubnetState state = Utils.fromJson(document, SubnetState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(LOAD_BALANCER_DOCUMENT_KIND)) {
LoadBalancerState state = Utils.fromJson(document, LoadBalancerState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(STORAGE_DESCRIPTION_DOCUMENT_KIND)) {
StorageDescription state = Utils.fromJson(document, StorageDescription.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(RESOURCE_GROUP_DOCUMENT_KIND)) {
ResourceGroupState state = Utils.fromJson(document, ResourceGroupState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceGroupService.PROPERTY_NAME_IS_USER_CREATED)) {
continue;
}
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(IMAGE_STATE_KIND)) {
ImageState state = Utils.fromJson(document, ImageState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(ROUTER_STATE_KIND)) {
RouterState state = Utils.fromJson(document, RouterState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(AUTH_CREDENTIALS_SERVICE_STATE_KIND)) {
AuthCredentialsServiceState state = Utils.fromJson(document, AuthCredentialsServiceState.class);
if (state.customProperties != null && state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK) != null) {
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK));
} else {
endpointLinkByDocumentLinks.put(state.documentSelfLink, EMPTY_STRING);
}
}
}
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class NicSecurityGroupsTaskServiceTest method createSecurityGroup.
private static SecurityGroupState createSecurityGroup(BaseModelTest test, String endpointLink) throws Throwable {
SecurityGroupState securityGroupState = new SecurityGroupState();
securityGroupState.authCredentialsLink = "authCredentialsLink";
securityGroupState.name = UUID.randomUUID().toString();
securityGroupState.regionId = "regionId";
securityGroupState.endpointLink = endpointLink;
securityGroupState.resourcePoolLink = "/resourcePoolLink";
securityGroupState.instanceAdapterReference = UriUtils.buildUri(test.getHost(), MockAdapter.MockSecurityGroupInstanceSuccessAdapter.SELF_LINK);
securityGroupState.id = UUID.randomUUID().toString();
ArrayList<SecurityGroupState.Rule> rules = new ArrayList<>();
SecurityGroupState.Rule ssh = new SecurityGroupState.Rule();
ssh.name = "ssh";
ssh.protocol = "tcp";
ssh.ipRangeCidr = "0.0.0.0/0";
ssh.ports = "22";
rules.add(ssh);
securityGroupState.ingress = rules;
securityGroupState.egress = rules;
return test.postServiceSynchronously(SecurityGroupService.FACTORY_LINK, securityGroupState, SecurityGroupState.class);
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskServiceTest method createSecurityGroupState.
private static SecurityGroupState createSecurityGroupState(BaseModelTest test, boolean success) {
SecurityGroupState securityGroupState = new SecurityGroupState();
securityGroupState.authCredentialsLink = "authCredentialsLink";
securityGroupState.name = UUID.randomUUID().toString();
securityGroupState.regionId = "regionId";
securityGroupState.resourcePoolLink = "/resourcePoolLink";
if (success) {
securityGroupState.instanceAdapterReference = UriUtils.buildUri(test.getHost(), MockSecurityGroupInstanceSuccessAdapter.SELF_LINK);
} else {
securityGroupState.instanceAdapterReference = UriUtils.buildUri(test.getHost(), MockSecurityGroupInstanceFailureAdapter.SELF_LINK);
}
securityGroupState.id = UUID.randomUUID().toString();
ArrayList<Rule> rules = new ArrayList<>();
Rule ssh = new Rule();
ssh.name = "ssh";
ssh.protocol = "tcp";
ssh.ipRangeCidr = "0.0.0.0/0";
ssh.ports = "22";
rules.add(ssh);
securityGroupState.ingress = rules;
securityGroupState.egress = rules;
return securityGroupState;
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskServiceTest method buildValidStartState.
private static ProvisionSecurityGroupTaskState buildValidStartState(BaseModelTest test, SecurityGroupInstanceRequest.InstanceRequestType requestType, boolean success) throws Throwable {
ProvisionSecurityGroupTaskState startState = new ProvisionSecurityGroupTaskState();
SecurityGroupState securityGroupState1 = createSecurityGroupState(test, success);
SecurityGroupState returnState1 = test.postServiceSynchronously(SecurityGroupService.FACTORY_LINK, securityGroupState1, SecurityGroupState.class);
SecurityGroupState securityGroupState2 = createSecurityGroupState(test, success);
SecurityGroupState returnState2 = test.postServiceSynchronously(SecurityGroupService.FACTORY_LINK, securityGroupState2, SecurityGroupState.class);
startState.requestType = requestType;
startState.securityGroupDescriptionLinks = Stream.of(returnState1.documentSelfLink, returnState2.documentSelfLink).collect(Collectors.toSet());
startState.isMockRequest = true;
return startState;
}
use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.
the class AWSLoadBalancerServiceTest method testCreateUpdateDeleteLoadBalancer.
@Test
public void testCreateUpdateDeleteLoadBalancer() throws Throwable {
// set name with invalid characters and more than 32 characters
this.lbName = generateLbName() + "-1234567890-1234567890-1234567890_;.,/";
LoadBalancerState lb = createLoadBalancerState(this.lbName);
// Provision load balancer
kickOffLoadBalancerProvision(InstanceRequestType.CREATE, lb.documentSelfLink, TaskStage.FINISHED);
lb = getServiceSynchronously(lb.documentSelfLink, LoadBalancerState.class);
this.lbName = lb.name;
if (!this.isMock) {
assertNotNull(lb.securityGroupLinks);
String securityGroupDocumentSelfLink = lb.securityGroupLinks.iterator().next();
assertNotNull(securityGroupDocumentSelfLink);
SecurityGroupState sgs = getServiceSynchronously(securityGroupDocumentSelfLink, SecurityGroupState.class);
this.sgId = sgs.id;
LoadBalancerDescription awsLoadBalancer = getAwsLoadBalancer(this.lbName);
assertNotNull(awsLoadBalancer);
assertEquals(awsLoadBalancer.getDNSName(), lb.address);
assertEquals("internet-facing", awsLoadBalancer.getScheme());
assertEquals(1, awsLoadBalancer.getInstances().size());
List<ListenerDescription> listeners = awsLoadBalancer.getListenerDescriptions();
assertEquals(2, listeners.size());
verifyListeners(lb.routes, listeners);
verifyHealthCheckConfiguration(lb.routes.get(0), awsLoadBalancer.getHealthCheck());
SecurityGroup securityGroup = getAwsSecurityGroup(sgs.id);
assertNotNull(securityGroup);
String lbSecGroupId = awsLoadBalancer.getSecurityGroups().stream().findFirst().orElse(null);
assertEquals(securityGroup.getGroupId(), lbSecGroupId);
}
// Update load balancer from 1 machines to 2 to simulate scale-out
if (!this.isMock) {
lb.computeLinks = new HashSet<>(Arrays.asList(this.cs1.documentSelfLink, this.cs2.documentSelfLink));
putServiceSynchronously(lb.documentSelfLink, lb);
}
kickOffLoadBalancerProvision(InstanceRequestType.UPDATE, lb.documentSelfLink, TaskStage.FINISHED);
if (!this.isMock) {
LoadBalancerDescription awsLoadBalancer = getAwsLoadBalancer(this.lbName);
assertNotNull(awsLoadBalancer);
assertEquals(2, awsLoadBalancer.getInstances().size());
// Update load balancer from 2 machines to 1 to simulate scale-in
lb.computeLinks = Collections.singleton(this.cs1.documentSelfLink);
putServiceSynchronously(lb.documentSelfLink, lb);
kickOffLoadBalancerProvision(InstanceRequestType.UPDATE, lb.documentSelfLink, TaskStage.FINISHED);
awsLoadBalancer = getAwsLoadBalancer(this.lbName);
assertNotNull(awsLoadBalancer);
assertEquals(1, awsLoadBalancer.getInstances().size());
}
kickOffLoadBalancerProvision(InstanceRequestType.DELETE, lb.documentSelfLink, TaskStage.FINISHED);
if (!this.isMock) {
assertNull(getAwsLoadBalancer(this.lbName));
assertNull(getAwsSecurityGroup(this.sgId));
}
this.lbName = null;
this.sgId = null;
}
Aggregations