Skip to main content

google_cloud_compute_v1/
operation.rs

1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(feature = "__enable-discovery-LRO")]
16use crate::model::Operation;
17#[cfg(feature = "__enable-discovery-LRO")]
18use google_cloud_gax::error::rpc::{Code, Status};
19
20#[cfg(feature = "__enable-discovery-LRO")]
21impl google_cloud_lro::internal::DiscoveryOperation for Operation {
22    fn name(&self) -> Option<&String> {
23        self.name.as_ref()
24    }
25    fn done(&self) -> bool {
26        self.status == Some(crate::model::operation::Status::Done)
27    }
28    fn error(&self) -> Option<Status> {
29        if self.error.is_none()
30            && self.http_error_status_code.is_none()
31            && self.http_error_message.is_none()
32        {
33            return None;
34        }
35
36        let mut status = Status::default();
37
38        let http_status = self.http_error_status_code.unwrap_or(200);
39        let mut code = match http_status {
40            200 => Code::Ok,
41            400 => Code::InvalidArgument,
42            401 => Code::Unauthenticated,
43            403 => Code::PermissionDenied,
44            404 => Code::NotFound,
45            409 => Code::AlreadyExists,
46            429 => Code::ResourceExhausted,
47            499 => Code::Cancelled,
48            500 => Code::Internal,
49            501 => Code::Unimplemented,
50            503 => Code::Unavailable,
51            504 => Code::DeadlineExceeded,
52            _ => Code::Unknown,
53        };
54
55        let mut message = self.http_error_message.clone().unwrap_or_default();
56
57        if let Some(first_err) = self.error.as_ref().and_then(|err| err.errors.first()) {
58            if code == Code::Ok {
59                code = Code::Unknown;
60            }
61            if let Some(err_code) = &first_err.code {
62                code = match err_code.as_str() {
63                    "QUOTA_EXCEEDED" => Code::ResourceExhausted,
64                    other => Code::try_from(other).unwrap_or(code),
65                };
66            }
67            if let Some(err_msg) = &first_err.message {
68                message = err_msg.clone();
69            }
70        }
71
72        if code == Code::Ok && http_status == 200 {
73            return None;
74        }
75
76        status = status.set_code(code).set_message(message);
77        Some(status)
78    }
79}