Scripts/DotsSources/Service-DependsOn.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    <#
    .SYNOPSIS
        Read Service-DependsOn definitions, add to Neo4j

    .DESCRIPTION
        Read Service-DependsOn definitions, add to Neo4j

        This clears out existing Service-DependsOn relationships, and IsPartOf->Service relationships before populating neo4j

        See Dots/Data/Service-DependsOn/0.template.yml for schema and an example

        This is invoked by Connect-TheDots

    .PARAMETER Path
        Path to yml files. Defaults to DataPath\Service-DependsOn\*.yml
        We append Service-DependsOn/*.yml to this path

        If TestMode is set, we override this to Dots/Mock/Data/*.yml
    .FUNCTIONALITY
        Dots
    #>

[cmdletbinding()]
param(
    [string[]]$Path
)
$ScriptName = $MyInvocation.MyCommand.Name -replace '.ps1$'
#service-dependson-server
Invoke-Neo4jQuery "MATCH (:Service)-[r:DependsOn]->() DELETE r"
Invoke-Neo4jQuery -Query "MATCH ()-[r:IsPartOf]->(:Service) DELETE r"

if($Script:TestMode){
    $Path = "$ModuleRoot/Mock/Data/$ScriptName/*.yml"
}
elseif(-not $PSBoundParameters.ContainsKey('Path')){
    $Path = $script:DataPath
}
Write-Verbose "Parsing $ScriptName files from [$Path]"
"`n###############"
"Running $ScriptName "
"###############"
foreach($PathItem in $Path) {
    $PathItem = Join-Path $PathItem "/$ScriptName/*.yml"
    $files = Get-ChildItem $PathItem -File | Where-Object {$_.BaseName -notmatch '^[0-9].*Template$'}
    foreach($file in $files) {
        "### PARSING ### $($file.fullname)"
        $yaml = Get-Content $file.fullname -Raw
        try {
            $data = ConvertFrom-Yaml -Yaml $yaml -ErrorAction stop
        }
        catch {
            "EEE ERROR PARSING $($file.fullname)"
            Write-Warning $_
            continue
        }
        foreach($DependencyKey in $data.keys) {
            $Params = @{
                Passthru = $True
            }
            $Dependency = $data[$DependencyKey]
            if($Dependency.name) {
                $DependencyName = $Dependency.Name
            }
            else {
                $DependencyName = $file.BaseName
            }
            if($Dependency.ContainsKey('properties') -and $Dependency.properties.keys.count -gt 0) {
                $Params.add('Properties', $Dependency.properties)
            }
            $PartOfService = $false
            if($Dependency.ContainsKey('part_of_service') -and $Dependency['part_of_service']) {
                $PartOfService = $True
            }

            if($Dependency.ContainsKey('servers') -and $Dependency.servers.count -gt 0) {
                $Left = "MATCH (left:Service) WHERE left.name_key = {name} OR left.name = {name}"
                $Right = "MATCH (right:Server) WHERE right.${Script:CMDBPrefix}Hostname IN {servers}"
                $Output = New-Neo4jRelationship @Params -LeftQuery $Left `
                                                        -RightQuery $Right `
                                                        -Type DependsOn `
                                                        -Parameters @{
                                                            name = $DependencyName
                                                            servers = $Dependency.servers
                                                        }
                Test-BadOutput -Ingestor $MyInvocation.MyCommand.Name `
                            -YamFile $File `
                            -DataHash $($Data[$DependencyKey] | Out-String) `
                            -DataKey $DependencyKey `
                            -Specific "service [$DependencyName], DependsOn servers [$($Dependency.servers)]" `
                            -Output $Output

                if($PartOfService) {
                    $Left = "MATCH (left:Server) WHERE left.${Script:CMDBPrefix}Hostname IN {servers}"
                    $Right = "MATCH (right:Service) WHERE right.name_key = {name} OR right.name = {name}"
                    $Output = New-Neo4jRelationship @Params -LeftQuery $Left `
                                                            -RightQuery $Right `
                                                            -Type IsPartOf `
                                                            -Parameters @{
                                                                name = $DependencyName
                                                                servers = $Dependency.servers
                                                            }
                    Test-BadOutput -Ingestor $MyInvocation.MyCommand.Name `
                                -YamFile $File `
                                -DataHash $($Data[$DependencyKey] | Out-String) `
                                -DataKey $DependencyKey `
                                -Specific "service [$DependencyName], IsComposedOf servers [$($Dependency.servers)]" `
                                -Output $Output
                }
            }
            if($Dependency.ContainsKey('services') -and $Dependency.services.count -gt 0) {
                $Left = "MATCH (left:Service) WHERE left.name_key = {name} OR left.name = {name}"
                $Right = "MATCH (right:Service) WHERE right.name IN {services} OR right.name_key IN {services}"
                $Output = New-Neo4jRelationship @Params -LeftQuery $Left `
                                                        -RightQuery $Right `
                                                        -Type DependsOn `
                                                        -Parameters @{
                                                            name = $DependencyName
                                                            services = $Dependency.services
                                                        }
                Test-BadOutput -Ingestor $MyInvocation.MyCommand.Name `
                            -YamFile $File `
                            -DataHash $($Data[$DependencyKey] | Out-String) `
                            -DataKey $DependencyKey `
                            -Specific "service [$DependencyName], services [$($Dependency.services)]" `
                            -Output $Output
                if($PartOfService) {
                    $Left = "MATCH (left:Service) WHERE left.name_key = {name} OR left.name = {name}"
                    $Right = "MATCH (right:Service) WHERE right.name IN {services} OR right.name_key IN {services}"
                    $Output = New-Neo4jRelationship @Params -LeftQuery $Left `
                                                            -RightQuery $Right `
                                                            -Type IsPartOf `
                                                            -Parameters @{
                                                                name = $DependencyName
                                                                services = $Dependency.services
                                                            }
                    Test-BadOutput -Ingestor $MyInvocation.MyCommand.Name `
                                -YamFile $File `
                                -DataHash $($Data[$DependencyKey] | Out-String) `
                                -DataKey $DependencyKey `
                                -Specific "service [$DependencyName], services [$($Dependency.services)]" `
                                -Output $Output            }
            }
        }
    }
}