Documentation
¶
Overview ¶
Package gcprest provides shared HTTP wire-format helpers for GCP Compute (and other GCP REST APIs) JSON handlers.
GCP REST URLs share a common shape:
/compute/v1/projects/{project}/zones/{zone}/{type}
/compute/v1/projects/{project}/zones/{zone}/{type}/{name}
/compute/v1/projects/{project}/zones/{zone}/{type}/{name}/{action}
/compute/v1/projects/{project}/global/{type}
/compute/v1/projects/{project}/regions/{region}/{type}
Mutating operations return Operation envelopes that real GCP SDKs poll on `selfLink` until status=DONE. Our mock returns DONE immediately.
Index ¶
- Constants
- func DecodeJSON(w http.ResponseWriter, r *http.Request, v any) bool
- func SelfLink(host, project, scope, scopeName, resourceType, name string) string
- func WriteCErr(w http.ResponseWriter, err error)
- func WriteError(w http.ResponseWriter, status int, reason, msg string)
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type Operation
- type ResourcePath
Constants ¶
const ( ScopeZones = "zones" ScopeRegions = "regions" ScopeGlobal = "global" )
Scope values used in GCP REST URL paths.
const BasePrefix = "/compute/v1/"
BasePrefix is the URL prefix that identifies a GCP Compute API request.
const ContentType = "application/json"
ContentType is the JSON content type used by all REST responses.
const MaxBodyBytes = 1 << 20
MaxBodyBytes caps incoming request bodies. GCP Compute Insert bodies for an instance are typically a few KB; 1 MiB is plenty of headroom.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSON ¶
DecodeJSON reads a JSON request body into v. Returns false (and writes a 400) on decode error.
func SelfLink ¶
SelfLink returns the canonical self-link for a GCP resource at the given scope. SDKs use this for polling and resource navigation.
func WriteCErr ¶
func WriteCErr(w http.ResponseWriter, err error)
WriteCErr maps a CloudEmu canonical error to the matching GCP HTTP status and reason.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, reason, msg string)
WriteError writes a GCP-style JSON error response.
Types ¶
type Operation ¶
type Operation struct {
Kind string `json:"kind"`
ID string `json:"id"`
Name string `json:"name"`
OperationType string `json:"operationType"`
TargetID string `json:"targetId,omitempty"`
TargetLink string `json:"targetLink,omitempty"`
Status string `json:"status"`
Progress int `json:"progress"`
InsertTime string `json:"insertTime"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
SelfLink string `json:"selfLink"`
Zone string `json:"zone,omitempty"`
}
Operation models the subset of GCP's compute#operation we need. Real ops are async; our mock returns DONE immediately so SDK clients that poll see completion on the first GET.
func NewDoneOperation ¶
NewDoneOperation builds an Operation in DONE state for opType targeting the resource at scope/scopeName/resourceType/name. host is the test server URL so selfLink/targetLink are absolute and SDKs can navigate them.
Operation.ID must be a numeric string (uint64) — GCP's protobuf JSON unmarshaling rejects anything else. The human-readable identifier goes in Name instead.
type ResourcePath ¶
type ResourcePath struct {
Project string
Scope string // "zones", "regions", "global"
ScopeName string // zone/region name; empty when Scope=="global"
ResourceType string // e.g. "instances", "operations"
ResourceName string // empty for collection paths
Action string // e.g. "start", "stop", "reset" — empty for resource ops
}
ResourcePath is a parsed GCP REST URL path.
func ParsePath ¶
func ParsePath(urlPath string) (ResourcePath, bool)
ParsePath extracts GCP REST path components from urlPath. Returns ok=false when the path doesn't match the /compute/v1/projects/... shape.