// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.batch.generated;

import com.azure.core.management.serializer.SerializerFactory;
import com.azure.core.util.serializer.SerializerEncoding;
import com.azure.resourcemanager.batch.models.ApplicationPackageReference;
import com.azure.resourcemanager.batch.models.AutoScaleSettings;
import com.azure.resourcemanager.batch.models.AutoUserScope;
import com.azure.resourcemanager.batch.models.AutoUserSpecification;
import com.azure.resourcemanager.batch.models.BatchPoolIdentity;
import com.azure.resourcemanager.batch.models.CachingType;
import com.azure.resourcemanager.batch.models.CertificateReference;
import com.azure.resourcemanager.batch.models.CertificateStoreLocation;
import com.azure.resourcemanager.batch.models.CertificateVisibility;
import com.azure.resourcemanager.batch.models.CloudServiceConfiguration;
import com.azure.resourcemanager.batch.models.ComputeNodeDeallocationOption;
import com.azure.resourcemanager.batch.models.ComputeNodeFillType;
import com.azure.resourcemanager.batch.models.DataDisk;
import com.azure.resourcemanager.batch.models.DeploymentConfiguration;
import com.azure.resourcemanager.batch.models.DiffDiskPlacement;
import com.azure.resourcemanager.batch.models.DiffDiskSettings;
import com.azure.resourcemanager.batch.models.DiskEncryptionConfiguration;
import com.azure.resourcemanager.batch.models.DiskEncryptionTarget;
import com.azure.resourcemanager.batch.models.ElevationLevel;
import com.azure.resourcemanager.batch.models.EnvironmentSetting;
import com.azure.resourcemanager.batch.models.FixedScaleSettings;
import com.azure.resourcemanager.batch.models.ImageReference;
import com.azure.resourcemanager.batch.models.InboundEndpointProtocol;
import com.azure.resourcemanager.batch.models.InboundNatPool;
import com.azure.resourcemanager.batch.models.InterNodeCommunicationState;
import com.azure.resourcemanager.batch.models.IpAddressProvisioningType;
import com.azure.resourcemanager.batch.models.LinuxUserConfiguration;
import com.azure.resourcemanager.batch.models.MetadataItem;
import com.azure.resourcemanager.batch.models.NetworkConfiguration;
import com.azure.resourcemanager.batch.models.NetworkSecurityGroupRule;
import com.azure.resourcemanager.batch.models.NetworkSecurityGroupRuleAccess;
import com.azure.resourcemanager.batch.models.NodeCommunicationMode;
import com.azure.resourcemanager.batch.models.NodePlacementConfiguration;
import com.azure.resourcemanager.batch.models.NodePlacementPolicyType;
import com.azure.resourcemanager.batch.models.OSDisk;
import com.azure.resourcemanager.batch.models.PoolEndpointConfiguration;
import com.azure.resourcemanager.batch.models.PoolIdentityType;
import com.azure.resourcemanager.batch.models.PublicIpAddressConfiguration;
import com.azure.resourcemanager.batch.models.ResourceFile;
import com.azure.resourcemanager.batch.models.ScaleSettings;
import com.azure.resourcemanager.batch.models.StartTask;
import com.azure.resourcemanager.batch.models.StorageAccountType;
import com.azure.resourcemanager.batch.models.TaskSchedulingPolicy;
import com.azure.resourcemanager.batch.models.UserAccount;
import com.azure.resourcemanager.batch.models.UserAssignedIdentities;
import com.azure.resourcemanager.batch.models.UserIdentity;
import com.azure.resourcemanager.batch.models.VMExtension;
import com.azure.resourcemanager.batch.models.VirtualMachineConfiguration;
import com.azure.resourcemanager.batch.models.WindowsConfiguration;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/** Samples for Pool Create. */
public final class PoolCreateSamples {
    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_SharedImageGallery.json
     */
    /**
     * Sample code: CreatePool - Custom Image.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolCustomImage(com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withVirtualMachineConfiguration(
                        new VirtualMachineConfiguration()
                            .withImageReference(
                                new ImageReference()
                                    .withId(
                                        "/subscriptions/subid/resourceGroups/networking-group/providers/Microsoft.Compute/galleries/testgallery/images/testimagedef/versions/0.0.1"))
                            .withNodeAgentSkuId("batch.node.ubuntu 18.04")))
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_CloudServiceConfiguration.json
     */
    /**
     * Sample code: CreatePool - Full CloudServiceConfiguration.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolFullCloudServiceConfiguration(com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withDisplayName("my-pool-name")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withCloudServiceConfiguration(
                        new CloudServiceConfiguration().withOsFamily("4").withOsVersion("WA-GUEST-OS-4.45_201708-01")))
            .withScaleSettings(
                new ScaleSettings()
                    .withFixedScale(
                        new FixedScaleSettings()
                            .withResizeTimeout(Duration.parse("PT8M"))
                            .withTargetDedicatedNodes(6)
                            .withTargetLowPriorityNodes(28)
                            .withNodeDeallocationOption(ComputeNodeDeallocationOption.TASK_COMPLETION)))
            .withInterNodeCommunication(InterNodeCommunicationState.ENABLED)
            .withNetworkConfiguration(
                new NetworkConfiguration()
                    .withSubnetId(
                        "/subscriptions/subid/resourceGroups/rg1234/providers/Microsoft.Network/virtualNetworks/network1234/subnets/subnet123")
                    .withPublicIpAddressConfiguration(
                        new PublicIpAddressConfiguration()
                            .withProvision(IpAddressProvisioningType.USER_MANAGED)
                            .withIpAddressIds(
                                Arrays
                                    .asList(
                                        "/subscriptions/subid1/resourceGroups/rg13/providers/Microsoft.Network/publicIPAddresses/ip135",
                                        "/subscriptions/subid2/resourceGroups/rg24/providers/Microsoft.Network/publicIPAddresses/ip268"))))
            .withTaskSlotsPerNode(13)
            .withTaskSchedulingPolicy(new TaskSchedulingPolicy().withNodeFillType(ComputeNodeFillType.PACK))
            .withUserAccounts(
                Arrays
                    .asList(
                        new UserAccount()
                            .withName("username1")
                            .withPassword("fakeTokenPlaceholder")
                            .withElevationLevel(ElevationLevel.ADMIN)
                            .withLinuxUserConfiguration(
                                new LinuxUserConfiguration()
                                    .withUid(1234)
                                    .withGid(4567)
                                    .withSshPrivateKey("fakeTokenPlaceholder"))))
            .withMetadata(
                Arrays
                    .asList(
                        new MetadataItem().withName("metadata-1").withValue("value-1"),
                        new MetadataItem().withName("metadata-2").withValue("value-2")))
            .withStartTask(
                new StartTask()
                    .withCommandLine("cmd /c SET")
                    .withResourceFiles(
                        Arrays
                            .asList(
                                new ResourceFile()
                                    .withHttpUrl("https://testaccount.blob.core.windows.net/example-blob-file")
                                    .withFilePath("c:\\temp\\gohere")
                                    .withFileMode("777")))
                    .withEnvironmentSettings(
                        Arrays.asList(new EnvironmentSetting().withName("MYSET").withValue("1234")))
                    .withUserIdentity(
                        new UserIdentity()
                            .withAutoUser(
                                new AutoUserSpecification()
                                    .withScope(AutoUserScope.POOL)
                                    .withElevationLevel(ElevationLevel.ADMIN)))
                    .withMaxTaskRetryCount(6)
                    .withWaitForSuccess(true))
            .withCertificates(
                Arrays
                    .asList(
                        new CertificateReference()
                            .withId(
                                "/subscriptions/subid/resourceGroups/default-azurebatch-japaneast/providers/Microsoft.Batch/batchAccounts/sampleacct/pools/testpool/certificates/sha1-1234567")
                            .withStoreLocation(CertificateStoreLocation.LOCAL_MACHINE)
                            .withStoreName("MY")
                            .withVisibility(Arrays.asList(CertificateVisibility.REMOTE_USER))))
            .withApplicationPackages(
                Arrays
                    .asList(
                        new ApplicationPackageReference()
                            .withId(
                                "/subscriptions/subid/resourceGroups/default-azurebatch-japaneast/providers/Microsoft.Batch/batchAccounts/sampleacct/pools/testpool/applications/app_1234")
                            .withVersion("asdf")))
            .withApplicationLicenses(Arrays.asList("app-license0", "app-license1"))
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_MinimalCloudServiceConfiguration.json
     */
    /**
     * Sample code: CreatePool - Minimal CloudServiceConfiguration.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolMinimalCloudServiceConfiguration(
        com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withCloudServiceConfiguration(new CloudServiceConfiguration().withOsFamily("5")))
            .withScaleSettings(new ScaleSettings().withFixedScale(new FixedScaleSettings().withTargetDedicatedNodes(3)))
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_MinimalVirtualMachineConfiguration.json
     */
    /**
     * Sample code: CreatePool - Minimal VirtualMachineConfiguration.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolMinimalVirtualMachineConfiguration(
        com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withVirtualMachineConfiguration(
                        new VirtualMachineConfiguration()
                            .withImageReference(
                                new ImageReference()
                                    .withPublisher("Canonical")
                                    .withOffer("UbuntuServer")
                                    .withSku("18.04-LTS")
                                    .withVersion("latest"))
                            .withNodeAgentSkuId("batch.node.ubuntu 18.04")))
            .withScaleSettings(
                new ScaleSettings()
                    .withAutoScale(
                        new AutoScaleSettings()
                            .withFormula("$TargetDedicatedNodes=1")
                            .withEvaluationInterval(Duration.parse("PT5M"))))
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_NoPublicIPAddresses.json
     */
    /**
     * Sample code: CreatePool - No public IP.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolNoPublicIP(com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withVirtualMachineConfiguration(
                        new VirtualMachineConfiguration()
                            .withImageReference(
                                new ImageReference()
                                    .withId(
                                        "/subscriptions/subid/resourceGroups/networking-group/providers/Microsoft.Compute/galleries/testgallery/images/testimagedef/versions/0.0.1"))
                            .withNodeAgentSkuId("batch.node.ubuntu 18.04")))
            .withNetworkConfiguration(
                new NetworkConfiguration()
                    .withSubnetId(
                        "/subscriptions/subid/resourceGroups/rg1234/providers/Microsoft.Network/virtualNetworks/network1234/subnets/subnet123")
                    .withPublicIpAddressConfiguration(
                        new PublicIpAddressConfiguration()
                            .withProvision(IpAddressProvisioningType.NO_PUBLIC_IPADDRESSES)))
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_VirtualMachineConfiguration_Extensions.json
     */
    /**
     * Sample code: CreatePool - VirtualMachineConfiguration Extensions.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolVirtualMachineConfigurationExtensions(
        com.azure.resourcemanager.batch.BatchManager manager) throws IOException {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withVirtualMachineConfiguration(
                        new VirtualMachineConfiguration()
                            .withImageReference(
                                new ImageReference()
                                    .withPublisher("Canonical")
                                    .withOffer("0001-com-ubuntu-server-focal")
                                    .withSku("20_04-lts"))
                            .withNodeAgentSkuId("batch.node.ubuntu 20.04")
                            .withExtensions(
                                Arrays
                                    .asList(
                                        new VMExtension()
                                            .withName("batchextension1")
                                            .withPublisher("Microsoft.Azure.Security.Monitoring")
                                            .withType("SecurityMonitoringForLinux")
                                            .withTypeHandlerVersion("1.0")
                                            .withAutoUpgradeMinorVersion(true)
                                            .withSettings(
                                                SerializerFactory
                                                    .createDefaultManagementSerializerAdapter()
                                                    .deserialize(
                                                        "{\"settingsKey\":\"settingsValue\"}",
                                                        Object.class,
                                                        SerializerEncoding.JSON))
                                            .withProtectedSettings(
                                                SerializerFactory
                                                    .createDefaultManagementSerializerAdapter()
                                                    .deserialize(
                                                        "{\"protectedSettingsKey\":\"protectedSettingsValue\"}",
                                                        Object.class,
                                                        SerializerEncoding.JSON))))))
            .withScaleSettings(
                new ScaleSettings()
                    .withAutoScale(
                        new AutoScaleSettings()
                            .withFormula("$TargetDedicatedNodes=1")
                            .withEvaluationInterval(Duration.parse("PT5M"))))
            .withTargetNodeCommunicationMode(NodeCommunicationMode.DEFAULT)
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_UserAssignedIdentities.json
     */
    /**
     * Sample code: CreatePool - UserAssignedIdentities.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolUserAssignedIdentities(com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withIdentity(
                new BatchPoolIdentity()
                    .withType(PoolIdentityType.USER_ASSIGNED)
                    .withUserAssignedIdentities(
                        mapOf(
                            "/subscriptions/subid/resourceGroups/default-azurebatch-japaneast/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1",
                            new UserAssignedIdentities(),
                            "/subscriptions/subid/resourceGroups/default-azurebatch-japaneast/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id2",
                            new UserAssignedIdentities())))
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withVirtualMachineConfiguration(
                        new VirtualMachineConfiguration()
                            .withImageReference(
                                new ImageReference()
                                    .withPublisher("Canonical")
                                    .withOffer("UbuntuServer")
                                    .withSku("18.04-LTS")
                                    .withVersion("latest"))
                            .withNodeAgentSkuId("batch.node.ubuntu 18.04")))
            .withScaleSettings(
                new ScaleSettings()
                    .withAutoScale(
                        new AutoScaleSettings()
                            .withFormula("$TargetDedicatedNodes=1")
                            .withEvaluationInterval(Duration.parse("PT5M"))))
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_PublicIPs.json
     */
    /**
     * Sample code: CreatePool - Public IPs.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolPublicIPs(com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withVirtualMachineConfiguration(
                        new VirtualMachineConfiguration()
                            .withImageReference(
                                new ImageReference()
                                    .withId(
                                        "/subscriptions/subid/resourceGroups/networking-group/providers/Microsoft.Compute/galleries/testgallery/images/testimagedef/versions/0.0.1"))
                            .withNodeAgentSkuId("batch.node.ubuntu 18.04")))
            .withNetworkConfiguration(
                new NetworkConfiguration()
                    .withSubnetId(
                        "/subscriptions/subid/resourceGroups/rg1234/providers/Microsoft.Network/virtualNetworks/network1234/subnets/subnet123")
                    .withPublicIpAddressConfiguration(
                        new PublicIpAddressConfiguration()
                            .withProvision(IpAddressProvisioningType.USER_MANAGED)
                            .withIpAddressIds(
                                Arrays
                                    .asList(
                                        "/subscriptions/subid1/resourceGroups/rg13/providers/Microsoft.Network/publicIPAddresses/ip135"))))
            .create();
    }

    /*
     * x-ms-original-file: specification/batch/resource-manager/Microsoft.Batch/stable/2022-10-01/examples/PoolCreate_VirtualMachineConfiguration.json
     */
    /**
     * Sample code: CreatePool - Full VirtualMachineConfiguration.
     *
     * @param manager Entry point to BatchManager.
     */
    public static void createPoolFullVirtualMachineConfiguration(com.azure.resourcemanager.batch.BatchManager manager) {
        manager
            .pools()
            .define("testpool")
            .withExistingBatchAccount("default-azurebatch-japaneast", "sampleacct")
            .withVmSize("STANDARD_D4")
            .withDeploymentConfiguration(
                new DeploymentConfiguration()
                    .withVirtualMachineConfiguration(
                        new VirtualMachineConfiguration()
                            .withImageReference(
                                new ImageReference()
                                    .withPublisher("MicrosoftWindowsServer")
                                    .withOffer("WindowsServer")
                                    .withSku("2016-Datacenter-SmallDisk")
                                    .withVersion("latest"))
                            .withNodeAgentSkuId("batch.node.windows amd64")
                            .withWindowsConfiguration(new WindowsConfiguration().withEnableAutomaticUpdates(false))
                            .withDataDisks(
                                Arrays
                                    .asList(
                                        new DataDisk()
                                            .withLun(0)
                                            .withCaching(CachingType.READ_WRITE)
                                            .withDiskSizeGB(30)
                                            .withStorageAccountType(StorageAccountType.PREMIUM_LRS),
                                        new DataDisk()
                                            .withLun(1)
                                            .withCaching(CachingType.NONE)
                                            .withDiskSizeGB(200)
                                            .withStorageAccountType(StorageAccountType.STANDARD_LRS)))
                            .withLicenseType("Windows_Server")
                            .withDiskEncryptionConfiguration(
                                new DiskEncryptionConfiguration()
                                    .withTargets(
                                        Arrays
                                            .asList(DiskEncryptionTarget.OS_DISK, DiskEncryptionTarget.TEMPORARY_DISK)))
                            .withNodePlacementConfiguration(
                                new NodePlacementConfiguration().withPolicy(NodePlacementPolicyType.ZONAL))
                            .withOsDisk(
                                new OSDisk()
                                    .withEphemeralOSDiskSettings(
                                        new DiffDiskSettings().withPlacement(DiffDiskPlacement.CACHE_DISK)))))
            .withScaleSettings(
                new ScaleSettings()
                    .withAutoScale(
                        new AutoScaleSettings()
                            .withFormula("$TargetDedicatedNodes=1")
                            .withEvaluationInterval(Duration.parse("PT5M"))))
            .withNetworkConfiguration(
                new NetworkConfiguration()
                    .withEndpointConfiguration(
                        new PoolEndpointConfiguration()
                            .withInboundNatPools(
                                Arrays
                                    .asList(
                                        new InboundNatPool()
                                            .withName("testnat")
                                            .withProtocol(InboundEndpointProtocol.TCP)
                                            .withBackendPort(12001)
                                            .withFrontendPortRangeStart(15000)
                                            .withFrontendPortRangeEnd(15100)
                                            .withNetworkSecurityGroupRules(
                                                Arrays
                                                    .asList(
                                                        new NetworkSecurityGroupRule()
                                                            .withPriority(150)
                                                            .withAccess(NetworkSecurityGroupRuleAccess.ALLOW)
                                                            .withSourceAddressPrefix("192.100.12.45")
                                                            .withSourcePortRanges(Arrays.asList("1", "2")),
                                                        new NetworkSecurityGroupRule()
                                                            .withPriority(3500)
                                                            .withAccess(NetworkSecurityGroupRuleAccess.DENY)
                                                            .withSourceAddressPrefix("*")
                                                            .withSourcePortRanges(Arrays.asList("*"))))))))
            .create();
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }
}
