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