Exemple #1
0
        public async Task <ResponseType> AddNic(Guid serverId, Guid?vlanId, string privateIpv4, string networkAdapter = null, bool?connected = null)
        {
            if (vlanId == null && string.IsNullOrEmpty(privateIpv4))
            {
                throw new ArgumentNullException("vlanId");
            }

            var nic = new NewNicType
            {
                networkAdapter = networkAdapter
            };

            // Private IP takes priority over vlanId
            // As Setting VlanId will make the api allocate the ip automatically ignoring the privateIp
            if (!string.IsNullOrEmpty(privateIpv4))
            {
                nic.privateIpv4 = privateIpv4;
            }
            else
            {
                nic.vlanId = vlanId.ToString();
            }

            //if (connected != null)
            //{
            //    nic.connected = connected.Value;
            //}

            AddNicType addNicType = new AddNicType
            {
                serverId = serverId.ToString(),
                nic      = nic
            };

            return(await _apiClient.PostAsync <AddNicType, ResponseType>(ApiUris.AddNic(_apiClient.OrganizationId), addNicType));
        }
        /// <summary>
        ///     The process record method.
        /// </summary>
        protected override void ProcessRecord()
        {
            Api.Contracts.Network20.ServerType deployedServer = null;
            base.ProcessRecord();
            try
            {
                var primaryNic = new NewNicType
                {
                    vlanId             = PrimaryVlan != null ? PrimaryVlan.id : null,
                    privateIpv4        = PrimaryPrivateIp,
                    connected          = PrimaryNicConnected.Value,
                    connectedSpecified = PrimaryNicConnected.HasValue
                };

                var server = new DeployServerType
                {
                    name                  = Name,
                    description           = Description,
                    imageId               = ServerImage.id,
                    start                 = IsStarted,
                    administratorPassword = AdminPassword,
                    networkInfo           =
                        new DeployServerTypeNetworkInfo
                    {
                        networkDomainId =
                            NetworkDomain.id,
                        primaryNic = primaryNic
                    }
                };

                var response = Connection.ApiClient.ServerManagement.Server.DeployServer(server).Result;

                // get the server id from status message
                var serverInfo = response.info.Single(info => info.name == "serverId");
                if (serverInfo != null)
                {
                    deployedServer = Connection.ApiClient.ServerManagement.Server.GetServer(Guid.Parse(serverInfo.value)).Result;
                }
            }
            catch (AggregateException ae)
            {
                ae.Handle(
                    e =>
                {
                    if (e is ComputeApiException)
                    {
                        WriteError(new ErrorRecord(e, "-2", ErrorCategory.InvalidOperation, Connection));
                    }
                    else
                    {
                        // if (e is HttpRequestException)
                        ThrowTerminatingError(new ErrorRecord(e, "-1", ErrorCategory.ConnectionError, Connection));
                    }

                    return(true);
                });
            }

            if (PassThru.IsPresent)
            {
                WriteObject(deployedServer);
            }
        }