-
Notifications
You must be signed in to change notification settings - Fork 49
Added new rule LroAzureAsyncOperationHeader #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7229d93
Added new rule LroAzureAsyncOperationHeader
tejaswiMinnu ed01a3b
Merge branch 'main' of https://github.com/Azure/azure-openapi-validat…
tejaswiMinnu 688c43a
I've addressed the comments and tweaked the logic to fit the rule for…
tejaswiMinnu 032a1d2
build failure
tejaswiMinnu c6c1e07
addressed comments
tejaswiMinnu f154e44
Reverted to initial logic to address all LRO operations to have Azure…
tejaswiMinnu 68964e2
added new function to check existence of both headers and Azure-Async…
tejaswiMinnu 8e6d248
somehow few changes not reflected pushing them again, and also addres…
tejaswiMinnu 449b5e4
trying to push changes again
tejaswiMinnu 0908e86
removed extra .
tejaswiMinnu c9519ad
added one more bad example
tejaswiMinnu 6a466c3
Merge branch 'main' into tejaswis/AddRPC-Async-V1-06
tejaswiMinnu a26684b
Merge branch 'main' into tejaswis/AddRPC-Async-V1-06
tejaswiMinnu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# LroAzureAsyncOperationHeader | ||
|
||
## Category | ||
|
||
ARM Error | ||
|
||
## Applies to | ||
|
||
ARM OpenAPI(swagger) specs | ||
|
||
## Related ARM Guideline Code | ||
|
||
- RPC-Async-V1-06 | ||
|
||
## Output Message | ||
|
||
All long-running operations must include an `Azure-AsyncOperation` response header. | ||
|
||
## Description | ||
|
||
ARM relies on the async operation header to poll for the status of the long running operation. Based on this and the | ||
final state of the operation, downstream services like ARN and ARG are notified of the current state of the operation | ||
and the status of the resource. If you are a brownfield service that does not implement this header, you may add a | ||
tejaswiMinnu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
suppression using the following TSG indicating the same. | ||
TSG link - https://github.com/Azure/autorest/blob/main/docs/generate/suppress-warnings.md. | ||
In the description for the suppression, please provide a rough timeline by which the header will be supported by your | ||
service. More details about this header can be found in the ARM Resource Provider Contract documentation here - https://github.com/cloud-and-ai-microsoft/resource-provider-contract/blob/master/v1.0/async-api-reference.md#azure-asyncoperation-resource-format | ||
|
||
## CreatedAt | ||
|
||
Oct 11, 2024 | ||
|
||
## How to fix the violation | ||
|
||
Adding the Azure-AsyncOperation header to the response. | ||
|
||
## Good Example | ||
tejaswiMinnu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```json | ||
"/api/configServers": { | ||
"put": { | ||
"operationId": "ConfigServers_Update", | ||
"responses": { | ||
"202": { | ||
"description": "Accepted", | ||
"headers": { | ||
"Azure-AsyncOperation": { | ||
"type": "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
``` | ||
|
||
## Bad Example 1 | ||
|
||
```json | ||
"/api/configServers": { | ||
"put": { | ||
"operationId": "ConfigServers_Update", | ||
"responses": { | ||
"202": { | ||
"description": "Success", | ||
"headers": { | ||
//No Azure-AsyncOperation header | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
``` | ||
|
||
## Bad Example 2 | ||
|
||
```json | ||
"/api/configServers": { | ||
"put": { | ||
"operationId": "ConfigServers_Update", | ||
"responses": { | ||
"202": { | ||
"description": "Success", | ||
//No headers | ||
}, | ||
}, | ||
}, | ||
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/rulesets/src/spectral/functions/lro-azure-async-operation-header.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const lroAzureAsyncOperationHeader = (headers: any, _opts: any, ctx: any) => { | ||
if (!Object.keys(headers).includes("headers") || !Object.keys(headers.headers).includes("Azure-AsyncOperation")) { | ||
return [ | ||
{ | ||
message: "All long-running operations must include an `Azure-AsyncOperation` response header.", | ||
path: ctx.path.concat("headers"), | ||
}, | ||
] | ||
} | ||
return [] | ||
} |
138 changes: 138 additions & 0 deletions
138
packages/rulesets/src/spectral/test/lro-azure-async-operation-header.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { Spectral } from "@stoplight/spectral-core" | ||
import linterForRule from "./utils" | ||
|
||
let linter: Spectral | ||
|
||
beforeAll(async () => { | ||
linter = await linterForRule("LroAzureAsyncOperationHeader") | ||
return linter | ||
}) | ||
|
||
const ERROR_MESSAGE = "All long-running operations must include an `Azure-AsyncOperation` response header." | ||
|
||
test("LroAzureAsyncOperationHeader should find no errors", () => { | ||
const myOpenApiDocument = { | ||
swagger: "2.0", | ||
paths: { | ||
"/foo1/operations": { | ||
get: { | ||
operationId: "foo_get", | ||
responses: { | ||
202: { | ||
description: "Accepted", | ||
// no header scenario and no x-ms-long-running-operation | ||
}, | ||
}, | ||
}, | ||
post: { | ||
operationId: "foo_post", | ||
"x-ms-long-running-operation": true, | ||
responses: { | ||
202: { | ||
description: "Accepted", | ||
headers: { | ||
"Azure-AsyncOperation": { | ||
description: "The URL where the status of the asynchronous operation can be checked.", | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
put: { | ||
operationId: "foo_put", | ||
"x-ms-long-running-operation": true, | ||
responses: { | ||
204: { | ||
description: "Accepted", | ||
headers: { | ||
"Azure-AsyncOperation": { | ||
description: "The URL where the status of the asynchronous operation can be checked.", | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return linter.run(myOpenApiDocument).then((results) => { | ||
expect(results.length).toBe(0) | ||
}) | ||
}) | ||
|
||
test("LroAzureAsyncOperationHeader should find errors with no Azure-AsyncOperation header", () => { | ||
const myOpenApiDocument = { | ||
swagger: "2.0", | ||
paths: { | ||
"/foo1/operations": { | ||
get: { | ||
operationId: "foo_get", | ||
"x-ms-long-running-operation": true, | ||
responses: { | ||
202: { | ||
description: "Accepted", | ||
headers: { | ||
Location: { | ||
description: "No Azure-AsyncOperation header", | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
post: { | ||
operationId: "foo_post", | ||
"x-ms-long-running-operation": true, | ||
responses: { | ||
202: { | ||
description: "No header case", | ||
bdefoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}, | ||
put: { | ||
operationId: "foo_put", | ||
"x-ms-long-running-operation": true, | ||
responses: { | ||
202: { | ||
description: "Accepted", | ||
headers: { | ||
"azure-asyncOperation1": { | ||
description: "check the wrong wording", | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
delete: { | ||
operationId: "foo_delete", | ||
"x-ms-long-running-operation": true, | ||
responses: { | ||
202: { | ||
description: "Accepted", | ||
headers: { | ||
"azure-asyncOperation": { | ||
description: "check the camel case", | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return linter.run(myOpenApiDocument).then((results) => { | ||
expect(results.length).toBe(4) | ||
expect(results[0].path.join(".")).toBe("paths./foo1/operations.get.responses.202.headers") | ||
expect(results[0].message).toEqual(ERROR_MESSAGE) | ||
expect(results[1].path.join(".")).toBe("paths./foo1/operations.post.responses.202") | ||
expect(results[1].message).toEqual(ERROR_MESSAGE) | ||
expect(results[2].path.join(".")).toBe("paths./foo1/operations.put.responses.202.headers") | ||
expect(results[2].message).toEqual(ERROR_MESSAGE) | ||
expect(results[3].path.join(".")).toBe("paths./foo1/operations.delete.responses.202.headers") | ||
expect(results[3].message).toEqual(ERROR_MESSAGE) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.