origincloud_core/lib.rs
1//! What every service crate of the Origin Cloud SDK shares: credentials,
2//! endpoints, retries, errors, long-running operations and
3//! read-your-writes.
4//!
5//! A program creates one [`Client`] and hands it to the clients of the
6//! services it uses:
7//!
8//! ```no_run
9//! # fn main() -> Result<(), origincloud_core::Error> {
10//! let client = origincloud_core::Client::new()?;
11//! // let volumes = origincloud_pvs::v1beta1::VolumeClient::new(&client);
12//! # Ok(())
13//! # }
14//! ```
15//!
16//! # Credentials
17//!
18//! Unless [`ClientBuilder::credentials`] names them, [`Client::new`] takes
19//! the first of (natively; in a browser there are none of these, see
20//! [`credentials`]):
21//!
22//! 1. `ORIGINCLOUD_ACCESS_TOKEN`: an access token, sent as it is.
23//! 2. `ORIGINCLOUD_CREDENTIALS_FILE`: the path of a service account key's
24//! credentials file, whose signed assertions are exchanged for access
25//! tokens as they expire
26#![cfg_attr(not(target_arch = "wasm32"), doc = " ([`ServiceAccount`]).")]
27#![cfg_attr(target_arch = "wasm32", doc = " (`ServiceAccount`).")]
28//! 3. A CI job's OIDC token, exchanged at the security token service for a
29//! `WORKLOAD` service account's access tokens
30#![cfg_attr(not(target_arch = "wasm32"), doc = " ([`Federated`]):")]
31#![cfg_attr(target_arch = "wasm32", doc = " (`Federated`):")]
32//! from the
33//! file `ORIGINCLOUD_OIDC_TOKEN_FILE` names, from `ORIGINCLOUD_OIDC_TOKEN`,
34//! which holds the token itself, from an HCP Terraform run's
35//! `TFC_WORKLOAD_IDENTITY_TOKEN` (`TFC_WORKLOAD_IDENTITY_TOKEN_<TAG>` when
36//! `ORIGINCLOUD_TFC_WORKLOAD_IDENTITY_TAG` names a tag), or in a GitHub
37//! Actions job granted
38//! `permissions: id-token: write`. `ORIGINCLOUD_OIDC_DISABLED=true`
39//! leaves this step out.
40//! 4. The session the `origin` CLI stored at its last login
41//! (`origin id login`).
42//! 5. On an instance that runs as a service account, its credential from
43//! the instance's metadata service. `ORIGINCLOUD_METADATA_DISABLED=true`
44//! leaves this step out.
45//!
46#![cfg_attr(
47 not(target_arch = "wasm32"),
48 doc = "[`ClientBuilder::impersonate_service_account`], or
49`ORIGINCLOUD_IMPERSONATE_SERVICE_ACCOUNT`, names a service account to act
50as instead: the token of the credentials found is exchanged at the
51security token service for the account's, which every call is sent with
52([`Impersonated`]).
53"
54)]
55//!
56//! # Endpoints
57//!
58//! Every service answers at `{service}.origincloudapis.eu`, which serves
59//! every resource wherever it lives. [`ClientBuilder::region`], or
60//! `ORIGINCLOUD_REGION`, picks a region's front door instead,
61//! `{service}.{region}.origincloudapis.eu`, for the services that have
62//! regional ones.
63//!
64//! # Retries
65//!
66//! A call the API answers as retryable is sent again, after the delay the
67//! answer asks for or a growing one, as long as sending it again is safe: a
68//! read, or a mutation with a request id. The client fills in a mutation's
69//! request id when it is empty, so that a retry can't apply it twice.
70//! [`ClientBuilder::max_attempts`] bounds the attempts of every call;
71//! [`CallOptions`] those of the calls through one service client, such as
72//! a mutation that must not be sent twice at all.
73//!
74//! # Read-your-writes
75//!
76//! The API answers reads from replicas that can be a moment behind a write
77//! that just succeeded. The client keeps the consistency token each
78//! mutation and each finished operation answers with, and sends it with
79//! every later call, so that a read through the same client shows the
80//! client's own writes. The token answered last replaces the one held: for
81//! writes made one after another, it covers them all; of writes made
82//! concurrently, it covers the one answered last (see
83//! [`Client::set_consistency_token`]). The token is kept per client, and
84//! shared by its clones; clients given one store share it
85//! ([`ClientBuilder::consistency_token_store`]). A call the API refuses
86//! because its token isn't one is sent again once without it, and the
87//! token is dropped.
88//!
89//! # Names
90//!
91//! A resource's `name` is its relative name; its full resource name,
92//! `//{service}.origincloudapis.eu/{name}`, is what references carry
93//! ([`FullName`]). A request's path fields take either. A full name is
94//! checked against the type the field refers to (the client's own service
95//! for its own resources, resourcemanager for a project or an organization
96//! as a `parent`) and sent as its relative name; one on any other host is
97//! refused before anything is sent.
98//!
99//! # Errors
100//!
101//! A call the API refuses returns an [`Error::Api`]: branch on
102//! [`Error::reason`], the platform's error code. [`ApiError::metadata`]
103//! says more about some reasons, such as the name of the resource a
104//! `CONFLICT` is about.
105//!
106//! # Runtime
107//!
108//! The clients are `async` and send their calls with
109//! [reqwest](https://docs.rs/reqwest), on [Tokio](https://tokio.rs).
110//!
111//! They also build for the browser, `wasm32-unknown-unknown`, where
112//! reqwest sends calls with the browser's `fetch`, retries wait on
113//! `setTimeout`, and no Tokio runtime is needed: run the futures with the
114//! UI framework's executor, such as `wasm-bindgen-futures`. There, the
115//! futures the SDK returns aren't `Send` ([`MaybeSend`]), the credentials
116//! are the page's own (see [`credentials`]), and the per-call
117//! [`ClientBuilder::timeout`] is the only one, as a browser's `fetch` has
118//! no connect timeout.
119
120#![warn(missing_docs)]
121
122mod client;
123pub mod consistency;
124pub mod credentials;
125mod error;
126pub mod longrunning;
127pub mod names;
128mod operation;
129mod pages;
130pub mod protobuf;
131pub mod rpc;
132mod runtime;
133mod transport;
134pub mod types;
135
136pub use client::{CallOptions, Client, ClientBuilder, Service, VERSION};
137pub use consistency::{ConsistencyTokenCell, ConsistencyTokenStore};
138#[cfg(any(not(target_arch = "wasm32"), feature = "service-account-wasm"))]
139pub use credentials::ServiceAccount;
140pub use credentials::{AccessToken, Credentials};
141#[cfg(not(target_arch = "wasm32"))]
142pub use credentials::{CliSession, Federated, Impersonated, InstanceMetadata, default_credentials};
143pub use error::{ApiError, Error, FieldViolation};
144pub use names::{FullName, FullNameError};
145pub use operation::Operation;
146pub use pages::Pages;
147pub use runtime::{MaybeSend, MaybeSendFuture};
148pub use transport::{
149 CONSISTENCY_TOKEN_HEADER, HttpBody, Method, Query, Request, json_body, new_request_id,
150 path_value, query,
151};