-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWS-PSDrive.psm1
160 lines (131 loc) · 5.38 KB
/
AWS-PSDrive.psm1
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using namespace Microsoft.PowerShell.SHiPS
$Global:PSDefaultParameterValues['*:Region'] = { if(($pwd -replace "\\","/") -match '^.*/regions/(?<Region>[^/]+)(/.*)*$') { $matches["Region"] } }
$Global:PSDefaultParameterValues['*-ECS*:Cluster'] = { if(($pwd -replace "\\","/") -match '^.*/ecs/clusters/(?<Cluster>[^/]+)(/.*)*$') { $matches["Cluster"] } }
[SHiPSProvider(UseCache=$true)]
class Aws : SHiPSDirectory
{
Aws([string]$name): base($name)
{
}
[object[]] GetChildItem()
{
return @(
[SSMParamsQuery]::new('regions', '/aws/service/global-infrastructure/regions'),
[SSMParamsQuery]::new('services', '/aws/service/global-infrastructure/services')
)
}
}
$script:serviceCommands = @{
'autoscaling' = @{
'auto-scaling-groups' = @{ FunctionName = "Get-ASAutoScalingGroup" };
};
'codebuild' = @{
'builds' = @{ FunctionName = "Get-CBBuildIdList" };
'projects' = @{ FunctionName = "Get-CBProjectList" };
};
'ec2' = @{
'instances' = @{ FunctionName = "Get-EC2Instance" };
'security-groups' = @{ FunctionName = "Get-EC2SecurityGroup" };
};
'ecr' = @{
'images' = @{ FunctionName = "Get-ECRImage" };
'repositories' = @{ FunctionName = "Get-ECRRepository" };
};
'ecs' = @{
'clusters' = @{
FunctionName = "Get-ECSClusterList";
ItemKeyGetter = { $args[0].Split(':')[-1] };
ChildCalls = @{
'container-instances' = @{ FunctionName = "Get-ECSContainerInstanceList" };
'services' = @{ FunctionName = "Get-ECSClusterService" };
'tasks' = @{ FunctionName = "Get-ECSTaskList" };
}
};
};
'rds' = @{
'clusters' = @{ FunctionName = "Get-RDSDBCluster" };
'instances' = @{ FunctionName = "Get-RDSDBInstance" };
'security-groups' = @{ FunctionName = "Get-RDSDBSecurityGroup" };
};
's3' = @{
'buckets' = @{ FunctionName = "Get-S3Bucket" }
};
}
function GetServiceFolders([string]$serviceName)
{
$stuffForService = $script:serviceCommands[$serviceName]
if ($stuffForService) {
return @( $stuffForService.keys | %{
$stuff = $script:serviceCommands[$serviceName][$_]
[CLICall]::new($_, $stuff.FunctionName, $stuff.FunctionArgs, $stuff.ItemKeyGetter, $stuff.ChildCalls)
})
}
else {
return @()
}
}
[SHiPSProvider(UseCache=$true)]
class SSMParamsQuery : SHiPSDirectory
{
[string]$QueryPath = $null
[string]$regionPatternA = '/aws/service/global-infrastructure/regions/*'
[string]$servicePatternA = '/aws/service/global-infrastructure/services/*'
[string]$servicePatternB = '/aws/service/global-infrastructure/regions/*/services/*'
[string]$regionPatternB = '/aws/service/global-infrastructure/services/*/regions/*'
SSMParamsQuery([string]$paramName, [string]$queryPath) : base ($paramName)
{
$this.QueryPath = $queryPath
}
[object[]] GetChildItem()
{
$additionalFolders = @()
$additionalFolders += if ($this.QueryPath -like $this.regionPatternA -and $this.QueryPath -notlike $this.regionPatternA + '/*') { [SSMParamsQuery]::new('services', '' + $this.QueryPath + '/services') }
$additionalFolders += if ($this.QueryPath -like $this.servicePatternA -and $this.QueryPath -notlike $this.servicePatternA + '/*') { [SSMParamsQuery]::new('regions', '' + $this.QueryPath + '/regions') }
$additionalFolders += if ($this.QueryPath -like $this.servicePatternA -or $this.QueryPath -like $this.servicePatternB) { GetServiceFolders($this.Name) }
return $additionalFolders + @(Get-SSMParametersByPath -Path $this.QueryPath | %{ [SSMParamsQuery]::new($_.Value, $_.Name) })
}
}
[SHiPSProvider(UseCache=$true)]
class CLICall : SHiPSDirectory
{
[string]$FunctionName = $null
[object]$FunctionArgs = $null
[object]$ItemKeyGetter = $null
[object]$ChildCalls = $null
CLICall([string]$paramName, [string]$functionName, [object]$funcArgs, [string]$itemKeyGetter, [object]$childCalls) : base ($paramName)
{
$this.FunctionName = $functionName
$this.FunctionArgs = $funcArgs
$this.ItemKeyGetter = $itemKeyGetter
$this.ChildCalls = $childCalls
}
[object[]] GetChildItem()
{
$funcArgs = if ($this.FunctionArgs) { $this.FunctionArgs } else { @{} }
$getter = $this.ItemKeyGetter
return @( & $this.FunctionName @funcArgs | %{ [CLIItem]::new((& $getter $_), $_, $this.ChildCalls) } )
}
}
[SHiPSProvider(UseCache=$true)]
class CLIItem : SHiPSDirectory
{
[object]$Underlying = $null
[object]$ChildCalls = $null
CLIItem([string]$paramName, [object]$underlying, [object]$childCalls) : base ($paramName)
{
$this.Underlying = $underlying
$this.ChildCalls = $childCalls
}
[object[]] GetChildItem()
{
$additionalFolders = @()
if ($this.ChildCalls) {
return @( $this.ChildCalls.keys | %{
$stuff = $this.ChildCalls[$_]
$additionalFolders += [CLICall]::new($_, $stuff.FunctionName, $stuff.FunctionArgs, $stuff.ItemKey, $stuff.ChildCalls)
})
}
return $additionalFolders + $this.Underlying
}
}