GcpCompute.Config (GcpCompute v0.2.0)

Copy Markdown View Source

Validated configuration — and the client handle you pass to every API call.

A %Config{} is cheap, immutable, and stateless: build it once and hand it to GcpCompute.Instances/GcpCompute.Operations (or the GcpCompute facade). There is no process to supervise — Req manages connection pooling under the hood.

Building a config

# Production: tokens minted by Goth
{:ok, config} =
  GcpCompute.Config.production(
    project: "my-project",
    zone: "us-central1-a",
    goth: MyApp.Goth
  )

# From application env
{:ok, config} = GcpCompute.Config.from_env(:my_app, :gcp_compute)

# Local / emulator / tests (static token)
{:ok, config} = GcpCompute.Config.local(project: "my-project")

Options

  • :project (String.t/0) - Required. GCP project id.

  • :zone (String.t/0) - Default zone for zonal operations. The default value is "us-central1-a".

  • :token_provider - A {module, arg} implementing GcpCompute.TokenProvider. The default value is {GcpCompute.TokenProvider.Goth, GcpCompute.Goth}.

  • :base_url (String.t/0) - Compute API base URL. Override for the emulator or a proxy. The default value is "https://compute.googleapis.com/compute/v1".

  • :req_options (keyword/0) - Extra options merged into every Req request (e.g. :adapter for tests, :retry). Treated as trusted application config: the request :method, :url, and :auth are always computed by the library and layered on top, so req_options cannot override the target URL or drop the bearer token. It can still set transport and connection options (:connect_options, including verify) — that is the app owner's choice, not attacker-controlled input. The default value is [].

  • :allow_insecure (boolean/0) - Permit a non-https:// :base_url. Off by default: a bearer token is never sent in cleartext unless you explicitly opt in. Set true only for a local emulator/proxy on a trusted network (local/1 sets it for you). Independent of the token provider — even a Static token stays TLS-only unless this is set. The default value is false.

Summary

Functions

Fetch a fresh access token using the configured GcpCompute.TokenProvider.

Load options from application env and build a config.

Build a config for local development, the emulator, or tests.

Same as local/1 but raises on invalid options.

Build and validate a config from a keyword list.

Same as new/1 but raises ArgumentError on invalid options.

Build a production config that mints tokens via Goth.

Types

t()

@type t() :: %GcpCompute.Config{
  allow_insecure: boolean(),
  base_url: String.t(),
  project: String.t(),
  req_options: keyword(),
  token_provider: {module(), term()},
  zone: String.t()
}

Functions

fetch_token(config)

@spec fetch_token(t()) :: {:ok, GcpCompute.TokenProvider.token()} | {:error, term()}

Fetch a fresh access token using the configured GcpCompute.TokenProvider.

from_env(app, key)

@spec from_env(atom(), atom()) :: {:ok, t()} | {:error, String.t()}

Load options from application env and build a config.

config :my_app, :gcp_compute,
  project: "my-project",
  zone: "europe-west4-a",
  token_provider: {GcpCompute.TokenProvider.Goth, MyApp.Goth}

{:ok, config} = GcpCompute.Config.from_env(:my_app, :gcp_compute)

local(opts)

@spec local(keyword()) :: {:ok, t()} | {:error, String.t()}

Build a config for local development, the emulator, or tests.

Defaults to a Static token provider so you never touch Goth. Pass :token to set the static token (default "local-token"), and :base_url to point at an emulator. Defaults allow_insecure: true so an http:// emulator URL works out of the box — override it to false to force TLS even locally.

{:ok, config} = GcpCompute.Config.local(project: "p", base_url: "http://localhost:8080/compute/v1")

local!(opts)

@spec local!(keyword()) :: t()

Same as local/1 but raises on invalid options.

new(opts)

@spec new(keyword()) :: {:ok, t()} | {:error, String.t()}

Build and validate a config from a keyword list.

Returns {:ok, config} or {:error, message} with a precise validation message.

new!(opts)

@spec new!(keyword()) :: t()

Same as new/1 but raises ArgumentError on invalid options.

production(opts)

@spec production(keyword()) :: {:ok, t()} | {:error, String.t()}

Build a production config that mints tokens via Goth.

Pass :goth (the registered Goth name) instead of :token_provider.

{:ok, config} = GcpCompute.Config.production(project: "p", goth: MyApp.Goth)

Uses a Goth token provider with allow_insecure defaulting to false, so new/1's https rule applies: the :base_url must be https:// and a bearer token can't be sent in cleartext.