You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: graph/patterns/upsert.md
+16-11Lines changed: 16 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,13 +21,17 @@ Additionally, IaC code scripts or templates usually employ client-provided names
21
21
22
22
The solution is to use an `Upsert` pattern, to solve for the non-idempotent creation and client-provided naming problems.
23
23
24
-
* For IaC scenarios, resources must use `Upsert` semantics with an [alternate key](./alternate-key.md):
25
-
* Use `PATCH` with a client-provided alternate key.
26
-
* For a non-existent resource (specified by the alternate key) the service must handle this as a "create". As part of creation, the service must still generate the primary key value.
27
-
* For an existing resource (specified by the alternate key) the service must handle this as an "update.
28
-
* Any new alternate key, used for IaC scenarios, should be called `uniqueName`, if there isn't already an existing property that could be used as an alternate key.
24
+
* For IaC scenarios, resources must use `Upsert` semantics with a client-provided key:
25
+
* Use `PATCH` with a client-provided key:
26
+
* If there is a natural client-provided key that can serve as the primary key, then the service should support `Upsert` with that key.
27
+
* If the primary key is service-generated, the client-provided key should use an [alternate key](./alternate-key.md) to support idempotent creation.
28
+
* For a non-existent resource (specified by the client-provided key) the service must handle this as a "create". As part of creation, the service must still generate the primary key value, if appropriate.
29
+
* For an existing resource (specified by the client-provided key) the service must handle this as an "update".
30
+
* Any new alternate key, used for IaC scenarios, should be called `uniqueName`, if there isn't already a more natural existing property that could be used as an alternate key.
29
31
* NOTE: the service must also support `GET` using the alternate key pattern.
30
-
* For consistent CRUD Microsoft Graph behaviors, all resources, **including** resources used in IaC scenarios, should use `POST` and a service-generated primary key, per existing guidelines, and support `GET`, `PATCH` and `DELETE` using the primary key.
32
+
* Services should always support `POST` to the collection URL.
33
+
* For service-generated keys, this should return the server generated key.
34
+
* For client-provided keys, the client should provide the key as part of the request payload.
31
35
* If a service does not support `Upsert`, then a `PATCH` call against a non-existent resource must result in an HTTP "409 conflict" error.
32
36
33
37
This solution allows for existing resources that follow Microsoft Graph conventions for CRUD operations to add `Upsert` without impacting existing apps or functionality.
@@ -40,12 +44,11 @@ This pattern should be adopted for resources that are managed through infrastruc
40
44
41
45
## Issues and considerations
42
46
43
-
* The addition of this new pattern (with alternate key) does not represent a breaking change.
47
+
* The addition of this new pattern (with an alternate key) to an existing API does not represent a breaking change.
44
48
However, some API producers may have concerns about accidental usages of this new pattern unwittingly creating many new resources when the intent was an update.
45
49
As a result, API producers can use the `Prefer: idempotent` to require clients to opt-in to the Upsert behavior.
46
50
* The client-provided alternate key must be immutable after being set. If its value is null then it should be settable as a way to backfill existing resources for use in IaC scenarios.
47
51
* API producers could use `PUT` operations to create or update, but generally this approach is not recommended due to the destructive nature of `PUT`'s replace semantics.
48
-
* API producers could use `Upsert` with a primary (client-provided) key and this may be appropriate for some scenarios. However, the recommendation is for resources to support creation using `POST` and a service-generated primary key, for consistency reasons.
49
52
* API producers may annotate entity sets, singletons and collections to indicate that entities can be "upserted". The example below shows this annotation for the `groups` entity set.
50
53
51
54
```xml
@@ -58,6 +61,8 @@ As a result, API producers can use the `Prefer: idempotent` to require clients t
58
61
</EntitySet>
59
62
```
60
63
64
+
*`Upsert` can also be supported against singletons, using a `PATCH` to the singleton's URL.
65
+
61
66
## Examples
62
67
63
68
For these examples we'll use the `group` entity type, which defines both a primary (service-generated) key (`id`) and an alternate (client-provided) key (`uniqueName`).
@@ -71,12 +76,12 @@ For these examples we'll use the `group` entity type, which defines both a prima
0 commit comments