Exemplo n.º 1
0
        private async Task OnExecuteAsync()
        {
            try
            {
                string fullUserObjectIdsFilePath = Path.GetFullPath(UserObjectIdsFile);
                string userAadObjectIdsString    = await File.ReadAllTextAsync(fullUserObjectIdsFilePath);

                UserAadObjectIdsDescription userAadObjectIds =
                    JsonConvert.DeserializeObject <UserAadObjectIdsDescription>(userAadObjectIdsString);
                if (!userAadObjectIds.AreRequiredValuesFilled())
                {
                    Console.WriteLine($"The {nameof( UserObjectIdsFile )} must have all the required properties filled." +
                                      " (Head Of Operations, Hotel Brand 1 Manager, Hotel 1 Manager, and Hotel 1 Employee)");
                    return;
                }

                HttpClient httpClient = await HttpClientHelper.GetHttpClientAsync(DigitalTwinsApiEndpoint, AadInstance, Tenant,
                                                                                  DigitalTwinsResourceId, ClientId, ClientSecret);

                string fullPathToDigitalTwinsProvisioningFile = Path.GetFullPath(DigitalTwinsProvisioningFile);
                directoryContainingDigitalTwinsProvisioningFile = Path.GetDirectoryName(fullPathToDigitalTwinsProvisioningFile);

                Console.WriteLine("Loading the provisioning files...");

                ProvisioningDescription provisioningDescription = ProvisioningHelper.LoadSmartHotelProvisioning(fullPathToDigitalTwinsProvisioningFile);

                Console.WriteLine("Successfully loaded provisioning files.");

                Console.WriteLine("Creating spaces and endpoints...");



                await CreateSpacesAsync(httpClient, provisioningDescription.spaces, Guid.Empty, Guid.Empty, userAadObjectIds);

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine($"Created {_spacesCreatedCount} spaces...");
                Console.WriteLine();
                Console.WriteLine();

                await CreateEndpointsAsync(httpClient, provisioningDescription.endpoints);

                if (!string.IsNullOrEmpty(OutputFile))
                {
                    IDictionary <string, List <DeviceDescription> > allDevices =
                        provisioningDescription.spaces.GetAllDeviceDescriptionsByDeviceIdPrefix(string.Empty);

                    await File.WriteAllTextAsync(OutputFile,
                                                 JsonConvert.SerializeObject(new SortedDictionary <string, List <DeviceDescription> >(allDevices)));
                }

                Console.WriteLine();
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error occurred during Digital Twins provisioning: {ex}");
            }
        }
Exemplo n.º 2
0
 private async Task CreateSpacesAsync(HttpClient httpClient, IList <SpaceDescription> spaceDescriptions,
                                      Guid parentId, Guid keystoreId, UserAadObjectIdsDescription userAadObjectIds)
 {
     foreach (SpaceDescription spaceDescription in spaceDescriptions)
     {
         await CreateSpaceAsync(httpClient, spaceDescription, parentId, keystoreId, userAadObjectIds);
     }
 }
Exemplo n.º 3
0
        private async Task CreateUserRoleAssignmentsAsync(HttpClient httpClient, IList <string> users, Guid spaceId,
                                                          UserAadObjectIdsDescription userAadObjectIds)
        {
            if (spaceId == Guid.Empty)
            {
                throw new ArgumentException($"User Role Assignment must have a {nameof( spaceId )}");
            }

            foreach (string user in users)
            {
                if (userAadObjectIds.TryGetValue(user, out string oid))
                {
                    string spacePath = await SpaceHelpers.GetSpaceFullPathAsync(httpClient, spaceId);

                    if (string.IsNullOrWhiteSpace(spacePath))
                    {
                        Console.WriteLine($"Unable to get the full path for the current space. Cannot create a role assignment. (Space Id: {spaceId})");
                        continue;
                    }

                    var roleAssignment = new RoleAssignment
                    {
                        RoleId       = RoleAssignment.RoleIds.User,
                        ObjectId     = oid,
                        ObjectIdType = RoleAssignment.ObjectIdTypes.UserId,
                        TenantId     = Tenant,
                        Path         = spacePath
                    };

                    var existingRoleAssignment = await roleAssignment.GetUniqueRoleAssignmentAsync(httpClient);

                    if (existingRoleAssignment == null)
                    {
                        await roleAssignment.CreateRoleAssignmentAsync(httpClient, JsonSerializerSettings);
                    }
                    else
                    {
                        Console.WriteLine($"{nameof( RoleAssignment )} already exists, so skipping creation.");
                    }
                }
            }
        }
Exemplo n.º 4
0
        private async Task CreateSpaceAsync(HttpClient httpClient, SpaceDescription spaceDescription,
                                            Guid parentId, Guid keystoreId, UserAadObjectIdsDescription userAadObjectIds)
        {
            Space spaceToCreate = spaceDescription.ToDigitalTwins(parentId);
            Space existingSpace = await SpaceHelpers.GetUniqueSpaceAsync(httpClient, spaceToCreate.Name, parentId, JsonSerializerSettings);

            var spaceId = !string.IsNullOrWhiteSpace(existingSpace?.Id)
                                ? Guid.Parse(existingSpace.Id)
                                : await CreateSpaceAsync(httpClient, spaceToCreate);

            Console.WriteLine();

            if (spaceId != Guid.Empty)
            {
                Guid keystoreIdToUseForChildren = keystoreId;
                // Keystore creation must happen first to ensure that devices down the tree can get their SaS tokens from it
                if (!string.IsNullOrWhiteSpace(spaceDescription.keystoreName))
                {
                    Keystore existingKeystore = await KeyStoresHelper.GetUniqueKeystoreAsync(httpClient,
                                                                                             spaceDescription.keystoreName, spaceId, JsonSerializerSettings);

                    Guid createdKeystoreId = !string.IsNullOrWhiteSpace(existingKeystore?.Id)
                                                ? Guid.Parse(existingKeystore.Id)
                                                : await CreateKeystoreAsync(httpClient, spaceDescription.keystoreName, spaceId);

                    if (createdKeystoreId != Guid.Empty)
                    {
                        keystoreIdToUseForChildren = createdKeystoreId;
                    }
                }

                // Resources must be created next to ensure that devices created down the tree will succeed
                if (spaceDescription.resources != null)
                {
                    await CreateResourcesAsync(httpClient, spaceDescription.resources, spaceId);
                }

                // Types must be created next to ensure that devices/sensors created down the tree will succeed
                if (spaceDescription.types != null)
                {
                    await CreateTypesAsync(httpClient, spaceDescription.types, spaceId);
                }

                if (spaceDescription.devices != null)
                {
                    await CreateDevicesAsync(httpClient, spaceDescription.devices, keystoreIdToUseForChildren, spaceId);
                }

                if (spaceDescription.matchers != null)
                {
                    await CreateMatchersAsync(httpClient, spaceDescription.matchers, spaceId);
                }

                if (spaceDescription.userDefinedFunctions != null)
                {
                    await CreateUserDefinedFunctionsAsync(httpClient, spaceDescription.userDefinedFunctions, spaceId);
                }

                if (spaceDescription.roleAssignments != null)
                {
                    await CreateRoleAssignmentsAsync(httpClient, spaceDescription.roleAssignments, spaceId);
                }

                if (spaceDescription.users != null)
                {
                    await CreateUserRoleAssignmentsAsync(httpClient, spaceDescription.users, spaceId, userAadObjectIds);
                }

                if (spaceDescription.propertyKeys != null)
                {
                    await CreatePropertyKeysAsync(httpClient, spaceDescription.propertyKeys, spaceId);
                }

                if (spaceDescription.properties != null)
                {
                    await CreatePropertiesAsync(httpClient, spaceDescription.properties, spaceId);
                }

                if (spaceDescription.spaces != null)
                {
                    await CreateSpacesAsync(httpClient, spaceDescription.spaces, spaceId, keystoreIdToUseForChildren, userAadObjectIds);
                }
            }
        }