PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/NewFIMResource.cs

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