public override void ExecuteCmdlet()
        {
            string resourceGroupName = ResourceGroupName;
            string clusterName       = ClusterName;
            string databaseName      = Name;
            string location          = null;

            if (ShouldProcess(Name, Resources.CreateNewKustoCluster))
            {
                try
                {
                    if (!string.IsNullOrEmpty(ResourceId))
                    {
                        KustoUtils.GetResourceGroupNameAndClusterNameFromClusterId(ResourceId, out resourceGroupName, out clusterName);
                    }

                    if (InputObject != null)
                    {
                        KustoUtils.GetResourceGroupNameAndClusterNameFromClusterId(InputObject.Id, out resourceGroupName, out clusterName);
                    }

                    var cluser = KustoClient.GetCluster(resourceGroupName, clusterName);
                    if (cluser == null)
                    {
                        throw new CloudException(string.Format(Resources.KustoClusterNotExist, clusterName));
                    }

                    location = cluser.Location;

                    var database = KustoClient.GetDatabase(resourceGroupName, clusterName, databaseName);
                    if (database != null)
                    {
                        throw new CloudException(string.Format(Resources.KustoClusterExists, Name));
                    }
                }
                catch (CloudException ex)
                {
                    if (ex.Body != null && !string.IsNullOrEmpty(ex.Body.Code) && ex.Body.Code == "ResourceNotFound" ||
                        ex.Message.Contains("ResourceNotFound"))
                    {
                        // there are 2 options:
                        // -database does not exists so go ahead and create one
                        // -cluster does not exist, so continue and let the command fail
                    }
                    else if (ex.Body != null && !string.IsNullOrEmpty(ex.Body.Code) &&
                             ex.Body.Code == "ResourceGroupNotFound" || ex.Message.Contains("ResourceGroupNotFound"))
                    {
                        // resource group not found, let create throw error don't throw from here
                    }
                    else
                    {
                        // all other exceptions should be thrown
                        throw;
                    }
                }

                var createdDatabase = KustoClient.CreateOrUpdateDatabase(resourceGroupName, clusterName, databaseName, HotCachePeriodInDays, SoftDeletePeriodInDays, location);
                WriteObject(createdDatabase);
            }
        }
Beispiel #2
0
        public override void ExecuteCmdlet()
        {
            string databaseName              = Name;
            string clusterName               = ClusterName;
            string resourceGroupName         = ResourceGroupName;
            string location                  = null;
            int    hotCachePeriodInDays      = 0;
            int    softRetentionPeriodInDays = 0;

            if (!string.IsNullOrEmpty(ResourceId))
            {
                KustoUtils.GetResourceGroupNameClusterNameAndDatabaseNameFromDatabaseId(ResourceId, out resourceGroupName, out clusterName, out databaseName);
            }
            else if (InputObject != null)
            {
                KustoUtils.GetResourceGroupNameClusterNameAndDatabaseNameFromDatabaseId(InputObject.Id, out resourceGroupName, out clusterName, out databaseName);
            }

            EnsureDatabaseClusterResourceGroupSpecified(resourceGroupName, clusterName, databaseName);

            if (ShouldProcess(databaseName, Resources.UpdatingKustoDatabase))
            {
                try
                {
                    var database = KustoClient.GetDatabase(resourceGroupName, clusterName, databaseName);
                    if (database == null)
                    {
                        throw new CloudException(string.Format(Resources.KustoDatabaseNotExist, databaseName));
                    }

                    location                  = database.Location;
                    hotCachePeriodInDays      = HotCachePeriodInDays ?? database.HotCachePeriodInDays.GetValueOrDefault();
                    softRetentionPeriodInDays = SoftDeletePeriodInDays ?? database.SoftDeletePeriodInDays;
                }
                catch (CloudException ex)
                {
                    if (ex.Body != null && !string.IsNullOrEmpty(ex.Body.Code) && ex.Body.Code == "ResourceNotFound" ||
                        ex.Message.Contains("ResourceNotFound"))
                    {
                        throw new CloudException(string.Format(Resources.KustoDatabaseNotExist, databaseName));
                    }
                    else if (ex.Body != null && !string.IsNullOrEmpty(ex.Body.Code) &&
                             ex.Body.Code == "ResourceGroupNotFound" || ex.Message.Contains("ResourceGroupNotFound"))
                    {
                        throw new CloudException(string.Format(Resources.ResourceGroupNotExist, resourceGroupName));
                    }
                    else
                    {
                        // all other exceptions should be thrown
                        throw;
                    }
                }

                var updatedDatabase = KustoClient.CreateOrUpdateDatabase(resourceGroupName, clusterName, databaseName, hotCachePeriodInDays, softRetentionPeriodInDays, location);
                WriteObject(updatedDatabase);
            }
        }