com.microsoft.azure.management.network.Subnet Java Examples

The following examples show how to use com.microsoft.azure.management.network.Subnet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AzureUtils.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public void validateSubnet(AzureClient client, Network network) {
    if (isExistingNetwork(network)) {
        String resourceGroupName = getCustomResourceGroupName(network);
        String networkId = getCustomNetworkId(network);
        Collection<String> subnetIds = getCustomSubnetIds(network);
        for (String subnetId : subnetIds) {
            try {
                Subnet subnet = client.getSubnetProperties(resourceGroupName, networkId, subnetId);
                if (subnet == null) {
                    throw new CloudConnectorException(
                            String.format("Subnet [%s] is not found in resource group [%s] and network [%s]", subnetId, resourceGroupName, networkId)
                    );
                }
            } catch (RuntimeException e) {
                throw new CloudConnectorException("Subnet validation failed, cause: " + e.getMessage(), e);
            }
        }
    }
}
 
Example #2
Source File: NetworkManager.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
List<Subnet> listAssociatedSubnets(List<SubnetInner> subnetRefs) {
    final Map<String, Network> networks = new HashMap<>();
    final List<Subnet> subnets = new ArrayList<>();

    if (subnetRefs != null) {
        for (SubnetInner subnetRef : subnetRefs) {
            String networkId = ResourceUtils.parentResourceIdFromResourceId(subnetRef.id());
            Network network = networks.get(networkId.toLowerCase());
            if (network == null) {
                network = this.networks().getById(networkId);
                networks.put(networkId.toLowerCase(), network);
            }

            String subnetName = ResourceUtils.nameFromResourceId(subnetRef.id());
            subnets.add(network.subnets().get(subnetName));
        }
    }

    return Collections.unmodifiableList(subnets);
}
 
Example #3
Source File: NetworkManager.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
Subnet getAssociatedSubnet(SubResource subnetRef) {
    if (subnetRef == null) {
        return null;
    }

    String vnetId = ResourceUtils.parentResourceIdFromResourceId(subnetRef.id());
    String subnetName = ResourceUtils.nameFromResourceId(subnetRef.id());

    if (vnetId == null || subnetName == null) {
        return null;
    }

    Network network = this.networks().getById(vnetId);
    if (network == null) {
        return null;
    }

    return network.subnets().get(subnetName);
}
 
Example #4
Source File: NicIPConfigurationBaseImpl.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
@Override
public NetworkSecurityGroup getNetworkSecurityGroup() {
    Network network = this.getNetwork();
    if (network == null) {
        return null;
    }

    String subnetName = this.subnetName();
    if (subnetName == null) {
        return null;
    }

    Subnet subnet = network.subnets().get(subnetName);
    if (subnet == null) {
        return null;
    }

    return subnet.getNetworkSecurityGroup();
}
 
Example #5
Source File: AzureStackViewProvider.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private long getAvailableAddresses(Subnet subnet) {
    SubnetUtils su = new SubnetUtils(subnet.addressPrefix());
    su.setInclusiveHostCount(true);
    long available = su.getInfo().getAddressCountLong();
    long used = subnet.networkInterfaceIPConfigurationCount();
    return available - used - AZURE_NUMBER_OF_RESERVED_IPS;
}
 
Example #6
Source File: RouteTableInventoryCollector.java    From pacbot with Apache License 2.0 5 votes vote down vote up
private List<RouteTableSubnet> getNetworkSecuritySubnetDetails(List<Subnet> subnetList) {
	List<RouteTableSubnet> subnetVHlist = new ArrayList<>();
	for (Subnet subnet : subnetList) {
		RouteTableSubnet routeTableSubnet = new RouteTableSubnet();
		routeTableSubnet.setAddressPrefix(subnet.addressPrefix());
		routeTableSubnet.setName(subnet.name());
		routeTableSubnet.setVnet(subnet.parent().id());
		subnetVHlist.add(routeTableSubnet);

	}
	return subnetVHlist;

}
 
Example #7
Source File: AzureStackViewProvider.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Map<String, Long> getNumberOfAvailableIPsInSubnets(AzureClient client, Network network) {
    Map<String, Long> result = new HashMap<>();
    String resourceGroup = network.getStringParameter("resourceGroupName");
    String networkId = network.getStringParameter("networkId");
    Collection<String> subnetIds = azureUtils.getCustomSubnetIds(network);
    for (String subnetId : subnetIds) {
        Subnet subnet = client.getSubnetProperties(resourceGroup, networkId, subnetId);
        long available = getAvailableAddresses(subnet);
        result.put(subnetId, available);
    }
    return result;
}
 
Example #8
Source File: AzurePlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private CloudNetwork convertToCloudNetwork(Network network) {
    Set<CloudSubnet> subnets = new HashSet<>();
    for (Entry<String, Subnet> subnet : network.subnets().entrySet()) {
        subnets.add(new CloudSubnet(subnet.getKey(), subnet.getKey(), null, subnet.getValue().addressPrefix()));
    }
    Map<String, Object> properties = new HashMap<>();
    properties.put("addressSpaces", network.addressSpaces());
    properties.put("dnsServerIPs", network.dnsServerIPs());
    properties.put("resourceGroupName", network.resourceGroupName());

    return new CloudNetwork(network.name(), network.id(), subnets, properties);
}
 
Example #9
Source File: TestRouteTables.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
/**
 * Outputs info about a route table
 * @param resource a route table
 */
public static void printRouteTable(RouteTable resource) {
    StringBuilder info = new StringBuilder();
    info.append("Route table: ").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());

    // Output routes
    Map<String, Route> routes = resource.routes();
    info.append("\n\tRoutes: ").append(routes.values().size());
    for (Route route : routes.values()) {
        info.append("\n\t\tName: ").append(route.name())
            .append("\n\t\t\tDestination address prefix: ").append(route.destinationAddressPrefix())
            .append("\n\t\t\tNext hop type: ").append(route.nextHopType().toString())
            .append("\n\t\t\tNext hop IP address: ").append(route.nextHopIPAddress());
    }

    // Output associated subnets
    List<Subnet> subnets = resource.listAssociatedSubnets();
    info.append("\n\tAssociated subnets: ").append(subnets.size());
    for (Subnet subnet : subnets) {
        info.append("\n\t\tResource group: ").append(subnet.parent().resourceGroupName())
            .append("\n\t\tNetwork name: ").append(subnet.parent().name())
            .append("\n\t\tSubnet name: ").append(subnet.name())
            .append("\n\tSubnet's route table ID: ").append(subnet.routeTableId());
    }
    info.append("\n\tDisable BGP route propagation: ").append(resource.isBgpRoutePropagationDisabled());
    System.out.println(info.toString());
}
 
Example #10
Source File: NSGInventoryCollector.java    From pacbot with Apache License 2.0 5 votes vote down vote up
private List<NSGSubnet> getNetworkSecuritySubnetDetails(List<Subnet> subnetList) {
	List<NSGSubnet> subnetVHlist = new ArrayList<>();
	for (Subnet subnet : subnetList) {
		NSGSubnet subnetVH = new NSGSubnet();
		subnetVH.setAddressPrefix(subnet.addressPrefix());
		subnetVH.setName(subnet.name());
		subnetVH.setVnet(subnet.parent().id());
		subnetVHlist.add(subnetVH);

	}
	return subnetVHlist;

}
 
Example #11
Source File: TestNetwork.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public Network createResource(Networks networks) throws Exception {
    final String newName = "net" + this.testId;
    Region region = Region.US_WEST;
    String groupName = "rg" + this.testId;


    // Create a network
    final Network network = networks.define(newName)
            .withRegion(region)
            .withNewResourceGroup(groupName)
            .withAddressSpace("10.0.0.0/28")
            .withSubnet("subnetA", "10.0.0.0/29")
            .defineSubnet("subnetB")
                .withAddressPrefix("10.0.0.8/29")
                .withAccessFromService(ServiceEndpointType.MICROSOFT_STORAGE)
                .attach()
            .create();

    // Verify address spaces
    Assert.assertEquals(1, network.addressSpaces().size());
    Assert.assertTrue(network.addressSpaces().contains("10.0.0.0/28"));

    // Verify subnets
    Assert.assertEquals(2, network.subnets().size());
    Subnet subnet = network.subnets().get("subnetA");
    Assert.assertEquals("10.0.0.0/29", subnet.addressPrefix());

    subnet = network.subnets().get("subnetB");
    Assert.assertEquals("10.0.0.8/29", subnet.addressPrefix());
    Assert.assertNotNull(subnet.servicesWithAccess());
    Assert.assertTrue(subnet.servicesWithAccess().containsKey(ServiceEndpointType.MICROSOFT_STORAGE));
    Assert.assertTrue(subnet.servicesWithAccess().get(ServiceEndpointType.MICROSOFT_STORAGE).size() > 0);
    return network;
}
 
Example #12
Source File: LoadBalancerFrontendImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Subnet getSubnet() {
    return this.parent().manager().getAssociatedSubnet(this.inner().subnet());
}
 
Example #13
Source File: NetworkVH.java    From pacbot with Apache License 2.0 4 votes vote down vote up
public Map<String, Subnet> getSubnets() {
	return subnets;
}
 
Example #14
Source File: NetworkVH.java    From pacbot with Apache License 2.0 4 votes vote down vote up
public void setSubnets(Map<String, Subnet> subnets) {
	this.subnets = subnets;
}
 
Example #15
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public Map<String, Subnet> getSubnets(String resourceGroup, String virtualNetwork) {
    return handleAuthException(() -> {
        Network network = getNetworkByResourceGroup(resourceGroup, virtualNetwork);
        return network == null ? emptyMap() : network.subnets();
    });
}
 
Example #16
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public Subnet getSubnetProperties(String resourceGroup, String virtualNetwork, String subnet) {
    return handleAuthException(() -> {
        Network networkByResourceGroup = getNetworkByResourceGroup(resourceGroup, virtualNetwork);
        return networkByResourceGroup == null ? null : networkByResourceGroup.subnets().get(subnet);
    });
}
 
Example #17
Source File: VirtualMachineOperationsTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
public void canCreateVirtualMachineWithNetworking() throws Exception {
    NetworkSecurityGroup nsg = this.networkManager.networkSecurityGroups().define("nsg")
        .withRegion(REGION)
        .withNewResourceGroup(RG_NAME)
        .defineRule("rule1")
            .allowInbound()
            .fromAnyAddress()
            .fromPort(80)
            .toAnyAddress()
            .toPort(80)
            .withProtocol(SecurityRuleProtocol.TCP)
            .attach()
        .create();

    Creatable<Network> networkDefinition = this.networkManager.networks().define("network1")
        .withRegion(REGION)
        .withNewResourceGroup(RG_NAME)
        .withAddressSpace("10.0.0.0/28")
        .defineSubnet("subnet1")
            .withAddressPrefix("10.0.0.0/29")
            .withExistingNetworkSecurityGroup(nsg)
            .attach();

    // Create
    VirtualMachine vm = computeManager.virtualMachines()
        .define(VMNAME)
            .withRegion(REGION)
            .withNewResourceGroup(RG_NAME)
            .withNewPrimaryNetwork(networkDefinition)
            .withPrimaryPrivateIPAddressDynamic()
            .withoutPrimaryPublicIPAddress()
            .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
            .withRootUsername("Foo12")
            .withRootPassword("abc!@#F0orL")
        .create();

    NetworkInterface primaryNic = vm.getPrimaryNetworkInterface();
    Assert.assertNotNull(primaryNic);
    NicIPConfiguration primaryIpConfig = primaryNic.primaryIPConfiguration();
    Assert.assertNotNull(primaryIpConfig);

    // Fetch the NSG the way before v1.2
    Assert.assertNotNull(primaryIpConfig.networkId());
    Network network = primaryIpConfig.getNetwork();
    Assert.assertNotNull(primaryIpConfig.subnetName());
    Subnet subnet = network.subnets().get(primaryIpConfig.subnetName());
    Assert.assertNotNull(subnet);
    nsg = subnet.getNetworkSecurityGroup();
    Assert.assertNotNull(nsg);
    Assert.assertEquals("nsg", nsg.name());
    Assert.assertEquals(1, nsg.securityRules().size());

    // Fetch the NSG the v1.2 way
    nsg = primaryIpConfig.getNetworkSecurityGroup();
    Assert.assertEquals("nsg", nsg.name());
}
 
Example #18
Source File: LoadBalancerInboundNatPoolImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public LoadBalancerInboundNatPoolImpl fromExistingSubnet(Subnet subnet) {
    return (null != subnet)
            ? this.fromExistingSubnet(subnet.parent().id(), subnet.name())
            : this;
}
 
Example #19
Source File: LoadBalancerInboundNatRuleImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public LoadBalancerInboundNatRuleImpl fromExistingSubnet(Subnet subnet) {
    return (null != subnet)
            ? this.fromExistingSubnet(subnet.parent().id(), subnet.name())
            : this;
}
 
Example #20
Source File: ApplicationGatewayIPConfigurationImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Subnet getSubnet() {
    return this.parent().manager().getAssociatedSubnet(this.inner().subnet());
}
 
Example #21
Source File: ApplicationGatewayIPConfigurationImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public ApplicationGatewayIPConfigurationImpl withExistingSubnet(Subnet subnet) {
    return this.withExistingSubnet(subnet.parent().id(), subnet.name());
}
 
Example #22
Source File: ApplicationGatewayFrontendImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Subnet getSubnet() {
    return this.parent().manager().getAssociatedSubnet(this.inner().subnet());
}
 
Example #23
Source File: NetworkImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Map<String, Subnet> subnets() {
    return Collections.unmodifiableMap(this.subnets);
}
 
Example #24
Source File: TestNetworkInterface.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public NetworkInterface createResource(NetworkInterfaces networkInterfaces) throws Exception {
    final String nicName = "nic" + this.testId;
    final String vnetName = "net" + this.testId;
    final String pipName = "pip" + this.testId;
    final Region region = Region.US_EAST;

    Network network = networkInterfaces.manager().networks().define(vnetName)
            .withRegion(region)
            .withNewResourceGroup()
            .withAddressSpace("10.0.0.0/28")
            .withSubnet("subnet1", "10.0.0.0/29")
            .withSubnet("subnet2", "10.0.0.8/29")
            .create();

    NetworkInterface nic = networkInterfaces.define(nicName)
            .withRegion(region)
            .withExistingResourceGroup(network.resourceGroupName())
            .withExistingPrimaryNetwork(network)
            .withSubnet("subnet1")
            .withPrimaryPrivateIPAddressDynamic()
            .withNewPrimaryPublicIPAddress(pipName)
            .withIPForwarding()
            .withAcceleratedNetworking()
            .create();

    // Verify NIC settings
    Assert.assertTrue(nic.isAcceleratedNetworkingEnabled());
    Assert.assertTrue(nic.isIPForwardingEnabled());

    // Verify IP configs
    NicIPConfiguration ipConfig = nic.primaryIPConfiguration();
    Assert.assertNotNull(ipConfig);
    network = ipConfig.getNetwork();
    Assert.assertNotNull(network);
    Subnet subnet = network.subnets().get(ipConfig.subnetName());
    Assert.assertNotNull(subnet);
    Assert.assertEquals(1, subnet.networkInterfaceIPConfigurationCount());
    Collection<NicIPConfiguration> ipConfigs = subnet.listNetworkInterfaceIPConfigurations();
    Assert.assertNotNull(ipConfigs);
    Assert.assertEquals(1, ipConfigs.size());
    NicIPConfiguration ipConfig2 = null;
    for (NicIPConfiguration i : ipConfigs) {
        if (i.name().equalsIgnoreCase(ipConfig.name())) {
            ipConfig2 = i;
            break;
        }
    }
    Assert.assertNotNull(ipConfig2);
    Assert.assertTrue(ipConfig.name().equalsIgnoreCase(ipConfig2.name()));

    return nic;
}
 
Example #25
Source File: TestNetwork.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Network updateResource(Network resource) throws Exception {
    NetworkSecurityGroup nsg = resource.manager().networkSecurityGroups().define("nsgB" + this.testId)
            .withRegion(resource.region())
            .withExistingResourceGroup(resource.resourceGroupName())
            .create();

    resource =  resource.update()
            .withTag("tag1", "value1")
            .withTag("tag2", "value2")
            .withAddressSpace("141.25.0.0/16")
            .withoutAddressSpace("10.1.0.0/28")
            .withSubnet("subnetC", "141.25.0.0/29")
            .withoutSubnet("subnetA")
            .updateSubnet("subnetB")
                .withAddressPrefix("141.25.0.8/29")
                .withoutNetworkSecurityGroup()
                .parent()
            .defineSubnet("subnetD")
                .withAddressPrefix("141.25.0.16/29")
                .withExistingNetworkSecurityGroup(nsg)
                .attach()
            .apply();
    Assert.assertTrue(resource.tags().containsKey("tag1"));

    // Verify address spaces
    Assert.assertEquals(2, resource.addressSpaces().size());
    Assert.assertFalse(resource.addressSpaces().contains("10.1.0.0/28"));

    // Verify subnets
    Assert.assertEquals(3, resource.subnets().size());
    Assert.assertFalse(resource.subnets().containsKey("subnetA"));

    Subnet subnet = resource.subnets().get("subnetB");
    Assert.assertNotNull(subnet);
    Assert.assertEquals("141.25.0.8/29", subnet.addressPrefix());
    Assert.assertNull(subnet.networkSecurityGroupId());

    subnet = resource.subnets().get("subnetC");
    Assert.assertNotNull(subnet);
    Assert.assertEquals("141.25.0.0/29", subnet.addressPrefix());
    Assert.assertNull(subnet.networkSecurityGroupId());

    subnet = resource.subnets().get("subnetD");
    Assert.assertNotNull(subnet);
    Assert.assertEquals("141.25.0.16/29", subnet.addressPrefix());
    Assert.assertTrue(nsg.id().equalsIgnoreCase(subnet.networkSecurityGroupId()));

    return resource;
}
 
Example #26
Source File: RouteTableImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public List<Subnet> listAssociatedSubnets() {
    return this.myManager.listAssociatedSubnets(this.inner().subnets());
}
 
Example #27
Source File: TestNetwork.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Network updateResource(Network resource) throws Exception {
    NetworkPeering localPeering = resource.peerings().list().get(0);

    // Verify remote IP invisibility to local network before peering
    Network remoteNetwork = localPeering.getRemoteNetwork();
    Assert.assertNotNull(remoteNetwork);
    Subnet remoteSubnet = remoteNetwork.subnets().get("subnet3");
    Assert.assertNotNull(remoteSubnet);
    Set<String> remoteAvailableIPs = remoteSubnet.listAvailablePrivateIPAddresses();
    Assert.assertNotNull(remoteAvailableIPs);
    Assert.assertFalse(remoteAvailableIPs.isEmpty());
    String remoteTestIP = remoteAvailableIPs.iterator().next();
    Assert.assertFalse(resource.isPrivateIPAddressAvailable(remoteTestIP));

    localPeering.update()
        .withoutTrafficForwardingFromEitherNetwork()
        .withAccessBetweenBothNetworks()
        .withoutAnyGatewayUse()
        .apply();

    // Verify local peering changes
    Assert.assertFalse(localPeering.isTrafficForwardingFromRemoteNetworkAllowed());
    Assert.assertTrue(localPeering.checkAccessBetweenNetworks());
    Assert.assertEquals(NetworkPeeringGatewayUse.NONE, localPeering.gatewayUse());

    // Verify remote peering changes
    NetworkPeering remotePeering = localPeering.getRemotePeering();
    Assert.assertNotNull(remotePeering);
    Assert.assertFalse(remotePeering.isTrafficForwardingFromRemoteNetworkAllowed());
    Assert.assertTrue(remotePeering.checkAccessBetweenNetworks());
    Assert.assertEquals(NetworkPeeringGatewayUse.NONE, remotePeering.gatewayUse());

    // Delete the peering
    resource.peerings().deleteById(remotePeering.id());

    // Verify deletion
    Assert.assertEquals(0, resource.peerings().list().size());
    Assert.assertEquals(0, remoteNetwork.peerings().list().size());

    return resource;
}
 
Example #28
Source File: ApplicationGatewayImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public ApplicationGatewayImpl withExistingSubnet(Subnet subnet) {
    ensureDefaultIPConfig().withExistingSubnet(subnet);
    return this;
}
 
Example #29
Source File: VirtualNetworkGatewayIPConfigurationImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public VirtualNetworkGatewayIPConfigurationImpl withExistingSubnet(Subnet subnet) {
    return this.withExistingSubnet(subnet.parent().id(), subnet.name());
}
 
Example #30
Source File: VirtualNetworkGatewayIPConfigurationImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Subnet getSubnet() {
    return this.parent().manager().getAssociatedSubnet(this.inner().subnet());
}