|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2021 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 | +# limitations under the License. |
| 16 | + |
| 17 | +# [START privateca_undelete_ca] |
| 18 | +import google.cloud.security.privateca_v1 as privateca_v1 |
| 19 | + |
| 20 | + |
| 21 | +def undelete_certificate_authority( |
| 22 | + project_id: str, location: str, ca_pool_name: str, ca_name: str |
| 23 | +) -> None: |
| 24 | + """ |
| 25 | + Restore a deleted CA, if still within the grace period of 30 days. |
| 26 | +
|
| 27 | + Args: |
| 28 | + project_id: project ID or project number of the Cloud project you want to use. |
| 29 | + location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations. |
| 30 | + ca_pool_name: the name of the CA pool under which the deleted CA is present. |
| 31 | + ca_name: the name of the CA to be restored (undeleted). |
| 32 | + """ |
| 33 | + |
| 34 | + caServiceClient = privateca_v1.CertificateAuthorityServiceClient() |
| 35 | + ca_path = caServiceClient.certificate_authority_path( |
| 36 | + project_id, location, ca_pool_name, ca_name |
| 37 | + ) |
| 38 | + |
| 39 | + # Confirm if the CA is in DELETED stage. |
| 40 | + ca_state = caServiceClient.get_certificate_authority(name=ca_path).state |
| 41 | + if ca_state != privateca_v1.CertificateAuthority.State.DELETED: |
| 42 | + print("CA is not deleted !") |
| 43 | + return |
| 44 | + |
| 45 | + # Create the Request. |
| 46 | + request = privateca_v1.UndeleteCertificateAuthorityRequest(name=ca_path) |
| 47 | + |
| 48 | + # Undelete the CA. |
| 49 | + operation = caServiceClient.undelete_certificate_authority(request=request) |
| 50 | + result = operation.result() |
| 51 | + |
| 52 | + print("Operation result", result) |
| 53 | + |
| 54 | + # Get the current CA state. |
| 55 | + ca_state = caServiceClient.get_certificate_authority(name=ca_path).state |
| 56 | + |
| 57 | + # CA state changes from DELETED to DISABLED if successfully restored. |
| 58 | + # Confirm if the CA is DISABLED. |
| 59 | + if ca_state == privateca_v1.CertificateAuthority.State.DISABLED: |
| 60 | + print("Successfully undeleted Certificate Authority:", ca_name) |
| 61 | + else: |
| 62 | + print( |
| 63 | + "Unable to restore the Certificate Authority! Please try again! Current state:", |
| 64 | + ca_state, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +# [END privateca_undelete_ca] |
0 commit comments