public async Task <DeviceGroupApiModel> CreateDefaultDeviceGroupAsync(string tenantId)
        {
            DeviceGroupApiModel defaultGroup = new DeviceGroupApiModel
            {
                DisplayName = "Default",
                Conditions  = new List <DeviceGroupConditionModel>(),
            };
            string url = this.RequestUrl("devicegroups/");

            return(await this.requestHelper.ProcessRequestAsync(HttpMethod.Post, url, defaultGroup, tenantId));
        }
        public async Task CreateAsyncThrowsConflictingResource()
        {
            this.mockStorage
            .Setup(x => x.CreateDeviceGroupAsync(
                       It.IsAny <DeviceGroup>()))
            .ThrowsAsync(new ConflictingResourceException());

            var group = new DeviceGroup
            {
                Id          = this.rand.NextString(),
                DisplayName = this.rand.NextString(),
                Conditions  = new List <DeviceGroupCondition>(),
            };

            var model = new DeviceGroupApiModel(group);

            await Assert.ThrowsAsync <ConflictingResourceException>(async() =>
                                                                    await this.controller.CreateAsync(model));
        }
Esempio n. 3
0
        public async Task GetDevicesAsyncTest()
        {
            var groupId    = rand.NextString();
            var conditions = new DeviceGroupConditionApiModel[] { };
            var group      = new DeviceGroupApiModel
            {
                Conditions = conditions
            };
            var deviceIds = new string[] { };

            mockHttpClient
            .Setup(x => x.GetAsync <DeviceGroupApiModel>(
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <bool>()))
            .ReturnsAsync(group);

            mockDevices
            .Setup(x => x.GetListAsync(It.IsAny <IEnumerable <DeviceGroupConditionApiModel> >()))
            .ReturnsAsync(deviceIds);

            var result = await deviceGroups.GetDevicesAsync(groupId);

            Assert.Equal(result, deviceIds);

            mockHttpClient
            .Verify(x => x.GetAsync <DeviceGroupApiModel>(
                        It.Is <string>(s => s == $"{config.DeviceGroupsUrl}/devicegroups/{groupId}"),
                        It.IsAny <string>(),
                        It.Is <bool>(b => b)),
                    Times.Once);

            mockDevices
            .Verify(x => x.GetListAsync(
                        It.Is <IEnumerable <DeviceGroupConditionApiModel> >(c => c == conditions)),
                    Times.Once);
        }
Esempio n. 4
0
        /**
         * Given a device group definition, returns the list of device ids in the group.
         * Will retry up to MAX_RETRY_COUNT if there is a failure, doubling the sleep time
         * on each retry.
         */
        private async Task <IEnumerable <string> > GetDevicesAsync(DeviceGroupApiModel group)
        {
            int       retryCount     = 0;
            Exception innerException = null;
            int       retryPause     = this.servicesConfig.InitialIotHubManagerRetryIntervalMs;

            while (retryCount < this.servicesConfig.IotHubManagerRetryCount)
            {
                if (retryCount > 0)
                {
                    this.thread.Sleep(retryPause);
                    retryPause *= this.servicesConfig.IotHubManagerRetryIntervalIncreaseFactor;
                }

                try
                {
                    if (@group?.Conditions == null)
                    {
                        this.logger.Error("Device group definitions or conditions were null", () => new { });
                        return(new string[] { });
                    }

                    return(await this.devices.GetListAsync(group.Conditions));
                }
                catch (Exception e)
                {
                    retryCount++;
                    string errorMessage = $"Failed to get list of devices. {this.servicesConfig.IotHubManagerRetryCount - retryCount} retries remaining.";
                    this.logger.Warn(errorMessage, () => new { e });
                    innerException = e;
                }
            }

            this.logger.Error("Failed to get list of devices after retrying", () => new { innerException, this.servicesConfig.IotHubManagerRetryCount });
            throw new ExternalDependencyException("Unable to get list of devices", innerException);
        }
Esempio n. 5
0
 public async Task <DeviceGroupApiModel> UpdateAsync(string id, [FromBody] DeviceGroupApiModel input)
 {
     return(new DeviceGroupApiModel(await this.storage.UpdateDeviceGroupAsync(id, input.ToServiceModel(), input.ETag)));
 }
Esempio n. 6
0
 public async Task <DeviceGroupApiModel> CreateAsync([FromBody] DeviceGroupApiModel input)
 {
     return(new DeviceGroupApiModel(await this.storage.CreateDeviceGroupAsync(input.ToServiceModel())));
 }