Skip to content

Commit 4ae2335

Browse files
VeryEarlylitchiyangMSFT
authored andcommitted
invoke-azrest (Azure#12291)
* support generic get * update * update test case * remove api version when construct uri * update uri construct * update localfeed * to use full response * convert output to PSHttpResponse * remove request id from pshttpresponse * rename to invoke-azrest * update parameter name * create help markdown for invoke-azrest * update common API reference * clean localfeed * add test case for invoke-azrest * update changelog.md * resolve comments * fix bug in get api version * update changelog * fix help * handle upper case in path * fix bug
1 parent ae35ca5 commit 4ae2335

File tree

11 files changed

+962
-11
lines changed

11 files changed

+962
-11
lines changed

src/Accounts/Accounts.Test/ErrorResolutionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void HandlesExceptionError()
7272
var autorestException = new Microsoft.Rest.Azure.CloudException("exception message")
7373
{
7474
Body = new Microsoft.Rest.Azure.CloudError { Code = "AutorestCode", Message = "Autorest message" },
75-
Request = new Rest.HttpRequestMessageWrapper(request, ""),
76-
Response = new Rest.HttpResponseMessageWrapper(response, ""),
75+
Request = new Microsoft.Rest.HttpRequestMessageWrapper(request, ""),
76+
Response = new Microsoft.Rest.HttpResponseMessageWrapper(response, ""),
7777
RequestId = "AutoRestRequestId"
7878
};
7979

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
16+
using Xunit;
17+
using Xunit.Abstractions;
18+
19+
namespace Microsoft.Azure.Commands.Profile.Test
20+
{
21+
public class InvokeAzRestTests : AccountsTestRunner
22+
{
23+
public InvokeAzRestTests(ITestOutputHelper output)
24+
: base(output)
25+
{
26+
}
27+
28+
[Fact]
29+
[Trait(Category.AcceptanceType, Category.CheckIn)]
30+
public void TestInvokeAzRest()
31+
{
32+
TestRunner.RunTestScript("Test-InvokeAzRest");
33+
}
34+
}
35+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Tests cmdlets Invoke-AzRest
18+
#>
19+
20+
function Test-InvokeAzRest
21+
{
22+
$put = "PUT"
23+
$get = "GET"
24+
$patch = "PATCH"
25+
$delete = "DELETE"
26+
$api = "2019-10-01"
27+
$name = "mockRG4Test"
28+
29+
$tag = "`{`"tags`": `{`"key`": `"val`"`}`}"
30+
$payload = "`{`"Location`": `"eastus`"`}"
31+
32+
$response = Invoke-AzRest -ResourceGroupName $name -ApiVersion $api -Method $put -payload $payload
33+
34+
Assert-AreEqual 201 $response.StatusCode
35+
Assert-AreEqual $put $response.Method
36+
Assert-NotNull $response.Content
37+
38+
$response = Invoke-AzRest -ResourceGroupName $name -ApiVersion $api -Method $get
39+
40+
Assert-AreEqual 200 $response.StatusCode
41+
Assert-AreEqual $get $response.Method
42+
Assert-NotNull $response.Content
43+
44+
$response = Invoke-AzRest -ResourceGroupName $name -ApiVersion $api -Method $patch -payload $tag
45+
46+
Assert-AreEqual 200 $response.StatusCode
47+
Assert-AreEqual $patch $response.Method
48+
Assert-NotNull $response.Content
49+
50+
$response = Invoke-AzRest -ResourceGroupName $name -ApiVersion $api -Method $delete
51+
52+
Assert-AreEqual 202 $response.StatusCode
53+
Assert-AreEqual $delete $response.Method
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
{
2+
"Entries": [
3+
{
4+
"RequestUri": "/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/resourceGroups/mockRG4Test?api-version=2019-10-01",
5+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvOWUyMjNkYmUtMzM5OS00ZTE5LTg4ZWItMDk3NWYwMmFjODdmL3Jlc291cmNlR3JvdXBzL21vY2tSRzRUZXN0P2FwaS12ZXJzaW9uPTIwMTktMTAtMDE=",
6+
"RequestMethod": "PUT",
7+
"RequestBody": "{\r\n \"Location\": \"eastus\"\r\n}",
8+
"RequestHeaders": {
9+
"x-ms-client-request-id": [
10+
"81de12fa-bbcf-47d5-a8fd-943c1ad886e1"
11+
],
12+
"Accept-Language": [
13+
"en-US"
14+
],
15+
"User-Agent": [
16+
"FxVersion/4.6.28928.01",
17+
"OSName/Windows",
18+
"OSVersion/Microsoft.Windows.10.0.18363.",
19+
"Microsoft.Azure.Internal.Common.AzureRestClient/1.3.17"
20+
],
21+
"Content-Type": [
22+
"application/json; charset=utf-8"
23+
],
24+
"Content-Length": [
25+
"22"
26+
]
27+
},
28+
"ResponseHeaders": {
29+
"Cache-Control": [
30+
"no-cache"
31+
],
32+
"Pragma": [
33+
"no-cache"
34+
],
35+
"x-ms-ratelimit-remaining-subscription-writes": [
36+
"1199"
37+
],
38+
"x-ms-request-id": [
39+
"7cc85e9c-718e-4ce9-92cd-76098f863590"
40+
],
41+
"x-ms-correlation-request-id": [
42+
"7cc85e9c-718e-4ce9-92cd-76098f863590"
43+
],
44+
"x-ms-routing-request-id": [
45+
"SOUTHEASTASIA:20200630T032251Z:7cc85e9c-718e-4ce9-92cd-76098f863590"
46+
],
47+
"Strict-Transport-Security": [
48+
"max-age=31536000; includeSubDomains"
49+
],
50+
"X-Content-Type-Options": [
51+
"nosniff"
52+
],
53+
"Date": [
54+
"Tue, 30 Jun 2020 03:22:51 GMT"
55+
],
56+
"Content-Length": [
57+
"219"
58+
],
59+
"Content-Type": [
60+
"application/json; charset=utf-8"
61+
],
62+
"Expires": [
63+
"-1"
64+
]
65+
},
66+
"ResponseBody": "{\r\n \"id\": \"/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/resourceGroups/mockRG4Test\",\r\n \"name\": \"mockRG4Test\",\r\n \"type\": \"Microsoft.Resources/resourceGroups\",\r\n \"location\": \"eastus\",\r\n \"properties\": {\r\n \"provisioningState\": \"Succeeded\"\r\n }\r\n}",
67+
"StatusCode": 201
68+
},
69+
{
70+
"RequestUri": "/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/resourceGroups/mockRG4Test?api-version=2019-10-01",
71+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvOWUyMjNkYmUtMzM5OS00ZTE5LTg4ZWItMDk3NWYwMmFjODdmL3Jlc291cmNlR3JvdXBzL21vY2tSRzRUZXN0P2FwaS12ZXJzaW9uPTIwMTktMTAtMDE=",
72+
"RequestMethod": "GET",
73+
"RequestBody": "",
74+
"RequestHeaders": {
75+
"x-ms-client-request-id": [
76+
"d64d1bd0-4477-4d0f-b846-10fd6344bb1a"
77+
],
78+
"Accept-Language": [
79+
"en-US"
80+
],
81+
"User-Agent": [
82+
"FxVersion/4.6.28928.01",
83+
"OSName/Windows",
84+
"OSVersion/Microsoft.Windows.10.0.18363.",
85+
"Microsoft.Azure.Internal.Common.AzureRestClient/1.3.17"
86+
]
87+
},
88+
"ResponseHeaders": {
89+
"Cache-Control": [
90+
"no-cache"
91+
],
92+
"Pragma": [
93+
"no-cache"
94+
],
95+
"x-ms-ratelimit-remaining-subscription-reads": [
96+
"11994"
97+
],
98+
"x-ms-request-id": [
99+
"3812dcc9-0254-4fe9-b88f-40706ff4a59a"
100+
],
101+
"x-ms-correlation-request-id": [
102+
"3812dcc9-0254-4fe9-b88f-40706ff4a59a"
103+
],
104+
"x-ms-routing-request-id": [
105+
"SOUTHEASTASIA:20200630T032251Z:3812dcc9-0254-4fe9-b88f-40706ff4a59a"
106+
],
107+
"Strict-Transport-Security": [
108+
"max-age=31536000; includeSubDomains"
109+
],
110+
"X-Content-Type-Options": [
111+
"nosniff"
112+
],
113+
"Date": [
114+
"Tue, 30 Jun 2020 03:22:51 GMT"
115+
],
116+
"Content-Type": [
117+
"application/json; charset=utf-8"
118+
],
119+
"Expires": [
120+
"-1"
121+
],
122+
"Content-Length": [
123+
"219"
124+
]
125+
},
126+
"ResponseBody": "{\r\n \"id\": \"/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/resourceGroups/mockRG4Test\",\r\n \"name\": \"mockRG4Test\",\r\n \"type\": \"Microsoft.Resources/resourceGroups\",\r\n \"location\": \"eastus\",\r\n \"properties\": {\r\n \"provisioningState\": \"Succeeded\"\r\n }\r\n}",
127+
"StatusCode": 200
128+
},
129+
{
130+
"RequestUri": "/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/resourceGroups/mockRG4Test?api-version=2019-10-01",
131+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvOWUyMjNkYmUtMzM5OS00ZTE5LTg4ZWItMDk3NWYwMmFjODdmL3Jlc291cmNlR3JvdXBzL21vY2tSRzRUZXN0P2FwaS12ZXJzaW9uPTIwMTktMTAtMDE=",
132+
"RequestMethod": "PATCH",
133+
"RequestBody": "{\r\n \"tags\": {\r\n \"key\": \"val\"\r\n }\r\n}",
134+
"RequestHeaders": {
135+
"x-ms-client-request-id": [
136+
"28343bc5-2bb1-4156-b4a4-aa5d93f6d5a1"
137+
],
138+
"Accept-Language": [
139+
"en-US"
140+
],
141+
"User-Agent": [
142+
"FxVersion/4.6.28928.01",
143+
"OSName/Windows",
144+
"OSVersion/Microsoft.Windows.10.0.18363.",
145+
"Microsoft.Azure.Internal.Common.AzureRestClient/1.3.17"
146+
],
147+
"Content-Type": [
148+
"application/json; charset=utf-8"
149+
],
150+
"Content-Length": [
151+
"24"
152+
]
153+
},
154+
"ResponseHeaders": {
155+
"Cache-Control": [
156+
"no-cache"
157+
],
158+
"Pragma": [
159+
"no-cache"
160+
],
161+
"x-ms-ratelimit-remaining-subscription-writes": [
162+
"1196"
163+
],
164+
"x-ms-request-id": [
165+
"61d06e8b-185e-4c2e-a7b3-83a92c0b252b"
166+
],
167+
"x-ms-correlation-request-id": [
168+
"61d06e8b-185e-4c2e-a7b3-83a92c0b252b"
169+
],
170+
"x-ms-routing-request-id": [
171+
"SOUTHEASTASIA:20200630T032254Z:61d06e8b-185e-4c2e-a7b3-83a92c0b252b"
172+
],
173+
"Strict-Transport-Security": [
174+
"max-age=31536000; includeSubDomains"
175+
],
176+
"X-Content-Type-Options": [
177+
"nosniff"
178+
],
179+
"Date": [
180+
"Tue, 30 Jun 2020 03:22:54 GMT"
181+
],
182+
"Content-Length": [
183+
"240"
184+
],
185+
"Content-Type": [
186+
"application/json; charset=utf-8"
187+
],
188+
"Expires": [
189+
"-1"
190+
]
191+
},
192+
"ResponseBody": "{\r\n \"id\": \"/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/resourceGroups/mockRG4Test\",\r\n \"name\": \"mockRG4Test\",\r\n \"type\": \"Microsoft.Resources/resourceGroups\",\r\n \"location\": \"eastus\",\r\n \"tags\": {\r\n \"key\": \"val\"\r\n },\r\n \"properties\": {\r\n \"provisioningState\": \"Succeeded\"\r\n }\r\n}",
193+
"StatusCode": 200
194+
},
195+
{
196+
"RequestUri": "/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/resourceGroups/mockRG4Test?api-version=2019-10-01",
197+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvOWUyMjNkYmUtMzM5OS00ZTE5LTg4ZWItMDk3NWYwMmFjODdmL3Jlc291cmNlR3JvdXBzL21vY2tSRzRUZXN0P2FwaS12ZXJzaW9uPTIwMTktMTAtMDE=",
198+
"RequestMethod": "DELETE",
199+
"RequestBody": "",
200+
"RequestHeaders": {
201+
"x-ms-client-request-id": [
202+
"3ab6d714-2aea-4d95-a43d-f07e26d47f81"
203+
],
204+
"Accept-Language": [
205+
"en-US"
206+
],
207+
"User-Agent": [
208+
"FxVersion/4.6.28928.01",
209+
"OSName/Windows",
210+
"OSVersion/Microsoft.Windows.10.0.18363.",
211+
"Microsoft.Azure.Internal.Common.AzureRestClient/1.3.17"
212+
]
213+
},
214+
"ResponseHeaders": {
215+
"Cache-Control": [
216+
"no-cache"
217+
],
218+
"Pragma": [
219+
"no-cache"
220+
],
221+
"Location": [
222+
"https://management.azure.com/subscriptions/9e223dbe-3399-4e19-88eb-0975f02ac87f/operationresults/eyJqb2JJZCI6IlJFU09VUkNFR1JPVVBERUxFVElPTkpPQi1NT0NLUkc0VEVTVC1FQVNUVVMiLCJqb2JMb2NhdGlvbiI6ImVhc3R1cyJ9?api-version=2019-10-01"
223+
],
224+
"Retry-After": [
225+
"15"
226+
],
227+
"x-ms-ratelimit-remaining-subscription-deletes": [
228+
"14999"
229+
],
230+
"x-ms-request-id": [
231+
"2307c7dc-bddf-42b1-81e0-5b42931cbc56"
232+
],
233+
"x-ms-correlation-request-id": [
234+
"2307c7dc-bddf-42b1-81e0-5b42931cbc56"
235+
],
236+
"x-ms-routing-request-id": [
237+
"SOUTHEASTASIA:20200630T032258Z:2307c7dc-bddf-42b1-81e0-5b42931cbc56"
238+
],
239+
"Strict-Transport-Security": [
240+
"max-age=31536000; includeSubDomains"
241+
],
242+
"X-Content-Type-Options": [
243+
"nosniff"
244+
],
245+
"Date": [
246+
"Tue, 30 Jun 2020 03:22:57 GMT"
247+
],
248+
"Expires": [
249+
"-1"
250+
],
251+
"Content-Length": [
252+
"0"
253+
]
254+
},
255+
"ResponseBody": "",
256+
"StatusCode": 202
257+
}
258+
],
259+
"Names": {},
260+
"Variables": {
261+
"SubscriptionId": "9e223dbe-3399-4e19-88eb-0975f02ac87f"
262+
}
263+
}

src/Accounts/Accounts/Az.Accounts.psd1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ CmdletsToExport = 'Disable-AzDataCollection', 'Disable-AzContextAutosave',
105105
'Disconnect-AzAccount', 'Get-AzContextAutosaveSetting',
106106
'Set-AzDefault', 'Get-AzDefault', 'Clear-AzDefault',
107107
'Register-AzModule', 'Enable-AzureRmAlias', 'Disable-AzureRmAlias',
108-
'Uninstall-AzureRm', 'Get-AzProfile', 'Select-AzProfile'
108+
'Uninstall-AzureRm', 'Get-AzProfile', 'Select-AzProfile',
109+
'Invoke-AzRestMethod'
109110

110111
# Variables to export from this module
111112
# VariablesToExport = @()
112113

113114
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
114115
AliasesToExport = 'Add-AzAccount', 'Login-AzAccount', 'Remove-AzAccount',
115116
'Logout-AzAccount', 'Select-AzSubscription', 'Resolve-Error',
116-
'Save-AzProfile', 'Get-AzDomain'
117+
'Save-AzProfile', 'Get-AzDomain', 'Invoke-AzRest'
117118

118119
# DSC resources to export from this module
119120
# DscResourcesToExport = @()

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added new cmdlet `Invoke-AzRestMethod`
2122

2223
* Fixed an issue that may cause authentication errors in multi-process scenarios such as running multiple Azure PowerShell cmdlets using `Start-Job` [#9448]
2324

0 commit comments

Comments
 (0)