PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Compute/Commands.ServiceManagement/Certificates/GetAzureCertificate.cs

https://gitlab.com/jslee1/azure-powershell
C# | 99 lines | 75 code | 8 blank | 16 comment | 4 complexity | 7028533e3a0312505c397349e5222485 MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using System;
  15. using System.Linq;
  16. using System.Management.Automation;
  17. using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
  18. using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
  19. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  20. using Microsoft.WindowsAzure.Management.Compute.Models;
  21. using Microsoft.WindowsAzure.Management.Compute;
  22. namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Certificates
  23. {
  24. /// <summary>
  25. /// Retrieve a specified service certificate.
  26. /// </summary>
  27. [Cmdlet(VerbsCommon.Get, "AzureCertificate"), OutputType(typeof(CertificateContext))]
  28. public class GetAzureCertificate : ServiceManagementBaseCmdlet
  29. {
  30. [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Hosted Service Name.")]
  31. [ValidateNotNullOrEmpty]
  32. public string ServiceName
  33. {
  34. get;
  35. set;
  36. }
  37. [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Certificate thumbprint algorithm.")]
  38. [ValidateNotNullOrEmpty]
  39. public string ThumbprintAlgorithm
  40. {
  41. get;
  42. set;
  43. }
  44. [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Certificate thumbprint.")]
  45. [ValidateNotNullOrEmpty]
  46. public string Thumbprint
  47. {
  48. get;
  49. set;
  50. }
  51. protected override void OnProcessRecord()
  52. {
  53. ServiceManagementProfile.Initialize();
  54. if (this.Thumbprint != null)
  55. {
  56. if (this.ThumbprintAlgorithm == null)
  57. {
  58. throw new ArgumentNullException("ThumbprintAlgorithm", Resources.MissingThumbprintAlgorithm);
  59. }
  60. var parameters = new ServiceCertificateGetParameters
  61. {
  62. ServiceName = ServiceName,
  63. Thumbprint = Thumbprint,
  64. ThumbprintAlgorithm = ThumbprintAlgorithm
  65. };
  66. ExecuteClientActionNewSM(
  67. null,
  68. CommandRuntime.ToString(),
  69. () => this.ComputeClient.ServiceCertificates.Get(parameters),
  70. (s, response) => new int[1].Select(i => ContextFactory<ServiceCertificateGetResponse, CertificateContext>(response, s)));
  71. }
  72. else
  73. {
  74. ExecuteClientActionNewSM(
  75. null,
  76. CommandRuntime.ToString(),
  77. () => this.ComputeClient.ServiceCertificates.List(this.ServiceName),
  78. (s, response) => response.Certificates.Select(c =>
  79. {
  80. var context = ContextFactory<ServiceCertificateListResponse.Certificate, CertificateContext>(c, s);
  81. context.ServiceName = this.ServiceName;
  82. return context;
  83. }));
  84. }
  85. }
  86. public void ExecuteCommand()
  87. {
  88. OnProcessRecord();
  89. }
  90. }
  91. }