Search in sources :

Example 26 with SecurityGroupState

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

the class AzureSecurityGroupServiceTest method testDeleteMissingSecurityGroup.

@Test
public void testDeleteMissingSecurityGroup() throws Throwable {
    SecurityGroupState securityGroupState = createSecurityGroupState(this.securityGroupName, new ArrayList<>(), new ArrayList<>());
    // attempt to delete the missing SG
    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);
    }
}
Also used : 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 27 with SecurityGroupState

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

the class AzureSecurityGroupServiceTest method testCreateSecurityGroupNoRules.

@Test
public void testCreateSecurityGroupNoRules() throws Throwable {
    SecurityGroupState securityGroupState = provisionSecurityGroup(new ArrayList<>(), new ArrayList<>(), 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(), 0);
        // 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 28 with SecurityGroupState

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

the class AzureSecurityGroupServiceTest method createSecurityGroupState.

private SecurityGroupState createSecurityGroupState(String name, List<Rule> inboundRules, List<Rule> outboudRules) throws Throwable {
    SecurityGroupState securityGroupState = new SecurityGroupState();
    securityGroupState.id = name;
    securityGroupState.name = name;
    securityGroupState.instanceAdapterReference = UriUtils.buildUri(this.host, AzureSecurityGroupService.SELF_LINK);
    securityGroupState.endpointLink = endpointState.documentSelfLink;
    securityGroupState.tenantLinks = endpointState.tenantLinks;
    securityGroupState.ingress = inboundRules;
    securityGroupState.egress = outboudRules;
    securityGroupState.authCredentialsLink = endpointState.authCredentialsLink;
    securityGroupState.resourcePoolLink = "test-resource-pool-link";
    securityGroupState.regionId = this.regionId;
    return postServiceSynchronously(SecurityGroupService.FACTORY_LINK, securityGroupState, SecurityGroupState.class);
}
Also used : AzureTestUtil.getSecurityGroupState(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.getSecurityGroupState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)

Example 29 with SecurityGroupState

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

the class AzureTestUtil method createDefaultNicStates.

/*
     * NOTE: It is highly recommended to keep this method in sync with its AWS counterpart:
     * TestAWSSetupUtils.createAWSNicStates
     */
public static List<NetworkInterfaceState> createDefaultNicStates(VerificationHost host, ComputeState computeHost, EndpointState endpointState, Set<String> networkRGLinks, Set<String> sgRGLinks, AzureNicSpecs azureNicSpecs, String azureVMName) throws Throwable {
    // Create network state.
    NetworkState networkState;
    {
        networkState = new NetworkState();
        networkState.id = azureNicSpecs.network.name;
        networkState.name = azureNicSpecs.network.name;
        networkState.subnetCIDR = azureNicSpecs.network.cidr;
        networkState.authCredentialsLink = endpointState.authCredentialsLink;
        networkState.endpointLink = endpointState.documentSelfLink;
        networkState.endpointLinks = new HashSet<>();
        networkState.endpointLinks.add(endpointState.documentSelfLink);
        networkState.tenantLinks = endpointState.tenantLinks;
        networkState.resourcePoolLink = computeHost.resourcePoolLink;
        networkState.groupLinks = networkRGLinks;
        networkState.regionId = azureNicSpecs.network.zoneId;
        networkState.instanceAdapterReference = UriUtils.buildUri(host, DEFAULT_INSTANCE_ADAPTER_REFERENCE);
        networkState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_vnet.name());
        networkState.computeHostLink = endpointState.computeHostLink;
        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 < azureNicSpecs.nicSpecs.size(); i++) {
        NicSpec nicSpec = azureNicSpecs.nicSpecs.get(i);
        // Create subnet state per NIC.
        SubnetState subnetState;
        {
            subnetState = new SubnetState();
            subnetState.id = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.name = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.subnetCIDR = azureNicSpecs.nicSpecs.get(i).subnetSpec.cidr;
            subnetState.zoneId = azureNicSpecs.nicSpecs.get(i).subnetSpec.zoneId;
            subnetState.networkLink = networkState.documentSelfLink;
            subnetState.endpointLink = endpointState.documentSelfLink;
            subnetState.endpointLinks = new HashSet<>();
            subnetState.endpointLinks.add(endpointState.documentSelfLink);
            subnetState.computeHostLink = endpointState.computeHostLink;
            subnetState.tenantLinks = endpointState.tenantLinks;
            subnetState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_subnet.name());
            subnetState = TestUtils.doPost(host, subnetState, SubnetState.class, UriUtils.buildUri(host, SubnetService.FACTORY_LINK));
        }
        // Create security group state
        SecurityGroupState securityGroupState;
        {
            securityGroupState = new SecurityGroupState();
            securityGroupState.name = AZURE_SECURITY_GROUP_NAME + "-" + azureVMName;
            securityGroupState.authCredentialsLink = endpointState.authCredentialsLink;
            securityGroupState.endpointLink = endpointState.documentSelfLink;
            securityGroupState.endpointLinks = new HashSet<>();
            securityGroupState.endpointLinks.add(endpointState.documentSelfLink);
            securityGroupState.computeHostLink = endpointState.computeHostLink;
            securityGroupState.tenantLinks = endpointState.tenantLinks;
            securityGroupState.groupLinks = sgRGLinks;
            securityGroupState.regionId = "regionId";
            securityGroupState.resourcePoolLink = "/link/to/rp";
            securityGroupState.instanceAdapterReference = new URI("http://instanceAdapterReference");
            {
                Rule ssh = new Rule();
                ssh.name = "ssh-in";
                ssh.protocol = "tcp";
                ssh.ipRangeCidr = "0.0.0.0/0";
                ssh.ports = "22";
                securityGroupState.ingress = Collections.singletonList(ssh);
            }
            {
                Rule out = new Rule();
                out.name = "out";
                out.protocol = "tcp";
                out.ipRangeCidr = "0.0.0.0/0";
                out.ports = SecurityGroupService.ALL_PORTS;
                securityGroupState.egress = Collections.singletonList(out);
            }
            securityGroupState = TestUtils.doPost(host, securityGroupState, SecurityGroupState.class, UriUtils.buildUri(host, SecurityGroupService.FACTORY_LINK));
        }
        // Create NIC description.
        NetworkInterfaceDescription nicDescription;
        {
            nicDescription = new NetworkInterfaceDescription();
            nicDescription.id = "nicDesc" + i;
            nicDescription.name = generateName("nicDesc" + i);
            nicDescription.deviceIndex = i;
            nicDescription.assignPublicIpAddress = azureNicSpecs.assignPublicIpAddress;
            nicDescription.tenantLinks = endpointState.tenantLinks;
            nicDescription.endpointLink = endpointState.documentSelfLink;
            nicDescription.endpointLinks = new HashSet<>();
            nicDescription.endpointLinks.add(endpointState.documentSelfLink);
            nicDescription.computeHostLink = endpointState.computeHostLink;
            nicDescription.assignment = nicSpec.getIpAssignment();
            // if staticIp is null, it will be assigned automatically by DHCP.
            nicDescription.address = nicSpec.ip();
            nicDescription = TestUtils.doPost(host, nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(host, NetworkInterfaceDescriptionService.FACTORY_LINK));
        }
        NetworkInterfaceState nicState = new NetworkInterfaceState();
        nicState.id = "nic" + i;
        nicState.name = generateName("nic" + i);
        nicState.deviceIndex = nicDescription.deviceIndex;
        nicState.networkInterfaceDescriptionLink = nicDescription.documentSelfLink;
        nicState.subnetLink = subnetState.documentSelfLink;
        nicState.networkLink = subnetState.networkLink;
        nicState.tenantLinks = endpointState.tenantLinks;
        nicState.endpointLink = endpointState.documentSelfLink;
        nicState.endpointLinks = new HashSet<>();
        nicState.endpointLinks.add(endpointState.documentSelfLink);
        nicState.computeHostLink = endpointState.computeHostLink;
        if (nicSpec.getIpAssignment() == IpAssignment.STATIC) {
            // There is a rule in:
            // \photon-model\photon-model\src\main\java\com\vmware\photon\controller\model\resources\NetworkInterfaceService.java::validateState()
            // // which will throws java.lang.IllegalArgumentException: both networkLink and IP
            // cannot be set
            nicState.networkLink = null;
        }
        if (i == 0) {
            // Attach security group only on the primary nic.
            nicState.securityGroupLinks = Collections.singletonList(securityGroupState.documentSelfLink);
        }
        nicState.tagLinks = Collections.singleton(TagsUtil.newTagState(TAG_KEY_TYPE, azure_net_interface.name(), false, endpointState.tenantLinks).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) URI(java.net.URI) NicSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) HashSet(java.util.HashSet)

Example 30 with SecurityGroupState

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

the class ModelUtils method createSecurityGroup.

public static SecurityGroupState createSecurityGroup(BaseModelTest test, String securityGroupName, ComputeState computeHost, EndpointState endpointState) throws Throwable {
    SecurityGroupState securityGroupState = new SecurityGroupState();
    securityGroupState.name = securityGroupName;
    securityGroupState.authCredentialsLink = endpointState.authCredentialsLink;
    securityGroupState.tenantLinks = endpointState.tenantLinks;
    securityGroupState.endpointLink = endpointState.documentSelfLink;
    securityGroupState.resourcePoolLink = computeHost.resourcePoolLink;
    securityGroupState.endpointLinks = new HashSet<>();
    securityGroupState.endpointLinks.add(endpointState.documentSelfLink);
    securityGroupState.computeHostLink = computeHost.documentSelfLink;
    securityGroupState.customProperties = new HashMap<>();
    securityGroupState.customProperties.put(ComputeProperties.COMPUTE_HOST_LINK_PROP_NAME, computeHost.documentSelfLink);
    Rule ssh = new Rule();
    ssh.name = "ssh";
    ssh.protocol = "tcp";
    ssh.ipRangeCidr = "0.0.0.0/0";
    ssh.ports = "22";
    securityGroupState.ingress = new ArrayList<>();
    securityGroupState.ingress.add(ssh);
    Rule out = new Rule();
    out.name = "out";
    out.protocol = "tcp";
    out.ipRangeCidr = "0.0.0.0/0";
    out.ports = "1-65535";
    securityGroupState.egress = new ArrayList<>();
    securityGroupState.egress.add(out);
    securityGroupState.regionId = "regionId";
    securityGroupState.resourcePoolLink = "/link/to/rp";
    securityGroupState.instanceAdapterReference = new URI("http://instanceAdapterReference");
    return test.postServiceSynchronously(ResourcePoolService.FACTORY_LINK, securityGroupState, SecurityGroupState.class);
}
Also used : SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) URI(java.net.URI)

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