|
| 1 | +# 2.0.0 Migration Guide |
| 2 | + |
| 3 | +The 2.0 release of the `google-cloud-kms` client is a significant upgrade based on a [next-gen code generator](https://github.com/googleapis/gapic-generator-python), and includes substantial interface changes. Existing code written for earlier versions of this library will likely require updates to use this version. This document describes the changes that have been made, and what you need to do to update your usage. |
| 4 | + |
| 5 | +If you experience issues or have questions, please file an [issue](https://github.com/googleapis/python-kms/issues). |
| 6 | + |
| 7 | +## Supported Python Versions |
| 8 | + |
| 9 | +> **WARNING**: Breaking change |
| 10 | +
|
| 11 | +The 2.0.0 release requires Python 3.6+. |
| 12 | + |
| 13 | + |
| 14 | +## Method Calls |
| 15 | + |
| 16 | +> **WARNING**: Breaking change |
| 17 | +
|
| 18 | +Methods expect request objects. We provide a script that will convert most common use cases. |
| 19 | + |
| 20 | +* Install the library |
| 21 | + |
| 22 | +```py |
| 23 | +python3 -m pip install google-cloud-kms |
| 24 | +``` |
| 25 | + |
| 26 | +* The script `fixup_kms_v1_keywords.py` is shipped with the library. It expects |
| 27 | +an input directory (with the code to convert) and an empty destination directory. |
| 28 | + |
| 29 | +```sh |
| 30 | +$ fixup_kms_v1_keywords.py --input-directory .samples/ --output-directory samples/ |
| 31 | +``` |
| 32 | + |
| 33 | +**Before:** |
| 34 | +```py |
| 35 | + from google.cloud import kms |
| 36 | + |
| 37 | +client = kms.KeyManagementServiceClient() |
| 38 | +location_name = client.location_path(project_id, location_id) |
| 39 | +key_ring = {} |
| 40 | + |
| 41 | +created_key_ring = client.create_key_ring(location_name, id, key_ring) |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +**After:** |
| 46 | +```py |
| 47 | +from google.cloud import kms |
| 48 | + |
| 49 | +client = kms.KeyManagementServiceClient() |
| 50 | +location_name = f'projects/{project_id}/locations/{location_id}' |
| 51 | +key_ring = {} |
| 52 | + |
| 53 | +created_key_ring = client.create_key_ring(request={'parent': location_name, 'key_ring_id': id, 'key_ring': key_ring}) |
| 54 | +``` |
| 55 | + |
| 56 | +### More Details |
| 57 | + |
| 58 | +In `google-cloud-kms<2.0.0`, parameters required by the API were positional parameters and optional parameters were keyword parameters. |
| 59 | + |
| 60 | +**Before:** |
| 61 | +```py |
| 62 | + def create_key_ring( |
| 63 | + self, |
| 64 | + parent, |
| 65 | + key_ring_id, |
| 66 | + key_ring, |
| 67 | + retry=google.api_core.gapic_v1.method.DEFAULT, |
| 68 | + timeout=google.api_core.gapic_v1.method.DEFAULT, |
| 69 | + metadata=None, |
| 70 | + ): |
| 71 | +``` |
| 72 | + |
| 73 | +In the 2.0.0 release, all methods have a single positional parameter `request`. Method docstrings indicate whether a parameter is required or optional. |
| 74 | + |
| 75 | +Some methods have additional keyword only parameters. The available parameters depend on the `google.api.method_signature` annotation specified by the API producer. |
| 76 | + |
| 77 | + |
| 78 | +**After:** |
| 79 | +```py |
| 80 | + def create_key_ring( |
| 81 | + self, |
| 82 | + request: service.CreateKeyRingRequest = None, |
| 83 | + *, |
| 84 | + parent: str = None, |
| 85 | + key_ring_id: str = None, |
| 86 | + key_ring: resources.KeyRing = None, |
| 87 | + retry: retries.Retry = gapic_v1.method.DEFAULT, |
| 88 | + timeout: float = None, |
| 89 | + metadata: Sequence[Tuple[str, str]] = (), |
| 90 | + ) -> resources.KeyRing: |
| 91 | +``` |
| 92 | + |
| 93 | +> **NOTE:** The `request` parameter and flattened keyword parameters for the API are mutually exclusive. |
| 94 | +> Passing both will result in an error. |
| 95 | +
|
| 96 | + |
| 97 | +Both of these calls are valid: |
| 98 | + |
| 99 | +```py |
| 100 | +response = client.create_key_ring( |
| 101 | + request={ |
| 102 | + "parent": parent, |
| 103 | + "key_ring_id": key_ring_id, |
| 104 | + "key_ring": key_ring |
| 105 | + } |
| 106 | +) |
| 107 | +``` |
| 108 | + |
| 109 | +```py |
| 110 | +response = client.create_key_ring( |
| 111 | + parent=parent, |
| 112 | + key_ring_id=key_ring_id, |
| 113 | + key_ring=key_ring |
| 114 | +) |
| 115 | +``` |
| 116 | + |
| 117 | +This call is invalid because it mixes `request` with a keyword argument `key_ring`. Executing this code |
| 118 | +will result in an error. |
| 119 | + |
| 120 | +```py |
| 121 | +response = client.create_key_ring( |
| 122 | + request={ |
| 123 | + "parent": parent, |
| 124 | + "key_ring_id": key_ring_id, |
| 125 | + }, |
| 126 | + key_ring=key_ring |
| 127 | +) |
| 128 | +``` |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +## Enums and Types |
| 133 | + |
| 134 | + |
| 135 | +> **WARNING**: Breaking change |
| 136 | +
|
| 137 | +The submodules `enums` and `types` have been removed. |
| 138 | + |
| 139 | +**Before:** |
| 140 | +```py |
| 141 | + |
| 142 | + from google.cloud import kms |
| 143 | + |
| 144 | +purpose = kms.enums.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_SIGN |
| 145 | +key_ring = kms.types.KeyRing() |
| 146 | +``` |
| 147 | + |
| 148 | + |
| 149 | +**After:** |
| 150 | +```py |
| 151 | +from google.cloud import kms |
| 152 | + |
| 153 | +purpose = kms.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_SIGN |
| 154 | +key_ring = kms.KeyRing() |
| 155 | +``` |
| 156 | + |
| 157 | +## Resource Path Helper Methods |
| 158 | + |
| 159 | +The resource path helper method `location_path` has been removed. Please construct |
| 160 | +this path manually. |
| 161 | + |
| 162 | +```py |
| 163 | +project = 'my-project' |
| 164 | +location = 'us-east1' |
| 165 | + |
| 166 | +location_path = f'projects/{project}/locations/{location}' |
| 167 | +``` |
0 commit comments