use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class AzureSubnetTaskServiceTest method testCreateSubnet.
@Test
public void testCreateSubnet() throws Throwable {
SubnetState subnetState = createSubnetState(this.subnetName);
kickOffSubnetProvision(InstanceRequestType.CREATE, subnetState, TaskStage.FINISHED);
subnetState = getServiceSynchronously(subnetState.documentSelfLink, SubnetState.class);
assertNotNull(subnetState.id);
assertNotEquals(subnetState.id, this.subnetName);
assertEquals(LifecycleState.READY, subnetState.lifecycleState);
if (!this.isMock) {
// Verify that the subnet was deleted.
SubnetInner subnetResponse = this.subnetsClient.get(this.rgName, this.vNetName, this.subnetName, null);
assertEquals(this.subnetName, subnetResponse.name());
}
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class AzureSubnetTaskServiceTest method testDeleteSubnet.
@Test
public void testDeleteSubnet() throws Throwable {
SubnetInner azureSubnet = createAzureSubnet();
SubnetState subnetState = createSubnetState(azureSubnet.id());
kickOffSubnetProvision(InstanceRequestType.DELETE, subnetState, TaskStage.FINISHED);
if (!this.isMock) {
// Verify that the subnet was deleted.
SubnetInner subnetInner = this.subnetsClient.get(this.rgName, this.vNetName, this.subnetName, null);
if (subnetInner != null) {
fail("Subnet should not exist in Azure.");
}
}
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState 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;
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class VSphereVirtualMachineEnumerationHelper method addNewInterfaceState.
/**
* Add new interface state to compute network interface links
* @param service
* @param enumerationProgress Enumeration progress
* @param state Compute state
* @param id The id of distributed virtual port or opaque network
* @param mode Either using distributed virtual port or opaque network
* @param docType The document class type
* @param type The type
* @param ipAddress The primary ip address of this interface
*/
static <T> Operation addNewInterfaceState(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, ComputeState state, String id, InterfaceStateMode mode, Class<? extends ServiceDocument> docType, Class<T> type, String ipAddress) {
String fieldKey;
String fieldValue;
switch(mode) {
case INTERFACE_STATE_WITH_DISTRIBUTED_VIRTUAL_PORT:
{
fieldKey = CustomProperties.MOREF;
fieldValue = TYPE_PORTGROUP + ":" + id;
break;
}
case INTERFACE_STATE_WITH_OPAQUE_NETWORK:
{
fieldKey = NsxProperties.OPAQUE_NET_ID;
fieldValue = id;
break;
}
default:
{
service.logFine(() -> String.format("invalid mode when creating compute state with " + "network interface links: [%s]", mode));
return null;
}
}
Operation operation = queryByPortGroupIdOrByOpaqueNetworkId(service, enumerationProgress, fieldKey, fieldValue, docType).setCompletion((o, e) -> {
if (e != null) {
service.logWarning(() -> String.format("Error processing queryByPortGroupIdOrByOpaqueNetworkId for id: [%s]," + "error: [%s]", fieldValue, e.toString()));
return;
}
QueryTask task = o.getBody(QueryTask.class);
if (task.results != null && !task.results.documentLinks.isEmpty()) {
T netState = VsphereEnumerationHelper.convertOnlyResultToDocument(task.results, type);
NetworkInterfaceState iface = null;
if (netState instanceof SubnetState) {
SubnetState subnetState = (SubnetState) netState;
iface = createNewInterfaceState(service, id, subnetState.networkLink, subnetState.documentSelfLink, ipAddress);
} else if (netState instanceof NetworkState) {
NetworkState networkState = (NetworkState) netState;
iface = createNewInterfaceState(service, id, null, networkState.documentSelfLink, ipAddress);
}
if (iface != null) {
state.networkInterfaceLinks.add(iface.documentSelfLink);
}
} else {
service.logFine(() -> String.format("Will not add nic with id: [%s]", fieldValue));
}
});
return operation;
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class VSphereNetworkEnumerationHelper method processFoundNetwork.
public static void processFoundNetwork(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, NetworkOverlay net, MoRefKeyedMap<NetworkOverlay> allNetworks) {
if (net.getParentSwitch() != null) {
// portgroup: create subnet
QueryTask task = queryForSubnet(enumerationProgress, net, net.getParentSwitch());
withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
if (result.documentLinks.isEmpty()) {
createNewSubnet(service, enumerationProgress, net, allNetworks.get(net.getParentSwitch()).getDvsUuid());
} else {
SubnetState oldDocument = convertOnlyResultToDocument(result, SubnetState.class);
updateSubnet(service, oldDocument, enumerationProgress, net, true);
}
});
} else {
// DVS or opaque network
QueryTask task = queryForNetwork(enumerationProgress, net);
withTaskResults(service, task, result -> {
if (result.documentLinks.isEmpty()) {
createNewNetwork(service, enumerationProgress, net);
} else {
NetworkState oldDocument = convertOnlyResultToDocument(result, NetworkState.class);
updateNetwork(service, oldDocument, enumerationProgress, net);
}
});
}
}
Aggregations