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

package com.azure.resourcemanager.cosmos.generated;

import com.azure.resourcemanager.cosmos.models.AnalyticalStorageConfiguration;
import com.azure.resourcemanager.cosmos.models.AnalyticalStorageSchemaType;
import com.azure.resourcemanager.cosmos.models.ApiProperties;
import com.azure.resourcemanager.cosmos.models.BackupStorageRedundancy;
import com.azure.resourcemanager.cosmos.models.Capacity;
import com.azure.resourcemanager.cosmos.models.ConsistencyPolicy;
import com.azure.resourcemanager.cosmos.models.ContinuousModeBackupPolicy;
import com.azure.resourcemanager.cosmos.models.ContinuousModeProperties;
import com.azure.resourcemanager.cosmos.models.ContinuousTier;
import com.azure.resourcemanager.cosmos.models.CorsPolicy;
import com.azure.resourcemanager.cosmos.models.CreateMode;
import com.azure.resourcemanager.cosmos.models.DatabaseAccountCreateUpdateParameters;
import com.azure.resourcemanager.cosmos.models.DatabaseAccountKind;
import com.azure.resourcemanager.cosmos.models.DatabaseRestoreResource;
import com.azure.resourcemanager.cosmos.models.DefaultConsistencyLevel;
import com.azure.resourcemanager.cosmos.models.IpAddressOrRange;
import com.azure.resourcemanager.cosmos.models.Location;
import com.azure.resourcemanager.cosmos.models.ManagedServiceIdentity;
import com.azure.resourcemanager.cosmos.models.ManagedServiceIdentityUserAssignedIdentities;
import com.azure.resourcemanager.cosmos.models.MinimalTlsVersion;
import com.azure.resourcemanager.cosmos.models.NetworkAclBypass;
import com.azure.resourcemanager.cosmos.models.PeriodicModeBackupPolicy;
import com.azure.resourcemanager.cosmos.models.PeriodicModeProperties;
import com.azure.resourcemanager.cosmos.models.PublicNetworkAccess;
import com.azure.resourcemanager.cosmos.models.ResourceIdentityType;
import com.azure.resourcemanager.cosmos.models.RestoreMode;
import com.azure.resourcemanager.cosmos.models.RestoreParameters;
import com.azure.resourcemanager.cosmos.models.ServerVersion;
import com.azure.resourcemanager.cosmos.models.VirtualNetworkRule;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/** Samples for DatabaseAccounts CreateOrUpdate. */
public final class DatabaseAccountsCreateOrUpdateSamples {
    /*
     * x-ms-original-file: specification/cosmos-db/resource-manager/Microsoft.DocumentDB/stable/2023-04-15/examples/CosmosDBRestoreDatabaseAccountCreateUpdate.json
     */
    /**
     * Sample code: CosmosDBRestoreDatabaseAccountCreateUpdate.json.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void cosmosDBRestoreDatabaseAccountCreateUpdateJson(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .cosmosDBAccounts()
            .manager()
            .serviceClient()
            .getDatabaseAccounts()
            .createOrUpdate(
                "rg1",
                "ddb1",
                new DatabaseAccountCreateUpdateParameters()
                    .withLocation("westus")
                    .withTags(mapOf())
                    .withKind(DatabaseAccountKind.GLOBAL_DOCUMENT_DB)
                    .withConsistencyPolicy(
                        new ConsistencyPolicy()
                            .withDefaultConsistencyLevel(DefaultConsistencyLevel.BOUNDED_STALENESS)
                            .withMaxStalenessPrefix(200L)
                            .withMaxIntervalInSeconds(10))
                    .withLocations(
                        Arrays
                            .asList(
                                new Location()
                                    .withLocationName("southcentralus")
                                    .withFailoverPriority(0)
                                    .withIsZoneRedundant(false)))
                    .withKeyVaultKeyUri("fakeTokenPlaceholder")
                    .withEnableFreeTier(false)
                    .withApiProperties(new ApiProperties().withServerVersion(ServerVersion.THREE_TWO))
                    .withEnableAnalyticalStorage(true)
                    .withCreateMode(CreateMode.RESTORE)
                    .withBackupPolicy(
                        new ContinuousModeBackupPolicy()
                            .withContinuousModeProperties(
                                new ContinuousModeProperties().withTier(ContinuousTier.CONTINUOUS30DAYS)))
                    .withRestoreParameters(
                        new RestoreParameters()
                            .withRestoreMode(RestoreMode.POINT_IN_TIME)
                            .withRestoreSource(
                                "/subscriptions/subid/providers/Microsoft.DocumentDB/locations/westus/restorableDatabaseAccounts/1a97b4bb-f6a0-430e-ade1-638d781830cc")
                            .withRestoreTimestampInUtc(OffsetDateTime.parse("2021-03-11T22:05:09Z"))
                            .withDatabasesToRestore(
                                Arrays
                                    .asList(
                                        new DatabaseRestoreResource()
                                            .withDatabaseName("db1")
                                            .withCollectionNames(Arrays.asList("collection1", "collection2")),
                                        new DatabaseRestoreResource()
                                            .withDatabaseName("db2")
                                            .withCollectionNames(Arrays.asList("collection3", "collection4")))))
                    .withMinimalTlsVersion(MinimalTlsVersion.TLS),
                com.azure.core.util.Context.NONE);
    }

    /*
     * x-ms-original-file: specification/cosmos-db/resource-manager/Microsoft.DocumentDB/stable/2023-04-15/examples/CosmosDBDatabaseAccountCreateMax.json
     */
    /**
     * Sample code: CosmosDBDatabaseAccountCreateMax.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void cosmosDBDatabaseAccountCreateMax(com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .cosmosDBAccounts()
            .manager()
            .serviceClient()
            .getDatabaseAccounts()
            .createOrUpdate(
                "rg1",
                "ddb1",
                new DatabaseAccountCreateUpdateParameters()
                    .withLocation("westus")
                    .withTags(mapOf())
                    .withKind(DatabaseAccountKind.MONGO_DB)
                    .withIdentity(
                        new ManagedServiceIdentity()
                            .withType(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED)
                            .withUserAssignedIdentities(
                                mapOf(
                                    "/subscriptions/fa5fc227-a624-475e-b696-cdd604c735bc/resourceGroups/eu2cgroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1",
                                    new ManagedServiceIdentityUserAssignedIdentities())))
                    .withConsistencyPolicy(
                        new ConsistencyPolicy()
                            .withDefaultConsistencyLevel(DefaultConsistencyLevel.BOUNDED_STALENESS)
                            .withMaxStalenessPrefix(200L)
                            .withMaxIntervalInSeconds(10))
                    .withLocations(
                        Arrays
                            .asList(
                                new Location()
                                    .withLocationName("southcentralus")
                                    .withFailoverPriority(0)
                                    .withIsZoneRedundant(false),
                                new Location()
                                    .withLocationName("eastus")
                                    .withFailoverPriority(1)
                                    .withIsZoneRedundant(false)))
                    .withIpRules(
                        Arrays
                            .asList(
                                new IpAddressOrRange().withIpAddressOrRange("23.43.230.120"),
                                new IpAddressOrRange().withIpAddressOrRange("110.12.240.0/12")))
                    .withIsVirtualNetworkFilterEnabled(true)
                    .withVirtualNetworkRules(
                        Arrays
                            .asList(
                                new VirtualNetworkRule()
                                    .withId(
                                        "/subscriptions/subId/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet1/subnets/subnet1")
                                    .withIgnoreMissingVNetServiceEndpoint(false)))
                    .withKeyVaultKeyUri("fakeTokenPlaceholder")
                    .withDefaultIdentity("FirstPartyIdentity")
                    .withPublicNetworkAccess(PublicNetworkAccess.ENABLED)
                    .withEnableFreeTier(false)
                    .withApiProperties(new ApiProperties().withServerVersion(ServerVersion.THREE_TWO))
                    .withEnableAnalyticalStorage(true)
                    .withAnalyticalStorageConfiguration(
                        new AnalyticalStorageConfiguration().withSchemaType(AnalyticalStorageSchemaType.WELL_DEFINED))
                    .withCreateMode(CreateMode.DEFAULT)
                    .withBackupPolicy(
                        new PeriodicModeBackupPolicy()
                            .withPeriodicModeProperties(
                                new PeriodicModeProperties()
                                    .withBackupIntervalInMinutes(240)
                                    .withBackupRetentionIntervalInHours(8)
                                    .withBackupStorageRedundancy(BackupStorageRedundancy.GEO)))
                    .withCors(Arrays.asList(new CorsPolicy().withAllowedOrigins("https://test")))
                    .withNetworkAclBypass(NetworkAclBypass.AZURE_SERVICES)
                    .withNetworkAclBypassResourceIds(
                        Arrays
                            .asList(
                                "/subscriptions/subId/resourcegroups/rgName/providers/Microsoft.Synapse/workspaces/workspaceName"))
                    .withCapacity(new Capacity().withTotalThroughputLimit(2000))
                    .withMinimalTlsVersion(MinimalTlsVersion.TLS12),
                com.azure.core.util.Context.NONE);
    }

    /*
     * x-ms-original-file: specification/cosmos-db/resource-manager/Microsoft.DocumentDB/stable/2023-04-15/examples/CosmosDBDatabaseAccountCreateMin.json
     */
    /**
     * Sample code: CosmosDBDatabaseAccountCreateMin.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void cosmosDBDatabaseAccountCreateMin(com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .cosmosDBAccounts()
            .manager()
            .serviceClient()
            .getDatabaseAccounts()
            .createOrUpdate(
                "rg1",
                "ddb1",
                new DatabaseAccountCreateUpdateParameters()
                    .withLocation("westus")
                    .withLocations(
                        Arrays
                            .asList(
                                new Location()
                                    .withLocationName("southcentralus")
                                    .withFailoverPriority(0)
                                    .withIsZoneRedundant(false)))
                    .withCreateMode(CreateMode.DEFAULT),
                com.azure.core.util.Context.NONE);
    }

    @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;
    }
}
