/// <summary> /// Adds the root tenant to the collection, using the <see cref="ITenancyService"/>. /// </summary> /// <param name="services">The service collection to which to add the root tenant.</param> /// <returns>The configured service collection.</returns> public static IServiceCollection AddTenantServiceClientRootTenant(this IServiceCollection services) { if (services.Any(s => typeof(RootTenant).IsAssignableFrom(s.ServiceType))) { return(services); } services.AddContent(contentFactory => contentFactory.RegisterTransientContent <Tenant>()); // Construct a root tenant from the tenant retrieved from the service, using the // root tenant ID. services.AddSingleton(s => { ITenancyService tenancyService = s.GetRequiredService <ITenancyService>(); ITenantMapper tenantMapper = s.GetRequiredService <ITenantMapper>(); IPropertyBagFactory propertyBagFactory = s.GetRequiredService <IPropertyBagFactory>(); ITenant fetchedRootTenant = tenantMapper.MapTenant(tenancyService.GetTenant(RootTenant.RootTenantId)); var localRootTenant = new RootTenant(propertyBagFactory); IReadOnlyDictionary <string, object> propertiesToSetOrAdd = fetchedRootTenant.Properties.AsDictionary(); localRootTenant.UpdateProperties(propertiesToSetOrAdd); return(localRootTenant); }); return(services); }
/// <summary> /// Adds services an Azure Blob storage-based implementation of <see cref="ITenantProvider"/>. /// </summary> /// <param name="services">The service collection.</param> /// <param name="getRootTenantStorageConfiguration"> /// A function that returns the <see cref="BlobStorageConfiguration"/> that will be used for the root tenant to /// determine where to store its children. /// </param> /// <returns>The modified service collection.</returns> public static IServiceCollection AddTenantProviderBlobStore( this IServiceCollection services, Func <IServiceProvider, BlobStorageConfiguration> getRootTenantStorageConfiguration) { if (services.Any(s => typeof(ITenantProvider).IsAssignableFrom(s.ServiceType))) { return(services); } services.AddRequiredTenancyServices(); services.AddSingleton(sp => { BlobStorageConfiguration rootTenantStorageConfig = getRootTenantStorageConfiguration(sp); IPropertyBagFactory propertyBagFactory = sp.GetRequiredService <IPropertyBagFactory>(); var rootTenant = new RootTenant(propertyBagFactory); rootTenant.UpdateProperties( values => values.AddBlobStorageConfiguration( TenantProviderBlobStore.ContainerDefinition, rootTenantStorageConfig)); ITenantCloudBlobContainerFactory tenantCloudBlobContainerFactory = sp.GetRequiredService <ITenantCloudBlobContainerFactory>(); IJsonSerializerSettingsProvider serializerSettingsProvider = sp.GetRequiredService <IJsonSerializerSettingsProvider>(); return(new TenantProviderBlobStore(rootTenant, propertyBagFactory, tenantCloudBlobContainerFactory, serializerSettingsProvider)); }); services.AddSingleton <ITenantStore>(sp => sp.GetRequiredService <TenantProviderBlobStore>()); services.AddSingleton <ITenantProvider>(sp => sp.GetRequiredService <TenantProviderBlobStore>()); return(services); }
/// <summary> /// Initializes a new instance of the <see cref="ClientTenantProvider"/> class. /// </summary> /// <param name="root">The Root tenant.</param> /// <param name="tenantService">The tenant service.</param> /// <param name="tenantMapper">The tenant mapper to use.</param> /// <param name="jsonSerializerSettingsProvider">The JSON serializer settings provider.</param> public ClientTenantStore( RootTenant root, ITenancyService tenantService, ITenantMapper tenantMapper, IJsonSerializerSettingsProvider jsonSerializerSettingsProvider) : base(root, tenantService, tenantMapper) { this.jsonSerializerSettingsProvider = jsonSerializerSettingsProvider ?? throw new ArgumentNullException(nameof(jsonSerializerSettingsProvider)); }
public static void Main() { // Enter credentials for Basic Authentication. Console.Write("Enter username: "******"Enter password: "******"../../../templates/tenants.json"); // Create new partner tenant under the root tenant of the Cloud instance. Tenant partnerTenant = new Tenant(); partnerTenant = JsonConvert.DeserializeObject <Tenant>(tenantJson); partnerTenant.name = "IVAYLO_TSVETKOV"; partnerTenant.kind = "partner"; partnerTenant.parent_id = rootTenant.items[0].id; string postData = JsonConvert.SerializeObject(partnerTenant); string partnerTenantInfo = partnerTenant.PostTenant(username, password, postData); // Store the info of the created partner into an object. TenantInfo createdPartner = new TenantInfo(); createdPartner = JsonConvert.DeserializeObject <TenantInfo>(partnerTenantInfo); // Get the information of all applications and store it into an object. Application applications = new Application(); string applicationsInfo = applications.GetApplicationsInfo(username, password); applications = JsonConvert.DeserializeObject <Application>(applicationsInfo); // Declare variables for storing the activated applications. string activatedApplications = "{\"items\": ["; string tempApp; // Enable "Backup" application and disable "File Sync & Share" application. // Looping all applications to find the required ones for purpose of the given task. // Storing all activated applications into the activatedApplications variable for later use. for (int i = 0; i < applications.items.Length; i++) { if (applications.items[i].name == "Backup") { createdPartner.EnableApplication(username, password, applications.items[i].id, createdPartner.id); tempApp = JsonConvert.SerializeObject(applications.items[i]); activatedApplications = activatedApplications + tempApp + ","; } else if (applications.items[i].name == "File Sync & Share") { createdPartner.DisableApplication(username, password, applications.items[i].id, createdPartner.id); } } // Trimming the trailing character and concatenate additional characters to build the json. activatedApplications = activatedApplications.Remove(activatedApplications.Length - 1); activatedApplications = activatedApplications + "]}"; // Load the json template for enabling of the offering items. string offeringItemsJson = File.ReadAllText("../../../templates/offering_items.json"); // Instantiate offering items object and deserialize the json template. OfferingItems offeringItems = new OfferingItems(); offeringItems = JsonConvert.DeserializeObject <OfferingItems>(offeringItemsJson); // Declare variable for storing the enabled offering items. string activatedOfferingItems = "{\"offering_items\": ["; // Looping all offering items and set enabled/disabled status according to task requirements. // Storing all offering items into the activatedOfferingItems variable for later use. for (int i = 0; i < offeringItems.offering_items.Length; i++) { if (offeringItems.offering_items[i].name.Contains("o365")) { offeringItems.offering_items[i].status = 0; } else { tempApp = JsonConvert.SerializeObject(offeringItems.offering_items[i]); activatedOfferingItems = activatedOfferingItems + tempApp + ","; } } // Trimming the trailing character and concatenate additional characters to build the json. activatedOfferingItems = activatedOfferingItems.Remove(activatedOfferingItems.Length - 1); activatedOfferingItems = activatedOfferingItems + "]}"; // Enable the offering items according to the configured status. string putData = JsonConvert.SerializeObject(offeringItems); offeringItems.EnableOfferingItems(username, password, createdPartner.id, putData); // Load the json template for creating of a new user. string userJson = File.ReadAllText("../../../templates/user.json"); // Creating a new user under the created partner tenant. User partnerUser = new User(); partnerUser = JsonConvert.DeserializeObject <User>(userJson); partnerUser.tenant_id = createdPartner.id; postData = JsonConvert.SerializeObject(partnerUser); string partnerUserInfo = partnerUser.PostUser(username, password, postData); // Store the info of the created partner user into an object. UserInfo createdPartnerUser = new UserInfo(); createdPartnerUser = JsonConvert.DeserializeObject <UserInfo>(partnerUserInfo); // Load the json template for assigning role on the user. string newUserRole = File.ReadAllText("../../../templates/user_roles.json"); // Setting partner_admin role on the created partner user. Roles partnerUserRole = new Roles(); partnerUserRole = JsonConvert.DeserializeObject <Roles>(newUserRole); partnerUserRole.items[0].trustee_id = createdPartnerUser.id; partnerUserRole.items[0].tenant_id = createdPartnerUser.tenant_id; partnerUserRole.items[0].role_id = "partner_admin"; putData = JsonConvert.SerializeObject(partnerUserRole); partnerUserRole.PutAccessPolicies(username, password, createdPartnerUser.id, putData); // Create new end-user/customer tenant under the partner tenant. Tenant customerTenant = new Tenant(); customerTenant = JsonConvert.DeserializeObject <Tenant>(tenantJson); customerTenant.name = "End-User"; customerTenant.kind = "customer"; customerTenant.parent_id = createdPartner.id; // Post the tenant and store the info of the created customer into an object. postData = JsonConvert.SerializeObject(customerTenant); string customerTenantInfo = customerTenant.PostTenant(username, password, postData); TenantInfo createdCustomer = new TenantInfo(); createdCustomer = JsonConvert.DeserializeObject <TenantInfo>(customerTenantInfo); // Inherit the activated applications from the partner tenant to the customer tenant. applications = JsonConvert.DeserializeObject <Application>(activatedApplications); for (int i = 0; i < applications.items.Length; i++) { createdCustomer.EnableApplication(username, password, applications.items[i].id, createdCustomer.id); } // Inherit the activated offering items from the partner tenant to the customer tenant. offeringItems.EnableOfferingItems(username, password, createdCustomer.id, activatedOfferingItems); // Load the json template for creating of a backup user. string backupUserJson = File.ReadAllText("../../../templates/backup_user.json"); // Creating a backup user under the created customer tenant. User backupUser = new User(); backupUser = JsonConvert.DeserializeObject <User>(backupUserJson); backupUser.tenant_id = createdCustomer.id; backupUser.contact.email = "*****@*****.**"; backupUser.login = backupUser.contact.email; backupUser.contact.firstname = "Backup"; backupUser.contact.lastname = "User"; postData = JsonConvert.SerializeObject(backupUser); string customerUserInfo = partnerUser.PostUser(username, password, postData); // Store the info of the created backup user into an object. UserInfo createdBackupUser = new UserInfo(); createdBackupUser = JsonConvert.DeserializeObject <UserInfo>(customerUserInfo); // Setting backup_user role on the created backup user. Roles backupUserRole = new Roles(); backupUserRole = JsonConvert.DeserializeObject <Roles>(newUserRole); backupUserRole.items[0].trustee_id = createdBackupUser.id; backupUserRole.items[0].tenant_id = createdBackupUser.tenant_id; backupUserRole.items[0].role_id = "backup_user"; backupUserRole.items[0].trustee_type = "user"; backupUserRole.items[0].version = 0; putData = JsonConvert.SerializeObject(backupUserRole); backupUserRole.PutAccessPolicies(username, password, createdBackupUser.id, putData); }
/// <summary> /// Initializes a new instance of the <see cref="ClientTenantProvider"/> class. /// </summary> /// <param name="root">The Root tenant.</param> /// <param name="tenantService">The tenant service.</param> /// <param name="tenantMapper">The tenant mapper to use.</param> public ClientTenantProvider(RootTenant root, ITenancyService tenantService, ITenantMapper tenantMapper) { this.Root = root ?? throw new ArgumentNullException(nameof(root)); this.TenantService = tenantService ?? throw new ArgumentNullException(nameof(tenantService)); this.TenantMapper = tenantMapper ?? throw new ArgumentNullException(nameof(tenantMapper)); }
public FakeTenantProvider(RootTenant rootTenant) { this.Root = rootTenant; }