use of com.microsoft.azure.management.network.implementation.VirtualNetworkInner in project photon-model by vmware.
the class AzureInstanceService method createNetwork.
private void createNetwork(AzureInstanceContext ctx, AzureInstanceStage nextStage, List<SubnetInner> subnetsToCreate) {
// All NICs MUST be at the same vNet (no cross vNet VMs),
// so we select the Primary vNet.
final AzureNicContext primaryNic = ctx.getPrimaryNic();
final VirtualNetworkInner vNetToCreate = newAzureVirtualNetwork(ctx, primaryNic, subnetsToCreate);
final String vNetName = primaryNic.networkState.name;
final String vNetRGName = primaryNic.networkRGState != null ? primaryNic.networkRGState.name : ctx.resourceGroup.name();
VirtualNetworksInner azureClient = getNetworkManagementClientImpl(ctx).virtualNetworks();
final String subnetNames = vNetToCreate.subnets().stream().map(SubnetInner::name).collect(Collectors.joining(","));
final String msg = "Creating Azure vNet-Subnet [v=" + vNetName + "; s=" + subnetNames + "] for [" + ctx.vmName + "] VM";
AzureProvisioningCallbackWithRetry<VirtualNetworkInner> handler = new AzureProvisioningCallbackWithRetry<VirtualNetworkInner>(this, msg) {
@Override
protected DeferredResult<VirtualNetworkInner> consumeProvisioningSuccess(VirtualNetworkInner vNet) {
// Populate NICs with Azure Subnet
for (AzureNicContext nicCtx : ctx.nics) {
if (nicCtx.subnet == null) {
nicCtx.subnet = vNet.subnets().stream().filter(subnet -> subnet.name().equals(nicCtx.subnetState.name)).findFirst().get();
}
}
return DeferredResult.completed(vNet);
}
@Override
protected String getProvisioningState(VirtualNetworkInner vNet) {
// or PROVISIONING_STATE_SUCCEEDED if all are Succeeded
if (vNet.subnets().size() == 0) {
return PROVISIONING_STATE_FAILED_NO_SUBNET;
}
String subnetPS = vNet.subnets().stream().map(SubnetInner::provisioningState).filter(ps -> !PROVISIONING_STATE_SUCCEEDED.equalsIgnoreCase(ps)).findFirst().orElse(PROVISIONING_STATE_SUCCEEDED);
if (PROVISIONING_STATE_SUCCEEDED.equalsIgnoreCase(vNet.provisioningState()) && PROVISIONING_STATE_SUCCEEDED.equalsIgnoreCase(subnetPS)) {
return PROVISIONING_STATE_SUCCEEDED;
}
return vNet.provisioningState() + ":" + subnetPS;
}
@Override
protected Runnable checkProvisioningStateCall(ServiceCallback<VirtualNetworkInner> checkProvisioningStateCallback) {
return () -> azureClient.getByResourceGroupAsync(vNetRGName, vNetName, null, /* expand */
checkProvisioningStateCallback);
}
@Override
protected Runnable retryServiceCall(ServiceCallback<VirtualNetworkInner> retryCallback) {
return () -> azureClient.createOrUpdateAsync(vNetRGName, vNetName, vNetToCreate, retryCallback);
}
};
azureClient.createOrUpdateAsync(vNetRGName, vNetName, vNetToCreate, handler);
handler.toDeferredResult().thenApply(ignore -> ctx).whenComplete(thenAllocation(ctx, nextStage, NETWORK_NAMESPACE));
}
use of com.microsoft.azure.management.network.implementation.VirtualNetworkInner in project photon-model by vmware.
the class AzureInstanceService method newAzureVirtualNetwork.
/**
* Converts Photon model constructs to underlying Azure VirtualNetwork model.
*/
private VirtualNetworkInner newAzureVirtualNetwork(AzureInstanceContext ctx, AzureNicContext nicCtx, List<SubnetInner> subnetsToCreate) {
VirtualNetworkInner vNet = new VirtualNetworkInner();
vNet.withLocation(ctx.resourceGroup.location());
// Azure's custom serializers don't handle well collections constructed with
// Collections.singletonList(), so initialize an ArrayList
List<String> prefix = new ArrayList<>();
prefix.add(nicCtx.networkState.subnetCIDR);
AddressSpace addressSpace = new AddressSpace().withAddressPrefixes(prefix);
vNet.withAddressSpace(addressSpace);
vNet.withSubnets(subnetsToCreate);
return vNet;
}
use of com.microsoft.azure.management.network.implementation.VirtualNetworkInner in project photon-model by vmware.
the class TestAzureProvisionTask method assertVmNetworksConfiguration.
private void assertVmNetworksConfiguration(AzureNicSpecs azureNicSpec, String vmName) throws Throwable {
// This assert is only suitable for real (non-mocking env).
if (this.isMock) {
return;
}
getHost().log(Level.INFO, "%s: Assert network configuration for [%s] VM", this.currentTestName.getMethodName(), this.vmState.name);
ComputeState vm = getHost().getServiceState(null, ComputeState.class, UriUtils.buildUri(getHost(), this.vmState.documentSelfLink));
NetworkInterfaceStateWithDescription primaryNicState = getHost().getServiceState(null, NetworkInterfaceStateWithDescription.class, NetworkInterfaceStateWithDescription.buildUri(UriUtils.buildUri(getHost(), vm.networkInterfaceLinks.get(0))));
// In case that private ip is set explicitly.
assertStaticPrivateIPAddress(azureNicSpec, primaryNicState.address);
assertNotNull("Primary NIC private IP should be set.", primaryNicState.address);
if (primaryNicState.description.assignPublicIpAddress == null || primaryNicState.description.assignPublicIpAddress == Boolean.TRUE) {
assertNotNull("VM address should be set.", vm.address);
assertNotEquals("VM address should not be the same as primary NIC private IP.", vm.address, primaryNicState.address);
} else {
assertNull("VM address should be empty.", vm.address);
}
assertNotNull("Primary NIC security group should be set.", primaryNicState.securityGroupLinks != null);
for (int i = 1; i < vm.networkInterfaceLinks.size(); i++) {
NetworkInterfaceState nonPrimaryNicState = getHost().getServiceState(null, NetworkInterfaceState.class, UriUtils.buildUri(getHost(), vm.networkInterfaceLinks.get(i)));
assertNotNull("Non-primary NIC" + i + " IP should not be set to the privatese ip.", nonPrimaryNicState.address);
assertNull("Non-primary NIC" + i + " security group should not be set.", nonPrimaryNicState.securityGroupLinks);
}
// Ensure that from the list of provided network resource groups,
// and security group resource groups, the one with the correct type has been chosen.
// Verifying the resources can be obtained from this RG, ensures they have been placed
// correctly.
NetworkManagementClientImpl networkClient = getAzureSdkClients().getNetworkManagementClientImpl();
final String vmRGName = vm.customProperties.get(ComputeProperties.RESOURCE_GROUP_NAME);
VirtualNetworkInner provisionedNetwork = AzureTestUtil.getAzureVirtualNetwork(networkClient, vmRGName, AzureTestUtil.AZURE_NETWORK_NAME);
assertNotNull("Azure virtual network object '" + vmRGName + "/" + AzureTestUtil.AZURE_NETWORK_NAME + "' is not found.", provisionedNetwork);
final String sgName = AzureTestUtil.AZURE_SECURITY_GROUP_NAME + "-" + vmName;
NetworkSecurityGroupInner provisionedSG = AzureTestUtil.getAzureSecurityGroup(networkClient, vmRGName, sgName);
assertNotNull("Azure security group object '" + vmRGName + "/" + sgName + "' is not found.", provisionedSG);
}
use of com.microsoft.azure.management.network.implementation.VirtualNetworkInner in project photon-model by vmware.
the class TestAzureLongRunningEnumeration method tagAzureResources.
/**
* Add tags, that later should be discovered as part of first enumeration cycle.
*/
private void tagAzureResources() throws Exception {
for (int i = 0; i < numOfVMsToTest; i++) {
// tag v-Net
VirtualNetworkInner vmNetwUpdate = getAzureVirtualNetwork(this.networkManagementClient, azureVMNames.get(i), nicSpecs.get(i).network.name);
Map<String, String> vmNetwTags = new HashMap<>();
vmNetwTags.put(NETWORK_TAG_KEY_PREFIX + azureVMNames.get(i), NETWORK_TAG_VALUE);
vmNetwUpdate.withTags(vmNetwTags);
updateAzureVirtualNetwork(this.networkManagementClient, azureVMNames.get(i), nicSpecs.get(i).network.name, vmNetwUpdate);
// tag VM
VirtualMachineInner vmUpdate = getAzureVirtualMachine(this.computeManagementClient, azureVMNames.get(i), azureVMNames.get(i));
Map<String, String> vmTags = new HashMap<>();
String timeStamp = String.valueOf(Utils.getNowMicrosUtc());
vmTags.put(VM_TAG_KEY_PREFIX + azureVMNames.get(i), VM_TAG_VALUE);
vmTags.put(TIME_STAMP_TAG_KEY, timeStamp);
vmUpdate.withTags(vmTags);
updateAzureVirtualMachine(this.computeManagementClient, azureVMNames.get(i), azureVMNames.get(i), vmUpdate);
// tag Security Group
NetworkSecurityGroupInner sgUpdate = getAzureSecurityGroup(this.networkManagementClient, azureVMNames.get(i), AZURE_SECURITY_GROUP_NAME + "-" + azureVMNames.get(i));
Map<String, String> sgTags = new HashMap<>();
sgTags.put(SG_TAG_KEY_PREFIX + azureVMNames.get(i), SG_TAG_VALUE);
sgUpdate.withTags(sgTags);
sgUpdate.withLocation(AzureTestUtil.AZURE_RESOURCE_GROUP_LOCATION);
updateAzureSecurityGroup(this.networkManagementClient, azureVMNames.get(i), AZURE_SECURITY_GROUP_NAME, sgUpdate);
}
}
use of com.microsoft.azure.management.network.implementation.VirtualNetworkInner in project photon-model by vmware.
the class AzureLoadBalancerServiceTest method setUpTests.
@Before
public void setUpTests() throws Throwable {
if (computeHost == null) {
ResourcePoolState resourcePool = createDefaultResourcePool(this.host);
endpointState = this.createEndpointState();
// create a compute host for the Azure
computeHost = createDefaultComputeHost(this.host, resourcePool.documentSelfLink, endpointState);
}
AuthCredentialsServiceState azureVMAuth = new AuthCredentialsServiceState();
azureVMAuth.userEmail = AZURE_ADMIN_USERNAME;
azureVMAuth.privateKey = AZURE_ADMIN_PASSWORD;
azureVMAuth = TestUtils.doPost(host, azureVMAuth, AuthCredentialsServiceState.class, UriUtils.buildUri(host, AuthCredentialsService.FACTORY_LINK));
ComputeDescription compDesc = buildComputeDescription(host, computeHost, endpointState, azureVMAuth);
this.rgName = AzureUtils.DEFAULT_GROUP_PREFIX + compDesc.documentCreationTimeMicros / 1000;
ResourceGroupState rgState = createDefaultResourceGroupState(this.host, this.rgName, computeHost, endpointState, ResourceGroupStateType.AzureResourceGroup);
networkState = createNetworkState(rgState.documentSelfLink);
vmStates = new ArrayList<>();
for (int i = 0; i < poolSize; i++) {
ComputeState vmState = createDefaultVMResource(getHost(), this.rgName + "VM" + i, computeHost, endpointState, NO_PUBLIC_IP_NIC_SPEC, null, 0, compDesc, this.rgName);
vmStates.add(vmState);
}
subnetState = createSubnetState(this.subnetName);
if (!this.isMock) {
this.loadBalancerClient = getAzureSdkClients().getNetworkManagementClientImpl().loadBalancers();
this.rgOpsClient = getAzureSdkClients().getResourceManagementClientImpl().resourceGroups();
ResourceGroupInner rg = new ResourceGroupInner();
rg.withName(this.rgName);
rg.withLocation(this.regionId);
this.rgOpsClient.createOrUpdate(this.rgName, rg);
VirtualNetworkInner vNet = new VirtualNetworkInner();
AddressSpace addressSpace = new AddressSpace();
addressSpace.withAddressPrefixes(Collections.singletonList(AZURE_DEFAULT_VPC_CIDR));
vNet.withAddressSpace(addressSpace);
vNet.withLocation(this.regionId);
VirtualNetworksInner vNetClient = getAzureSdkClients().getNetworkManagementClientImpl().virtualNetworks();
vNetClient.createOrUpdate(this.rgName, this.vNetName, vNet);
kickOffComputeProvision();
kickOffSubnetProvision(InstanceRequestType.CREATE, subnetState, TaskStage.FINISHED);
}
}
Aggregations