@azure-tools/typespec-azure-resource-manager 0.31.0-dev.2 → 0.31.0-dev.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -327
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,17 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
This is a library that provides model types and interfaces which can be used to define an Azure Resource Manager service API.
|
|
4
4
|
|
|
5
|
-
## Table of Contents
|
|
6
|
-
|
|
7
|
-
- [Getting Started](#getting-started)
|
|
8
|
-
- [Creating a project with `tsp init`](#creating-a-project-with-typespec-init)
|
|
9
|
-
- [Creating a project manually](#creating-a-project-manually)
|
|
10
|
-
- [Concepts](#concepts)
|
|
11
|
-
- [Defining the Service](#defining-the-service)
|
|
12
|
-
- [Defining Resource Types](#defining-resource-types)
|
|
13
|
-
- [Generating an OpenAPI Specification](#generating-an-openapi-specification)
|
|
14
|
-
- [A Complete Example](#a-complete-example)
|
|
15
|
-
|
|
16
5
|
## Getting Started
|
|
17
6
|
|
|
18
7
|
To author an Azure Resource Manager service definition, you will first need to create a TypeSpec project for your service.
|
|
@@ -24,323 +13,12 @@ https://github.com/Azure/typespec-azure/blob/main/packages/typespec-providerhub-
|
|
|
24
13
|
|
|
25
14
|
Before using this library, you should familiarize yourself with the TypeSpec language and tools. You can do this by reading the [TypeSpec tutorial](https://github.com/microsoft/typespec/blob/main/docs/tutorial.md).
|
|
26
15
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
To define an Azure Resource Manager service, the first thing you will need to do is define the service namespace and decorate it with the `serviceTitle`, `serviceVersion` and `armProviderNamespace` decorators:
|
|
30
|
-
|
|
31
|
-
```typespec
|
|
32
|
-
@armProviderNamespace
|
|
33
|
-
@service({title: "<service name>", version: "<service version>"})
|
|
34
|
-
namespace <mynamespace>;
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
For example:
|
|
38
|
-
|
|
39
|
-
```typespec
|
|
40
|
-
@armProviderNamespace
|
|
41
|
-
@service({
|
|
42
|
-
title: "Contoso User Service",
|
|
43
|
-
version: "2020-10-01-preview",
|
|
44
|
-
})
|
|
45
|
-
namespace Contoso.Users;
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### The `using` keyword
|
|
49
|
-
|
|
50
|
-
Just after the `namespace` declaration, you will also need to include a few `using` statements to pull in symbols from the namespaces of libraries you will for your specification.
|
|
51
|
-
|
|
52
|
-
For example, these lines pull in symbols from the `@typespec/rest` and `@azure-tools/typespec-azure-resource-manager`:
|
|
53
|
-
|
|
54
|
-
```
|
|
55
|
-
using TypeSpec.Http;
|
|
56
|
-
using TypeSpec.Rest;
|
|
57
|
-
using Azure.ResourceManager;
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### Defining Resource Types
|
|
61
|
-
|
|
62
|
-
A resource provider is composed of resources. The TypeSpec Azure Resource Manager library makes it much easier to define the structure and endpoints of such resources.
|
|
63
|
-
|
|
64
|
-
There are three essential components of a resource defined with TypeSpec:
|
|
65
|
-
|
|
66
|
-
- A model type representing the resource, derived from one of the [base resource types](#base-resource-types)
|
|
67
|
-
- A model type defining the properties of the resource type
|
|
68
|
-
- An interface that defines the operations that can be performed on the resource type, usually a combination of [standard resource operations](#standard-resource-operations) and [custom actions](#custom-actions)
|
|
69
|
-
|
|
70
|
-
Read the [TypeSpec tutorial](https://github.com/Microsoft/typespec/blob/main/docs/tutorial.md) to learn the basics about TypeSpec model types and interfaces.
|
|
71
|
-
|
|
72
|
-
#### 1. **Define a model representing the `properties` of the ARM resource**
|
|
73
|
-
|
|
74
|
-
Each resource type must have a properties type which defines its custom properties. This type will be exposed as the `properties` property of the resource type.
|
|
75
|
-
|
|
76
|
-
```typespec
|
|
77
|
-
@doc("The properties of UserResource")
|
|
78
|
-
model UserResourceProperties {
|
|
79
|
-
@doc("The user's full name")
|
|
80
|
-
fullName: string;
|
|
81
|
-
|
|
82
|
-
@doc("The user's email address.")
|
|
83
|
-
emailAddress: string;
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
#### 2. **Define a model representing the resource type**
|
|
88
|
-
|
|
89
|
-
Resource types are defined as plain models which pull in a standard resource type using the `is` keyword.
|
|
90
|
-
|
|
91
|
-
You define a resource type, you need the following:
|
|
92
|
-
|
|
93
|
-
- A `name` property which is marked with the following decorators
|
|
94
|
-
- `@key`: Specifies the parameter name for this resource type in the service URI hierarchy
|
|
95
|
-
- `@segment`: Specifies the name of the resource "collection", the URI segment that comes just before the parameter name which identifies the resource type
|
|
96
|
-
- A second model type which defines the resource type's custom properties as we described in step 1
|
|
97
|
-
|
|
98
|
-
Here we define a tracked resource called `UserResource`:
|
|
99
|
-
|
|
100
|
-
```typespec
|
|
101
|
-
@doc("A UserResource")
|
|
102
|
-
model UserResource is TrackedResource<UserResourceProperties> {
|
|
103
|
-
@key("userName")
|
|
104
|
-
@segment("users")
|
|
105
|
-
name: string;
|
|
106
|
-
}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
#### 3. **Define an interface with operations for the resource type**
|
|
110
|
-
|
|
111
|
-
```typespec
|
|
112
|
-
@armResourceOperations
|
|
113
|
-
interface Users extends TrackedResourceOperations<UserResource> {}
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
This will now produce all the endpoints(`get`, `post`, `put`, `patch` and `delete`, listByResourceGroup, listBySubscription) for a resource called `UserResources` and the `operations` endpoint for the service:
|
|
117
|
-
|
|
118
|
-
| Method & Path | Description |
|
|
119
|
-
| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------- |
|
|
120
|
-
| `GET /providers/Contoso.Users/operations` | List all operations for your service |
|
|
121
|
-
| `GET /subscriptions/{subscriptionId}/providers/Contoso.Users/users` | list all UserResource by subscription |
|
|
122
|
-
| `GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Contoso.Users/users` | list all UserResource by resource group |
|
|
123
|
-
| `GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Contoso.Users/users/{userName}` | get item |
|
|
124
|
-
| `PUT /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Contoso.Users/users/{userName}` | insert item |
|
|
125
|
-
| `PATCH /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Contoso.Users/users/{userName}` | patch item |
|
|
126
|
-
| `DELETE /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Contoso.Users/users/{userName}` | delete item |
|
|
127
|
-
|
|
128
|
-
#### Base Resource Types
|
|
129
|
-
|
|
130
|
-
Here are the base resource types you can use when defining your own ARM resources:
|
|
131
|
-
|
|
132
|
-
| Name | Description |
|
|
133
|
-
| ------------------------------ | -------------------------------------------------------------------------------------- |
|
|
134
|
-
| TrackedResource<TProperties> | Defines a normal ARM resource where `TProperties` is the model of the `properties` |
|
|
135
|
-
| ProxyResource<TProperties> | Defines a proxy ARM resource where `TProperties` is the model of the `properties` |
|
|
136
|
-
| ExtensionResource<TProperties> | Defines an extension ARM resource where `TProperties` is the model of the `properties` |
|
|
137
|
-
|
|
138
|
-
### Defining Child Resource Types
|
|
139
|
-
|
|
140
|
-
You can create parent/child relationships between resource types by using the `@parentResource` decorator when defining a resource type.
|
|
141
|
-
|
|
142
|
-
For example, here's how you could create a new `AddressResource` resource under the `UserResource` defined above:
|
|
143
|
-
|
|
144
|
-
```typespec
|
|
145
|
-
@doc("An address resource belonging to a user resource.")
|
|
146
|
-
@parentResource(UserResource)
|
|
147
|
-
model AddressResource is ProxyResource<AddressResourceProperties> {
|
|
148
|
-
@key("addressName")
|
|
149
|
-
@segment("addresses")
|
|
150
|
-
name: string;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
@doc("The properties of AddressResource")
|
|
154
|
-
model AddressResourceProperties {
|
|
155
|
-
@doc("The street address.")
|
|
156
|
-
streetAddress: string;
|
|
157
|
-
|
|
158
|
-
@doc("The city of the address.")
|
|
159
|
-
city: string;
|
|
160
|
-
|
|
161
|
-
@doc("The state of the address.")
|
|
162
|
-
state: string;
|
|
163
|
-
|
|
164
|
-
@doc("The zip code of the address.")
|
|
165
|
-
zip: int32;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
@armResourceOperations
|
|
169
|
-
interface Addresses extends ProxyResourceOperations<AddressResource> {}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
### Defining Custom Actions
|
|
173
|
-
|
|
174
|
-
Some resources will provide more than the standard CRUD operations and will need to define a custom action endpoint. Additional resource operations can be added to the `interface` where you pulled in standard resource operations.
|
|
175
|
-
|
|
176
|
-
For example, to add an additional `POST` action called `/notify` to the standard operations of `UserResource`:
|
|
177
|
-
|
|
178
|
-
```typespec
|
|
179
|
-
@doc("The details of a user notification.")
|
|
180
|
-
model NotificationDetails {
|
|
181
|
-
@doc("The notification message.")
|
|
182
|
-
message: string;
|
|
183
|
-
|
|
184
|
-
@doc("If true, the notification is urgent.")
|
|
185
|
-
urgent: boolean;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
@armResourceOperations
|
|
189
|
-
interface Users extends TrackedResourceOperations<UserResource, UserResourceProperties> {
|
|
190
|
-
@post
|
|
191
|
-
@doc("Send a notification to the user")
|
|
192
|
-
@segment("notify")
|
|
193
|
-
NotifyUser(
|
|
194
|
-
...ResourceInstanceParameters<UserResource>,
|
|
195
|
-
@body notification: NotificationDetails
|
|
196
|
-
): ArmResponse<string> | ErrorResponse;
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
#### ARM Response Types
|
|
201
|
-
|
|
202
|
-
Custom operations in ARM still need to respect the correct response schema. This library provides standard ARM response types to help with reusability and compliance.
|
|
203
|
-
|
|
204
|
-
| Model | Code | Description |
|
|
205
|
-
| ----------------------------- | ---- | --------------------------------------------- |
|
|
206
|
-
| `ArmResponse<T>` | 200 | Base Arm 200 response. |
|
|
207
|
-
| `ArmCreatedResponse<T>` | 201 | Resource created response |
|
|
208
|
-
| `ArmDeletedResponse` | 200 | Resource deleted response |
|
|
209
|
-
| `ArmDeleteAcceptedResponse` | 202 | Resource deletion in progress response |
|
|
210
|
-
| `ArmDeletedNoContentResponse` | 204 | Resource deleted response |
|
|
211
|
-
| `Page<T>` | 200 | Return a list of resource with ARM pagination |
|
|
212
|
-
| `ErrorResponse<T>` | x | Error response |
|
|
213
|
-
|
|
214
|
-
#### Common Operation Parameters
|
|
215
|
-
|
|
216
|
-
There are a number of model types which specify common parameters which are used in resource type operations:
|
|
217
|
-
|
|
218
|
-
| Model | In | Description |
|
|
219
|
-
| ---------------------------- | ------------ | ------------------------------------------------------------------ |
|
|
220
|
-
| `ApiVersionParameter` | query | `api-version` parameter |
|
|
221
|
-
| `SubscriptionIdParameter` | path | Subscription ID path parameter |
|
|
222
|
-
| `ResourceGroupNameParameter` | path | Resource Group Name path parameter |
|
|
223
|
-
| `CommonResourceParameters` | path & query | Group of Api version, Subscription ID and Resource group parameter |
|
|
224
|
-
| `ResourceUriParameter` | path | Resource uri path parameter |
|
|
225
|
-
| `OperationIdParameter` | path | Operation Id path parameter |
|
|
226
|
-
|
|
227
|
-
## Generating an OpenAPI Specification
|
|
228
|
-
|
|
229
|
-
To generate an OpenAPI v2 (Swagger) specification from the service definition, run the following command inside of the project folder:
|
|
230
|
-
|
|
231
|
-
```
|
|
232
|
-
tsp compile . --emit @azure-tools/typespec-autorest
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
This will create a file in the `tsp-output` subfolder called `openapi.json`.
|
|
236
|
-
|
|
237
|
-
## A Complete Example
|
|
238
|
-
|
|
239
|
-
Here's a complete example `main.tsp` file based on all of the snippets in this README:
|
|
240
|
-
|
|
241
|
-
```typespec
|
|
242
|
-
import "@typespec/http";
|
|
243
|
-
import "@typespec/rest";
|
|
244
|
-
import "@typespec/versioning";
|
|
245
|
-
import "@azure-tools/typespec-providerhub";
|
|
246
|
-
import "@azure-tools/typespec-azure-core";
|
|
247
|
-
import "@azure-tools/typespec-azure-resource-manager";
|
|
248
|
-
|
|
249
|
-
using TypeSpec.Http;
|
|
250
|
-
using TypeSpec.Rest;
|
|
251
|
-
using TypeSpec.Versioning;
|
|
252
|
-
using Azure.Core;
|
|
253
|
-
using Azure.ResourceManager;
|
|
254
|
-
|
|
255
|
-
@armProviderNamespace
|
|
256
|
-
@service({
|
|
257
|
-
title: "ContosoProviderHubClient",
|
|
258
|
-
version: "2021-01-01-preview",
|
|
259
|
-
})
|
|
260
|
-
@doc("Contoso Resource Provider management API.")
|
|
261
|
-
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
|
|
262
|
-
namespace Microsoft.ContosoProviderHub;
|
|
263
|
-
|
|
264
|
-
interface Operations extends Azure.ResourceManager.Operations {}
|
|
265
|
-
|
|
266
|
-
@lroStatus
|
|
267
|
-
enum ProvisioningState {
|
|
268
|
-
...ResourceProvisioningState,
|
|
269
|
-
Provisioning,
|
|
270
|
-
Updating,
|
|
271
|
-
Deleting,
|
|
272
|
-
Accepted,
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
@doc("The properties of UserResource")
|
|
276
|
-
model UserResourceProperties {
|
|
277
|
-
@doc("The user's full name")
|
|
278
|
-
fullName: string;
|
|
279
|
-
|
|
280
|
-
@doc("The user's email address.")
|
|
281
|
-
emailAddress: string;
|
|
282
|
-
|
|
283
|
-
@doc("The status of the last operation.")
|
|
284
|
-
provisioningState?: ProvisioningState;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
@doc("A UserResource")
|
|
288
|
-
model UserResource is TrackedResource<UserResourceProperties> {
|
|
289
|
-
@key("userName")
|
|
290
|
-
@segment("users")
|
|
291
|
-
@doc("Address name")
|
|
292
|
-
@path
|
|
293
|
-
name: string;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
@doc("The details of a user notification.")
|
|
297
|
-
model NotificationDetails {
|
|
298
|
-
@doc("The notification message.")
|
|
299
|
-
message: string;
|
|
300
|
-
|
|
301
|
-
@doc("If true, the notification is urgent.")
|
|
302
|
-
urgent: boolean;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
@armResourceOperations
|
|
306
|
-
interface Users extends TrackedResourceOperations<UserResource, UserResourceProperties> {
|
|
307
|
-
@post
|
|
308
|
-
@doc("Send a notification to the user")
|
|
309
|
-
@segment("notify")
|
|
310
|
-
notifyUser(
|
|
311
|
-
...ResourceInstanceParameters<UserResource>,
|
|
312
|
-
@body notification: NotificationDetails
|
|
313
|
-
): ArmResponse<string> | ErrorResponse;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
@doc("An address resource belonging to a user resource.")
|
|
317
|
-
@parentResource(UserResource)
|
|
318
|
-
model AddressResource is ProxyResource<AddressResourceProperties> {
|
|
319
|
-
@doc("Address name")
|
|
320
|
-
@key("addressName")
|
|
321
|
-
@segment("addresses")
|
|
322
|
-
@path
|
|
323
|
-
name: string;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
@doc("The properties of AddressResource")
|
|
327
|
-
model AddressResourceProperties {
|
|
328
|
-
@doc("The street address.")
|
|
329
|
-
streetAddress: string;
|
|
330
|
-
|
|
331
|
-
@doc("The city of the address.")
|
|
332
|
-
city: string;
|
|
16
|
+
## Documentation
|
|
333
17
|
|
|
334
|
-
|
|
335
|
-
state: string;
|
|
18
|
+
All relevant documentation for using the TypeSpec ARM library can be found [here](https://azure.github.io/typespec-azure/).
|
|
336
19
|
|
|
337
|
-
|
|
338
|
-
zip: int32;
|
|
20
|
+
[Getting Started Tutorial](https://azure.github.io/typespec-azure/docs/getstarted/azure-resource-manager/step00)
|
|
339
21
|
|
|
340
|
-
|
|
341
|
-
provisioningState?: ProvisioningState;
|
|
342
|
-
}
|
|
22
|
+
[How-tos and Examples](https://azure.github.io/typespec-azure/docs/howtos/ARM/arm-rules)
|
|
343
23
|
|
|
344
|
-
|
|
345
|
-
interface Addresses extends ProxyResourceOperations<AddressResource> {}
|
|
346
|
-
```
|
|
24
|
+
[Reference](https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/reference)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure-tools/typespec-azure-resource-manager",
|
|
3
|
-
"version": "0.31.0-dev.
|
|
3
|
+
"version": "0.31.0-dev.3",
|
|
4
4
|
"author": "Microsoft Corporation",
|
|
5
5
|
"description": "TypeSpec Azure Resource Manager library",
|
|
6
6
|
"homepage": "https://azure.github.io/typespec-azure",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"@typespec/compiler": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
46
46
|
"@azure-tools/typespec-azure-core": "~0.30.1 || >=0.31.0-dev <0.31.0",
|
|
47
|
-
"@azure-tools/typespec-autorest": "~0.30.
|
|
47
|
+
"@azure-tools/typespec-autorest": "~0.30.1 || >=0.31.0-dev <0.31.0",
|
|
48
48
|
"@typespec/openapi": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
49
49
|
"@typespec/rest": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
50
50
|
"@typespec/http": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"@typespec/compiler": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
58
58
|
"@typespec/openapi": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
59
59
|
"@azure-tools/typespec-azure-core": "~0.30.1 || >=0.31.0-dev <0.31.0",
|
|
60
|
-
"@azure-tools/typespec-autorest": "~0.30.
|
|
60
|
+
"@azure-tools/typespec-autorest": "~0.30.1 || >=0.31.0-dev <0.31.0",
|
|
61
61
|
"@typespec/rest": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
62
62
|
"@typespec/http": "~0.44.0 || >=0.45.0-dev <0.45.0",
|
|
63
63
|
"@typespec/versioning": "~0.44.0 || >=0.45.0-dev <0.45.0",
|