Class/Dune.Class.ps1

enum Environments {
    Lab
    Poc
    Dev
    Test
    Int
    Uat
    Prod
}

enum OperationalStates {
    None # Resource w/o states, e.g. Subnets, Disks
    Starting
    Running
    Stopping
    Stopped
    Restarting
    Deleting
    Unknown # e.g. New, Missing
}

enum LifecycleStates {
    New
    Deploying
    Deployed
    Removing
    Removed
    Failure
}

enum JobStates {
    Pending
    Starting
    Running
    Suspended
    Stopping
    Completed
    Failed
}

enum JobTypes {
    Create
    Update
    Delete
    Start
    Stop
    Restart
    Backup
    Restore
    Patch
}

enum JobEventStates {
    Started
    Skipped
    Completed
    Failed
}

enum MonthlyDayOccurence {
    First = 1
    Second = 2
    Third = 3
    Fourth = 4
}

enum ComputeNodeTypes {
    VirtualMachine
    PhysicalMachine
}

enum SequenceUsage {
    onStart
    onStop
}

class DuneTag {
    [String]$Name
    [PSObject]$Value
    [bool]$IsProtected

    [String]ToString() {
        return ($this.Name, $this.Value -join ":")
    }
    [Hashtable]ToHashtable() {
        return @{$this.Name = $this.Value }
    }
    [Hashtable]ToPropertiesHashtable() {
        return @{ Name = $this.Name; Value = $this.Value }
    }
}

class DuneVariable {
    [string]$Name
    hidden [PSObject] $_value
    [string]$Type
    [bool]$IsInherited
    [nullable[Environments]]$Environment
    [string]$Description

    static [hashtable[]] $MemberDefinitions = @(
        @{
            MemberType  = 'ScriptProperty'
            MemberName  = 'Value'
            Value       = { $this._value } # Getter
            SecondValue = { # Setter
                $this._value = ConvertFrom-Json -InputObject $args[0]
            }
        }
    )

    static DuneVariable() {
        $TypeName = [DuneVariable].Name
        foreach ($Definition in [DuneVariable]::MemberDefinitions) {
            Update-TypeData -TypeName $TypeName @Definition
        }
    }

    [string]ToString() {
        return "$($this.Name): $($this.Value)"
    }
    [Hashtable]ToHashtable() {
        return @{ $this.Name = $this.Value }
    }
    [Hashtable]ToPropertiesHashtableJsonValue() {
        return @{ name = $this.Name; value = (ConvertTo-Json -InputObject $this.Value); type = $this.Type.toLower(); environment = $this.Environment; description = $this.Description }
    }
}

class DuneParameter {
    [string]$Name
    [PSObject]$Value
    hidden [PSObject] $_default
    [string]$Type
    [bool]$Optional
    [string]$Validation
    [string]$Description

    static [hashtable[]] $MemberDefinitions = @(
        @{
            MemberType  = 'ScriptProperty'
            MemberName  = 'Default'
            Value       = { $this._default } # Getter
            SecondValue = { # Setter
                $this._default = ConvertFrom-Json -InputObject $args[0]
            }
        }
    )

    static DuneParameter() {
        $TypeName = [DuneParameter].Name
        foreach ($Definition in [DuneParameter]::MemberDefinitions) {
            Update-TypeData -TypeName $TypeName @Definition
        }
    }
}

class DuneConfig {
    [string]$Name
    [PSObject]$Value
    [bool]$IsSecure

    # [string]ToString() {
    # return "$($this.Name): $($this.Value)"
    # }
    # [Hashtable]ToHashtable() {
    # return @{ $this.Name = $this.Value }
    # }
    # [Hashtable]ToPropertiesHashtable() {
    # return @{ Name = $this.Name; Value = $this.Value; IsSecure = $this.IsSecure }
    # }
}

class DuneConfigItem {
    [guid]$Id
    [string]$Name
    [string]$Description
    [datetime]$Created
    [string]$CreateUser
    [int]$CreateUserId
    [datetime]$Modified
    [string]$ModifyUser
    [int]$ModifyUserId
    [bool]$IsDeleted
}

class DuneTenant : DuneConfigItem {
    [string]$DisplayName
    [array]$AllowedMailDomains
    [DuneVariable[]]$Variables
    [DuneTag[]]$Tags
    [string[]]$LicensedModules
    [int]$DefaultDeploymentLifetime
    [int]$MaxDeploymentLifetime
    [int]$DefaultSystemPatchCycle
    [int]$SystemUnpatchedWarning
    [int]$SystemUnpatchedCritical
    [int]$SystemBackupMissingWarning
    [int]$SystemBackupMissingCritical

    [String]ToString() {
        return $this.Name
    }
}

class DuneCollection : DuneConfigItem {
    [string]$DisplayName
    [guid]$TenantId
    [DuneVariable[]]$Variables
    [DuneTag[]]$Tags
    [bool]$IsLostAndFound

    [String]ToString() {
        return $this.Name
    }
}

class DuneDeployment : DuneConfigItem {
    [string]$ShortId
    [string]$DisplayName
    [LifecycleStates]$State
    [datetime]$ValidFrom
    [datetime]$ValidUntil
    [datetime]$NextPatchDate
    [bool]$IsNeverExpiring
    [bool]$IsLocked
    [guid]$ParentId
    [guid]$TenantId
    [bool]$isTemplateBased
    [nullable[guid]]$TemplateId
    [DuneDeploymentTemplate]$Template
    [Environments]$Environment
    [guid]$DefaultResourceProviderId
    [PSObject]$Parameters
    [DuneVariable[]]$Variables
    [array]$Owners
    [DuneTag[]]$Tags

    [String]ToString() {
        return $this.Name
    }
}

class DuneResourceGroup : DuneConfigItem {
    [string]$ShortId
    [string]$DisplayName
    [LifecycleStates]$State
    [Environments]$Environment
    [guid]$ParentId
    [guid]$TenantId
    [bool]$isTemplateBased
    [nullable[guid]]$TemplateId
    [array]$Parameters
    [DuneVariable[]]$Variables
    [array]$Owners
    [DuneTag[]]$Tags

    [String]ToString() {
        return $this.Name
    }
    [Hashtable]GetTagsHashtable() {
        $TagsHT = @{}
        $this.Tags | ForEach-Object { $TagsHT.Add($_.Name, $_.Value) }
        return $TagsHT
    }
}

class DuneResource : DuneConfigItem {
    [guid]$TenantId
    [guid]$ParentId
    [guid]$DeploymentId
    [guid]$CollectionId
    [guid]$ResourceProviderId
    [string]$ExtId
    [string]$ExtType
    [DuneDeployment]$Deployment
    [string]$DisplayName
    [string]$ObjectType
    [Environments]$Environment
    [OperationalStates]$State
    [bool]$IsForeign
    [bool]$IsAlerting
    [string]$AlertingDisabledId
    [string]$AlertingDisabledReason
    [bool]$IsVulnerable
    [string]$VulnerabilityReason
    [bool]$IsMissingOnProvider
    [string]$Location
    [nullable[double]]$CostCurrentMonth
    [nullable[double]]$CostLastMonth
    [string]$CostCurrency
    [DuneVariable[]]$Variables
    [nullable[datetime]]$LastSeen
    [DuneTag[]]$Tags

    [String]ToString() {
        return $this.Name
    }
}

class DuneComputeNode : DuneResource {
    [string]$Hostname
    [string]$Fqdn
    [string[]]$Alias
    [string]$Model
    [DuneDisk[]]$Disks
    [string]$DnsDomain
    [bool]$isRebootPending
    [bool]$isVirtual
    [DuneIpAddress[]]$IpAddresses
    [int]$MemoryMb
    [int]$LogicalCpus
    [string]$OperatingSystem
    [string]$OperatingSystemVersion
    [string]$OperatingSystemFamily

    [String]ToString() {
        return $this.Hostname
    }
}

class DuneGenericResource : DuneResource {
    [object]$Config

    [String]ToString() {
        return $this.Name
    }
}

class DuneDisk : DuneResource {
    [nullable[guid]]$ResourceId
    [nullable[int]]$LunIndex
    [int]$SizeMb
    [object[]]$Volumes
    [PSCustomObject]$Config
    [bool]$IsOsDisk

    [String]ToString() {
        return $this.Name
    }
}


class DuneDeploymentTemplate : DuneConfigItem {
    [guid]$TenantId
    [string]$DisplayName
    [version]$Version
    [DuneVariable[]]$Variables
    [DuneResourceProvider[]]$AllowedResourceProviders
    [Environments[]]$AllowedEnvironments
    [array]$Parameters
    [int]$SchemaVersion
    [string]$Definition

    [String]ToString() {
        return $this.Name
    }
}

class DuneDeploymentTemplateSchema {
    [int]$Version
    [string]$Schema
}

class DuneResourceGroupTemplate : DuneConfigItem {
    [guid]$TenantId
    [string]$DisplayName
    [version]$Version
    [DuneVariable[]]$Variables
    [DuneParameter[]]$Parameters
    [int]$SchemaVersion
    [string]$Definition
}

class DuneResourceGroupTemplateSchema {
    [int]$Version
    [string]$Schema
}

class DuneJob {
    [guid]$Id
    [string]$Name
    [guid]$TenantId
    [string]$Message
    [JobTypes]$JobType
    [JobStates]$State
    [datetime]$StartTime
    [nullable[datetime]]$EndTime
    [guid]$ConfigItemId
    [string]$ConfigItemType
    [bool]$SendNotifications
    [bool]$IsRunning
    [datetime]$Created
    [string]$CreateUser
    [int]$CreateUserId
    [datetime]$Modified
    [string]$ModifyUser
    [int]$ModifyUserId
    [bool]$IsDeleted
}

class DuneJobEvent {
    [guid]$Id
    [guid]$TenantId
    [guid]$JobId
    [string]$Name
    [JobEventStates]$State
    [datetime]$StartTime
    [nullable[datetime]]$EndTime
    [double]$Duration
    [string]$Message
}

class DuneResourceProvider : DuneConfigItem {
    [string]$DisplayName
    [guid]$TenantId
    [Environments]$DefaultEnvironment
    [Environments[]]$AllowedEnvironments
    [string]$Type
    [bool]$IsReadyForDeployments
    [bool]$IsDisabled
    [DuneConfig[]]$Config
    [DuneTag[]]$Tags

    [String]ToString() {
        return $this.Name
    }
}

class DuneSubnet : DuneResource {
    [string]$NetworkAddress
    [int]$SubnetBits
    [string]$Cidr
    [string]$Version
    [object]$Config

    [String]ToString() {
        return $this.Cidr
    }
}

class DuneIpAddress : DuneConfigItem {
    [string]$DisplayName
    [string]$ExtId
    [guid]$TenantId
    [nullable[guid]]$ResourceId
    [nullable[guid]]$SubnetId
    [string]$Address
    [int]$SubnetBits
    [string]$Cidr
    [bool]$IsPrimary
    [string]$Version

    [String]ToString() {
        return $this.Cidr
    }
}

class DuneResourceAddons {
    [guid]$Id
    [string]$Name
    [string]$DisplayName
    [guid]$TenantId
    [guid]$ResourceId
}

class DuneFact : DuneResourceAddons {
    [string]$Value
    [DateTime]$LastSeen

    [String]ToString() {
        return $this.Name
    }
}

class DunePackage : DuneResourceAddons {
    [version]$Version
    [string]$Vendor
    [string]$Source
    [DateTime]$FirstSeen
    [DateTime]$LastSeen

    [String]ToString() {
        return "$($this.Name)($($this.Version))"
    }
}

class DuneService : DuneResourceAddons {
    [string]$State
    [string]$StartType
    [bool]$IsAppSpecific
    [string]$RunAs
    [DateTime]$LastSeen

    [String]ToString() {
        return $this.Name
    }
}

class DuneUser {
    [int]$Id
    [string]$FirstName
    [string]$LastName
    [string]$DisplayName
    [string]$Email
    [string[]]$GlobalRoles
    [datetime]$CreatedDate
    [datetime]$ModifiedDate

    [String]ToString() {
        return $this.email
    }
}

class DunePatchingWindow {
    [guid]$Id
    [string]$Name
    [string]$DayOfWeek
    [string]$MonthlyOccurence
    [string]$TimeOfDay

    [string]ToString() {
        return $this.Name
    }
}

class DunePatchingWindowAssignment {
    [guid]$Id
    [guid]$ResourceGroupId
    [DuneResourceGroup]$ResourceGroup
    [guid]$patchingWindowId
    [DunePatchingWindow]$PatchingWindow

    [string]ToString() {
        return "$($this.Deployment.Name):$($this.PatchingWindow.Name)"
    }
}

class DuneUpdate {
    [guid]$Id
    [string]$Name
    [string]$Severity
    [datetime]$Published

    [string]ToString() {
        return $this.Name
    }
}

class DuneSequence {
    [guid]$Id
    [SequenceUsage]$Usage
    [PSObject]$Batches

    [string]ToString() {
        return $this.Name
    }
}