public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "post",
                                                           Route = "ProcessTabularModel/{databaseName}/tables/{tableName}/repartition/{count}/bydate/{date?}")] HttpRequestMessage req,
                                              string databaseName,
                                              string tableName,
                                              string count,
                                              string date,
                                              TraceWriter log)
        {
            log.Info($"Received request to auto repartition {databaseName}/{tableName} by months.");

            DateTime targetDate        = DateTime.TryParse(date, out targetDate) ? targetDate : DateTime.Today;
            int      maxPartitionCount = Int32.TryParse(count, out maxPartitionCount) ? maxPartitionCount : 180;

            log.Info($"Start Date:  {targetDate} | Maximum Partitions: {maxPartitionCount}");

            try
            {
                // Determine number of partitions to be created by month based on number of partitions and end date and their properties
                List <NewPartitionInfo> newPartitionInfoList = new List <NewPartitionInfo>();

                for (int partitionCount = 0;
                     partitionCount < maxPartitionCount;
                     partitionCount++)
                {
                    DateTime         currentTargetDate = targetDate.AddMonths(-partitionCount);
                    NewPartitionInfo newPartitionInfo  = new NewPartitionInfo()
                    {
                        TableName     = tableName,
                        PartitionName = QueryHelper.GeneratePartitionKey(currentTargetDate, PartitionGranularity.Monthly),
                        SourceQuery   = QueryHelper.GetSourceQueryBasedOnDate(tableName, currentTargetDate, PartitionGranularity.Monthly)
                    };
                    newPartitionInfoList.Add(newPartitionInfo);
                }

                // Create the missing partitions in the database
                SqlServerAnalysisServerTabular tabularModel = new SqlServerAnalysisServerTabular()
                {
                    ConnectionString = ConfigurationManager.ConnectionStrings["SsasTabularConnection"].ConnectionString,
                    DatabaseName     = databaseName ?? ConfigurationManager.AppSettings["DatabaseName"]
                };

                if (newPartitionInfoList.Count > 0)
                {
                    tabularModel.CreatePartitions(tableName, newPartitionInfoList.ToArray());
                }
            }
            catch (Exception e)
            {
                log.Info($"C# HTTP trigger function exception: {e.ToString()}");
                return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }

            return(req.CreateResponse(HttpStatusCode.OK,
                                      new { result = $"Repartitioned  {databaseName}/{tableName}  - {maxPartitionCount} partitions from {targetDate.ToShortDateString()}" }));
        }
        public async static Task <HttpResponseMessage> Run([
                                                               HttpTrigger(AuthorizationLevel.Function, "post",
                                                                           Route = "ProcessTabularModel/{databaseName}/tables/{tableName}/partitions/new")] HttpRequestMessage req,
                                                           string databaseName,
                                                           string tableName,
                                                           TraceWriter log)
        {
            log.Info($"Received request to create new partitions in {databaseName} /{tableName}");

            try
            {
                SqlServerAnalysisServerTabular tabularModel = new SqlServerAnalysisServerTabular()
                {
                    ConnectionString = ConfigurationManager.ConnectionStrings["SsasTabularConnection"].ConnectionString,
                    DatabaseName     = databaseName ?? ConfigurationManager.AppSettings["DatabaseName"]
                };

                //read the request content and de-serialize the JSON payload of new partition information
                string partitionInfoSerialized = await req.Content.ReadAsStringAsync();

                NewPartitionInfo[] partitionInfoList = JsonConvert.DeserializeObject <NewPartitionInfo[]>(partitionInfoSerialized);

                if (partitionInfoList != null && partitionInfoList.Length > 0)
                {
                    tabularModel.CreatePartitions(tableName, partitionInfoList);
                }
            }
            catch (Exception e)
            {
                log.Error($"Error occured creating new partitions in {databaseName}/{tableName}: {e.ToString()}", e);
                return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }

            var successMessage = $"Created partitions on {databaseName}/{tableName}";

            log.Info(successMessage);
            return(req.CreateResponse(HttpStatusCode.OK, new { result = successMessage }));
        }