8. Defining custom resource actions
Often your resource types will need additional operations that are not covered by the standard resource operation shapes. For this, there are a set of operation signatures for defining resource actions at the instance and collection level.
To define a custom action you can use the ResourceAction
and ResourceCollectionAction
signatures from the Azure.Core.ResourceOperations
interface. Letβs define a couple of custom actions for the Widget
and WidgetPart
resources:
The scheduleRepairs
operation defines a custom action for all instances of the Widget
resource. All collection action templates expect 3 parameters: the resource type, the request action parameters, and the response type. In this case, WidgetRepairRequest
is both the parameter and response type because we are using it as the body of both the request and the response of this operation.
NOTE: The request parameters and response type do not have to be the same type!
We also define an collection operation called reorderParts
. Similarly to scheduleRepairs
, it uses the WidgetPartReorderRequest
as the request and response body.
Here are what the routes of these two operations will look like:
Notice that the operation name is used as the action name in the route!
There are also long-running operation versions of these two operations:
LongRunningResourceAction
LongRunningResourceCollectionAction
The same rules described in the long-running operations section also apply to these long-running action signatures.