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

package com.azure.resourcemanager.sql.generated;

import com.azure.core.util.Context;
import com.azure.resourcemanager.sql.fluent.models.ManagedDatabaseInner;
import com.azure.resourcemanager.sql.models.ManagedDatabaseCreateMode;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

/** Samples for ManagedDatabases CreateOrUpdate. */
public final class ManagedDatabasesCreateOrUpdateSamples {
    /*
     * x-ms-original-file: specification/sql/resource-manager/Microsoft.Sql/stable/2021-11-01/examples/ManagedDatabaseCreateRecovery.json
     */
    /**
     * Sample code: Creates a new managed database from restoring a geo-replicated backup.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createsANewManagedDatabaseFromRestoringAGeoReplicatedBackup(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .sqlServers()
            .manager()
            .serviceClient()
            .getManagedDatabases()
            .createOrUpdate(
                "Default-SQL-SouthEastAsia",
                "server1",
                "testdb_recovered",
                new ManagedDatabaseInner()
                    .withLocation("southeastasia")
                    .withCreateMode(ManagedDatabaseCreateMode.RECOVERY)
                    .withRecoverableDatabaseId(
                        "/subscriptions/11111111-2222-3333-4444-555555555555/resourceGroups/Default-SQL-WestEurope/providers/Microsoft.Sql/managedInstances/testsvr/recoverableDatabases/testdb"),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/sql/resource-manager/Microsoft.Sql/stable/2021-11-01/examples/ManagedDatabaseCreateRestoreExternalBackup.json
     */
    /**
     * Sample code: Creates a new managed database by restoring from an external backup.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createsANewManagedDatabaseByRestoringFromAnExternalBackup(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .sqlServers()
            .manager()
            .serviceClient()
            .getManagedDatabases()
            .createOrUpdate(
                "Default-SQL-SouthEastAsia",
                "managedInstance",
                "managedDatabase",
                new ManagedDatabaseInner()
                    .withLocation("southeastasia")
                    .withCollation("SQL_Latin1_General_CP1_CI_AS")
                    .withCreateMode(ManagedDatabaseCreateMode.RESTORE_EXTERNAL_BACKUP)
                    .withStorageContainerUri("https://myaccountname.blob.core.windows.net/backups")
                    .withStorageContainerSasToken("fakeTokenPlaceholder")
                    .withAutoCompleteRestore(true)
                    .withLastBackupName("last_backup_name"),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/sql/resource-manager/Microsoft.Sql/stable/2021-11-01/examples/ManagedDatabaseCreateMax.json
     */
    /**
     * Sample code: Creates a new managed database with maximal properties.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createsANewManagedDatabaseWithMaximalProperties(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .sqlServers()
            .manager()
            .serviceClient()
            .getManagedDatabases()
            .createOrUpdate(
                "Default-SQL-SouthEastAsia",
                "managedInstance",
                "managedDatabase",
                new ManagedDatabaseInner().withLocation("southeastasia").withTags(mapOf("tagKey1", "TagValue1")),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/sql/resource-manager/Microsoft.Sql/stable/2021-11-01/examples/ManagedDatabaseCreateMin.json
     */
    /**
     * Sample code: Creates a new managed database with minimal properties.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createsANewManagedDatabaseWithMinimalProperties(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .sqlServers()
            .manager()
            .serviceClient()
            .getManagedDatabases()
            .createOrUpdate(
                "Default-SQL-SouthEastAsia",
                "managedInstance",
                "managedDatabase",
                new ManagedDatabaseInner().withLocation("southeastasia"),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/sql/resource-manager/Microsoft.Sql/stable/2021-11-01/examples/ManagedDatabaseCreatePointInTimeRestore.json
     */
    /**
     * Sample code: Creates a new managed database using point in time restore.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createsANewManagedDatabaseUsingPointInTimeRestore(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .sqlServers()
            .manager()
            .serviceClient()
            .getManagedDatabases()
            .createOrUpdate(
                "Default-SQL-SouthEastAsia",
                "managedInstance",
                "managedDatabase",
                new ManagedDatabaseInner()
                    .withLocation("southeastasia")
                    .withRestorePointInTime(OffsetDateTime.parse("2017-07-14T05:35:31.503Z"))
                    .withCreateMode(ManagedDatabaseCreateMode.POINT_IN_TIME_RESTORE)
                    .withSourceDatabaseId(
                        "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/Default-SQL-SouthEastAsia/providers/Microsoft.Sql/managedInstances/testsvr/databases/testdb"),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/sql/resource-manager/Microsoft.Sql/stable/2021-11-01/examples/ManagedDatabaseCreateRestoreLtrBackup.json
     */
    /**
     * Sample code: Creates a new managed database from restoring a long term retention backup.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createsANewManagedDatabaseFromRestoringALongTermRetentionBackup(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .sqlServers()
            .manager()
            .serviceClient()
            .getManagedDatabases()
            .createOrUpdate(
                "Default-SQL-SouthEastAsia",
                "managedInstance",
                "managedDatabase",
                new ManagedDatabaseInner()
                    .withLocation("southeastasia")
                    .withCollation("SQL_Latin1_General_CP1_CI_AS")
                    .withCreateMode(ManagedDatabaseCreateMode.RESTORE_EXTERNAL_BACKUP)
                    .withStorageContainerUri("https://myaccountname.blob.core.windows.net/backups")
                    .withStorageContainerSasToken("fakeTokenPlaceholder"),
                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;
    }
}
