Search in sources :

Example 21 with SecurityGroupState

use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.

the class AzureSecurityGroupServiceTest method testDeleteSecurityGroup.

@Test
public void testDeleteSecurityGroup() throws Throwable {
    SecurityGroupState securityGroupState = provisionSecurityGroup(new ArrayList<>(), new ArrayList<>(), TaskStage.FINISHED);
    startSecurityGroupProvisioning(InstanceRequestType.DELETE, securityGroupState, TaskStage.FINISHED);
    // verify security group state was deleted
    try {
        getSecurityGroupState(this.host, securityGroupState.documentSelfLink);
    } catch (Exception e) {
        assertTrue(e instanceof ServiceNotFoundException);
    }
    if (!this.isMock) {
        // Verify that the security group was deleted from Azure.
        NetworkSecurityGroupInner sgResponse = this.securityGroupsClient.getByResourceGroup(this.rgName, this.securityGroupName);
        if (sgResponse != null) {
            fail("Security group should not exist in Azure.");
        }
    }
}
Also used : NetworkSecurityGroupInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupInner) AzureTestUtil.getSecurityGroupState(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.getSecurityGroupState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Test(org.junit.Test)

Example 22 with SecurityGroupState

use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.

the class AzureSecurityGroupServiceTest method testCreateSecurityGroupWithNonDefaultRules.

@Test
public void testCreateSecurityGroupWithNonDefaultRules() throws Throwable {
    SecurityGroupState securityGroupState = provisionSecurityGroup(buildNonDefaultRules(), buildNonDefaultRules(), TaskStage.FINISHED);
    assertNotNull(securityGroupState.id);
    assertNotEquals(securityGroupState.id, this.securityGroupName);
    if (!this.isMock) {
        // Verify that the security group was created.
        NetworkSecurityGroupInner sgResponse = this.securityGroupsClient.getByResourceGroup(this.rgName, this.securityGroupName);
        assertEquals(this.securityGroupName, sgResponse.name());
        assertEquals(securityGroupState.id, sgResponse.id());
        assertEquals(sgResponse.securityRules().size(), 2 * securityGroupState.ingress.size());
        validateAzureSecurityRules(sgResponse.securityRules(), securityGroupState.ingress.size());
        // delete the security group
        startSecurityGroupProvisioning(InstanceRequestType.DELETE, securityGroupState, TaskStage.FINISHED);
    }
}
Also used : NetworkSecurityGroupInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupInner) AzureTestUtil.getSecurityGroupState(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.getSecurityGroupState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Test(org.junit.Test)

Example 23 with SecurityGroupState

use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.

the class AzureSecurityGroupServiceTest method testCreateSecurityGroupWithDefaultRules.

@Test
public void testCreateSecurityGroupWithDefaultRules() throws Throwable {
    SecurityGroupState securityGroupState = provisionSecurityGroup(buildDefaultRules(), buildDefaultRules(), TaskStage.FINISHED);
    assertNotNull(securityGroupState.id);
    assertNotEquals(securityGroupState.id, this.securityGroupName);
    if (!this.isMock) {
        // Verify that the security group was created.
        NetworkSecurityGroupInner sgResponse = this.securityGroupsClient.getByResourceGroup(this.rgName, this.securityGroupName);
        assertEquals(this.securityGroupName, sgResponse.name());
        assertEquals(securityGroupState.id, sgResponse.id());
        assertEquals(sgResponse.securityRules().size(), securityGroupState.ingress.size());
        validateAzureSecurityRules(sgResponse.securityRules(), securityGroupState.ingress.size() - 1);
        // delete the security group
        startSecurityGroupProvisioning(InstanceRequestType.DELETE, securityGroupState, TaskStage.FINISHED);
    }
}
Also used : NetworkSecurityGroupInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupInner) AzureTestUtil.getSecurityGroupState(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.getSecurityGroupState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Test(org.junit.Test)

Example 24 with SecurityGroupState

use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.

the class TestAWSEnumerationTask method validateSecurityGroupTagLinks.

/**
 * Validates the taglinks for the security group to follow the expected norm
 * i.e. /resources/security-groups/UUID
 */
private void validateSecurityGroupTagLinks(Map<String, SecurityGroupState> allSecurityGroupStatesMap) {
    for (Map.Entry<String, SecurityGroupState> securityGroupState : allSecurityGroupStatesMap.entrySet()) {
        Set<String> tagLinks = securityGroupState.getValue().tagLinks;
        if (tagLinks != null) {
            for (String tag : tagLinks) {
                assertTrue(tag.startsWith(TagService.FACTORY_LINK));
            }
        }
        TagService.TagState expectedInternalTypeTag = newTagState(TAG_KEY_TYPE, AWSConstants.AWSResourceType.ec2_security_group.toString(), false, securityGroupState.getValue().tenantLinks);
        assertTrue(tagLinks.contains(expectedInternalTypeTag.documentSelfLink));
    }
}
Also used : TagState(com.vmware.photon.controller.model.resources.TagService.TagState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) TagService(com.vmware.photon.controller.model.resources.TagService) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with SecurityGroupState

use of com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState in project photon-model by vmware.

the class TestAWSSetupUtils method createAWSNicStates.

/*
     * NOTE: It is highly recommended to keep this method in sync with its Azure counterpart:
     * AzureTestUtil.createDefaultNicStates
     */
public static List<NetworkInterfaceState> createAWSNicStates(VerificationHost host, ComputeState computeHost, EndpointState endpointState, String vmName, AwsNicSpecs nicSpecs, boolean addNewSecurityGroup, Map<String, Object> awsTestContext) throws Throwable {
    // Create network state.
    NetworkState networkState;
    {
        networkState = new NetworkState();
        networkState.id = nicSpecs.network.id;
        networkState.name = nicSpecs.network.name;
        networkState.subnetCIDR = nicSpecs.network.cidr;
        networkState.authCredentialsLink = endpointState.authCredentialsLink;
        networkState.resourcePoolLink = computeHost.resourcePoolLink;
        networkState.instanceAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_NETWORK_ADAPTER);
        networkState.regionId = regionId;
        networkState.endpointLink = endpointState.documentSelfLink;
        networkState.endpointLinks = new HashSet<String>();
        networkState.endpointLinks.add(endpointState.documentSelfLink);
        networkState.tenantLinks = endpointState.tenantLinks;
        networkState.computeHostLink = computeHost.documentSelfLink;
        networkState = TestUtils.doPost(host, networkState, NetworkState.class, UriUtils.buildUri(host, NetworkService.FACTORY_LINK));
    }
    // Create NIC states.
    List<NetworkInterfaceState> nics = new ArrayList<>();
    for (int i = 0; i < nicSpecs.nicSpecs.size(); i++) {
        // Create subnet state per NIC.
        SubnetState subnetState;
        {
            subnetState = new SubnetState();
            subnetState.id = nicSpecs.nicSpecs.get(i).subnetSpec.id;
            subnetState.name = nicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.subnetCIDR = nicSpecs.nicSpecs.get(i).subnetSpec.cidr;
            subnetState.zoneId = nicSpecs.nicSpecs.get(i).subnetSpec.zoneId;
            subnetState.networkLink = networkState.documentSelfLink;
            subnetState.regionId = regionId;
            subnetState.endpointLink = endpointState.documentSelfLink;
            subnetState.endpointLinks = new HashSet<String>();
            subnetState.endpointLinks.add(endpointState.documentSelfLink);
            subnetState.tenantLinks = endpointState.tenantLinks;
            subnetState = TestUtils.doPost(host, subnetState, SubnetState.class, UriUtils.buildUri(host, SubnetService.FACTORY_LINK));
        }
        // Create NIC description.
        NetworkInterfaceDescription nicDescription;
        NicSpec nicSpec = nicSpecs.nicSpecs.get(0);
        {
            nicDescription = new NetworkInterfaceDescription();
            nicDescription.id = "nicDesc" + i;
            nicDescription.name = "nicDesc" + i;
            nicDescription.deviceIndex = i;
            nicDescription.assignment = nicSpec.getIpAssignment();
            nicDescription.regionId = regionId;
            nicDescription.endpointLink = endpointState.documentSelfLink;
            nicDescription.endpointLinks = new HashSet<String>();
            nicDescription.endpointLinks.add(endpointState.documentSelfLink);
            nicDescription.tenantLinks = endpointState.tenantLinks;
            nicDescription = TestUtils.doPost(host, nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(host, NetworkInterfaceDescriptionService.FACTORY_LINK));
        }
        // Create security group state for an existing security group
        SecurityGroupState existingSecurityGroupState = createSecurityGroupState(host, computeHost, endpointState, true, awsTestContext);
        NetworkInterfaceState nicState = new NetworkInterfaceState();
        nicState.id = UUID.randomUUID().toString();
        nicState.name = vmName + "-nic-" + i;
        nicState.deviceIndex = nicDescription.deviceIndex;
        nicState.networkLink = networkState.documentSelfLink;
        nicState.subnetLink = subnetState.documentSelfLink;
        nicState.networkInterfaceDescriptionLink = nicDescription.documentSelfLink;
        nicState.regionId = regionId;
        nicState.endpointLink = endpointState.documentSelfLink;
        nicState.endpointLinks = new HashSet<String>();
        nicState.endpointLinks.add(endpointState.documentSelfLink);
        nicState.tenantLinks = endpointState.tenantLinks;
        nicState.securityGroupLinks = new ArrayList<>();
        nicState.securityGroupLinks.add(existingSecurityGroupState.documentSelfLink);
        if (addNewSecurityGroup) {
            // Create security group state for a new security group
            SecurityGroupState newSecurityGroupState = createSecurityGroupState(host, computeHost, endpointState, false, awsTestContext);
            nicState.securityGroupLinks.add(newSecurityGroupState.documentSelfLink);
        }
        nicState = TestUtils.doPost(host, nicState, NetworkInterfaceState.class, UriUtils.buildUri(host, NetworkInterfaceService.FACTORY_LINK));
        nics.add(nicState);
    }
    return nics;
}
Also used : NetworkInterfaceDescription(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ArrayList(java.util.ArrayList) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) NicSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) HashSet(java.util.HashSet)

Aggregations

SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)41 Operation (com.vmware.xenon.common.Operation)16 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)15 URI (java.net.URI)12 AzureTestUtil.getSecurityGroupState (com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.getSecurityGroupState)9 UriUtils (com.vmware.xenon.common.UriUtils)9 List (java.util.List)9 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)8 HashSet (java.util.HashSet)8 AzureBaseTest (com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest)7 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)7 StatelessService (com.vmware.xenon.common.StatelessService)7 HashMap (java.util.HashMap)7 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)6 NetworkSecurityGroupInner (com.microsoft.azure.management.network.implementation.NetworkSecurityGroupInner)6 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)5