private void AddIotDpsLinkedHub() { IotHubDefinitionDescription iotDpsHub = new IotHubDefinitionDescription() { ConnectionString = this.IotHubConnectionString, Location = this.IotHubLocation, AllocationWeight = this.AllocationWeight, ApplyAllocationPolicy = this.ApplyAllocationPolicy.IsPresent }; ProvisioningServiceDescription provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name); provisioningServiceDescription.Properties.IotHubs.Add(iotDpsHub); IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription); IList <IotHubDefinitionDescription> iotDpsHubs = GetIotDpsHubs(this.ResourceGroupName, this.Name); if (iotDpsHubs.Count == 1) { this.WritePSObject(iotDpsHubs[0]); } else { this.WritePSObjects(iotDpsHubs); } }
public override void ExecuteCmdlet() { if (ShouldProcess(Name, DPSResources.RemoveLinkedHub)) { if (ParameterSetName.Equals(InputObjectParameterSet)) { this.ResourceGroupName = this.InputObject.ResourceGroupName; this.Name = this.InputObject.Name; this.LinkedHubName = this.InputObject.LinkedHubName; } if (ParameterSetName.Equals(ResourceIdParameterSet)) { this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId); this.Name = IotDpsUtils.GetIotDpsName(this.ResourceId); } ProvisioningServiceDescription provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name); IList <IotHubDefinitionDescription> linkedHubs = GetIotDpsHubs(this.ResourceGroupName, this.Name); IotHubDefinitionDescription linkedHub = linkedHubs.FirstOrDefault(hubs => hubs.Name.Equals(this.LinkedHubName)); provisioningServiceDescription.Properties.IotHubs = linkedHubs.Where(x => x.ConnectionString != linkedHub.ConnectionString).ToList(); IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription); if (PassThru) { this.WriteObject(true); } } }
public static PSIotHubDefinitionDescription ToPSIotHubDefinitionDescription(IotHubDefinitionDescription linkedHub, string resourceGroupName, string name) { PSIotHubDefinitionDescription psIotHubDefinitionDescription = ConvertObject <IotHubDefinitionDescription, PSIotHubDefinitionDescription>(linkedHub); psIotHubDefinitionDescription.ResourceGroupName = resourceGroupName; psIotHubDefinitionDescription.Name = name; return(psIotHubDefinitionDescription); }
private void UpdateIotDpsLinkedHub() { ProvisioningServiceDescription provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name); IotHubDefinitionDescription iotHub = provisioningServiceDescription.Properties.IotHubs.FirstOrDefault(x => x.Name.Equals(this.LinkedHubName, StringComparison.OrdinalIgnoreCase)); iotHub.ApplyAllocationPolicy = this.ApplyAllocationPolicy.IsPresent; iotHub.AllocationWeight = this.AllocationWeight ?? iotHub.AllocationWeight; IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription); this.WriteObject(IotDpsUtils.ToPSIotHubDefinitionDescription(GetIotDpsHubs(this.ResourceGroupName, this.Name, this.LinkedHubName), this.ResourceGroupName, this.Name), false); }
private void WritePSObject(IotHubDefinitionDescription iotDpsHub) { this.WriteObject(IotDpsUtils.ToPSIotHubDefinitionDescription(iotDpsHub, this.ResourceGroupName, this.Name), false); }
public async Task CreateAndDelete() { using var context = MockContext.Start(GetType()); var testName = "unitTestingDPSLinkedHubCreateUpdateDelete"; Initialize(context); var iotHubClient = GetClient <IotHubClient>(context); ResourceGroup resourceGroup = await GetResourceGroupAsync(testName).ConfigureAwait(false); ProvisioningServiceDescription testedService = await GetServiceAsync(resourceGroup.Name, testName).ConfigureAwait(false); IotHubDescription iotHub = await GetIotHubAsync(iotHubClient, resourceGroup, testName).ConfigureAwait(false); IPage <SharedAccessSignatureAuthorizationRule> keys = await iotHubClient.IotHubResource .ListKeysAsync( resourceGroup.Name, iotHub.Name) .ConfigureAwait(false); SharedAccessSignatureAuthorizationRule key = keys.FirstOrDefault(x => x.Rights.HasFlag(AccessRights.ServiceConnect)); var connectionString = $"HostName={iotHub.Name}.azure-devices.net;SharedAccessKeyName={key.KeyName};SharedAccessKey={key.PrimaryKey}"; testedService.Properties.IotHubs = testedService.Properties.IotHubs ?? new List <IotHubDefinitionDescription>(1); testedService.Properties.IotHubs.Add(new IotHubDefinitionDescription(connectionString, resourceGroup.Location, name: testName)); ProvisioningServiceDescription updatedInstance = await _provisioningClient.IotDpsResource .CreateOrUpdateAsync( resourceGroup.Name, testName, testedService) .ConfigureAwait(false); IotHubDefinitionDescription returnedHub = updatedInstance.Properties.IotHubs .FirstOrDefault(x => x.Name.Equals($"{iotHub.Name}.azure-devices.net")); returnedHub.Should().NotBeNull(); connectionString = returnedHub.ConnectionString; bool hasUpdatedApplyPolicy = !(returnedHub.ApplyAllocationPolicy ?? false); returnedHub.ApplyAllocationPolicy = hasUpdatedApplyPolicy; int updatedPolicyWeight = Helpers.Constants.RandomAllocationWeight; returnedHub.AllocationWeight = updatedPolicyWeight; updatedInstance = await _provisioningClient.IotDpsResource .CreateOrUpdateAsync( resourceGroup.Name, testName, updatedInstance) .ConfigureAwait(false); IotHubDefinitionDescription updatedHub = updatedInstance.Properties.IotHubs .FirstOrDefault(x => x.ConnectionString == connectionString); updatedHub.Should().NotBeNull(); updatedHub.ApplyAllocationPolicy.Should().Be(hasUpdatedApplyPolicy); updatedHub.AllocationWeight.Should().Be(updatedPolicyWeight); // Delete the linked hub testedService.Properties.IotHubs = testedService.Properties.IotHubs .Except(testedService.Properties.IotHubs.Where(x => x.Name == testName)) .ToList(); updatedInstance = await _provisioningClient.IotDpsResource .CreateOrUpdateAsync( resourceGroup.Name, testName, testedService) .ConfigureAwait(false); updatedInstance.Properties.IotHubs.Should().NotContain(connectionString); }