PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/NewFIMResource.cs

#
C# | 131 lines | 105 code | 19 blank | 7 comment | 5 complexity | 6ade3156bdedc6a99055cb6331620ae2 MD5 | raw file
  1. using System;
  2. using System.Management.Automation;
  3. using Microsoft.ResourceManagement.ObjectModel;
  4. using Microsoft.ResourceManagement.Client;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. namespace Quest.FIMPowerShellSnapin
  8. {
  9. [Cmdlet(VerbsCommon.New, Constants.Nouns.FIMResource, SupportsShouldProcess = true)]
  10. public class NewIlmResource : PSCmdlet
  11. {
  12. private const string AttributesParamName = "Attributes";
  13. private String _type;
  14. private FIMPSResource _instance;
  15. private Hashtable _attributes;
  16. private bool _passThru;
  17. private FIMPSSession _session;
  18. [Parameter(Mandatory = true, ParameterSetName = "Type", ValueFromPipelineByPropertyName = true)]
  19. [ValidateNotNullOrEmpty]
  20. public String Type
  21. {
  22. get { return _type; }
  23. set { _type = value; }
  24. }
  25. [Parameter(Mandatory = true, ParameterSetName = "Instance", ValueFromPipeline = true)]
  26. [ValidateNotNullOrEmpty]
  27. public FIMPSResource Instance
  28. {
  29. get { return _instance; }
  30. set { _instance = value; }
  31. }
  32. [Parameter(ValueFromPipelineByPropertyName = true)]
  33. [ValidateNotNullOrEmpty]
  34. public Hashtable Attributes
  35. {
  36. get { return _attributes; }
  37. set { _attributes = value; }
  38. }
  39. [Parameter]
  40. public SwitchParameter PassThru
  41. {
  42. get { return (SwitchParameter)_passThru; }
  43. set { _passThru = (bool)value; }
  44. }
  45. [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  46. [ValidateNotNullOrEmpty]
  47. public FIMPSSession Session
  48. {
  49. get { return _session; }
  50. set { _session = value; }
  51. }
  52. protected override void ProcessRecord()
  53. {
  54. RmResource newObject = new RmResource();
  55. // Initialize a new resource object either by specifying it's ObjectType, or by assigning it the
  56. // all the attribute values from a resource object specified on the command line.
  57. switch (ParameterSetName)
  58. {
  59. case "Type":
  60. // RmAttributeValue assignment semantics add values to an existing value, even if the attribute type
  61. // is single-valued. To avoid multiple ObjectType values, we first remove the existing value for
  62. // ObjectType, and then set it to the new value.
  63. newObject.ObjectType = null;
  64. newObject.ObjectType = _type;
  65. break;
  66. case "Instance":
  67. newObject = _instance.Resource; //!!! verify this does the right thing.
  68. break;
  69. default:
  70. throw new Exception(Constants.Messages.InternalError_UnknownParameterSet);
  71. }
  72. // Sets additional attribute values specified by the hashtable from the Attributes command line parameter. Note that the
  73. // semantics are to overwrite any existing values, even if they are multi-valued.
  74. foreach (KeyValuePair<RmAttributeName, RmAttributeValue> attr in new AttributeHashtable(_attributes))
  75. {
  76. RmAttributeValue oldValue = null;
  77. if (newObject.TryGetValue(attr.Key, out oldValue))
  78. {
  79. newObject[attr.Key] = attr.Value;
  80. }
  81. else
  82. {
  83. newObject.Add(attr.Key, attr.Value);
  84. }
  85. }
  86. #if false //!!!fix
  87. bool shouldProcess = CmdletCommons.ShouldProcess(
  88. this, Messages.VerboseNew, Messages.ConfirmNew, rmObject);
  89. if (shouldProcess)
  90. {
  91. var conFactory = new ConnectionFactory(session, SessionState);
  92. WSTransferClient transferClient =
  93. new WSTransferClient(conFactory, this.session.SchemaMgr);
  94. String newId = transferClient.Create(rmObject);
  95. if (passThru)
  96. {
  97. rmObject.SetOneValueOfAttributeWithoutHistory(
  98. WsClient.Constants.AttributeTypeObjectId,
  99. newId,
  100. this.session.SchemaMgr.AttributeFactory);
  101. WriteObject(new ILMResource(rmObject, this.session));
  102. }
  103. }
  104. #endif
  105. RmReference newObjectID = _session.Client.Create(newObject);
  106. if (_passThru) // N.B. the ObjectID of newObject is set by the call to Create()
  107. {
  108. WriteObject(new FIMPSResource(newObject, _session));
  109. }
  110. }
  111. }
  112. }