public override void ExecuteCmdlet() { if (ShouldProcess(this.IotHubName, Properties.Resources.AddIotHubConfiguration)) { IotHubDescription iotHubDescription; if (ParameterSetName.Equals(InputObjectParameterSet)) { this.ResourceGroupName = this.InputObject.Resourcegroup; this.IotHubName = this.InputObject.Name; iotHubDescription = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject); } else { if (ParameterSetName.Equals(ResourceIdParameterSet)) { this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); } iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName); } IEnumerable <SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName); SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryWrite); PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); PSConfiguration psConfiguration = new PSConfiguration(); psConfiguration.Id = this.Name; psConfiguration.Priority = this.Priority; psConfiguration.TargetCondition = string.IsNullOrEmpty(this.TargetCondition) ? "" : this.TargetCondition; psConfiguration.Labels = new Hashtable(); psConfiguration.Metrics = new PSConfigurationMetrics() { Queries = new Hashtable(), Results = new Hashtable() }; psConfiguration.Content = new PSConfigurationContent() { DeviceContent = new Hashtable(), ModulesContent = new Hashtable() }; if (this.IsParameterBound(c => c.Label)) { psConfiguration.Labels = this.Label; } if (this.IsParameterBound(c => c.Metric)) { psConfiguration.Metrics.Queries = this.Metric; } psConfiguration.Content.ModulesContent = this.IsParameterBound(c => c.ModulesContent) ? this.ModulesContent : this.GetEdgeConfigurationContent(); this.WriteObject(IotHubDataPlaneUtils.ToPSDeployment(registryManager.AddConfigurationAsync(IotHubDataPlaneUtils.ToConfiguration(psConfiguration)).GetAwaiter().GetResult())); } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.IotHubName, Properties.Resources.SetIotHubConfiguration)) { IotHubDescription iotHubDescription; if (ParameterSetName.Equals(InputObjectParameterSet)) { this.ResourceGroupName = this.InputObject.Resourcegroup; this.IotHubName = this.InputObject.Name; iotHubDescription = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject); } else { if (ParameterSetName.Equals(ResourceIdParameterSet)) { this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); } iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName); } IEnumerable <SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName); SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryWrite); PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); Configuration config = registryManager.GetConfigurationAsync(this.Name).GetAwaiter().GetResult(); if (config != null && config.Content.ModulesContent != null) { PSDeployment psDeployment = IotHubDataPlaneUtils.ToPSDeployment(config); if (this.IsParameterBound(c => c.Priority)) { psDeployment.Priority = this.Priority; } if (this.IsParameterBound(c => c.TargetCondition)) { psDeployment.TargetCondition = this.TargetCondition; } if (this.IsParameterBound(c => c.Label)) { psDeployment.Labels = this.Label; } if (this.IsParameterBound(c => c.Metric)) { psDeployment.Metrics.Queries = this.Metric; } this.WriteObject(IotHubDataPlaneUtils.ToPSDeployment(registryManager.UpdateConfigurationAsync(IotHubDataPlaneUtils.ToConfiguration(psDeployment), this.Force.IsPresent).GetAwaiter().GetResult())); } else { throw new ArgumentException("The deployment doesn't exist."); } } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.IotHubName, Properties.Resources.AddIotHubConfiguration)) { IotHubDescription iotHubDescription; if (ParameterSetName.Equals(InputObjectParameterSet)) { this.ResourceGroupName = this.InputObject.Resourcegroup; this.IotHubName = this.InputObject.Name; iotHubDescription = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject); } else { if (ParameterSetName.Equals(ResourceIdParameterSet)) { this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); } iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName); } IEnumerable <SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName); SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryWrite); PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); PSConfiguration psConfiguration = new PSConfiguration(); psConfiguration.Id = this.Name; psConfiguration.Priority = this.Priority; psConfiguration.TargetCondition = string.IsNullOrEmpty(this.TargetCondition) ? "" : this.TargetCondition; psConfiguration.Labels = new Hashtable(); psConfiguration.Metrics = new PSConfigurationMetrics() { Queries = new Hashtable(), Results = new Hashtable() }; psConfiguration.Content = new PSConfigurationContent() { DeviceContent = new Hashtable(), ModulesContent = new Hashtable() }; if (this.IsParameterBound(c => c.Label)) { psConfiguration.Labels = this.Label; } if (this.IsParameterBound(c => c.Metric)) { psConfiguration.Metrics.Queries = this.Metric; } if (this.IsParameterBound(c => c.DeviceContent)) { foreach (DictionaryEntry kvp in this.DeviceContent) { if (!Convert.ToString(kvp.Key).StartsWith("properties.desired", StringComparison.InvariantCultureIgnoreCase)) { throw new ArgumentException("The device content key must begin with \"properties.desired\"."); } } psConfiguration.Content.DeviceContent = this.DeviceContent; } this.WriteObject(IotHubDataPlaneUtils.ToPSConfiguration(registryManager.AddConfigurationAsync(IotHubDataPlaneUtils.ToConfiguration(psConfiguration)).GetAwaiter().GetResult())); } }