Example #1
0
        private static Task <Either <PowershellFailure, VMStorageSettings> > ConfigToVMStorageSettings(MachineConfig config, HostSettings hostSettings)
        {
            var projectName       = Prelude.Some("default");
            var environmentName   = Prelude.Some("default");
            var dataStoreName     = Prelude.Some("default");
            var storageIdentifier = Option <string> .None;

            var names = new StorageNames()
            {
                ProjectName     = projectName,
                EnvironmentName = environmentName,
                DataStoreName   = dataStoreName,
            };

            if (!string.IsNullOrWhiteSpace(config.VM.Slug))
            {
                storageIdentifier = Prelude.Some(config.VM.Slug);
            }

            return(ResolveStorageBasePath(names, hostSettings.DefaultDataPath).MapAsync(path => new VMStorageSettings
            {
                StorageNames = names,
                StorageIdentifier = storageIdentifier,
                VMPath = path
            }));
        }
Example #2
0
        public static (StorageNames Names, Option <string> StorageIdentifier) PathToStorageNames(string path, string defaultPath)
        {
            var projectName       = Prelude.Some("default");
            var environmentName   = Prelude.Some("default");
            var dataStoreName     = Option <string> .None;
            var dataStorePath     = Option <string> .None;
            var storageIdentifier = Option <string> .None;

            if (path.StartsWith(defaultPath, StringComparison.InvariantCultureIgnoreCase))
            {
                dataStoreName = Prelude.Some("default");
                dataStorePath = defaultPath;
            }

            if (dataStorePath.IsSome)
            {
                var pathAfterDS = path.Remove(0, dataStorePath.ValueUnsafe().Length).TrimStart('\\');

                var pathRoot = Path.GetPathRoot(pathAfterDS);
                if (pathRoot.StartsWith("haipa_p"))
                {
                    projectName = pathRoot.Remove(0, "haipa_p".Length);
                }

                var idCandidate = pathAfterDS;

                if (idCandidate.Contains(Path.DirectorySeparatorChar))
                {
                    idCandidate = Path.GetDirectoryName(pathAfterDS);
                }

                if (!string.IsNullOrWhiteSpace(idCandidate) && pathRoot != idCandidate)
                {
                    storageIdentifier = idCandidate;
                }
            }

            var names = new StorageNames
            {
                DataStoreName   = dataStoreName,
                ProjectName     = projectName,
                EnvironmentName = environmentName
            };

            return(names, storageIdentifier);
        }
Example #3
0
 public Option <T> TryPeek() =>
 Count > 0
         ? Prelude.Some(value)
         : Prelude.None;
Example #4
0
 public Tuple <Stck <T>, Option <T> > TryPop() =>
 Count > 0
         ? Tuple.Create(tail, Prelude.Some(value))
         : Tuple.Create <Stck <T>, Option <T> >(this, Prelude.None);
Example #5
0
 public static async Task <Option <A> > lastOrNoneAsync <FOLD, F, A>(F fa) where FOLD : FoldableAsync <F, A> =>
 (await toSeqAsync <FOLD, F, A>(fa))
 .Map(x => Prelude.Some(x))
 .DefaultIfEmpty(Option <A> .None)
 .LastOrDefault();
Example #6
0
        private static Task <Either <PowershellFailure, VMDriveStorageSettings> > DriveConfigToDriveStorageSettings(int index,
                                                                                                                    VirtualMachineDriveConfig driveConfig, VMStorageSettings storageSettings, HostSettings hostSettings)
        {
            const int controllerNumber   = 0;     //currently this will not be configurable, but keep it here at least as constant
            var       controllerLocation = index; //later, when adding controller config support, we will have to add a logic to

            //set location relative to the free slots for each controller


            //if it is not a vhd, we only need controller settings
            if (driveConfig.Type != VirtualMachineDriveType.VHD)
            {
                VMDriveStorageSettings result;
                if (driveConfig.Type == VirtualMachineDriveType.DVD)
                {
                    result = new VMDVdStorageSettings
                    {
                        ControllerNumber   = controllerNumber,
                        ControllerLocation = controllerLocation,
                        Type = VirtualMachineDriveType.DVD
                    };
                }
                else
                {
                    result = new VMDriveStorageSettings
                    {
                        ControllerNumber   = controllerNumber,
                        ControllerLocation = controllerLocation,
                        Type = driveConfig.Type.GetValueOrDefault(VirtualMachineDriveType.PHD)
                    };
                }

                return(Prelude.RightAsync <PowershellFailure, VMDriveStorageSettings>(result).ToEither());
            }

            //so far for the simple part, now the complicated case - a vhd disk...

            var projectName       = Prelude.Some("default");
            var environmentName   = Prelude.Some("default");
            var dataStoreName     = Prelude.Some("default");
            var storageIdentifier = Option <string> .None;

            var names = new StorageNames()
            {
                ProjectName     = projectName,
                EnvironmentName = environmentName,
                DataStoreName   = dataStoreName,
            };



            if (storageIdentifier.IsNone)
            {
                storageIdentifier = storageSettings.StorageIdentifier;
            }


            return
                ((from resolvedPath in ResolveStorageBasePath(names, hostSettings.DefaultVirtualHardDiskPath).ToAsync()
                  from identifier in storageIdentifier.ToEither(new PowershellFailure
            {
                Message = $"Unexpected missing storage identifier for disk '{driveConfig.Name}'."
            })
                  .ToAsync()
                  .ToEither().ToAsync()

                  let planned = new VMDiskStorageSettings
            {
                Type = driveConfig.Type.Value,
                StorageNames = names,
                StorageIdentifier = storageIdentifier,
                ParentPath = driveConfig.Template,
                Path = Path.Combine(resolvedPath, identifier),
                AttachPath = Path.Combine(Path.Combine(resolvedPath, identifier), $"{driveConfig.Name}.vhdx"),
                // ReSharper disable once StringLiteralTypo
                Name = driveConfig.Name,
                SizeBytes = driveConfig.Size.ToOption().Match(None: () => 1 * 1024L * 1024 * 1024,
                                                              Some: s => s * 1024L * 1024 * 1024),
                ControllerNumber = controllerNumber,
                ControllerLocation = controllerLocation
            }
                  select planned as VMDriveStorageSettings).ToEither());
        }
Example #7
0
 public static Option <A> lastOrNone <FOLD, F, A>(F fa) where FOLD : struct, Foldable <F, A> =>
 toSeq <FOLD, F, A>(fa)
 .Map(x => Prelude.Some(x))
 .DefaultIfEmpty(Option <A> .None)
 .LastOrDefault();