|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2022 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | + |
| 16 | +import argparse |
| 17 | + |
| 18 | + |
| 19 | +# [START secretmanager_update_secret_with_alias] |
| 20 | +def update_secret_with_alias(project_id, secret_id): |
| 21 | + """ |
| 22 | + Update the metadata about an existing secret. |
| 23 | + """ |
| 24 | + |
| 25 | + # Import the Secret Manager client library. |
| 26 | + from google.cloud import secretmanager |
| 27 | + |
| 28 | + # Create the Secret Manager client. |
| 29 | + client = secretmanager.SecretManagerServiceClient() |
| 30 | + |
| 31 | + # Build the resource name of the secret. |
| 32 | + name = client.secret_path(project_id, secret_id) |
| 33 | + |
| 34 | + # Update the secret. |
| 35 | + secret = {"name": name, "version_aliases": {"test": 1}} |
| 36 | + update_mask = {"paths": ["version_aliases"]} |
| 37 | + response = client.update_secret( |
| 38 | + request={"secret": secret, "update_mask": update_mask} |
| 39 | + ) |
| 40 | + |
| 41 | + # Print the new secret name. |
| 42 | + print("Updated secret: {}".format(response.name)) |
| 43 | + # [END secretmanager_update_secret_with_alias] |
| 44 | + |
| 45 | + return response |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + parser = argparse.ArgumentParser( |
| 50 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 51 | + ) |
| 52 | + parser.add_argument("project_id", help="id of the GCP project") |
| 53 | + parser.add_argument("--secret-id", required=True) |
| 54 | + args = parser.parse_args() |
| 55 | + |
| 56 | + update_secret_with_alias(args.project_id, args.secret_id) |
0 commit comments