use of com.microsoft.azure.management.network.NetworkSecurityGroup in project azure-sdk-for-java by Azure.
the class Utils method print.
/**
* Print network info.
* @param resource a network
* @throws IOException IO errors
* @throws CloudException Cloud errors
*/
public static void print(Network resource) {
StringBuilder info = new StringBuilder();
info.append("Network: ").append(resource.id()).append("Name: ").append(resource.name()).append("\n\tResource group: ").append(resource.resourceGroupName()).append("\n\tRegion: ").append(resource.region()).append("\n\tTags: ").append(resource.tags()).append("\n\tAddress spaces: ").append(resource.addressSpaces()).append("\n\tDNS server IPs: ").append(resource.dnsServerIPs());
// Output subnets
for (Subnet subnet : resource.subnets().values()) {
info.append("\n\tSubnet: ").append(subnet.name()).append("\n\t\tAddress prefix: ").append(subnet.addressPrefix());
NetworkSecurityGroup subnetNsg = subnet.getNetworkSecurityGroup();
if (subnetNsg != null) {
info.append("\n\t\tNetwork security group: ").append(subnetNsg.id());
}
}
System.out.println(info.toString());
}
use of com.microsoft.azure.management.network.NetworkSecurityGroup in project azure-sdk-for-java by Azure.
the class AzureTests method testGenericResources.
/**
* Tests basic generic resources retrieval.
* @throws Exception
*/
@Test
public void testGenericResources() throws Exception {
// Create some resources
NetworkSecurityGroup nsg = azure.networkSecurityGroups().define(SdkContext.randomResourceName("nsg", 13)).withRegion(Region.US_EAST).withNewResourceGroup().create();
azure.publicIPAddresses().define(SdkContext.randomResourceName("pip", 13)).withRegion(Region.US_EAST).withExistingResourceGroup(nsg.resourceGroupName()).create();
PagedList<GenericResource> resources = azure.genericResources().listByResourceGroup(nsg.resourceGroupName());
Assert.assertEquals(2, resources.size());
GenericResource firstResource = resources.get(0);
GenericResource resourceById = azure.genericResources().getById(firstResource.id());
GenericResource resourceByDetails = azure.genericResources().get(firstResource.resourceGroupName(), firstResource.resourceProviderNamespace(), firstResource.resourceType(), firstResource.name());
Assert.assertTrue(resourceById.id().equalsIgnoreCase(resourceByDetails.id()));
azure.resourceGroups().beginDeleteByName(nsg.resourceGroupName());
}
use of com.microsoft.azure.management.network.NetworkSecurityGroup in project cloudbreak by hortonworks.
the class AzureUtils method validateSubnetRules.
public void validateSubnetRules(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] does not found with resourceGroupName [%s] and network [%s]", subnetId, resourceGroupName, networkId));
}
NetworkSecurityGroup networkSecurityGroup = subnet.getNetworkSecurityGroup();
if (networkSecurityGroup != null) {
validateSecurityGroup(client, networkSecurityGroup);
}
} catch (RuntimeException e) {
throw new CloudConnectorException("Subnet validation failed, cause: " + e.getMessage(), e);
}
}
}
}
use of com.microsoft.azure.management.network.NetworkSecurityGroup in project cloudbreak by hortonworks.
the class AzureUtils method validateSecurityGroup.
private void validateSecurityGroup(AzureClient client, HasId networkSecurityGroup) {
String securityGroupId = networkSecurityGroup.id();
String[] parts = securityGroupId.split("/");
if (parts.length != ID_SEGMENTS) {
LOGGER.info("Cannot get the security group's properties, id: {}", securityGroupId);
return;
}
try {
NetworkSecurityGroup securityGroup = client.getSecurityGroupProperties(parts[RG_PART], parts[SEC_GROUP_PART]);
LOGGER.info("Retrieved security group properties: {}", securityGroup);
Map<String, NetworkSecurityRule> securityRules = securityGroup.securityRules();
boolean port22Found = false;
boolean port443Found = false;
for (NetworkSecurityRule securityRule : securityRules.values()) {
if (isValidInboundRule(securityRule)) {
String destinationPortRange = securityRule.destinationPortRange();
if ("*".equals(destinationPortRange)) {
return;
}
String[] range = destinationPortRange.split("-");
port443Found = port443Found || isPortFound(PORT_443, range);
port22Found = port22Found || isPortFound(PORT_22, range);
if (port22Found && port443Found) {
return;
}
}
}
} catch (RuntimeException e) {
throw new CloudConnectorException("Validating security group failed.", e);
}
throw new CloudConnectorException("The specified subnet's security group does not allow traffic for port 22 and/or 443");
}
use of com.microsoft.azure.management.network.NetworkSecurityGroup in project azure-tools-for-java by Microsoft.
the class SettingsStep method retrieveNetworkSecurityGroups.
private void retrieveNetworkSecurityGroups() {
AzureTaskManager.getInstance().runInBackground("Loading network security groups...", new Runnable() {
@Override
public void run() {
if (networkSecurityGroups == null) {
networkSecurityGroups = wizard.getAzure().networkSecurityGroups().list();
}
DefaultLoader.getIdeHelper().invokeLater(new Runnable() {
@Override
public void run() {
nsgCombo.removeAll();
nsgCombo.add(NONE);
for (NetworkSecurityGroup nsg : networkSecurityGroups) {
nsgCombo.add(nsg.name());
nsgCombo.setData(nsg.name(), nsg);
}
nsgCombo.select(0);
}
});
}
});
nsgCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (NONE.equals(nsgCombo.getText())) {
wizard.setNetworkSecurityGroup(null);
} else if (nsgCombo.getData(nsgCombo.getText()) instanceof NetworkSecurityGroup) {
wizard.setNetworkSecurityGroup((NetworkSecurityGroup) nsgCombo.getData(nsgCombo.getText()));
}
}
});
if (networkSecurityGroups == null) {
DefaultLoader.getIdeHelper().invokeAndWait(new Runnable() {
@Override
public void run() {
nsgCombo.setItems(new String[] { NONE, LOADING });
nsgCombo.select(0);
}
});
}
}
Aggregations