private void CreateNewSiteCollection(object modelHost, ClientContext context, O365SiteDefinition o365SiteModel)
        {
            var tenant = new Tenant(context);
            var siteCollectionProperties = MapSiteCollectionProperties(o365SiteModel);

            tenant.CreateSite(siteCollectionProperties);

            context.Load(tenant);
            context.ExecuteQuery();

            // from here site collection is being provisioned by O365 asynchronously
            // we need to have sorta delay with querying new site collection and continue deployment flow once it has been fully provisioned

            var siteCreated = WaitForSiteProvision(context, o365SiteModel.Url);

            if (siteCreated)
            {
                WithExistingSiteAndWeb(context, o365SiteModel.Url, (site, web) =>
                {
                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = site,
                        ObjectType = typeof(Site),
                        ObjectDefinition = o365SiteModel,
                        ModelHost = modelHost
                    });
                });
            }
            else
            {
                // probably, we should throw an exception as site was not created

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = null,
                    ObjectType = typeof(Site),
                    ObjectDefinition = o365SiteModel,
                    ModelHost = modelHost
                });
            }
        }
        private void DeployO365SiteCollection(object modelHost, ClientContext context, O365SiteDefinition o365SiteModel)
        {
            var doesSiteExist = false;
            var currentSiteCollection = (Site)null;

            WithExistingSiteAndWeb(context, o365SiteModel.Url, (site, web) =>
            {
                doesSiteExist = site != null;

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = site,
                    ObjectType = typeof(Site),
                    ObjectDefinition = o365SiteModel,
                    ModelHost = modelHost
                });

                if (doesSiteExist)
                {
                    // TODO, some updates if possible


                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = site,
                        ObjectType = typeof(Site),
                        ObjectDefinition = o365SiteModel,
                        ModelHost = modelHost
                    });
                }
            });

            if (doesSiteExist)
                return;

            CreateNewSiteCollection(modelHost, context, o365SiteModel);
        }
 private SiteCreationProperties MapSiteCollectionProperties(O365SiteDefinition o365SiteModel)
 {
     return new SiteCreationProperties()
     {
         Url = o365SiteModel.Url,
         Owner = o365SiteModel.Owner,
         Template = o365SiteModel.Template
     };
 }