-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTFModule.ps1
More file actions
319 lines (235 loc) · 10.1 KB
/
TFModule.ps1
File metadata and controls
319 lines (235 loc) · 10.1 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<# AVAILABLE
https://www.terraform.io/docs/cloud/api/run.html
Search Modules
GET <base_url>/search
Latest Version for a Specific Module Provider
GET <base_url>/:namespace/:name/:provider
Download Source Code for a Specific Module Version
GET <base_url>/:namespace/:name/:provider/:version/download
Download the Latest Version of a Module
GET <base_url>/:namespace/:name/:provider/download
https://www.terraform.io/docs/cloud/api/modules.html
Create a Module
POST /organizations/:organization_name/registry-modules
Create a Module Version
POST /registry-modules/:organization_name/:name/:provider/versions
Upload a Module Version
PUT https://archivist.terraform.io/v1/object/<UNIQUE OBJECT ID>
#>
function Get-TFModule {
<#
.SYNOPSIS
Get modules in terraform enterprise registry.
Run cmdlet to list all modules. Use the NAME parameter to list all available versions.
Specify the NAME with the specific VERSION or LATEST switch to get module properties.
Specify the SERVER, APITOKEN and ORG within the cmdlet or use Set-Terraform to store them globally.
APIToken can be generated at https://<TFE>/app/settings/tokens
.DESCRIPTION
List Modules
GET <base_url>/:namespace
List Available Versions for a Specific Module
GET <base_url>/:namespace/:name/:provider/versions
List Latest Version of Module for All Providers
GET <base_url>/:namespace/:name
Get a Specific Module
GET <base_url>/:namespace/:name/:provider/:version
.EXAMPLE
Get-TFModule | ogv
List Modules and pipe objects to Out-GridView
.EXAMPLE
Get-TFModule -Name instance
List all versions of a specific Module
.EXAMPLE
Get-TFModule -Name instance -Latest
List the latest version of a specific Module
.EXAMPLE
Get-TFModule -Name instance -Version 0.0.1
List all properties of a specific Module version
.LINK
https://www.terraform.io/docs/registry/api.html
#>
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]$Name,
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]$Version,
[switch]$Latest,
[ValidateSet('azurerm','vsphere','infoblox','chef','aws')]
[string]$Provider = 'azurerm',
[string]$Server = $Terraform.Server,
[string]$APIToken = $Terraform.Token,
[string]$Org = $Terraform.Org,
[int]$ResultPageSize = 20
)
if (!$Server -or !$APIToken) {Write-Warning "Missing Server and APIToken, use Connect-Terraform"; Continue}
$Uri = "https://$Server/api/registry/v1/modules"
$Headers = @{
Authorization = "Bearer $APIToken"
'Content-Type' = 'application/vnd.api+json'
}
if ($Name) {
try {
if ($Version) {
#Get a Specific Module
$Results = (Invoke-RestMethod -Uri "$($Uri)/$Org/$Name/$Provider/$Version" -Headers $Headers -Method Get)
$Results
} else {
if ($Latest) {
#List Latest Version of Module for All Providers
$Results = (Invoke-RestMethod -Uri "$($Uri)/$Org/$Name" -Headers $Headers -Method Get).Modules
$Results
} else {
#List Available Versions for a Specific Module
$Results = (Invoke-RestMethod -Uri "$($Uri)/$Org/$Name/$Provider/versions" -Headers $Headers -Method Get).Modules
foreach ($Version in ($Results | Select-Object -ExpandProperty Versions).version) {
[PSCustomObject]@{
Name = $name
Version = $version
Provider = $provider
}
}
}
}
} catch {
Write-Warning "Unable to get modules for $Name : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
Continue
}
} else {
try {
$i = 0
do {
#List Modules (sort is done per page and not for the entire results)
$Results = (Invoke-RestMethod -Uri "$($Uri)?namespace=$Org&limit=$ResultPageSize&offset=$i" -Headers $Headers -Method Get).Modules | Sort-Object Provider,Name
$Results
Write-Verbose "Page $i; Results Count $($Results.count)"
$i += $ResultPageSize
} while ($Results.count -eq $ResultPageSize)
} catch {
Write-Warning "Unable to get modules : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
Continue
}
}
}
function Publish-TFModule {
<#
.SYNOPSIS
Publishes a new registry module from a VCS repository, with module versions managed automatically by the repository's tags.
Name: TFE/terraform-<PROVIDER>-<NAME>
Specify the SERVER, APITOKEN and ORG within the cmdlet or use Set-Terraform to store them globally.
APIToken can be generated at https://<TFE>/app/settings/tokens
.DESCRIPTION
Publish a Module from a VCS
POST /registry-modules
.EXAMPLE
Publish-TFModule -Name terraform-azurerm-notification-hub -VCS TFE
Publish the module notification-hub using <VCS>/terraform-<PROVIDER>-<NAME>
.LINK
https://www.terraform.io/docs/cloud/api/modules.html
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
Param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]$Name,
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidateSet('Bitbucket','GitHub')]
[string]$VCS,
[string]$Server = $Terraform.Server,
[string]$APIToken = $Terraform.Token,
[string]$Org = $Terraform.Org
)
PROCESS {
if (!$Server -or !$APIToken) {Write-Warning "Missing Server and APIToken, use Connect-Terraform"; Continue}
$Uri = "https://$Server/api/v2/registry-modules"
$Headers = @{
Authorization = "Bearer $APIToken"
'Content-Type' = 'application/vnd.api+json'
}
$OAuthToken = Get-TFOAuthToken -VCS $VCS -Server $Server -APIToken $APIToken -Org $Org
try {
$Data = @{
attributes = @{
'vcs-repo' = @{
identifier = $Name
'oauth-token-id' = $OAuthToken
display_identifier = $Name
}
}
type = 'registry-modules'
}
$Body = @{data=$Data} | ConvertTo-Json -Depth 3
Write-Verbose "$Body"
if ($PSCmdlet.ShouldProcess($Name)) {
Invoke-RestMethod -Uri $Uri -Headers $Headers -Body $Body -Method Post | Out-Null
}
} catch {
Write-Warning "Unable to publish $Name : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
Continue
}
}
}
function Remove-TFModule {
<#
.SYNOPSIS
Delete modules in terraform enterprise registry.
Delete a specific VERSION or use the ALL switch to remove the entire module.
Specify the SERVER, APITOKEN and ORG within the cmdlet or use Set-Terraform to store them globally.
APIToken can be generated at https://<TFE>/app/settings/tokens
.DESCRIPTION
Delete a Module - specific version
POST /registry-modules/actions/delete/:organization_name/:name/:provider/:version
Delete a Module - entire module
POST /registry-modules/actions/delete/:organization_name/:name
.EXAMPLE
Get-TFModule notification-hub -Latest | Remove-TFModule
Get latest module and delete it.
.EXAMPLE
Remove-TFModule notification-hub -All
Delete all versions of the module
.LINK
https://www.terraform.io/docs/cloud/api/modules.html
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
Param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]$Name,
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,Position=1,ParameterSetName='Ver')]
[string]$Version,
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,Position=2,ParameterSetName='Ver')]
[ValidateSet('azurerm','vsphere','infoblox','chef','aws')]
[string]$Provider = 'azurerm',
[Parameter(Mandatory,Position=1,ParameterSetName='All')]
[switch]$All,
[string]$Server = $Terraform.Server,
[string]$APIToken = $Terraform.Token,
[string]$Org = $Terraform.Org
)
PROCESS {
if (!$Server -or !$APIToken) {Write-Warning "Missing Server and APIToken, use Connect-Terraform"; Continue}
$Headers = @{
Authorization = "Bearer $APIToken"
'Content-Type' = 'application/vnd.api+json'
}
try {
if ($PSCmdlet.ParameterSetName -eq 'Ver') {
$Uri = "https://$Server/api/v2/registry-modules/actions/delete/$Org/$Name/$Provider/$Version"
$Confirm = "$Name $Version"
} elseif ($PSCmdlet.ParameterSetName -eq 'All') {
$Uri = "https://$Server/api/v2/registry-modules/actions/delete/$Org/$Name"
$Confirm = "$Name All"
}
if ($PSCmdlet.ShouldProcess($Confirm)) {
Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Post | Out-Null
}
} catch {
Write-Warning "Unable to remove $Name : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
Continue
}
}
}
Set-Alias gtfmo Get-TFModule
Set-Alias pbtfmo Publish-TFModule
Set-Alias rtfmo Remove-TFModule