Search in sources :

Example 26 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class AWSNetworkUtils method mapVPCToNetworkState.

public static NetworkState mapVPCToNetworkState(Vpc vpc, String regionId, String resourcePoolLink, String endpointLink, String authCredentialsLink, String parentComputeLink, List<String> tenantLinks, URI adapterUri) {
    if (vpc == null) {
        throw new IllegalArgumentException("Cannot map VPC to network state for null instance");
    }
    NetworkState networkState = new NetworkState();
    networkState.id = vpc.getVpcId();
    // calculate vpc name
    if (vpc.getTags() == null) {
        networkState.name = vpc.getVpcId();
    } else {
        networkState.name = vpc.getTags().stream().filter(tag -> tag.getKey().equals(AWS_TAG_NAME)).map(tag -> tag.getValue()).findFirst().orElse(vpc.getVpcId());
    }
    networkState.subnetCIDR = vpc.getCidrBlock();
    networkState.regionId = regionId;
    networkState.resourcePoolLink = resourcePoolLink;
    networkState.endpointLink = endpointLink;
    if (networkState.endpointLinks == null) {
        networkState.endpointLinks = new HashSet<>();
    }
    networkState.endpointLinks.add(endpointLink);
    networkState.authCredentialsLink = authCredentialsLink;
    networkState.instanceAdapterReference = adapterUri;
    networkState.tenantLinks = tenantLinks;
    networkState.computeHostLink = parentComputeLink;
    networkState.customProperties = new HashMap<>();
    networkState.customProperties.put("defaultInstance", String.valueOf(vpc.isDefault()));
    return networkState;
}
Also used : AWSEnumerationUtils.getTagValue(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getTagValue) Arrays(java.util.Arrays) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) StringUtil(io.netty.util.internal.StringUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) Subnet(com.amazonaws.services.ec2.model.Subnet) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) StatelessService(com.vmware.xenon.common.StatelessService) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) Vpc(com.amazonaws.services.ec2.model.Vpc) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Set(java.util.Set) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) List(java.util.List) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Example 27 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class ResourceOperationUtils method lookupByResourceState.

/**
 * Lookup for {@link ResourceOperationSpec} by given {@code resourceState} and {@code operation}
 * @param host
 *         host to use to create operation
 * @param refererURI
 *         the referer to use when send the operation
 * @param resourceState
 *         the resource state specialization for which to lookup the spec
 * @param operation
 *         the operation
 * @param authorizationContext
 *         authorization context that will be used for operations (if set to null the context
 *         will not be changed)
 * @return
 */
public static <T extends ResourceState> DeferredResult<List<ResourceOperationSpec>> lookupByResourceState(ServiceHost host, URI refererURI, T resourceState, String operation, AuthorizationContext authorizationContext) {
    AssertUtil.assertNotNull(resourceState, "'resourceState' must be set.");
    String endpointLink = null;
    // All endpoint links have to be of the same type, so it's enough to get the first one
    if (resourceState.endpointLinks != null && !resourceState.endpointLinks.isEmpty()) {
        endpointLink = resourceState.endpointLinks.iterator().next();
    }
    ResourceType resourceType;
    if (resourceState instanceof ComputeState) {
        ComputeState compute = (ComputeState) resourceState;
        endpointLink = endpointLink == null ? compute.endpointLink : endpointLink;
        resourceType = ResourceType.COMPUTE;
    } else if (resourceState instanceof NetworkState) {
        NetworkState network = (NetworkState) resourceState;
        endpointLink = endpointLink == null ? network.endpointLink : endpointLink;
        resourceType = ResourceType.NETWORK;
    } else {
        throw new IllegalArgumentException("Unsupported resource state: " + resourceState.getClass().getName());
    }
    AssertUtil.assertNotNull(endpointLink, " must be set.");
    return host.sendWithDeferredResult(Operation.createGet(host, endpointLink).setReferer(refererURI), EndpointState.class).thenCompose(ep -> lookUp(host, refererURI, (ep).endpointType, resourceType, operation, resourceState.tenantLinks, authorizationContext));
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceType(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceType) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Example 28 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class BaseVSphereAdapterTest method createNetwork.

protected NetworkState createNetwork(String name) throws Throwable {
    if (name == null) {
        name = "name-not-defined";
    }
    NetworkState net = new NetworkState();
    net.adapterManagementReference = getAdapterManagementReference();
    net.authCredentialsLink = this.auth.documentSelfLink;
    net.name = name;
    net.instanceAdapterReference = UriUtils.buildUri(this.host, VSphereUriPaths.INSTANCE_SERVICE);
    net.subnetCIDR = "0.0.0.0/0";
    net.resourcePoolLink = this.resourcePool.documentSelfLink;
    if (this.datacenterId == null) {
        net.regionId = "datacenter-not-defined";
    } else {
        net.regionId = this.datacenterId;
    }
    ManagedObjectReference ref = new ManagedObjectReference();
    ref.setValue("network-3");
    ref.setType(VimNames.TYPE_NETWORK);
    CustomProperties.of(net).put(CustomProperties.TYPE, VimNames.TYPE_NETWORK).put(CustomProperties.MOREF, ref);
    return TestUtils.doPost(this.host, net, NetworkState.class, UriUtils.buildUri(this.host, NetworkService.FACTORY_LINK));
}
Also used : NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 29 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class TestVSpherePortgroupProvisioning method createPortgroup.

@Test
public void createPortgroup() throws Throwable {
    this.endpoint = createEndpoint(cs -> this.computeHost = cs, null);
    // enumerate all resources hoping to find the template
    doRefresh();
    snapshotFactoryState("networks", NetworkService.class);
    NetworkState dvsSwitch = fetchServiceState(NetworkState.class, findDvs(networkId));
    SubnetState subnet = new SubnetState();
    subnet.subnetCIDR = "0.0.0.0/0";
    subnet.networkLink = dvsSwitch.documentSelfLink;
    subnet.name = nextName("pg");
    subnet.instanceAdapterReference = UriUtils.buildUri(this.host, DvsNetworkService.SELF_LINK);
    subnet = TestUtils.doPost(this.host, subnet, SubnetState.class, UriUtils.buildUri(this.host, SubnetService.FACTORY_LINK));
    ProvisionSubnetTaskState createTask = new ProvisionSubnetTaskState();
    createTask.options = createOptions();
    createTask.requestType = InstanceRequestType.CREATE;
    createTask.subnetLink = subnet.documentSelfLink;
    createTask = TestUtils.doPost(this.host, createTask, ProvisionSubnetTaskState.class, UriUtils.buildUri(this.host, ProvisionSubnetTaskService.FACTORY_LINK));
    awaitTaskEnd(createTask);
    // refresh state
    subnet = getSubnetState(subnet);
    // delete the portgroup
    deleteThePortgroup(subnet);
}
Also used : SubnetService(com.vmware.photon.controller.model.resources.SubnetService) ProvisionSubnetTaskState(com.vmware.photon.controller.model.tasks.ProvisionSubnetTaskService.ProvisionSubnetTaskState) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) Operation(com.vmware.xenon.common.Operation) QueryTask(com.vmware.xenon.services.common.QueryTask) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) ProvisionSubnetTaskService(com.vmware.photon.controller.model.tasks.ProvisionSubnetTaskService) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) ServiceDocument(com.vmware.xenon.common.ServiceDocument) Test(org.junit.Test) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) Query(com.vmware.xenon.services.common.QueryTask.Query) Ignore(org.junit.Ignore) DvsNetworkService(com.vmware.photon.controller.model.adapters.vsphere.network.DvsNetworkService) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) UriUtils(com.vmware.xenon.common.UriUtils) InstanceRequestType(com.vmware.photon.controller.model.adapterapi.SubnetInstanceRequest.InstanceRequestType) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Assert(org.junit.Assert) EnumSet(java.util.EnumSet) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) ProvisionSubnetTaskState(com.vmware.photon.controller.model.tasks.ProvisionSubnetTaskService.ProvisionSubnetTaskState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Test(org.junit.Test)

Example 30 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class AzureSubnetTaskServiceTest method createNetworkState.

private NetworkState createNetworkState(String resourceGroupLink) throws Throwable {
    NetworkState networkState = new NetworkState();
    networkState.id = this.vNetName;
    networkState.name = this.vNetName;
    networkState.subnetCIDR = AZURE_DEFAULT_VPC_CIDR;
    networkState.tenantLinks = endpointState.tenantLinks;
    networkState.endpointLink = endpointState.documentSelfLink;
    networkState.resourcePoolLink = "dummyResourcePoolLink";
    networkState.groupLinks = Collections.singleton(resourceGroupLink);
    networkState.regionId = this.regionId;
    networkState.instanceAdapterReference = UriUtils.buildUri(this.host, AzureInstanceService.SELF_LINK);
    return postServiceSynchronously(NetworkService.FACTORY_LINK, networkState, NetworkState.class);
}
Also used : NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Aggregations

NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)43 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)23 Operation (com.vmware.xenon.common.Operation)15 URI (java.net.URI)14 ArrayList (java.util.ArrayList)13 NetworkService (com.vmware.photon.controller.model.resources.NetworkService)10 HashMap (java.util.HashMap)10 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)9 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 SubnetService (com.vmware.photon.controller.model.resources.SubnetService)9 UriUtils (com.vmware.xenon.common.UriUtils)9 Query (com.vmware.xenon.services.common.QueryTask.Query)9 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)8 HashSet (java.util.HashSet)8 List (java.util.List)8 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)7 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)7 DeferredResult (com.vmware.xenon.common.DeferredResult)7 StatelessService (com.vmware.xenon.common.StatelessService)7 Map (java.util.Map)7