/// <summary>
        /// Executes a Provisioning Job
        /// </summary>
        /// <param name="job">The Provisioning Job to run</param>
        public void RunJob(ProvisioningJob job)
        {
            try
            {
                // Set the Job status as Running
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                job.JobId,
                ProvisioningJobStatus.Running,
                String.Empty);

                // Run the Job
                RunJobInternal(job);

                // Set the Job status as Provisioned (i.e. Completed)
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Provisioned,
                    String.Empty);
            }
            catch (Exception ex)
            {
                // Set the Job status as Failed, including the exception details
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Failed,
                    ex.Message);

                Console.WriteLine("Exception occurred: {0}\nStack Trace:\n{1}\n", ex.Message, ex.StackTrace);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Executes a Provisioning Job
        /// </summary>
        /// <param name="job">The Provisioning Job to run</param>
        public void RunJob(ProvisioningJob job)
        {
            try
            {
                // Set the Job status as Running
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Running,
                    String.Empty);

                // Run the Job
                RunJobInternal(job);

                // Set the Job status as Provisioned (i.e. Completed)
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Provisioned,
                    String.Empty);
            }
            catch (Exception ex)
            {
                // Set the Job status as Failed, including the exception details
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Failed,
                    ex.Message);

                Console.WriteLine("Exception occurred: {0}\nStack Trace:\n{1}\n", ex.Message, ex.StackTrace);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Executes a Provisioning Job
        /// </summary>
        /// <param name="job">The Provisioning Job to run</param>
        public void RunJob(ProvisioningJob job)
        {
            try
            {
                // Set the Job status as Running
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Running,
                    String.Empty);

                // Run the Job
                RunJobInternal(job);

                // Set the Job status as Provisioned (i.e. Completed)
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Provisioned,
                    String.Empty);
            }
            catch (Exception ex)
            {
                // Set the Job status as Failed, including the exception details
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Failed,
                    ex.Message);
            }
        }
        private void ProvisionSiteCollection(ProvisioningJob provisioningJob)
        {
            using (PnPMonitoredScope Log = new PnPMonitoredScope("ProvisionSiteCollection"))
            {
                var siteCollectionProvisioningJob = provisioningJob as SiteCollectionProvisioningJob;
                if (siteCollectionProvisioningJob == null)
                {
                    // throw exception
                }

                ProvisioningJobHandler siteCollectionJobHandler
                    = new SiteCollectionProvisioningJobHandler();

                if (siteCollectionProvisioningJob.JobId == Guid.Empty)
                {
                    siteCollectionProvisioningJob.JobId = Guid.NewGuid();
                }

                siteCollectionProvisioningJob.StorageWarningLevel = 70;
                siteCollectionProvisioningJob.StorageMaximumLevel = 100;
                siteCollectionProvisioningJob.CreationTimeStamp   = DateTime.UtcNow;
                siteCollectionProvisioningJob.JobType
                    = siteCollectionProvisioningJob.GetType().FullName;
                siteCollectionProvisioningJob.Status = JobStatus.Pending;

                siteCollectionJobHandler.RunJob(provisioningJob);
            }
        }
Esempio n. 5
0
        // This function will get triggered/executed when a new message is written
        // on an Azure Queue called queue.
        public static void ProcessQueueMessage([QueueTrigger(PnPPartnerPackSettings.StorageQueueName)] ContinousJobItem content, TextWriter log)
        {
            // Attach TextWriter log to Trace
            // https://blog.josequinto.com/2017/02/16/enable-azure-invocation-log-at-a-web-job-function-level-for-pnp-provisioning/
            TextWriterTraceListener twtl = new TextWriterTraceListener(log);

            twtl.Name = "ContinousJobLogger";
            string[] notShownWords = new string[] { "TokenCache", "AcquireTokenHandlerBase" };
            twtl.Filter = new RemoveWordsFilter(notShownWords);
            Trace.Listeners.Add(twtl);
            Trace.AutoFlush = true;

            log.WriteLine(String.Format("Found Job: {0}", content.JobId));

            // Get the info about the Provisioning Job
            ProvisioningJobInformation jobInfo =
                ProvisioningRepositoryFactory.Current.GetProvisioningJob(content.JobId, true);

            // Get a reference to the Provisioning Job
            ProvisioningJob job = jobInfo.JobFile.FromJsonStream(jobInfo.Type);

            if (PnPPartnerPackSettings.ContinousJobHandlers.ContainsKey(job.GetType()))
            {
                PnPPartnerPackSettings.ContinousJobHandlers[job.GetType()].RunJob(job);
            }

            log.WriteLine("Completed Job execution");

            // Remove Trace Listener
            Trace.Listeners.Remove(twtl);
        }
        /// <summary>
        /// Executes a Provisioning Job
        /// </summary>
        /// <param name="job">The Provisioning Job to run</param>
        public void RunJob(ProvisioningJob job)
        {
            try
            {
                // Set the Job status as Running
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                job.JobId,
                ProvisioningJobStatus.Running,
                String.Empty);

                // Run the Job
                RunJobInternal(job);

                // Set the Job status as Provisioned (i.e. Completed)
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Provisioned,
                    String.Empty);
            }
            catch (Exception ex)
            {
                // Set the Job status as Failed, including the exception details
                ProvisioningRepositoryFactory.Current.UpdateProvisioningJob(
                    job.JobId,
                    ProvisioningJobStatus.Failed,
                    ex.Message);
            }
        }
        public void UpdateProvisioningJob(Guid jobId, ProvisioningJobStatus status, String errorMessage = null)
        {
            // Connect to the Infrastructural Site Collection
            using (var context = PnPPartnerPackContextProvider.GetAppOnlyClientContext(PnPPartnerPackSettings.InfrastructureSiteUrl))
            {
                // Get a reference to the target library
                Web  web  = context.Web;
                List list = web.Lists.GetByTitle(PnPPartnerPackConstants.PnPProvisioningJobs);
                context.Load(list, l => l.RootFolder);

                CamlQuery query = new CamlQuery();
                query.ViewXml =
                    @"<View>
                        <Query>
                            <Where>
                                <Eq>
                                    <FieldRef Name='FileLeafRef' />
                                    <Value Type='Text'>" + jobId + @".job</Value>
                                </Eq>
                            </Where>
                        </Query>
                    </View>";

                ListItemCollection items = list.GetItems(query);
                context.Load(items,
                             includes => includes.IncludeWithDefaultProperties(
                                 j => j[PnPPartnerPackConstants.PnPProvisioningJobStatus],
                                 j => j[PnPPartnerPackConstants.PnPProvisioningJobError],
                                 j => j[PnPPartnerPackConstants.PnPProvisioningJobType]),
                             includes => includes.Include(j => j.File));
                context.ExecuteQueryRetry();

                if (items.Count > 0)
                {
                    ListItem jobItem = items[0];

                    // Update the ProvisioningJob object internal status
                    ProvisioningJob job = GetProvisioningJobStreamFromSharePoint(context, jobItem)
                                          .FromJsonStream((String)jobItem[PnPPartnerPackConstants.PnPProvisioningJobType]);

                    job.Status       = status;
                    job.ErrorMessage = errorMessage;

                    // Update the SharePoint ListItem behind the Provisioning Job item
                    // jobItem[PnPPartnerPackConstants.ContentTypeIdField] = PnPPartnerPackConstants.PnPProvisioningJobContentTypeId;
                    jobItem[PnPPartnerPackConstants.PnPProvisioningJobStatus] = status.ToString();
                    jobItem[PnPPartnerPackConstants.PnPProvisioningJobError]  = errorMessage;

                    jobItem.Update();
                    context.ExecuteQueryRetry();

                    // Update the file
                    list.RootFolder.UploadFile(jobItem.File.Name, job.ToJsonStream(), true);
                }
            }
        }
        protected override void RunJobInternal(ProvisioningJob job)
        {
            SiteCollectionProvisioningJob scj = job as SiteCollectionProvisioningJob;
            if (scj == null)
            {
                throw new ArgumentException("Invalid job type for SiteCollectionProvisioningJobHandler.");
            }

            CreateSiteCollection(scj);
        }
        protected override void RunJobInternal(ProvisioningJob job)
        {
            RefreshSitesJob updateJob = job as RefreshSitesJob;
            if (updateJob == null)
            {
                throw new ArgumentException("Invalid job type for RefreshSitesJobHandler.");
            }

            UpdateTemplates(updateJob);
        }
        protected override void RunJobInternal(ProvisioningJob job)
        {
            RefreshSitesJob updateJob = job as RefreshSitesJob;
            if (updateJob == null)
            {
                throw new ArgumentException("Invalid job type for BrandingJobHandler.");
            }

            UpdateTemplates(updateJob);
        }
        protected override void RunJobInternal(ProvisioningJob job)
        {
            SubSiteProvisioningJob ssj = job as SubSiteProvisioningJob;
            if (ssj == null)
            {
                throw new ArgumentException("Invalid job type for SubSiteProvisioningJobHandler.");
            }

            CreateSubSite(ssj);
        }
Esempio n. 12
0
        protected override void RunJobInternal(ProvisioningJob job)
        {
            BrandingJob brandingJob = job as BrandingJob;

            if (brandingJob == null)
            {
                throw new ArgumentException("Invalid job type for BrandingJobHandler.");
            }

            ApplyBranding(brandingJob);
        }
        protected override void RunJobInternal(ProvisioningJob job)
        {
            SubSiteProvisioningJob ssj = job as SubSiteProvisioningJob;

            if (ssj == null)
            {
                throw new ArgumentException("Invalid job type for SubSiteProvisioningJobHandler.");
            }

            CreateSubSite(ssj);
        }
        internal override void RunJob(ProvisioningJob provisioningJob)
        {
            var siteCollectionProvisioningJob = provisioningJob as SiteCollectionProvisioningJob;

            if (siteCollectionProvisioningJob == null)
            {
                throw new ArgumentException("$(Invalid job type for SiteCollectionPrivisioningJobHandler)");
            }

            CreateSiteCollection(siteCollectionProvisioningJob);
        }
Esempio n. 15
0
        protected override void RunJobInternal(ProvisioningJob job)
        {
            SiteCollectionsBatchJob batchJob = job as SiteCollectionsBatchJob;

            if (batchJob == null)
            {
                throw new ArgumentException("Invalid job type for SiteCollectionsBatchJobHandler.");
            }

            CreateSiteCollectionsBatch(batchJob);
        }
        public void Provision(ProvisioningJob provisioningJob)
        {
            var siteCollectionProvisioningJob = provisioningJob as SiteCollectionProvisioningJob;

            if (siteCollectionProvisioningJob != null)
            {
                ProvisionSiteCollection(siteCollectionProvisioningJob);
            }

            var subSiteProvisioningJob = provisioningJob as SubSiteProvisioningJob;

            if (subSiteProvisioningJob != null)
            {
                ProvisionSubSite(subSiteProvisioningJob);
            }
        }
 protected override void RunJobInternal(ProvisioningJob job)
 {
     // Determine the type of job to run
     if (job is GetProvisioningTemplateJob)
     {
         RunGetProvisioningTemplateJob(job as GetProvisioningTemplateJob);
     }
     else if (job is ApplyProvisioningTemplateJob)
     {
         RunApplyProvisioningTemplateJob(job as ApplyProvisioningTemplateJob);
     }
     else
     {
         throw new ArgumentException("Invalid job type for ProvisioningTemplateJobHandler.");
     }
 }
Esempio n. 18
0
        // This function will get triggered/executed when a new message is written
        // on an Azure Queue called queue.
        public static void ProcessQueueMessage([QueueTrigger(PnPPartnerPackSettings.StorageQueueName)] ContinousJobItem content, TextWriter log)
        {
            log.WriteLine(String.Format("Found Job: {0}", content.JobId));

            // Get the info about the Provisioning Job
            ProvisioningJobInformation jobInfo =
                ProvisioningRepositoryFactory.Current.GetProvisioningJob(content.JobId, true);

            // Get a reference to the Provisioning Job
            ProvisioningJob job = jobInfo.JobFile.FromJsonStream(jobInfo.Type);

            if (PnPPartnerPackSettings.ContinousJobHandlers.ContainsKey(job.GetType()))
            {
                PnPPartnerPackSettings.ContinousJobHandlers[job.GetType()].RunJob(job);
            }

            log.WriteLine("Completed Job execution");
        }
Esempio n. 19
0
        public async Task Status_JobWithStatusResult_ReturnsStatusResult()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            var fakeServiceScope = Substitute.For <IServiceScope>();

            var fakeServiceProvider = Substitute.For <IServiceProvider>();

            fakeServiceProvider
            .GetService(typeof(IMediator))
            .Returns(fakeMediator);

            fakeServiceScope.ServiceProvider.Returns(fakeServiceProvider);

            var fakeProvisioningStateFlow = Substitute.For <IProvisioningStateFlow>();

            var provisioningJob = new ProvisioningJob(
                fakeProvisioningStateFlow,
                fakeServiceScope)
            {
                Exception = new StateUpdateException("dummy", new ConflictResult())
            };
            await provisioningJob.InitializeAsync();

            var fakeProvisioningService = Substitute.For <IProvisioningService>();

            fakeProvisioningService
            .GetJobByIdAsync("some-job-id")
            .Returns(provisioningJob);

            var mapper = AutoMapperFactory.CreateValidMapper();

            var controller = new JobsController(
                mapper,
                fakeProvisioningService);

            //Act
            var status = await controller.Status("some-job-id");

            //Assert
            Assert.IsNotNull(status);
            Assert.AreEqual(StatusCodes.Status409Conflict, status.GetStatusCode());
        }
        private void ProvisionSubSite(ProvisioningJob subSiteProvisioningJob)
        {
            using (PnPMonitoredScope Log = new PnPMonitoredScope("ProvisionSubSite"))
            {
                ProvisioningJobHandler siteCollectionJobHandler
                    = new SubSiteProvisioningJobHandler();

                if (subSiteProvisioningJob.JobId == Guid.Empty)
                {
                    subSiteProvisioningJob.JobId = Guid.NewGuid();
                }

                subSiteProvisioningJob.CreationTimeStamp = DateTime.UtcNow;
                subSiteProvisioningJob.JobType
                    = subSiteProvisioningJob.GetType().FullName;
                subSiteProvisioningJob.Status = JobStatus.Pending;

                siteCollectionJobHandler.RunJob(subSiteProvisioningJob);
            }
        }
Esempio n. 21
0
        public async Task Initialize_FlowGiven_SetsInitialStateFromFlow()
        {
            //Arrange
            var fakeInitialState = Substitute.For <IProvisioningState>();

            var fakeFlow = Substitute.For <IProvisioningStateFlow>();

            fakeFlow
            .GetInitialStateAsync(Arg.Any <InitialStateContext>())
            .Returns(fakeInitialState);

            var provisioningJob = new ProvisioningJob(
                fakeFlow,
                Substitute.For <IServiceScope>());

            //Act
            await provisioningJob.InitializeAsync();

            //Assert
            Assert.AreSame(provisioningJob.CurrentState, fakeInitialState);
        }
Esempio n. 22
0
        public async Task Status_JobExists_ReturnsJobStatus()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            var fakeServiceScope = Substitute.For <IServiceScope>();

            var fakeServiceProvider = Substitute.For <IServiceProvider>();

            fakeServiceProvider
            .GetService(typeof(IMediator))
            .Returns(fakeMediator);

            fakeServiceScope.ServiceProvider.Returns(fakeServiceProvider);

            var fakeProvisioningStateFlow = Substitute.For <IProvisioningStateFlow>();

            var provisioningJob = new ProvisioningJob(
                fakeProvisioningStateFlow,
                fakeServiceScope);
            await provisioningJob.InitializeAsync();

            var fakeProvisioningService = Substitute.For <IProvisioningService>();

            fakeProvisioningService
            .GetJobByIdAsync("some-job-id")
            .Returns(provisioningJob);

            var mapper = AutoMapperFactory.CreateValidMapper();

            var controller = new JobsController(
                mapper,
                fakeProvisioningService);

            //Act
            var status = await controller.Status("some-job-id");

            //Assert
            Assert.IsNotNull(status);
        }
Esempio n. 23
0
        public async Task Initialize_FlowGiven_CallsInitializeOnInitialState()
        {
            //Arrange
            var fakeInitialState = Substitute.For <IProvisioningState>();

            var fakeFlow = Substitute.For <IProvisioningStateFlow>();

            fakeFlow
            .GetInitialStateAsync(Arg.Any <InitialStateContext>())
            .Returns(fakeInitialState);

            var provisioningJob = new ProvisioningJob(
                fakeFlow,
                Substitute.For <IServiceScope>());

            //Act
            await provisioningJob.InitializeAsync();

            //Assert
            await fakeInitialState
            .Received(1)
            .InitializeAsync();
        }
        protected override void RunJobInternal(ProvisioningJob job)
        {
            BrandingJob brandingJob = job as BrandingJob;
            if (brandingJob == null)
            {
                throw new ArgumentException("Invalid job type for BrandingJobHandler.");
            }

            ApplyBranding(brandingJob);
        }
        public Guid EnqueueProvisioningJob(ProvisioningJob job)
        {
            // Prepare the Job ID
            Guid jobId = Guid.NewGuid();

            // Connect to the Infrastructural Site Collection
            using (var context = PnPPartnerPackContextProvider.GetAppOnlyClientContext(PnPPartnerPackSettings.InfrastructureSiteUrl))
            {
                // Set the initial status of the Job
                job.JobId  = jobId;
                job.Status = ProvisioningJobStatus.Pending;

                // Convert the current Provisioning Job into a Stream
                Stream stream = job.ToJsonStream();

                // Get a reference to the target library
                Web  web  = context.Web;
                List list = web.Lists.GetByTitle(PnPPartnerPackConstants.PnPProvisioningJobs);
                Microsoft.SharePoint.Client.File file = list.RootFolder.UploadFile(String.Format("{0}.job", jobId), stream, false);

                Microsoft.SharePoint.Client.User ownerUser = web.EnsureUser(job.Owner);
                context.Load(ownerUser);
                context.ExecuteQueryRetry();

                ListItem item = file.ListItemAllFields;

                item[PnPPartnerPackConstants.ContentTypeIdField]       = PnPPartnerPackConstants.PnPProvisioningJobContentTypeId;
                item[PnPPartnerPackConstants.TitleField]               = job.Title;
                item[PnPPartnerPackConstants.PnPProvisioningJobStatus] = ProvisioningJobStatus.Pending.ToString();
                item[PnPPartnerPackConstants.PnPProvisioningJobError]  = String.Empty;
                item[PnPPartnerPackConstants.PnPProvisioningJobType]   = job.GetType().FullName;

                FieldUserValue ownerUserValue = new FieldUserValue();
                ownerUserValue.LookupId = ownerUser.Id;
                item[PnPPartnerPackConstants.PnPProvisioningJobOwner] = ownerUserValue;

                item.Update();

                context.ExecuteQueryRetry();

                // Check if we need to enqueue a message in the Azure Storage Queue, as well
                // This happens when the Provisioning Job has to be executed in Continous mode
                if (PnPPartnerPackSettings.ContinousJobHandlers.ContainsKey(job.GetType()))
                {
                    // Get the storage account for Azure Storage Queue
                    CloudStorageAccount storageAccount =
                        CloudStorageAccount.Parse(PnPPartnerPackSettings.StorageQueueConnectionString);

                    // Get queue ... and create if it does not exist
                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                    CloudQueue       queue       = queueClient.GetQueueReference(PnPPartnerPackSettings.StorageQueueName);
                    queue.CreateIfNotExists();

                    // Add entry to queue
                    ContinousJobItem content = new ContinousJobItem {
                        JobId = job.JobId
                    };
                    queue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(content)));
                }
            }

            return(jobId);
        }
Esempio n. 26
0
 abstract internal void RunJob(ProvisioningJob provisioningJob);
Esempio n. 27
0
 /// <summary>
 /// Internal implementation for running a Provisioning Job
 /// </summary>
 /// <param name="job">The Provisioning Job to run</param>
 protected abstract void RunJobInternal(ProvisioningJob job);
 /// <summary>
 /// Internal implementation for running a Provisioning Job
 /// </summary>
 /// <param name="job">The Provisioning Job to run</param>
 protected abstract void RunJobInternal(ProvisioningJob job);