use of com.azure.resourcemanager.network.models.NetworkSecurityGroup in project terra-workspace-manager by DataBiosphere.
the class CreateAzureNetworkStep method doStep.
@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
ComputeManager computeManager = crlService.getComputeManager(azureCloudContext, azureConfig);
try {
NetworkSecurityGroup subnetNsg = computeManager.networkManager().networkSecurityGroups().define(resource.getSubnetName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).withTag("workspaceId", resource.getWorkspaceId().toString()).withTag("resourceId", resource.getResourceId().toString()).defineRule("AllowHttpInComing").allowInbound().fromAddress("INTERNET").fromAnyPort().toAnyAddress().toPort(8080).withProtocol(SecurityRuleProtocol.TCP).attach().create(Defaults.buildContext(CreateNetworkSecurityGroupRequestData.builder().setName(resource.getSubnetName()).setRegion(Region.fromName(resource.getRegion())).setTenantId(azureCloudContext.getAzureTenantId()).setSubscriptionId(azureCloudContext.getAzureSubscriptionId()).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).setRules(Collections.emptyList()).build()));
computeManager.networkManager().networks().define(resource.getNetworkName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).withTag("workspaceId", resource.getWorkspaceId().toString()).withTag("resourceId", resource.getResourceId().toString()).withAddressSpace(resource.getAddressSpaceCidr()).defineSubnet(resource.getSubnetName()).withAddressPrefix(resource.getSubnetAddressCidr()).withExistingNetworkSecurityGroup(subnetNsg).attach().create(Defaults.buildContext(CreateNetworkRequestData.builder().setName(resource.getNetworkName()).setTenantId(azureCloudContext.getAzureTenantId()).setSubscriptionId(azureCloudContext.getAzureSubscriptionId()).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).setRegion(Region.fromName(resource.getRegion())).setSubnetName(resource.getSubnetName()).setNetworkSecurityGroup(subnetNsg).setAddressPrefix(resource.getSubnetAddressCidr()).setAddressSpaceCidr(resource.getAddressSpaceCidr()).build()));
} catch (ManagementException e) {
// https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/common-deployment-errors
if (StringUtils.equals(e.getValue().getCode(), "Conflict")) {
logger.info("Azure Network {} in managed resource group {} already exists", resource.getNetworkName(), azureCloudContext.getAzureResourceGroupId());
return StepResult.getStepResultSuccess();
}
if (StringUtils.equals(e.getValue().getCode(), "SubnetsNotInSameVnet")) {
logger.info("Azure Network {} and Subnet {} in managed resource group {} must belong to the same virtual network", resource.getNetworkName(), resource.getSubnetName(), azureCloudContext.getAzureResourceGroupId());
return StepResult.getStepResultSuccess();
}
return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
}
return StepResult.getStepResultSuccess();
}
use of com.azure.resourcemanager.network.models.NetworkSecurityGroup in project terra-cloud-resource-lib by DataBiosphere.
the class ComputeRequestDataTest method mockNetworkSecurityGroup.
private static NetworkSecurityGroup mockNetworkSecurityGroup() {
NetworkSecurityGroup mock = mock(NetworkSecurityGroup.class);
when(mock.name()).thenReturn("my-nsg");
return mock;
}
use of com.azure.resourcemanager.network.models.NetworkSecurityGroup in project azure-vm-agents-plugin by jenkinsci.
the class ITAzureVMManagementServiceDelegate method createDeploymentWithExistingNSG.
@Test
public void createDeploymentWithExistingNSG() {
try {
final String nsgName = TestEnvironment.GenerateRandomString(12);
NetworkSecurityGroup nsg = azureClient.networkSecurityGroups().define(nsgName).withRegion(testEnv.azureLocation).withNewResourceGroup(testEnv.azureResourceGroup).create();
AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
AzureVMDeploymentInfo deploymentInfo = createDefaultDeployment(1, nsgName, deploymentRegistrar);
VirtualMachine deployedVM = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, deploymentInfo.getVmBaseName() + "0");
final String actualNSGId = deployedVM.getPrimaryNetworkInterface().getNetworkSecurityGroup().id();
Assert.assertEquals(nsg.id(), actualNSGId);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
Assert.fail(e.getMessage());
}
}
Aggregations