Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/resourcemanager_v3/services/tag_bindings/transports/grpc.py: 41%

80 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-06 06:03 +0000

1# -*- coding: utf-8 -*- 

2# Copyright 2022 Google LLC 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15# 

16from typing import Callable, Dict, Optional, Sequence, Tuple, Union 

17import warnings 

18 

19from google.api_core import gapic_v1, grpc_helpers, operations_v1 

20import google.auth # type: ignore 

21from google.auth import credentials as ga_credentials # type: ignore 

22from google.auth.transport.grpc import SslCredentials # type: ignore 

23from google.longrunning import operations_pb2 # type: ignore 

24import grpc # type: ignore 

25 

26from google.cloud.resourcemanager_v3.types import tag_bindings 

27 

28from .base import DEFAULT_CLIENT_INFO, TagBindingsTransport 

29 

30 

31class TagBindingsGrpcTransport(TagBindingsTransport): 

32 """gRPC backend transport for TagBindings. 

33 

34 Allow users to create and manage TagBindings between 

35 TagValues and different Google Cloud resources throughout the 

36 GCP resource hierarchy. 

37 

38 This class defines the same methods as the primary client, so the 

39 primary client can load the underlying transport implementation 

40 and call it. 

41 

42 It sends protocol buffers over the wire using gRPC (which is built on 

43 top of HTTP/2); the ``grpcio`` package must be installed. 

44 """ 

45 

46 _stubs: Dict[str, Callable] 

47 

48 def __init__( 

49 self, 

50 *, 

51 host: str = "cloudresourcemanager.googleapis.com", 

52 credentials: Optional[ga_credentials.Credentials] = None, 

53 credentials_file: Optional[str] = None, 

54 scopes: Optional[Sequence[str]] = None, 

55 channel: Optional[grpc.Channel] = None, 

56 api_mtls_endpoint: Optional[str] = None, 

57 client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, 

58 ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, 

59 client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, 

60 quota_project_id: Optional[str] = None, 

61 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

62 always_use_jwt_access: Optional[bool] = False, 

63 api_audience: Optional[str] = None, 

64 ) -> None: 

65 """Instantiate the transport. 

66 

67 Args: 

68 host (Optional[str]): 

69 The hostname to connect to. 

70 credentials (Optional[google.auth.credentials.Credentials]): The 

71 authorization credentials to attach to requests. These 

72 credentials identify the application to the service; if none 

73 are specified, the client will attempt to ascertain the 

74 credentials from the environment. 

75 This argument is ignored if ``channel`` is provided. 

76 credentials_file (Optional[str]): A file with credentials that can 

77 be loaded with :func:`google.auth.load_credentials_from_file`. 

78 This argument is ignored if ``channel`` is provided. 

79 scopes (Optional(Sequence[str])): A list of scopes. This argument is 

80 ignored if ``channel`` is provided. 

81 channel (Optional[grpc.Channel]): A ``Channel`` instance through 

82 which to make calls. 

83 api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. 

84 If provided, it overrides the ``host`` argument and tries to create 

85 a mutual TLS channel with client SSL credentials from 

86 ``client_cert_source`` or application default SSL credentials. 

87 client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): 

88 Deprecated. A callback to provide client SSL certificate bytes and 

89 private key bytes, both in PEM format. It is ignored if 

90 ``api_mtls_endpoint`` is None. 

91 ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials 

92 for the grpc channel. It is ignored if ``channel`` is provided. 

93 client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): 

94 A callback to provide client certificate bytes and private key bytes, 

95 both in PEM format. It is used to configure a mutual TLS channel. It is 

96 ignored if ``channel`` or ``ssl_channel_credentials`` is provided. 

97 quota_project_id (Optional[str]): An optional project to use for billing 

98 and quota. 

99 client_info (google.api_core.gapic_v1.client_info.ClientInfo): 

100 The client info used to send a user-agent string along with 

101 API requests. If ``None``, then default info will be used. 

102 Generally, you only need to set this if you're developing 

103 your own client library. 

104 always_use_jwt_access (Optional[bool]): Whether self signed JWT should 

105 be used for service account credentials. 

106 

107 Raises: 

108 google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport 

109 creation failed for any reason. 

110 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` 

111 and ``credentials_file`` are passed. 

112 """ 

113 self._grpc_channel = None 

114 self._ssl_channel_credentials = ssl_channel_credentials 

115 self._stubs: Dict[str, Callable] = {} 

116 self._operations_client: Optional[operations_v1.OperationsClient] = None 

117 

118 if api_mtls_endpoint: 

119 warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) 

120 if client_cert_source: 

121 warnings.warn("client_cert_source is deprecated", DeprecationWarning) 

122 

123 if channel: 

124 # Ignore credentials if a channel was passed. 

125 credentials = False 

126 # If a channel was explicitly provided, set it. 

127 self._grpc_channel = channel 

128 self._ssl_channel_credentials = None 

129 

130 else: 

131 if api_mtls_endpoint: 

132 host = api_mtls_endpoint 

133 

134 # Create SSL credentials with client_cert_source or application 

135 # default SSL credentials. 

136 if client_cert_source: 

137 cert, key = client_cert_source() 

138 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

139 certificate_chain=cert, private_key=key 

140 ) 

141 else: 

142 self._ssl_channel_credentials = SslCredentials().ssl_credentials 

143 

144 else: 

145 if client_cert_source_for_mtls and not ssl_channel_credentials: 

146 cert, key = client_cert_source_for_mtls() 

147 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

148 certificate_chain=cert, private_key=key 

149 ) 

150 

151 # The base transport sets the host, credentials and scopes 

152 super().__init__( 

153 host=host, 

154 credentials=credentials, 

155 credentials_file=credentials_file, 

156 scopes=scopes, 

157 quota_project_id=quota_project_id, 

158 client_info=client_info, 

159 always_use_jwt_access=always_use_jwt_access, 

160 api_audience=api_audience, 

161 ) 

162 

163 if not self._grpc_channel: 

164 self._grpc_channel = type(self).create_channel( 

165 self._host, 

166 # use the credentials which are saved 

167 credentials=self._credentials, 

168 # Set ``credentials_file`` to ``None`` here as 

169 # the credentials that we saved earlier should be used. 

170 credentials_file=None, 

171 scopes=self._scopes, 

172 ssl_credentials=self._ssl_channel_credentials, 

173 quota_project_id=quota_project_id, 

174 options=[ 

175 ("grpc.max_send_message_length", -1), 

176 ("grpc.max_receive_message_length", -1), 

177 ], 

178 ) 

179 

180 # Wrap messages. This must be done after self._grpc_channel exists 

181 self._prep_wrapped_messages(client_info) 

182 

183 @classmethod 

184 def create_channel( 

185 cls, 

186 host: str = "cloudresourcemanager.googleapis.com", 

187 credentials: Optional[ga_credentials.Credentials] = None, 

188 credentials_file: Optional[str] = None, 

189 scopes: Optional[Sequence[str]] = None, 

190 quota_project_id: Optional[str] = None, 

191 **kwargs, 

192 ) -> grpc.Channel: 

193 """Create and return a gRPC channel object. 

194 Args: 

195 host (Optional[str]): The host for the channel to use. 

196 credentials (Optional[~.Credentials]): The 

197 authorization credentials to attach to requests. These 

198 credentials identify this application to the service. If 

199 none are specified, the client will attempt to ascertain 

200 the credentials from the environment. 

201 credentials_file (Optional[str]): A file with credentials that can 

202 be loaded with :func:`google.auth.load_credentials_from_file`. 

203 This argument is mutually exclusive with credentials. 

204 scopes (Optional[Sequence[str]]): A optional list of scopes needed for this 

205 service. These are only used when credentials are not specified and 

206 are passed to :func:`google.auth.default`. 

207 quota_project_id (Optional[str]): An optional project to use for billing 

208 and quota. 

209 kwargs (Optional[dict]): Keyword arguments, which are passed to the 

210 channel creation. 

211 Returns: 

212 grpc.Channel: A gRPC channel object. 

213 

214 Raises: 

215 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` 

216 and ``credentials_file`` are passed. 

217 """ 

218 

219 return grpc_helpers.create_channel( 

220 host, 

221 credentials=credentials, 

222 credentials_file=credentials_file, 

223 quota_project_id=quota_project_id, 

224 default_scopes=cls.AUTH_SCOPES, 

225 scopes=scopes, 

226 default_host=cls.DEFAULT_HOST, 

227 **kwargs, 

228 ) 

229 

230 @property 

231 def grpc_channel(self) -> grpc.Channel: 

232 """Return the channel designed to connect to this service.""" 

233 return self._grpc_channel 

234 

235 @property 

236 def operations_client(self) -> operations_v1.OperationsClient: 

237 """Create the client designed to process long-running operations. 

238 

239 This property caches on the instance; repeated calls return the same 

240 client. 

241 """ 

242 # Quick check: Only create a new client if we do not already have one. 

243 if self._operations_client is None: 

244 self._operations_client = operations_v1.OperationsClient(self.grpc_channel) 

245 

246 # Return the client from cache. 

247 return self._operations_client 

248 

249 @property 

250 def list_tag_bindings( 

251 self, 

252 ) -> Callable[ 

253 [tag_bindings.ListTagBindingsRequest], tag_bindings.ListTagBindingsResponse 

254 ]: 

255 r"""Return a callable for the list tag bindings method over gRPC. 

256 

257 Lists the TagBindings for the given Google Cloud resource, as 

258 specified with ``parent``. 

259 

260 NOTE: The ``parent`` field is expected to be a full resource 

261 name: 

262 https://cloud.google.com/apis/design/resource_names#full_resource_name 

263 

264 Returns: 

265 Callable[[~.ListTagBindingsRequest], 

266 ~.ListTagBindingsResponse]: 

267 A function that, when called, will call the underlying RPC 

268 on the server. 

269 """ 

270 # Generate a "stub function" on-the-fly which will actually make 

271 # the request. 

272 # gRPC handles serialization and deserialization, so we just need 

273 # to pass in the functions for each. 

274 if "list_tag_bindings" not in self._stubs: 

275 self._stubs["list_tag_bindings"] = self.grpc_channel.unary_unary( 

276 "/google.cloud.resourcemanager.v3.TagBindings/ListTagBindings", 

277 request_serializer=tag_bindings.ListTagBindingsRequest.serialize, 

278 response_deserializer=tag_bindings.ListTagBindingsResponse.deserialize, 

279 ) 

280 return self._stubs["list_tag_bindings"] 

281 

282 @property 

283 def create_tag_binding( 

284 self, 

285 ) -> Callable[[tag_bindings.CreateTagBindingRequest], operations_pb2.Operation]: 

286 r"""Return a callable for the create tag binding method over gRPC. 

287 

288 Creates a TagBinding between a TagValue and a Google 

289 Cloud resource. 

290 

291 Returns: 

292 Callable[[~.CreateTagBindingRequest], 

293 ~.Operation]: 

294 A function that, when called, will call the underlying RPC 

295 on the server. 

296 """ 

297 # Generate a "stub function" on-the-fly which will actually make 

298 # the request. 

299 # gRPC handles serialization and deserialization, so we just need 

300 # to pass in the functions for each. 

301 if "create_tag_binding" not in self._stubs: 

302 self._stubs["create_tag_binding"] = self.grpc_channel.unary_unary( 

303 "/google.cloud.resourcemanager.v3.TagBindings/CreateTagBinding", 

304 request_serializer=tag_bindings.CreateTagBindingRequest.serialize, 

305 response_deserializer=operations_pb2.Operation.FromString, 

306 ) 

307 return self._stubs["create_tag_binding"] 

308 

309 @property 

310 def delete_tag_binding( 

311 self, 

312 ) -> Callable[[tag_bindings.DeleteTagBindingRequest], operations_pb2.Operation]: 

313 r"""Return a callable for the delete tag binding method over gRPC. 

314 

315 Deletes a TagBinding. 

316 

317 Returns: 

318 Callable[[~.DeleteTagBindingRequest], 

319 ~.Operation]: 

320 A function that, when called, will call the underlying RPC 

321 on the server. 

322 """ 

323 # Generate a "stub function" on-the-fly which will actually make 

324 # the request. 

325 # gRPC handles serialization and deserialization, so we just need 

326 # to pass in the functions for each. 

327 if "delete_tag_binding" not in self._stubs: 

328 self._stubs["delete_tag_binding"] = self.grpc_channel.unary_unary( 

329 "/google.cloud.resourcemanager.v3.TagBindings/DeleteTagBinding", 

330 request_serializer=tag_bindings.DeleteTagBindingRequest.serialize, 

331 response_deserializer=operations_pb2.Operation.FromString, 

332 ) 

333 return self._stubs["delete_tag_binding"] 

334 

335 @property 

336 def list_effective_tags( 

337 self, 

338 ) -> Callable[ 

339 [tag_bindings.ListEffectiveTagsRequest], tag_bindings.ListEffectiveTagsResponse 

340 ]: 

341 r"""Return a callable for the list effective tags method over gRPC. 

342 

343 Return a list of effective tags for the given Google Cloud 

344 resource, as specified in ``parent``. 

345 

346 Returns: 

347 Callable[[~.ListEffectiveTagsRequest], 

348 ~.ListEffectiveTagsResponse]: 

349 A function that, when called, will call the underlying RPC 

350 on the server. 

351 """ 

352 # Generate a "stub function" on-the-fly which will actually make 

353 # the request. 

354 # gRPC handles serialization and deserialization, so we just need 

355 # to pass in the functions for each. 

356 if "list_effective_tags" not in self._stubs: 

357 self._stubs["list_effective_tags"] = self.grpc_channel.unary_unary( 

358 "/google.cloud.resourcemanager.v3.TagBindings/ListEffectiveTags", 

359 request_serializer=tag_bindings.ListEffectiveTagsRequest.serialize, 

360 response_deserializer=tag_bindings.ListEffectiveTagsResponse.deserialize, 

361 ) 

362 return self._stubs["list_effective_tags"] 

363 

364 def close(self): 

365 self.grpc_channel.close() 

366 

367 @property 

368 def get_operation( 

369 self, 

370 ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: 

371 r"""Return a callable for the get_operation method over gRPC.""" 

372 # Generate a "stub function" on-the-fly which will actually make 

373 # the request. 

374 # gRPC handles serialization and deserialization, so we just need 

375 # to pass in the functions for each. 

376 if "get_operation" not in self._stubs: 

377 self._stubs["get_operation"] = self.grpc_channel.unary_unary( 

378 "/google.longrunning.Operations/GetOperation", 

379 request_serializer=operations_pb2.GetOperationRequest.SerializeToString, 

380 response_deserializer=operations_pb2.Operation.FromString, 

381 ) 

382 return self._stubs["get_operation"] 

383 

384 @property 

385 def kind(self) -> str: 

386 return "grpc" 

387 

388 

389__all__ = ("TagBindingsGrpcTransport",)