Network network = null;
Set<? extends NodeMetadata> nodes = null;
try {
assert view.getComputeService().listAssignableLocations().size() > 0;
Template template = view.getComputeService().templateBuilder().build();
// get the zone we are launching into
String zoneId = template.getLocation().getId();
// cleanup before running the test
deleteNetworksInZoneWithVlanId(zoneId, vlanId);
// find a network offering that supports vlans in our zone
String offeringId = get(
cloudStackContext.getApi().getOfferingClient().listNetworkOfferings(specifyVLAN(true).zoneId(zoneId)), 0).getId();
// create an arbitrary network
network = domainAdminClient
.getNetworkClient()
// startIP/endIP/netmask/gateway must be specified together
.createNetworkInZone(zoneId, offeringId, group, group,
vlan(vlanId).startIP("192.168.1.2").netmask("255.255.255.0").gateway("192.168.1.1"));
// set options to specify this network id
template.getOptions().as(CloudStackTemplateOptions.class).networkId(network.getId());
// launch the VM
nodes = view.getComputeService().createNodesInGroup(group, 1, template);
assert nodes.size() > 0;