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() { 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() { 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); Twin deviceTwin = registryManager.GetTwinAsync(this.DeviceId).GetAwaiter().GetResult(); if (deviceTwin == null) { throw new ArgumentException($"The entered device \"{this.DeviceId}\" doesn't exist."); } this.WriteObject(IotHubDataPlaneUtils.ToPSDeviceTwin(deviceTwin)); }
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.ModuleId, Properties.Resources.AddIotHubModule)) { 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); PSAuthenticationMechanism auth = new PSAuthenticationMechanism(); PSModule module = new PSModule(); module.Id = this.ModuleId; module.DeviceId = this.DeviceId; switch (this.AuthMethod) { case PSDeviceAuthType.x509_thumbprint: auth.Type = PSAuthenticationType.SelfSigned; auth.X509Thumbprint = new PSX509Thumbprint(); auth.X509Thumbprint.PrimaryThumbprint = this.authTypeDynamicParameter.PrimaryThumbprint; auth.X509Thumbprint.SecondaryThumbprint = this.authTypeDynamicParameter.SecondaryThumbprint; break; case PSDeviceAuthType.x509_ca: auth.Type = PSAuthenticationType.CertificateAuthority; break; default: auth.SymmetricKey = new PSSymmetricKey(); auth.Type = PSAuthenticationType.Sas; break; } module.Authentication = auth; RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); this.WriteObject(IotHubDataPlaneUtils.ToPSModule(registryManager.AddModuleAsync(IotHubDataPlaneUtils.ToModule(module)).GetAwaiter().GetResult())); } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.IotHubName, Properties.Resources.UpdateIotHubModuleTwin)) { 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); Twin moduleTwin = registryManager.GetTwinAsync(this.DeviceId, this.ModuleId).GetAwaiter().GetResult(); if (moduleTwin == null) { throw new ArgumentException($"The entered module \"{this.ModuleId}\" doesn't exist."); } if (this.IsParameterBound(c => c.Tag)) { moduleTwin.Tags = new TwinCollection(JsonConvert.SerializeObject(this.Tag)); } if (this.IsParameterBound(c => c.Desired)) { moduleTwin.Properties.Desired = new TwinCollection(JsonConvert.SerializeObject(this.Desired)); } if (this.Partial.IsPresent) { this.WriteObject(IotHubDataPlaneUtils.ToPSModuleTwin(registryManager.UpdateTwinAsync(this.DeviceId, this.ModuleId, moduleTwin, moduleTwin.ETag).GetAwaiter().GetResult())); } else { this.WriteObject(IotHubDataPlaneUtils.ToPSModuleTwin(registryManager.ReplaceTwinAsync(this.DeviceId, this.ModuleId, moduleTwin, moduleTwin.ETag).GetAwaiter().GetResult())); } } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.DeviceId, Properties.Resources.SetIotHubDistributedTracing)) { 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); PSDistributedTracing psDistributedTracing = new PSDistributedTracing(); Twin deviceTwin = registryManager.GetTwinAsync(this.DeviceId).GetAwaiter().GetResult(); IotHubDataPlaneUtils.ValidateDeviceTracing(this.DeviceId, iotHubDescription.Sku.Tier.Value.ToString(), iotHubDescription.Location, deviceTwin.Capabilities.IotEdge); if (deviceTwin.Properties.Desired.Contains(IotHubDataPlaneUtils.TracingProperty)) { psDistributedTracing = JsonConvert.DeserializeObject <PSDistributedTracing>(deviceTwin.Properties.Desired[IotHubDataPlaneUtils.TracingProperty].ToString()); } psDistributedTracing.SamplingMode = this.SamplingMode; if (this.SamplingMode.Equals(PSDistributedTracingSamplingMode.Enabled)) { if (this.IsParameterBound(c => c.SamplingRate)) { psDistributedTracing.SamplingRate = this.SamplingRate; } } deviceTwin.Properties.Desired[IotHubDataPlaneUtils.TracingProperty] = psDistributedTracing; deviceTwin = registryManager.UpdateTwinAsync(this.DeviceId, deviceTwin, deviceTwin.ETag).GetAwaiter().GetResult(); this.WriteObject(IotHubDataPlaneUtils.GetDeviceTracing(this.DeviceId, deviceTwin)); } }
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.RegistryRead); PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); if (this.DeviceId != null) { this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult())); } else { IList <Device> devices = new List <Device>(); IEnumerable <string> deviceResults = registryManager.CreateQuery("Select * from Devices").GetNextAsJsonAsync().GetAwaiter().GetResult(); foreach (string deviceResult in deviceResults) { Device d = JsonConvert.DeserializeObject <Device>(deviceResult); devices.Add(registryManager.GetDeviceAsync(d.Id).GetAwaiter().GetResult()); } if (devices.Count == 1) { this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(devices[0])); } else { this.WriteObject(IotHubDataPlaneUtils.ToPSDevices(devices), true); } } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.DeviceId, Properties.Resources.AddIotHubDevice)) { 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); Device childDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult(); Device parentDevice = registryManager.GetDeviceAsync(this.ParentDeviceId).GetAwaiter().GetResult(); if (childDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be non-edge device."); } if (!parentDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered parent device \"{this.ParentDeviceId}\" should be edge device."); } if (!string.IsNullOrEmpty(childDevice.Scope) && !childDevice.Scope.Equals(parentDevice.Scope) && !this.Force.IsPresent) { throw new ArgumentException($"The entered device \"{this.DeviceId}\" already has a parent device, please use '-Force' to overwrite."); } childDevice.Scope = parentDevice.Scope; this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.UpdateDeviceAsync(childDevice).GetAwaiter().GetResult())); } }
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.RegistryRead); PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); Device childDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult(); if (childDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be non-edge device."); } if (string.IsNullOrEmpty(childDevice.Scope)) { throw new ArgumentException($"The entered device \"{this.DeviceId}\" doesn\'t support parent device functionality."); } string parentDeviceId = childDevice.Scope.Substring(Properties.Resources.DEVICE_DEVICESCOPE_PREFIX.Length, childDevice.Scope.LastIndexOf("-") - Properties.Resources.DEVICE_DEVICESCOPE_PREFIX.Length); this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.GetDeviceAsync(parentDeviceId).GetAwaiter().GetResult())); }
public override void ExecuteCmdlet() { if (ShouldProcess(this.IotHubName, Properties.Resources.SetIotHubEdgeModules)) { 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); PSConfigurationContent content = new PSConfigurationContent() { DeviceContent = null, ModulesContent = this.ModulesContent }; registryManager.ApplyConfigurationContentOnDeviceAsync(this.DeviceId, IotHubUtils.ConvertObject <PSConfigurationContent, ConfigurationContent>(content)).GetAwaiter().GetResult(); this.WriteObject(IotHubDataPlaneUtils.ToPSModules(registryManager.GetModulesOnDeviceAsync(this.DeviceId).GetAwaiter().GetResult()), true); } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.DeviceId, Properties.Resources.AddIotHubDevice)) { IotHubDescription iotHubDescription; IList <Device> childDevices = null; 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); PSDeviceCapabilities psDeviceCapabilities = new PSDeviceCapabilities(); psDeviceCapabilities.IotEdge = this.EdgeEnabled.IsPresent; PSAuthenticationMechanism auth = new PSAuthenticationMechanism(); PSDevice device = new PSDevice(); device.Id = this.DeviceId; switch (this.AuthMethod) { case PSDeviceAuthType.x509_thumbprint: auth.Type = PSAuthenticationType.SelfSigned; auth.X509Thumbprint = new PSX509Thumbprint(); auth.X509Thumbprint.PrimaryThumbprint = this.authTypeDynamicParameter.PrimaryThumbprint; auth.X509Thumbprint.SecondaryThumbprint = this.authTypeDynamicParameter.SecondaryThumbprint; break; case PSDeviceAuthType.x509_ca: auth.Type = PSAuthenticationType.CertificateAuthority; break; default: auth.SymmetricKey = new PSSymmetricKey(); auth.Type = PSAuthenticationType.Sas; break; } device.Authentication = auth; device.Capabilities = psDeviceCapabilities; device.Status = this.Status; device.StatusReason = this.StatusReason; if (this.EdgeEnabled.IsPresent) { if (this.Children != null) { childDevices = new List <Device>(); foreach (string childDeviceId in this.Children) { Device childDevice = registryManager.GetDeviceAsync(childDeviceId).GetAwaiter().GetResult(); if (childDevice == null) { throw new ArgumentException($"The entered children device \"{childDeviceId}\" doesn't exist."); } if (childDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered children device \"{childDeviceId}\" should be non-edge device."); } if (!string.IsNullOrEmpty(childDevice.Scope) && !this.Force.IsPresent) { throw new ArgumentException($"The entered children device \"{childDeviceId}\" already has a parent device, please use '-Force' to overwrite."); } childDevices.Add(childDevice); } } } else { if (this.ParentDeviceId != null) { Device parentDevice = registryManager.GetDeviceAsync(this.ParentDeviceId).GetAwaiter().GetResult(); if (parentDevice == null) { throw new ArgumentException($"The entered parent device \"{this.ParentDeviceId}\" doesn't exist."); } if (!parentDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered parent device \"{this.ParentDeviceId}\" should be an edge device."); } device.Scope = parentDevice.Scope; } } Device newDevice = registryManager.AddDeviceAsync(IotHubDataPlaneUtils.ToDevice(device)).GetAwaiter().GetResult(); this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(newDevice)); if (this.EdgeEnabled.IsPresent && childDevices != null) { foreach (Device childDevice in childDevices) { childDevice.Scope = newDevice.Scope; registryManager.UpdateDeviceAsync(childDevice).GetAwaiter().GetResult(); } } } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.DeviceId, Properties.Resources.UpdateIotHubDevice)) { IotHubDescription iotHubDescription = null; switch (ParameterSetName) { case InputObjectParameterSetForAuth: case InputObjectParameterSetForStatus: case InputObjectParameterSetForEdgeEnabled: this.ResourceGroupName = this.InputObject.Resourcegroup; this.IotHubName = this.InputObject.Name; iotHubDescription = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject); break; case ResourceIdParameterSetForAuth: case ResourceIdParameterSetForStatus: case ResourceIdParameterSetForEdgeEnabled: this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); break; } if (iotHubDescription == null) { 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); PSDevice device = IotHubDataPlaneUtils.ToPSDevice(registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult()); switch (ParameterSetName) { case InputObjectParameterSetForAuth: case ResourceIdParameterSetForAuth: case ResourceParameterSetForAuth: PSAuthenticationMechanism auth = new PSAuthenticationMechanism(); switch (this.AuthMethod) { case PSDeviceAuthType.x509_thumbprint: auth.Type = PSAuthenticationType.SelfSigned; auth.X509Thumbprint = new PSX509Thumbprint(); auth.X509Thumbprint.PrimaryThumbprint = this.authTypeDynamicParameter.PrimaryThumbprint; auth.X509Thumbprint.SecondaryThumbprint = this.authTypeDynamicParameter.SecondaryThumbprint; break; case PSDeviceAuthType.x509_ca: auth.Type = PSAuthenticationType.CertificateAuthority; break; default: auth.SymmetricKey = new PSSymmetricKey(); auth.Type = PSAuthenticationType.Sas; break; } device.Authentication = auth; break; case InputObjectParameterSetForStatus: case ResourceIdParameterSetForStatus: case ResourceParameterSetForStatus: device.Status = this.Status; device.StatusReason = this.StatusReason; break; case InputObjectParameterSetForEdgeEnabled: case ResourceIdParameterSetForEdgeEnabled: case ResourceParameterSetForEdgeEnabled: device.Capabilities.IotEdge = this.EdgeEnabled; break; } this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.UpdateDeviceAsync(IotHubDataPlaneUtils.ToDevice(device)).GetAwaiter().GetResult())); } }
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())); } }
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.RegistryRead); PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); IList <Device> parentDevices = new List <Device>(); if (this.DeviceId != null) { Device parentDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult(); if (parentDevice == null) { throw new ArgumentException($"The entered parent device \"{this.DeviceId}\" doesn't exist."); } if (!parentDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be an edge device."); } parentDevices.Add(parentDevice); } else { IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetEdgeDevices()).GetNextAsJsonAsync().GetAwaiter().GetResult(); foreach (string deviceResult in deviceResults) { Device d = JsonConvert.DeserializeObject <Device>(deviceResult); parentDevices.Add(registryManager.GetDeviceAsync(d.Id).GetAwaiter().GetResult()); } } IList <PSDevicesChildren> psDevicesChildren = new List <PSDevicesChildren>(); foreach (Device parentDevice in parentDevices) { PSDevicesChildren psDeviceChildren = new PSDevicesChildren { DeviceId = parentDevice.Id, ChildrenDeviceId = new List <string>() }; IList <Device> childDevices = new List <Device>(); IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetNonEdgeDevices(parentDevice.Scope)).GetNextAsJsonAsync().GetAwaiter().GetResult(); foreach (string deviceResult in deviceResults) { Device d = JsonConvert.DeserializeObject <Device>(deviceResult); psDeviceChildren.ChildrenDeviceId.Add(d.Id); } psDevicesChildren.Add(psDeviceChildren); } this.WriteObject(psDevicesChildren, true); }
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."); } } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.DeviceId, Properties.Resources.RemoveIotHubDeviceChildren)) { 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); Device parentDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult(); if (parentDevice == null) { throw new ArgumentException($"The entered parent device \"{this.DeviceId}\" doesn't exist."); } if (!parentDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be an edge device."); } IList <Device> childDevices = new List <Device>(); if (this.Children != null) { foreach (string childDeviceId in this.Children) { Device childDevice = registryManager.GetDeviceAsync(childDeviceId).GetAwaiter().GetResult(); if (childDevice == null) { throw new ArgumentException($"The entered children device \"{childDeviceId}\" doesn't exist."); } if (childDevice.Capabilities.IotEdge) { throw new ArgumentException($"The entered children device \"{childDeviceId}\" should be non-edge device."); } if (!string.IsNullOrEmpty(childDevice.Scope) && !childDevice.Scope.Equals(parentDevice.Scope)) { throw new ArgumentException($"The entered children device \"{childDeviceId}\" isn\'t assigned as a child of entered device \"{this.DeviceId}\"."); } childDevices.Add(childDevice); } } else { IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetNonEdgeDevices(parentDevice.Scope)).GetNextAsJsonAsync().GetAwaiter().GetResult(); foreach (string deviceResult in deviceResults) { Device d = JsonConvert.DeserializeObject <Device>(deviceResult); childDevices.Add(registryManager.GetDeviceAsync(d.Id).GetAwaiter().GetResult()); } } try { foreach (Device childDevice in childDevices) { childDevice.Scope = string.Empty; registryManager.UpdateDeviceAsync(childDevice).GetAwaiter().GetResult(); } if (PassThru.IsPresent) { this.WriteObject(true); } } catch { if (PassThru.IsPresent) { this.WriteObject(false); } } } }