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
15use crate::model::Operation;
16use google_cloud_gax::error::rpc::{Code, Status};
17
18impl google_cloud_lro::internal::DiscoveryOperation for Operation {
19    fn name(&self) -> Option<&String> {
20        self.name.as_ref()
21    }
22    fn done(&self) -> bool {
23        self.status == Some(crate::model::operation::Status::Done)
24    }
25    fn error(&self) -> Option<Status> {
26        if self.error.is_none()
27            && self.http_error_status_code.is_none()
28            && self.http_error_message.is_none()
29        {
30            return None;
31        }
32
33        let mut status = Status::default();
34
35        let http_status = self.http_error_status_code.unwrap_or(200);
36        let mut code = match http_status {
37            200 => Code::Ok,
38            400 => Code::InvalidArgument,
39            401 => Code::Unauthenticated,
40            403 => Code::PermissionDenied,
41            404 => Code::NotFound,
42            409 => Code::AlreadyExists,
43            429 => Code::ResourceExhausted,
44            499 => Code::Cancelled,
45            500 => Code::Internal,
46            501 => Code::Unimplemented,
47            503 => Code::Unavailable,
48            504 => Code::DeadlineExceeded,
49            _ => Code::Unknown,
50        };
51
52        let mut message = self.http_error_message.clone().unwrap_or_default();
53
54        if let Some(first_err) = self.error.as_ref().and_then(|err| err.errors.first()) {
55            if code == Code::Ok {
56                code = Code::Unknown;
57            }
58            if let Some(err_code) = &first_err.code {
59                code = match err_code.as_str() {
60                    "QUOTA_EXCEEDED" => Code::ResourceExhausted,
61                    other => Code::try_from(other).unwrap_or(code),
62                };
63            }
64            if let Some(err_msg) = &first_err.message {
65                message = err_msg.clone();
66            }
67        }
68
69        if code == Code::Ok && http_status == 200 {
70            return None;
71        }
72
73        status = status.set_code(code).set_message(message);
74        Some(status)
75    }
76}