Skip to content

Commit

Permalink
Merge pull request #333 from reubenmiller/simple-cancel-operation
Browse files Browse the repository at this point in the history
feat(operations cancel): add new convenience command to cancel an operation
  • Loading branch information
reubenmiller authored Jan 1, 2024
2 parents dbcc230 + 964d6bc commit 8cf5da4
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 0 deletions.
78 changes: 78 additions & 0 deletions api/spec/json/devicecontrol.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,84 @@
}
]
},
{
"name": "cancelOperation",
"description": "Cancel operation",
"descriptionLong": "Cancel an operation. This is a convenience command to set an operation to the FAILED status along with a sensible default failure reason.\nNote: Cancelling an operation does not guarantee that any client that is already processing the operation will stop and in\nnormal circumstances this command should be used sparingly.\n",
"examples": {
"powershell": [
{
"description": "Cancel an operation",
"command": "Invoke-CancelOperation -Id {{ NewOperation }}"
},
{
"description": "Cancel multiple operations",
"beforeEach": [
"$Agent = PSc8y\\New-TestAgent",
"$Operation1 = PSc8y\\New-TestOperation -Device $Agent.id",
"$Operation2 = PSc8y\\New-TestOperation -Device $Agent.id"
],
"command": "Get-OperationCollection -Device $Agent.id -Status PENDING | Invoke-CancelOperation -FailureReason \"manually cancelled\"",
"afterEach": [
"PSc8y\\Remove-ManagedObject -Id $Agent.id"
]
}
],
"go": [
{
"description": "Cancel an operation",
"command": "c8y operations cancel --id 12345",
"assertStdOut": {
"json": {
"method": "PUT",
"path": "/devicecontrol/operations/12345",
"body.status": "FAILED",
"body.failureReason": "User cancelled operation"
}
}
}
]
},
"method": "PUT",
"path": "devicecontrol/operations/{id}",
"accept": "application/vnd.com.nsn.cumulocity.operation+json",
"alias": {
"go": "cancel",
"powershell": "Invoke-CancelOperation"
},
"body": [
{
"name": "status",
"type": "stringStatic",
"description": "Operation status",
"value": "FAILED"
},
{
"name": "failureReason",
"type": "string",
"required": false,
"default": "User cancelled operation",
"description": "Reason for the failure"
},
{
"name": "data",
"type": "json",
"description": "Additional properties describing the operation which will be performed on the device."
}
],
"bodyRequiredKeys": [
"status"
],
"pathParameters": [
{
"name": "id",
"type": "id[]",
"description": "Operation id",
"pipeline": true,
"required": true
}
]
},
{
"name": "deleteOperationCollection",
"description": "Delete operation collection",
Expand Down
60 changes: 60 additions & 0 deletions api/spec/yaml/devicecontrol.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,66 @@ commands:
pipeline: true
required: true

- name: cancelOperation
description: Cancel operation
descriptionLong: |
Cancel an operation. This is a convenience command to set an operation to the FAILED status along with a sensible default failure reason.
Note: Cancelling an operation does not guarantee that any client that is already processing the operation will stop and in
normal circumstances this command should be used sparingly.
examples:
powershell:
- description: Cancel an operation
command: Invoke-CancelOperation -Id {{ NewOperation }}

- description: Cancel multiple operations
beforeEach:
- $Agent = PSc8y\New-TestAgent
- $Operation1 = PSc8y\New-TestOperation -Device $Agent.id
- $Operation2 = PSc8y\New-TestOperation -Device $Agent.id
command: Get-OperationCollection -Device $Agent.id -Status PENDING | Invoke-CancelOperation -FailureReason "manually cancelled"
afterEach:
- PSc8y\Remove-ManagedObject -Id $Agent.id

go:
- description: Cancel an operation
command: c8y operations cancel --id 12345
assertStdOut:
json:
method: PUT
path: /devicecontrol/operations/12345
body.status: FAILED
body.failureReason: User cancelled operation

method: PUT
path: devicecontrol/operations/{id}
accept: application/vnd.com.nsn.cumulocity.operation+json
alias:
go: cancel
powershell: Invoke-CancelOperation
body:
- name: status
type: stringStatic
description: Operation status
value: FAILED

- name: failureReason
type: string
required: false
default: User cancelled operation
description: Reason for the failure

- name: data
type: json
description: Additional properties describing the operation which will be performed on the device.
bodyRequiredKeys:
- status
pathParameters:
- name: id
type: id[]
description: Operation id
pipeline: true
required: true

- name: deleteOperationCollection
description: Delete operation collection
descriptionLong: |
Expand Down
177 changes: 177 additions & 0 deletions pkg/cmd/operations/cancel/cancel.auto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/cmd/operations/operations.auto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package operations

import (
cmdCancel "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/operations/cancel"
cmdCreate "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/operations/create"
cmdDeleteCollection "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/operations/deletecollection"
cmdGet "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/operations/get"
Expand Down Expand Up @@ -29,6 +30,7 @@ func NewSubCommand(f *cmdutil.Factory) *SubCmdOperations {
cmd.AddCommand(cmdGet.NewGetCmd(f).GetCommand())
cmd.AddCommand(cmdCreate.NewCreateCmd(f).GetCommand())
cmd.AddCommand(cmdUpdate.NewUpdateCmd(f).GetCommand())
cmd.AddCommand(cmdCancel.NewCancelCmd(f).GetCommand())
cmd.AddCommand(cmdDeleteCollection.NewDeleteCollectionCmd(f).GetCommand())

ccmd.SubCommand = subcommand.NewSubCommand(cmd)
Expand Down
10 changes: 10 additions & 0 deletions tests/auto/devicecontrol/tests/operations_cancel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests:
operations_cancel_Cancel an operation:
command: c8y operations cancel --id 12345
exit-code: 0
stdout:
json:
body.failureReason: User cancelled operation
body.status: FAILED
method: PUT
path: /devicecontrol/operations/12345
1 change: 1 addition & 0 deletions tools/PSc8y/PSc8y.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ FunctionsToExport = @(
'Get-UserMembershipCollection',
'Install-FirmwareVersion',
'Install-SoftwareVersion',
'Invoke-CancelOperation',
'Invoke-UserLogout',
'New-Agent',
'New-Alarm',
Expand Down
Loading

0 comments on commit 8cf5da4

Please sign in to comment.