/// <summary>
 /// Update device details for a device by passing the updated details themselves.
 /// </summary>
 /// <param name="updatedDetails">The new state of DeviceDetails for the device.</param>
 /// <returns></returns>
 public TaskStatusInfo UpdateDeviceDetails(DeviceDetails updatedDetails)
 {
     var request = new DeviceDetailsRequest();
     MiscUtils.CopyProperties(updatedDetails, request);
     var taskStatusInfo = this.GetStorSimpleClient().DeviceDetails.UpdateDeviceDetails(request, this.GetCustomRequestHeaders());
     return taskStatusInfo;
 }
 /// <summary>
 /// Update device details for specified device given the specified data
 /// </summary>
 /// <param name="deviceDetails">Current device details for the device.</param>
 /// <param name="newName">New friendly name for the device. Null if its not to be changed</param>
 /// <param name="timeZone">New timeZone value for the device. Null if its not to be changed</param>
 /// <param name="secondaryDnsServer">New Secondary DNS Server address for the device. Null if its not to be changed</param>
 /// <param name="networkConfigs">An array or NetworkConfig objects for different interfaces. Null if its not to be changed</param>
 /// <returns></returns>
 public TaskStatusInfo UpdateDeviceDetails(DeviceDetails deviceDetails, string newName, TimeZoneInfo timeZone, IPAddress secondaryDnsServer, NetworkConfig[] networkConfigs)
 {
     // Update the object
     this.UpdateDeviceDetailsObject(deviceDetails, newName, timeZone, secondaryDnsServer, networkConfigs);
     // Copy stuff over from the DeviceDetails object into a new DeviceDetailsRequest object.
     var request = new DeviceDetailsRequest();
     MiscUtils.CopyProperties(deviceDetails, request);
     var taskStatusInfo = this.GetStorSimpleClient().DeviceDetails.UpdateDeviceDetails(request, this.GetCustomRequestHeaders());
     return taskStatusInfo;
 }
 /// <summary>
 /// Validate that all network configs are valid.
 /// 
 /// Its mandatory to provide either (IPv4 Address and netmask) or IPv6 orefix for an interface that
 /// is being enabled. ( Was previously disabled and is now being configured)
 /// </summary>
 internal void ValidateNetworkConfigs(DeviceDetails details, NetworkConfig[] StorSimpleNetworkConfig)
 {
     if (StorSimpleNetworkConfig == null)
     {
         return;
     }
     foreach (var netConfig in StorSimpleNetworkConfig)
     {
         // get corresponding netInterface in device details.
         var netInterface = details.NetInterfaceList.FirstOrDefault(x => x.InterfaceId == netConfig.InterfaceAlias);
         // If its being enabled and its not Data0, it must have IP Address info
         if (netInterface == null || (netInterface.InterfaceId != NetInterfaceId.Data0 && !netInterface.IsEnabled))
         {
             // If its not an enabled interface either IPv6(prefix) or IPv4(address and mask) must be provided.
             if ((netConfig.IPv4Address == null || netConfig.IPv4Netmask == null) && netConfig.IPv6Prefix == null)
             {
                 throw new ArgumentException(string.Format(Resources.IPAddressesNotProvidedForNetInterfaceBeingEnabled, StorSimpleContext.ResourceName, details.DeviceProperties.DeviceId));
             }
         }
     }
 }
        /// <summary>
        /// Helper method to determine if this device has already been configured or not
        /// </summary>
        /// <returns></returns>
        public bool IsDeviceConfigurationCompleteForDevice(DeviceDetails details)
        {
            bool data0Configured = false;

            if (details.NetInterfaceList != null)
            {
                NetInterface data0 = details.NetInterfaceList.Where(x => x.InterfaceId == NetInterfaceId.Data0).ToList<NetInterface>().First<NetInterface>();
                if (data0 != null
                    && data0.IsEnabled
                    && data0.NicIPv4Settings != null
                    && !string.IsNullOrEmpty(data0.NicIPv4Settings.Controller0IPv4Address))
                    data0Configured = true;
            }
            return data0Configured;
        }
        public void UpdateVirtualDeviceDetails(DeviceDetails details, TimeZoneInfo timeZone, string sek, string adminPasswd, string snapshotPasswd, string cik, StorSimpleCryptoManager cryptoManager)
        {
            if (timeZone != null)
            {
                details.TimeServer.TimeZone = timeZone.StandardName;
            }
            // encrypt supplied secret with the device public key
            var encryptedSecretKey = this.EncryptWithDevicePublicKey(details.DeviceProperties.DeviceId, sek);

            details.VirtualApplianceProperties.EncodedServiceEncryptionKey = encryptedSecretKey;

            // Also set the CIK before making the request - service needs it.
            var encryptedCik = this.EncryptWithDevicePublicKey(details.DeviceProperties.DeviceId, cik);
            details.VirtualApplianceProperties.EncodedChannelIntegrityKey = encryptedCik;

            // Set the admin password
            string encryptedAdminPasswd = null;
            cryptoManager.EncryptSecretWithRakPub(adminPasswd, out encryptedAdminPasswd);
            details.RemoteMinishellSecretInfo.MinishellSecret = encryptedAdminPasswd;

            // Set the snapshot manager password
            string encryptedSnapshotManagerPasswd = null;
            cryptoManager.EncryptSecretWithRakPub(snapshotPasswd, out encryptedSnapshotManagerPasswd);
            details.Snapshot.SnapshotSecret = encryptedSnapshotManagerPasswd;

            // Set the cert thumbprint for the key used.
            details.SecretEncryptionCertThumbprint = cryptoManager.GetSecretsEncryptionThumbprint();

            // mark everything that we dont intend to modify as null - indicating
            // to the service that there has been no change
            details.AlertNotification = null;
            details.Chap = null;
            details.DnsServer = null;
            details.NetInterfaceList = null;
            details.RemoteMgmtSettingsInfo = null;
            details.WebProxy = null;
        }
        /// <summary>
        /// Modify the provided DeviceDetails object with the data provided
        /// </summary>
        /// <param name="details"></param>
        private void UpdateDeviceDetailsObject(DeviceDetails deviceDetails, string newName, TimeZoneInfo timeZone, IPAddress secondaryDnsServer, NetworkConfig[] networkConfigs)
        {
            // modify details for non-null data provided to cmdlet

            if (!string.IsNullOrEmpty(newName))
            {
                deviceDetails.DeviceProperties.FriendlyName = newName;
            }

            if (timeZone != null)
            {
                deviceDetails.TimeServer.TimeZone = timeZone.StandardName;
            }

            if (secondaryDnsServer != null)
            {
                var secondaryDnsString = secondaryDnsServer.ToString();
                var secondaryDnsType = secondaryDnsServer.AddressFamily;
                if (secondaryDnsType == AddressFamily.InterNetwork)   // IPv4
                {
                    deviceDetails.DnsServer.SecondaryIPv4.Clear();
                    deviceDetails.DnsServer.SecondaryIPv4.Add(secondaryDnsString);
                }
                else if (secondaryDnsType == AddressFamily.InterNetworkV6)
                {
                    deviceDetails.DnsServer.SecondaryIPv6.Clear();
                    deviceDetails.DnsServer.SecondaryIPv6.Add(secondaryDnsString);
                }
            }

            if (networkConfigs != null && networkConfigs.Count()  > 0)
            {
                foreach (var netConfig in networkConfigs)
                {
                    this.UpdateDeviceDetailsWithNetworkConfig(deviceDetails, netConfig);
                }
            }

            // There are a bunch of details that this cmdlet never edits and the service 
            // considers null values for them to mean that there have been no changes.
            deviceDetails.AlertNotification = null;
            deviceDetails.Chap = null;
            deviceDetails.RemoteMgmtSettingsInfo = null;
            deviceDetails.RemoteMinishellSecretInfo = null;
            deviceDetails.SecretEncryptionCertThumbprint = null;
            deviceDetails.Snapshot = null;
            deviceDetails.VirtualApplianceProperties = null;
            deviceDetails.WebProxy = null;
        }
        /// <summary>
        /// Update the specified DeviceDetails object with given network config data
        /// </summary>
        /// <param name="details">DeviceDetails object to be updated</param>
        /// <param name="netConfig">network config object to pick new details from</param>
        private void UpdateDeviceDetailsWithNetworkConfig(DeviceDetails details, NetworkConfig netConfig)
        {
            // See if deviceDetails already has an object for the interface for which network config has been provided.
            // If not, then a new object is to be provided.
            var netInterface = details.NetInterfaceList.FirstOrDefault(x => x.InterfaceId == netConfig.InterfaceAlias);
            if (netInterface == null)
            {
                netInterface = new NetInterface
                {
                    NicIPv4Settings = new NicIPv4(),
                    NicIPv6Settings = new NicIPv6()
                };
                details.NetInterfaceList.Add(netInterface);
            }

            netInterface.IsIScsiEnabled = netConfig.IsIscsiEnabled.HasValue ? netConfig.IsIscsiEnabled.Value : netInterface.IsIScsiEnabled;

            netInterface.IsCloudEnabled = netConfig.IsCloudEnabled.HasValue ? netConfig.IsCloudEnabled.Value : netInterface.IsCloudEnabled;

            if (netConfig.InterfaceAlias == NetInterfaceId.Data0)
            {   // Other interfaces are not supposed to have controller IPs
                if (netConfig.Controller0IPv4Address != null)
                {
                    netInterface.NicIPv4Settings.Controller0IPv4Address = netConfig.Controller0IPv4Address.ToString();
                }
                if (netConfig.Controller1IPv4Address != null)
                {
                    netInterface.NicIPv4Settings.Controller1IPv4Address = netConfig.Controller1IPv4Address.ToString();
                }
            }
            if (netConfig.IPv4Gateway != null)
            {
                netInterface.NicIPv4Settings.IPv4Gateway = netConfig.IPv4Gateway.ToString();
            }
            if (netConfig.IPv4Address != null)
            {
                netInterface.NicIPv4Settings.IPv4Address = netConfig.IPv4Address.ToString();
            }
            if (netConfig.IPv4Netmask != null)
            {
                netInterface.NicIPv4Settings.IPv4Netmask = netConfig.IPv4Netmask.ToString();
            }
            if (netConfig.IPv6Prefix != null)
            {
                netInterface.NicIPv6Settings.IPv6Prefix = netConfig.IPv6Prefix.ToString();
            }
            if (netConfig.IPv6Gateway != null)
            {
                netInterface.NicIPv6Settings.IPv6Gateway = netConfig.IPv6Gateway.ToString();
            }

            // Make sure that the interface gets enabled as well
            netInterface.IsEnabled = true;
        }