google_cloud_orgpolicy_v2/client.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// Code generated by sidekick. DO NOT EDIT.
16#![allow(rustdoc::redundant_explicit_links)]
17#![allow(rustdoc::broken_intra_doc_links)]
18
19/// Implements a client for the Organization Policy API.
20///
21/// # Example
22/// ```
23/// # tokio_test::block_on(async {
24/// # use google_cloud_orgpolicy_v2::client::OrgPolicy;
25/// let client = OrgPolicy::builder().build().await?;
26/// // use `client` to make requests to the Organization Policy API.
27/// # gax::client_builder::Result::<()>::Ok(()) });
28/// ```
29///
30/// # Service Description
31///
32/// An interface for managing organization policies.
33///
34/// The Organization Policy Service provides a simple mechanism for
35/// organizations to restrict the allowed configurations across their entire
36/// resource hierarchy.
37///
38/// You can use a policy to configure restrictions on resources. For
39/// example, you can enforce a policy that restricts which Google
40/// Cloud APIs can be activated in a certain part of your resource
41/// hierarchy, or prevents serial port access to VM instances in a
42/// particular folder.
43///
44/// Policies are inherited down through the resource hierarchy. A policy
45/// applied to a parent resource automatically applies to all its child resources
46/// unless overridden with a policy lower in the hierarchy.
47///
48/// A constraint defines an aspect of a resource's configuration that can be
49/// controlled by an organization's policy administrator. Policies are a
50/// collection of constraints that defines their allowable configuration on a
51/// particular resource and its child resources.
52///
53/// # Configuration
54///
55/// To configure `OrgPolicy` use the `with_*` methods in the type returned
56/// by [builder()][OrgPolicy::builder]. The default configuration should
57/// work for most applications. Common configuration changes include
58///
59/// * [with_endpoint()]: by default this client uses the global default endpoint
60/// (`https://orgpolicy.googleapis.com`). Applications using regional
61/// endpoints or running in restricted networks (e.g. a network configured
62// with [Private Google Access with VPC Service Controls]) may want to
63/// override this default.
64/// * [with_credentials()]: by default this client uses
65/// [Application Default Credentials]. Applications using custom
66/// authentication may need to override this default.
67///
68/// [with_endpoint()]: super::builder::org_policy::ClientBuilder::with_endpoint
69/// [with_credentials()]: super::builder::org_policy::ClientBuilder::credentials
70/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
71/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
72///
73/// # Pooling and Cloning
74///
75/// `OrgPolicy` holds a connection pool internally, it is advised to
76/// create one and the reuse it. You do not need to wrap `OrgPolicy` in
77/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
78/// already uses an `Arc` internally.
79#[derive(Clone, Debug)]
80pub struct OrgPolicy {
81 inner: std::sync::Arc<dyn super::stub::dynamic::OrgPolicy>,
82}
83
84impl OrgPolicy {
85 /// Returns a builder for [OrgPolicy].
86 ///
87 /// ```
88 /// # tokio_test::block_on(async {
89 /// # use google_cloud_orgpolicy_v2::client::OrgPolicy;
90 /// let client = OrgPolicy::builder().build().await?;
91 /// # gax::client_builder::Result::<()>::Ok(()) });
92 /// ```
93 pub fn builder() -> super::builder::org_policy::ClientBuilder {
94 gax::client_builder::internal::new_builder(super::builder::org_policy::client::Factory)
95 }
96
97 /// Creates a new client from the provided stub.
98 ///
99 /// The most common case for calling this function is in tests mocking the
100 /// client's behavior.
101 pub fn from_stub<T>(stub: T) -> Self
102 where
103 T: super::stub::OrgPolicy + 'static,
104 {
105 Self {
106 inner: std::sync::Arc::new(stub),
107 }
108 }
109
110 pub(crate) async fn new(
111 config: gaxi::options::ClientConfig,
112 ) -> gax::client_builder::Result<Self> {
113 let inner = Self::build_inner(config).await?;
114 Ok(Self { inner })
115 }
116
117 async fn build_inner(
118 conf: gaxi::options::ClientConfig,
119 ) -> gax::client_builder::Result<std::sync::Arc<dyn super::stub::dynamic::OrgPolicy>> {
120 if gaxi::options::tracing_enabled(&conf) {
121 return Ok(std::sync::Arc::new(Self::build_with_tracing(conf).await?));
122 }
123 Ok(std::sync::Arc::new(Self::build_transport(conf).await?))
124 }
125
126 async fn build_transport(
127 conf: gaxi::options::ClientConfig,
128 ) -> gax::client_builder::Result<impl super::stub::OrgPolicy> {
129 super::transport::OrgPolicy::new(conf).await
130 }
131
132 async fn build_with_tracing(
133 conf: gaxi::options::ClientConfig,
134 ) -> gax::client_builder::Result<impl super::stub::OrgPolicy> {
135 Self::build_transport(conf)
136 .await
137 .map(super::tracing::OrgPolicy::new)
138 }
139
140 /// Lists constraints that could be applied on the specified resource.
141 pub fn list_constraints(&self) -> super::builder::org_policy::ListConstraints {
142 super::builder::org_policy::ListConstraints::new(self.inner.clone())
143 }
144
145 /// Retrieves all of the policies that exist on a particular resource.
146 pub fn list_policies(&self) -> super::builder::org_policy::ListPolicies {
147 super::builder::org_policy::ListPolicies::new(self.inner.clone())
148 }
149
150 /// Gets a policy on a resource.
151 ///
152 /// If no policy is set on the resource, `NOT_FOUND` is returned. The
153 /// `etag` value can be used with `UpdatePolicy()` to update a
154 /// policy during read-modify-write.
155 pub fn get_policy(&self) -> super::builder::org_policy::GetPolicy {
156 super::builder::org_policy::GetPolicy::new(self.inner.clone())
157 }
158
159 /// Gets the effective policy on a resource. This is the result of merging
160 /// policies in the resource hierarchy and evaluating conditions. The
161 /// returned policy will not have an `etag` or `condition` set because it is
162 /// an evaluated policy across multiple resources.
163 /// Subtrees of Resource Manager resource hierarchy with 'under:' prefix will
164 /// not be expanded.
165 pub fn get_effective_policy(&self) -> super::builder::org_policy::GetEffectivePolicy {
166 super::builder::org_policy::GetEffectivePolicy::new(self.inner.clone())
167 }
168
169 /// Creates a policy.
170 ///
171 /// Returns a `google.rpc.Status` with `google.rpc.Code.NOT_FOUND` if the
172 /// constraint does not exist.
173 /// Returns a `google.rpc.Status` with `google.rpc.Code.ALREADY_EXISTS` if the
174 /// policy already exists on the given Google Cloud resource.
175 pub fn create_policy(&self) -> super::builder::org_policy::CreatePolicy {
176 super::builder::org_policy::CreatePolicy::new(self.inner.clone())
177 }
178
179 /// Updates a policy.
180 ///
181 /// Returns a `google.rpc.Status` with `google.rpc.Code.NOT_FOUND` if the
182 /// constraint or the policy do not exist.
183 /// Returns a `google.rpc.Status` with `google.rpc.Code.ABORTED` if the etag
184 /// supplied in the request does not match the persisted etag of the policy
185 ///
186 /// Note: the supplied policy will perform a full overwrite of all
187 /// fields.
188 pub fn update_policy(&self) -> super::builder::org_policy::UpdatePolicy {
189 super::builder::org_policy::UpdatePolicy::new(self.inner.clone())
190 }
191
192 /// Deletes a policy.
193 ///
194 /// Returns a `google.rpc.Status` with `google.rpc.Code.NOT_FOUND` if the
195 /// constraint or organization policy does not exist.
196 pub fn delete_policy(&self) -> super::builder::org_policy::DeletePolicy {
197 super::builder::org_policy::DeletePolicy::new(self.inner.clone())
198 }
199
200 /// Creates a custom constraint.
201 ///
202 /// Returns a `google.rpc.Status` with `google.rpc.Code.NOT_FOUND` if the
203 /// organization does not exist.
204 /// Returns a `google.rpc.Status` with `google.rpc.Code.ALREADY_EXISTS` if the
205 /// constraint already exists on the given organization.
206 pub fn create_custom_constraint(&self) -> super::builder::org_policy::CreateCustomConstraint {
207 super::builder::org_policy::CreateCustomConstraint::new(self.inner.clone())
208 }
209
210 /// Updates a custom constraint.
211 ///
212 /// Returns a `google.rpc.Status` with `google.rpc.Code.NOT_FOUND` if the
213 /// constraint does not exist.
214 ///
215 /// Note: the supplied policy will perform a full overwrite of all
216 /// fields.
217 pub fn update_custom_constraint(&self) -> super::builder::org_policy::UpdateCustomConstraint {
218 super::builder::org_policy::UpdateCustomConstraint::new(self.inner.clone())
219 }
220
221 /// Gets a custom or managed constraint.
222 ///
223 /// Returns a `google.rpc.Status` with `google.rpc.Code.NOT_FOUND` if the
224 /// custom or managed constraint does not exist.
225 pub fn get_custom_constraint(&self) -> super::builder::org_policy::GetCustomConstraint {
226 super::builder::org_policy::GetCustomConstraint::new(self.inner.clone())
227 }
228
229 /// Retrieves all of the custom constraints that exist on a particular
230 /// organization resource.
231 pub fn list_custom_constraints(&self) -> super::builder::org_policy::ListCustomConstraints {
232 super::builder::org_policy::ListCustomConstraints::new(self.inner.clone())
233 }
234
235 /// Deletes a custom constraint.
236 ///
237 /// Returns a `google.rpc.Status` with `google.rpc.Code.NOT_FOUND` if the
238 /// constraint does not exist.
239 pub fn delete_custom_constraint(&self) -> super::builder::org_policy::DeleteCustomConstraint {
240 super::builder::org_policy::DeleteCustomConstraint::new(self.inner.clone())
241 }
242}