-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTFOAuth.ps1
127 lines (100 loc) · 3.92 KB
/
TFOAuth.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
<#AVAILABLE
OAUTH CLIENTS API
https://www.terraform.io/docs/cloud/api/oauth-clients.html
Show an OAuth Client
GET /oauth-clients/:id
Create an OAuth Client
POST /organizations/:organization_name/oauth-clients
Update an OAuth Client
PATCH /oauth-clients/:id
Destroy an OAuth Client
DELETE /oauth-clients/:id
OAUTH TOKENS
https://www.terraform.io/docs/cloud/api/oauth-tokens.html
List OAuth Tokens
GET /oauth-clients/:oauth_client_id/oauth-tokens
Show an OAuth Token
GET /oauth-tokens/:id
Update an OAuth Token
PATCH /oauth-tokens/:id
Destroy an OAuth Token
DELETE /oauth-tokens/:id
#>
function Get-TFOAuthClient {
<#
.SYNOPSIS
Returns VCS OAuth Clients.
An OAuth Client represents the connection between an organization and a VCS provider.
This endpoint allows you to list VCS connections between an organization and a VCS provider (GitHub, Bitbucket, or GitLab) for use when creating or setting up workspaces.
.DESCRIPTION
List OAuth Clients
GET /organizations/:organization_name/oauth-clients
#>
[CmdletBinding()]
Param
(
[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"
$Headers = @{
Authorization = "Bearer $APIToken"
'Content-Type' = 'application/vnd.api+json'
}
try{
$OAuthClients = (Invoke-RestMethod "$Uri/organizations/$Org/oauth-clients" -Headers $Headers -Method Get).data
foreach ($OAuthClient in $OAuthClients){
[PSCustomObject]@{
Name=$OAuthClient.attributes.name
Id=$OAuthClient.id
}
}
}
catch{
Write-Warning "Unable to get OAuth Client : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
Continue
}
}
}
function Get-TFOAuthToken {
<#
.SYNOPSIS
Returns OAuth Token from VCS OAuth Client.
An OAuth Client represents the connection between an organization and a VCS provider.
The oauth-token object represents a VCS configuration which includes the OAuth connection and the associated OAuth token.
This object is used when creating a workspace to identify which VCS connection to use.
.DESCRIPTION
List all the OAuth Tokens for a given OAuth Client
GET /oauth-clients/:oauth_client_id/oauth-tokens
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[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"
$Headers = @{
Authorization = "Bearer $APIToken"
'Content-Type' = 'application/vnd.api+json'
}
try{
$OAuthClients = (Invoke-RestMethod "$Uri/organizations/$Org/oauth-clients" -Headers $Headers -Method Get).data
$OAuthClient = ($OAuthClients | Where-Object {$_.attributes -match $VCS}).id
Write-Verbose "VCS $VCS : OAuthClient $OAuthClient"
(Invoke-RestMethod "$Uri/oauth-clients/$OAuthClient/oauth-tokens" -Headers $Headers -Method Get).data.id
}
catch{
Write-Warning "Unable to get OAuth Token : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
Continue
}
}
}