Ejemplo n.º 1
0
        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.ToPSDeployment(registryManager.GetConfigurationAsync(this.Name).GetAwaiter().GetResult()));
            }
            else
            {
                this.WriteObject(IotHubDataPlaneUtils.ToPSDeployments(registryManager.GetConfigurationsAsync(Int32.MaxValue).GetAwaiter().GetResult()), true);
            }
        }
        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.");
                }
            }
        }
Ejemplo n.º 3
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;
                }

                psConfiguration.Content.ModulesContent = this.IsParameterBound(c => c.ModulesContent) ? this.ModulesContent : this.GetEdgeConfigurationContent();

                this.WriteObject(IotHubDataPlaneUtils.ToPSDeployment(registryManager.AddConfigurationAsync(IotHubDataPlaneUtils.ToConfiguration(psConfiguration)).GetAwaiter().GetResult()));
            }
        }
Ejemplo n.º 4
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);

                Configuration config = registryManager.GetConfigurationAsync(this.Name).GetAwaiter().GetResult();
                if (config != null && config.Content.ModulesContent != null)
                {
                    PSDeployment psDeployment = IotHubDataPlaneUtils.ToPSDeployment(config);
                    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";
                        }

                        if (this.MetricName.Equals("reporting success", StringComparison.OrdinalIgnoreCase))
                        {
                            metricKey = "reportedSuccessfulCount";
                        }

                        if (this.MetricName.Equals("reporting failure", StringComparison.OrdinalIgnoreCase))
                        {
                            metricKey = "reportedFailedCount";
                        }

                        queries = psDeployment.SystemMetrics.Queries;
                    }
                    else
                    {
                        queries = psDeployment.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 deployment '{1}'", this.MetricName, this.Name));
                    }
                }
                else
                {
                    throw new ArgumentException("The deployment doesn't exist.");
                }
            }
        }