Search in sources :

Example 1 with NetworkSecurityGroup

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();
}
Also used : NetworkSecurityGroup(com.azure.resourcemanager.network.models.NetworkSecurityGroup) ManagementException(com.azure.core.management.exception.ManagementException) AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StepResult(bio.terra.stairway.StepResult) ComputeManager(com.azure.resourcemanager.compute.ComputeManager)

Example 2 with NetworkSecurityGroup

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;
}
Also used : AzureNetworkSecurityGroup(bio.terra.janitor.model.AzureNetworkSecurityGroup) NetworkSecurityGroup(com.azure.resourcemanager.network.models.NetworkSecurityGroup)

Example 3 with NetworkSecurityGroup

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());
    }
}
Also used : NetworkSecurityGroup(com.azure.resourcemanager.network.models.NetworkSecurityGroup) AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Aggregations

NetworkSecurityGroup (com.azure.resourcemanager.network.models.NetworkSecurityGroup)3 ManagementException (com.azure.core.management.exception.ManagementException)2 AzureNetworkSecurityGroup (bio.terra.janitor.model.AzureNetworkSecurityGroup)1 StepResult (bio.terra.stairway.StepResult)1 AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)1 ComputeManager (com.azure.resourcemanager.compute.ComputeManager)1 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)1 AzureVMAgentCleanUpTask (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask)1 AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)1 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)1 AzureUtil (com.microsoft.azure.vmagent.util.AzureUtil)1 IOException (java.io.IOException)1 Test (org.junit.Test)1