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