public override void ExecuteCmdlet()
        {
            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);

            if (this.Name != null)
            {
                this.WriteObject(IotHubDataPlaneUtils.ToPSConfiguration(registryManager.GetConfigurationAsync(this.Name).GetAwaiter().GetResult()));
            }
            else
            {
                this.WriteObject(IotHubDataPlaneUtils.ToPSConfigurations(registryManager.GetConfigurationsAsync(Int32.MaxValue).GetAwaiter().GetResult()), true);
            }
        }
Example #2
0
        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)
                {
                    PSConfiguration psConfiguration = IotHubDataPlaneUtils.ToPSConfiguration(config);

                    if (this.IsParameterBound(c => c.Priority))
                    {
                        psConfiguration.Priority = this.Priority;
                    }

                    if (this.IsParameterBound(c => c.TargetCondition))
                    {
                        psConfiguration.TargetCondition = this.TargetCondition;
                    }

                    if (this.IsParameterBound(c => c.Label))
                    {
                        psConfiguration.Labels = this.Label;
                    }

                    if (this.IsParameterBound(c => c.Metric))
                    {
                        psConfiguration.Metrics.Queries = this.Metric;
                    }

                    this.WriteObject(IotHubDataPlaneUtils.ToPSConfiguration(registryManager.UpdateConfigurationAsync(IotHubDataPlaneUtils.ToConfiguration(psConfiguration), this.Force.IsPresent).GetAwaiter().GetResult()));
                }
                else
                {
                    throw new ArgumentException("The configuration doesn't exist.");
                }
            }
        }
Example #3
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.IotHubName, Properties.Resources.InvokeIotHubConfigurationMetricsQuery))
            {
                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 config = IotHubDataPlaneUtils.ToPSConfiguration(registryManager.GetConfigurationAsync(this.Name).GetAwaiter().GetResult());
                if (config != null)
                {
                    Hashtable queries;
                    string    metricKey = this.MetricName;
                    if (this.MetricType.Equals(PSConfigurationMetricType.System))
                    {
                        if (this.MetricName.Equals("targeted", StringComparison.OrdinalIgnoreCase))
                        {
                            metricKey = "targetedCount";
                        }

                        if (this.MetricName.Equals("applied", StringComparison.OrdinalIgnoreCase))
                        {
                            metricKey = "appliedCount";
                        }

                        queries = config.SystemMetrics.Queries;
                    }
                    else
                    {
                        queries = config.Metrics.Queries;
                    }

                    if (queries.ContainsKey(metricKey))
                    {
                        PSConfigurationMetricsResult psConfigurationMetricsResult = new PSConfigurationMetricsResult();
                        psConfigurationMetricsResult.Name     = this.MetricName;
                        psConfigurationMetricsResult.Criteria = queries[metricKey].ToString();
                        IQuery metricQuery = registryManager.CreateQuery(queries[metricKey].ToString());
                        psConfigurationMetricsResult.Result = metricQuery.GetNextAsJsonAsync().GetAwaiter().GetResult().ToList();
                        this.WriteObject(psConfigurationMetricsResult);
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("The metric '{0}' is not defined in the configuration '{1}'", this.MetricName, this.Name));
                    }
                }
                else
                {
                    throw new ArgumentException("The configuration doesn't exist.");
                }
            }
        }
Example #4
0
        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()));
            }
        }