-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOSH_RS_API.psm1
More file actions
151 lines (126 loc) · 4.96 KB
/
POSH_RS_API.psm1
File metadata and controls
151 lines (126 loc) · 4.96 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
Function Get-RSServiceCatalog {
param(
[Parameter()][string]$Username,
[Parameter()][string]$APIKey,
[Parameter()][string]$region
)
if ($username -ne "") {
write-verbose "Username $username passed."
$script:RS_Username = $username
}
if ($APIKey -ne "") {
write-verbose "API Key $apikey passed."
$script:RS_apikey = $APIKey
}
if ($region -ne "") {
write-verbose "Region $region passed."
$script:RS_region = $region
}
if ($script:RS_username -eq $null) {
write-verbose "Rackspace Username not set. Prompting"
$script:RS_Username = read-host "Enter Racksace username"
}
write-Debug "Using Rackspace Username: $RS_Username"
if ($script:RS_apikey -eq $null) {
write-verbose "Rackspace API Key not set. Prompting"
$script:RS_apikey = read-host "Enter Racksace API key"
}
write-Debug "Using Rackspace APIKey: $rs_apikey"
if ($script:RS_catalog -ne $null) {
write-verbose "Existing catalog found"
if ([datetime]$RS_catalog.access.token.expires -lt (get-date)){
write-verbose "Existing Catalog has expired and needs to be refreshed."
$script:RS_catalog = $null
} elseif($RS_catalog.access.user.name -ne $RS_Username){
write-verbose "Existing Catalog does not match username. Purging."
$script:RS_catalog = $null
}
}
if ($script:RS_catalog -eq $null){
write-verbose "Requesting new catalog."
$script:RS_Catalog = (Invoke-RestMethod -Uri $("https://identity.api.rackspacecloud.com/v2.0/tokens") -Method POST -Body $(@{"auth" = @{"RAX-KSKEY:apiKeyCredentials" = @{"username" = $RS_Username; "apiKey" = $RS_APIKey}}} | convertTo-Json) -ContentType application/json)
}
if ($script:RS_region -eq $null) {
write-verbose "Rackspace DC not set. Setting to account default"
Set-RSRegion -region $rs_catalog.access.user.'RAX-AUTH:defaultRegion'
}
return $script:RS_catalog
}
Function Get-RSDDI { return $script:RS_catalog.access.token.tenant.id }
Function Get-RSAPIToken { return $script:RS_catalog.access.token.id }
Function Get-RSRegion { return $script:RS_region }
Function Get-RSRegions { return $script:RS_catalog.access.serviceCatalog.endpoints.region | Sort-Object | Get-Unique }
Function Get-RSProducts { return $script:RS_catalog.access.serviceCatalog.name }
Function Set-RSRegion {
Param (
[Parameter(Mandatory=$True)]
[string]$region
)
$script:RS_region = $region
}
Function GetAuthToken {
return @{"X-Auth-Token"=($RS_catalog.access.token.id)}
}
Function GetEndpoint {
Param (
[Parameter(Mandatory=$True)]
[string]$product
)
write-verbose "Getting Endpoint"
$endpoints = ((Get-RSServiceCatalog).access.serviceCatalog | where {$_.name -eq "$product"}).endpoints
if ($endpoints.count -eq 1 ){
write-verbose "$product only has a single endpoint: $($endpoints.publicURL)"
$endpoints.publicURL
} else {
write-verbose "$product endpoint in ${RS_region}: $(($endpoints | where {$_.region -eq $RS_region}).publicURL)"
($endpoints | where {$_.region -eq $RS_region}).publicURL
}
}
function Invoke-RSAPIGET(){
param(
[Parameter()][string]$uri
)
PROCESS {
write-verbose "Invoke-RestMethod -Uri $uri -Method GET -Headers $(GetAuthToken) -ContentType application/json)"
Invoke-RestMethod -Uri $uri -Method GET -Headers (GetAuthToken) -ContentType application/json
}
END {
}
}
function Invoke-RSAPIPOST(){
param(
[Parameter()][string]$uri,
[Parameter()][string]$body
)
PROCESS {
write-verbose "Invoke-RestMethod -Uri $uri -Method GET -Headers $(GetAuthToken) -ContentType application/json)"
Invoke-RestMethod -Uri $uri -Method POST -Headers (GetAuthToken) -body $body -ContentType application/json
}
END {
}
}
function Invoke-RSAPIPUT(){
param(
[Parameter()][string]$uri,
[Parameter()][string]$body
)
PROCESS {
write-verbose "Invoke-RestMethod -Uri $uri -Method PUT -Headers (GetAuthToken) -body $body -ContentType application/json"
Invoke-RestMethod -Uri $uri -Method PUT -Headers (GetAuthToken) -body $body -ContentType application/json
}
END {
}
}
function Invoke-RSAPICall(){
param(
[Parameter(Mandatory=$True)][string]$product,
[Parameter(Mandatory=$True)][string]$URI,
[Parameter(Mandatory=$True)][ValidateSet("GET","POST","PUT","DELETE")][string]$Method,
[Parameter()][string]$body
)
if ($method -in @("POST","PUT")){
Invoke-RestMethod -Uri ((getendpoint -product $product) + $uri) -Method $method -Headers (GetAuthToken) -body $body -ContentType application/json
} else {
Invoke-RestMethod -Uri ((getendpoint -product $product) + $uri) -Method $method -Headers (GetAuthToken) -ContentType application/json
}
}