Search in sources :

Example 6 with NetworkInterface

use of com.microsoft.azure.management.network.NetworkInterface in project azure-sdk-for-java by Azure.

the class LoadBalancerBackendImpl method getVirtualMachineIds.

@Override
public Set<String> getVirtualMachineIds() {
    Set<String> vmIds = new HashSet<>();
    Map<String, String> nicConfigs = this.backendNicIPConfigurationNames();
    if (nicConfigs != null) {
        for (String nicId : nicConfigs.keySet()) {
            try {
                NetworkInterface nic = this.parent().manager().networkInterfaces().getById(nicId);
                if (nic == null || nic.virtualMachineId() == null) {
                    continue;
                } else {
                    vmIds.add(nic.virtualMachineId());
                }
            } catch (CloudException | IllegalArgumentException e) {
                continue;
            }
        }
    }
    return vmIds;
}
Also used : NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) CloudException(com.microsoft.azure.CloudException) HashSet(java.util.HashSet)

Example 7 with NetworkInterface

use of com.microsoft.azure.management.network.NetworkInterface in project azure-sdk-for-java by Azure.

the class VirtualMachineImpl method handleNetworkSettings.

private void handleNetworkSettings() {
    if (isInCreateMode()) {
        NetworkInterface primaryNetworkInterface = null;
        if (this.creatablePrimaryNetworkInterfaceKey != null) {
            primaryNetworkInterface = (NetworkInterface) this.createdResource(this.creatablePrimaryNetworkInterfaceKey);
        } else if (this.existingPrimaryNetworkInterfaceToAssociate != null) {
            primaryNetworkInterface = this.existingPrimaryNetworkInterfaceToAssociate;
        }
        if (primaryNetworkInterface != null) {
            NetworkInterfaceReferenceInner nicReference = new NetworkInterfaceReferenceInner();
            nicReference.withPrimary(true);
            nicReference.withId(primaryNetworkInterface.id());
            this.inner().networkProfile().networkInterfaces().add(nicReference);
        }
    }
    //
    for (String creatableSecondaryNetworkInterfaceKey : this.creatableSecondaryNetworkInterfaceKeys) {
        NetworkInterface secondaryNetworkInterface = (NetworkInterface) this.createdResource(creatableSecondaryNetworkInterfaceKey);
        NetworkInterfaceReferenceInner nicReference = new NetworkInterfaceReferenceInner();
        nicReference.withPrimary(false);
        nicReference.withId(secondaryNetworkInterface.id());
        this.inner().networkProfile().networkInterfaces().add(nicReference);
    }
    for (NetworkInterface secondaryNetworkInterface : this.existingSecondaryNetworkInterfacesToAssociate) {
        NetworkInterfaceReferenceInner nicReference = new NetworkInterfaceReferenceInner();
        nicReference.withPrimary(false);
        nicReference.withId(secondaryNetworkInterface.id());
        this.inner().networkProfile().networkInterfaces().add(nicReference);
    }
}
Also used : NetworkInterface(com.microsoft.azure.management.network.NetworkInterface)

Example 8 with NetworkInterface

use of com.microsoft.azure.management.network.NetworkInterface in project azure-sdk-for-java by Azure.

the class TestNSG method createResource.

@Override
public NetworkSecurityGroup createResource(NetworkSecurityGroups nsgs) throws Exception {
    final String newName = "nsg" + this.testId;
    final String resourceGroupName = "rg" + this.testId;
    final String nicName = "nic" + this.testId;
    final Region region = Region.US_WEST;
    final SettableFuture<NetworkSecurityGroup> nsgFuture = SettableFuture.create();
    // Create
    Observable<Indexable> resourceStream = nsgs.define(newName).withRegion(region).withNewResourceGroup(resourceGroupName).defineRule("rule1").allowOutbound().fromAnyAddress().fromPort(80).toAnyAddress().toPort(80).withProtocol(SecurityRuleProtocol.TCP).attach().defineRule("rule2").allowInbound().fromAnyAddress().fromAnyPort().toAnyAddress().toPortRange(22, 25).withAnyProtocol().withPriority(200).withDescription("foo!!").attach().createAsync();
    Utils.<NetworkSecurityGroup>rootResource(resourceStream).subscribe(new Subscriber<NetworkSecurityGroup>() {

        @Override
        public void onCompleted() {
            System.out.print("completed");
        }

        @Override
        public void onError(Throwable throwable) {
            nsgFuture.setException(throwable);
        }

        @Override
        public void onNext(NetworkSecurityGroup networkSecurityGroup) {
            nsgFuture.set(networkSecurityGroup);
        }
    });
    NetworkSecurityGroup nsg = nsgFuture.get();
    NetworkInterface nic = nsgs.manager().networkInterfaces().define(nicName).withRegion(region).withExistingResourceGroup(resourceGroupName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withExistingNetworkSecurityGroup(nsg).create();
    nsg.refresh();
    // Verify
    Assert.assertTrue(nsg.region().equals(region));
    Assert.assertTrue(nsg.securityRules().size() == 2);
    // Confirm NIC association
    Assert.assertEquals(1, nsg.networkInterfaceIds().size());
    Assert.assertTrue(nsg.networkInterfaceIds().contains(nic.id()));
    return nsg;
}
Also used : NetworkSecurityGroup(com.microsoft.azure.management.network.NetworkSecurityGroup) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) Indexable(com.microsoft.azure.management.resources.fluentcore.model.Indexable)

Example 9 with NetworkInterface

use of com.microsoft.azure.management.network.NetworkInterface in project azure-sdk-for-java by Azure.

the class TestVirtualMachineNics method createResource.

@Override
public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception {
    // Prepare the resource group definition
    final String rgName = "rg" + this.testId;
    Creatable<ResourceGroup> resourceGroupCreatable = virtualMachines.manager().resourceManager().resourceGroups().define(rgName).withRegion(Region.US_EAST);
    // Prepare the virtual network definition [shared by primary and secondary network interfaces]
    final String vnetName = "vnet" + this.testId;
    Creatable<Network> networkCreatable = this.networkManager.networks().define(vnetName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withAddressSpace("10.0.0.0/28");
    // Prepare the secondary network interface definition
    final String secondaryNicName = "nic" + this.testId;
    Creatable<NetworkInterface> secondaryNetworkInterfaceCreatable = this.networkManager.networkInterfaces().define(secondaryNicName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.5");
    // .withNewPrimaryPublicIPAddress();
    // [Secondary NIC cannot have PublicIP - Only primary network interface can reference a public IP address]
    // Prepare the secondary network interface definition
    final String secondaryNicName2 = "nic2" + this.testId;
    Creatable<NetworkInterface> secondaryNetworkInterfaceCreatable2 = this.networkManager.networkInterfaces().define(secondaryNicName2).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.6");
    // Create Virtual Machine
    final String vmName = "vm" + this.testId;
    final String primaryPipName = "pip" + vmName;
    VirtualMachine virtualMachine = virtualMachines.define(vmName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.4").withNewPrimaryPublicIPAddress(primaryPipName).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_14_04_LTS).withRootUsername("testuser").withRootPassword("12NewPA$$w0rd!").withSize(VirtualMachineSizeTypes.STANDARD_A9).withNewSecondaryNetworkInterface(secondaryNetworkInterfaceCreatable).withNewSecondaryNetworkInterface(secondaryNetworkInterfaceCreatable2).create();
    Assert.assertTrue(virtualMachine.networkInterfaceIds().size() == 3);
    NetworkInterface primaryNetworkInterface = virtualMachine.getPrimaryNetworkInterface();
    Assert.assertEquals(primaryNetworkInterface.primaryPrivateIP(), "10.0.0.4");
    PublicIPAddress primaryPublicIPAddress = primaryNetworkInterface.primaryIPConfiguration().getPublicIPAddress();
    Assert.assertTrue(primaryPublicIPAddress.fqdn().startsWith(primaryPipName));
    return virtualMachine;
}
Also used : Network(com.microsoft.azure.management.network.Network) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 10 with NetworkInterface

use of com.microsoft.azure.management.network.NetworkInterface in project azure-sdk-for-java by Azure.

the class TestPublicIPAddress method printPIP.

public static void printPIP(PublicIPAddress resource) {
    StringBuilder info = new StringBuilder().append("Public IP Address: ").append(resource.id()).append("\n\tName: ").append(resource.name()).append("\n\tResource group: ").append(resource.resourceGroupName()).append("\n\tRegion: ").append(resource.region()).append("\n\tTags: ").append(resource.tags()).append("\n\tIP Address: ").append(resource.ipAddress()).append("\n\tLeaf domain label: ").append(resource.leafDomainLabel()).append("\n\tFQDN: ").append(resource.fqdn()).append("\n\tReverse FQDN: ").append(resource.reverseFqdn()).append("\n\tIdle timeout (minutes): ").append(resource.idleTimeoutInMinutes()).append("\n\tIP allocation method: ").append(resource.ipAllocationMethod().toString()).append("\n\tIP version: ").append(resource.version().toString());
    // Show the associated load balancer if any
    info.append("\n\tLoad balancer association: ");
    if (resource.hasAssignedLoadBalancer()) {
        final LoadBalancerPublicFrontend frontend = resource.getAssignedLoadBalancerFrontend();
        final LoadBalancer lb = frontend.parent();
        info.append("\n\t\tLoad balancer ID: ").append(lb.id()).append("\n\t\tFrontend name: ").append(frontend.name());
    } else {
        info.append("(None)");
    }
    // Show the associated NIC if any
    info.append("\n\tNetwork interface association: ");
    if (resource.hasAssignedNetworkInterface()) {
        final NicIPConfiguration nicIp = resource.getAssignedNetworkInterfaceIPConfiguration();
        final NetworkInterface nic = nicIp.parent();
        info.append("\n\t\tNetwork interface ID: ").append(nic.id()).append("\n\t\tIP config name: ").append(nicIp.name());
    } else {
        info.append("(None)");
    }
    System.out.println(info.toString());
}
Also used : LoadBalancerPublicFrontend(com.microsoft.azure.management.network.LoadBalancerPublicFrontend) LoadBalancer(com.microsoft.azure.management.network.LoadBalancer) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) NicIPConfiguration(com.microsoft.azure.management.network.NicIPConfiguration)

Aggregations

NetworkInterface (com.microsoft.azure.management.network.NetworkInterface)15 Network (com.microsoft.azure.management.network.Network)7 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)6 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)5 NicIPConfiguration (com.microsoft.azure.management.network.NicIPConfiguration)4 ArrayList (java.util.ArrayList)4 LoadBalancer (com.microsoft.azure.management.network.LoadBalancer)3 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)3 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)3 Date (java.util.Date)3 CloudException (com.microsoft.azure.CloudException)2 AvailabilitySet (com.microsoft.azure.management.compute.AvailabilitySet)2 NetworkSecurityGroup (com.microsoft.azure.management.network.NetworkSecurityGroup)2 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)2 Indexable (com.microsoft.azure.management.resources.fluentcore.model.Indexable)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 LoadBalancerPublicFrontend (com.microsoft.azure.management.network.LoadBalancerPublicFrontend)1 Subnet (com.microsoft.azure.management.network.Subnet)1