/
PoshStack-OpenStackNetworks.psm1
247 lines (187 loc) · 8.2 KB
/
PoshStack-OpenStackNetworks.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<############################################################################################
PoshStack
Databases
Description
-----------
**TODO**
############################################################################################>
function Script:Get-Provider {
Param(
[Parameter (Mandatory=$True)] [string] $Account = $(throw "Please specify required Cloud Account by using the -Account parameter"),
[Parameter (Mandatory=$False)][string] $RegionOverride = $null
)
$Provider = Get-OpenStackNetworkProvider -Account $Account
if ($RegionOverride){
$Global:RegionOverride = $RegionOverride
}
# Use Region code associated with Account, or was an override provided?
if ($RegionOverride) {
$Region = $Global:RegionOverride
} else {
$Region = $Credentials.Region
}
Add-Member -InputObject $Provider -MemberType NoteProperty -Name Region -Value $Region
Return $Provider
}
function Get-OpenStackNetworkProvider {
Param(
[Parameter (Mandatory=$True)] [string] $Account = $(throw "Please specify required Cloud Account by using the -Account parameter")
)
# The Account comes from the file CloudAccounts.csv
# It has information regarding credentials and the type of provider (Generic or Rackspace)
Get-OpenStackAccount -Account $Account
if ($RegionOverride){
$Global:RegionOverride = $RegionOverride
}
# Use Region code associated with Account, or was an override provided?
if ($RegionOverride) {
$Region = $Global:RegionOverride
} else {
$Region = $Credentials.Region
}
# Is this Rackspace or Generic OpenStack?
switch ($Credentials.Type)
{
"Rackspace" {
# Get Identity Provider
$cloudId = New-Object net.openstack.Core.Domain.CloudIdentity
$cloudId.Username = $Credentials.CloudUsername
$cloudId.APIKey = $Credentials.CloudAPIKey
$Global:CloudId = New-Object net.openstack.Providers.Rackspace.CloudIdentityProvider($cloudId)
Return New-Object net.openstack.Providers.Rackspace.CloudNetworksProvider($cloudId)
}
"OpenStack" {
$CloudIdentityWithProject = New-Object net.openstack.Core.Domain.CloudIdentityWithProject
$CloudIdentityWithProject.Password = $Credentials.CloudPassword
$CloudIdentityWithProject.Username = $Credentials.CloudUsername
$CloudIdentityWithProject.ProjectId = New-Object net.openstack.Core.Domain.ProjectId($Credentials.TenantId)
$CloudIdentityWithProject.ProjectName = $Credentials.TenantId
$Uri = New-Object System.Uri($Credentials.IdentityEndpointUri)
$OpenStackIdentityProvider = New-Object net.openstack.Core.Providers.OpenStackIdentityProvider($Uri, $CloudIdentityWithProject)
Return New-Object net.openstack.Providers.Rackspace.CloudNetworksProvider($OpenStackIdentityProvider)
}
}
}
# Issue 20 Implement Remove-CloudNetwork
function Remove-OpenStackNetwork {
Param(
[Parameter (Mandatory=$True)] [string] $Account = $(throw "Please specify required Cloud Account by using the -Account parameter"),
[Parameter (Mandatory=$True)] [string] $NetworkID = $(throw "Please specify the required Network ID by using the -NetworkID parameter"),
[Parameter (Mandatory=$False)][string] $RegionOverride
)
$Provider = Get-Provider -Account $Account -RegionOverride $RegionOverride
try {
# DEBUGGING
Write-Debug -Message "Remove-OpenStackNetwork"
Write-Debug -Message "Account.........................: $Account"
Write-Debug -Message "NetworkID.......................: $NetworkID"
Write-Debug -Message "RegionOverride..................: $RegionOverride"
Write-Debug -Message "Region..........................: $Region"
$Provider.DeleteNetwork($NetworkID, $Provider.Region, $Null)
}
catch {
Invoke-Exception($_.Exception)
}
<#
.SYNOPSIS
Remove network
.DESCRIPTION
The Remove-OpenStackNetwork cmdlet will remove a network.
.PARAMETER Account
Use this parameter to indicate which account you would like to execute this request against.
Valid choices are defined in PoshStack configuration file.
.PARAMETER NetworkID
The ID of the network to be removed.
.PARAMETER RegionOverride
This parameter will temporarily override the default region set in PoshStack configuration file.
.EXAMPLE
PS C:\Users\Administrator>
.LINK
http://api.rackspace.com/api-ref-networks.html
#>
}
# Issue 22 Implement New-CloudNetwork
function New-OpenStackNetwork {
Param(
[Parameter (Mandatory=$True)] [string] $Account = $(throw "Please specify required Cloud Account by using the -Account parameter"),
[Parameter (Mandatory=$True)] [string] $NetworkName = $(throw "Please specify the required Network Name by using the -NetworkName parameter"),
[Parameter (Mandatory=$True)] [string] $CIDR = $(throw "Please specify the required CIDR (e.g. 10.1.0.0/24) by using the -CIDR parameter"),
[Parameter (Mandatory=$False)][string] $RegionOverride
)
$Provider = Get-Provider -Account $Account -RegionOverride $RegionOverride
try {
# DEBUGGING
Write-Debug -Message "New-OpenStackNetwork"
Write-Debug -Message "Account.........................: $Account"
Write-Debug -Message "NetworkName.....................: $NetworkName"
Write-Debug -Message "CIDR............................: $CIDR"
Write-Debug -Message "RegionOverride..................: $RegionOverride"
Write-Debug -Message "Region..........................: $Region"
$Provider.CreateNetwork($CIDR, $NetworkName, $Provider.Region, $Null)
}
catch {
Invoke-Exception($_.Exception)
}
<#
.SYNOPSIS
Create a network
.DESCRIPTION
The New-OpenStackNetwork cmdlet will create a network.
.PARAMETER Account
Use this parameter to indicate which account you would like to execute this request against.
Valid choices are defined in PoshStack configuration file.
.PARAMETER NetworkName
The user-friendly name to be assigned to the network.
.PARAMETER CIDR
The inter-domain routing settings, e.g. 10.1.0.0/24.
.PARAMETER RegionOverride
This parameter will temporarily override the default region set in PoshStack configuration file.
.EXAMPLE
PS C:\Users\Administrator>
.LINK
http://api.rackspace.com/api-ref-networks.html
#>
}
# Issue 23 Implement Get-CloudNetwork
function Get-OpenStackNetwork {
Param(
[Parameter (Mandatory=$True)] [string] $Account = $(throw "Please specify required Cloud Account by using the -Account parameter"),
[Parameter (Mandatory=$False)][string] $NetworkID = $null,
[Parameter (Mandatory=$False)][string] $RegionOverride
)
$Provider = Get-Provider -Account $Account -RegionOverride $RegionOverride
try {
# DEBUGGING
Write-Debug -Message "Get-OpenStackNetwork"
Write-Debug -Message "Account.........................: $Account"
Write-Debug -Message "NetworkID.......................: $NetworkID"
Write-Debug -Message "RegionOverride..................: $RegionOverride"
Write-Debug -Message "Region..........................: $Region"
IF([string]::IsNullOrEmpty($NetworkID)) {
$Provider.ListNetworks($Provider.Region, $Null)
} else {
$Provider.ShowNetwork($NetworkID, $Provider.Region, $Null)
}
}
catch {
Invoke-Exception($_.Exception)
}
<#
.SYNOPSIS
Get network
.DESCRIPTION
The Get-OpenStackNetwork cmdlet will retrieve one or more cloud networks.
.PARAMETER Account
Use this parameter to indicate which account you would like to execute this request against.
Valid choices are defined in PoshStack configuration file.
.PARAMETER NetworkID
Use this parameter if you wish to retrieve one specific network. If omitted, all networks for the region will be retrieved.
.PARAMETER RegionOverride
This parameter will temporarily override the default region set in PoshStack configuration file.
.EXAMPLE
PS C:\Users\Administrator>
.LINK
http://api.rackspace.com/api-ref-networks.html
#>
}
Export-ModuleMember -Function *