Esempio n. 1
0
        public async Task <InvokeResult> AddDeviceRepositoryAsync(DeviceRepository repo, EntityHeader org, EntityHeader user)
        {
            if (EntityHeader.IsNullOrEmpty(repo.RepositoryType))
            {
                return(InvokeResult.FromErrors(new ErrorMessage("Respository Type is a Required Field.")));
            }

            //TODO: When we create a stand-along repo for a user, we will allocate it here.
            if (repo.RepositoryType.Value == RepositoryTypes.NuvIoT ||
                repo.RepositoryType.Value == RepositoryTypes.AzureIoTHub)
            {
                repo.DeviceArchiveStorageSettings = new ConnectionSettings()
                {
                    AccountId = _deviceMgmtSettings.DefaultDeviceTableStorage.AccountId,
                    AccessKey = _deviceMgmtSettings.DefaultDeviceTableStorage.AccessKey
                };

                repo.PEMStorageSettings = new ConnectionSettings()
                {
                    AccountId = _deviceMgmtSettings.DefaultDeviceTableStorage.AccountId,
                    AccessKey = _deviceMgmtSettings.DefaultDeviceTableStorage.AccessKey
                };

                repo.DeviceStorageSettings = new ConnectionSettings()
                {
                    Uri          = _deviceMgmtSettings.DefaultDeviceStorage.Uri,
                    AccessKey    = _deviceMgmtSettings.DefaultDeviceStorage.AccessKey,
                    ResourceName = _deviceMgmtSettings.DefaultDeviceStorage.ResourceName
                };

                repo.DeviceWatchdogStorageSettings = new ConnectionSettings()
                {
                    Uri          = _deviceMgmtSettings.DefaultDeviceTableStorage.Uri,
                    AccessKey    = _deviceMgmtSettings.DefaultDeviceTableStorage.AccessKey,
                    ResourceName = $"Watchdlog{repo.Id}{repo.Key}"
                };
            }
            else if (repo.RepositoryType.Value == RepositoryTypes.Local)
            {
                //TODO: when we update this for remote server access, we need to figure out what if anything needs to be secured.
                repo.DeviceArchiveStorageSettings = new ConnectionSettings()
                {
                    Uri          = "mysql",
                    ResourceName = "nuviot",
                    Port         = "3306"
                };

                repo.DeviceWatchdogStorageSettings = new ConnectionSettings()
                {
                    Uri          = "mysql",
                    ResourceName = "nuviot",
                    Port         = "3306"
                };

                repo.PEMStorageSettings = new ConnectionSettings()
                {
                    Uri          = "mongodb",
                    Port         = "27017",
                    ResourceName = "nuviot"
                };

                repo.DeviceStorageSettings = new ConnectionSettings()
                {
                    Uri          = "mongodb",
                    ResourceName = "nuviot",
                    Port         = "27017"
                };
            }
            /* If repository type == dedicated, values must be provided when inserting the record, this is confirmed on the validation in the next step */

            ValidationCheck(repo, Actions.Create);
            await AuthorizeAsync(repo, AuthorizeResult.AuthorizeActions.Create, user, org);

            var addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.DeviceArchiveStorageSettings));

            if (!addKeyResult.Successful)
            {
                return(addKeyResult.ToInvokeResult());
            }

            repo.DeviceArchiveStorageSettingsSecureId = addKeyResult.Result;
            repo.DeviceArchiveStorageSettings         = null;

            addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.DeviceStorageSettings));

            if (!addKeyResult.Successful)
            {
                return(addKeyResult.ToInvokeResult());
            }

            repo.DeviceStorageSecureSettingsId = addKeyResult.Result;
            repo.DeviceStorageSettings         = null;

            addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.DeviceWatchdogStorageSettings));

            if (!addKeyResult.Successful)
            {
                return(addKeyResult.ToInvokeResult());
            }

            repo.DeviceWatchdogStorageSecureId = addKeyResult.Result;
            repo.DeviceWatchdogStorageSettings = null;


            addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.PEMStorageSettings));

            if (!addKeyResult.Successful)
            {
                return(addKeyResult.ToInvokeResult());
            }

            repo.PEMStorageSettingsSecureId = addKeyResult.Result;
            repo.PEMStorageSettings         = null;

            if (!string.IsNullOrEmpty(repo.AccessKey))
            {
                var addSecretResult = await _secureStorage.AddSecretAsync(org, repo.AccessKey);

                if (!addSecretResult.Successful)
                {
                    return(addSecretResult.ToInvokeResult());
                }

                repo.SecureAccessKeyId = addSecretResult.Result;
                repo.AccessKey         = null;
            }

            await _deviceRepositoryRepo.AddDeviceRepositoryAsync(repo);

            return(InvokeResult.Success);
        }