gcp

package
v1.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package gcp provides GCP Marketplace integration for Zerfoo Cloud, including Cloud Commerce Partner Procurement API integration, SaaS entitlement management, Service Control API usage metering, and token-based billing.

Index

Constants

View Source
const DimensionTokens = "zerfoo/tokens_1m"

DimensionTokens is the metering metric name for token-based billing.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Name       string       `json:"name"`
	Provider   string       `json:"provider"`
	State      AccountState `json:"state"`
	CreateTime time.Time    `json:"createTime"`
	UpdateTime time.Time    `json:"updateTime"`
}

Account represents a GCP Marketplace procurement account.

type AccountState

type AccountState string

AccountState represents the state of a procurement account.

const (
	AccountActive              AccountState = "ACCOUNT_ACTIVE"
	AccountPendingVerification AccountState = "ACCOUNT_PENDING_VERIFICATION"
)

type EntitlementManager

type EntitlementManager struct {
	// contains filtered or unexported fields
}

EntitlementManager handles the full lifecycle of GCP Marketplace entitlements using the Partner Procurement API. It supports approve, reject, suspend, and reinstate operations with local state tracking.

func NewEntitlementManager

func NewEntitlementManager(procurement ProcurementAPI, store EntitlementStore, cacheTTL time.Duration) *EntitlementManager

NewEntitlementManager creates an EntitlementManager backed by the given procurement client, store, and cache TTL.

func (*EntitlementManager) Approve

func (m *EntitlementManager) Approve(ctx context.Context, entitlementName string) error

Approve approves a pending entitlement and stores it locally.

func (*EntitlementManager) IsActive

func (m *EntitlementManager) IsActive(ctx context.Context, entitlementName string) (bool, error)

IsActive checks whether an entitlement is currently active. Results are cached for the configured TTL.

func (*EntitlementManager) Reinstate

func (m *EntitlementManager) Reinstate(ctx context.Context, entitlementName string) error

Reinstate reinstates a suspended entitlement.

func (*EntitlementManager) Reject

func (m *EntitlementManager) Reject(ctx context.Context, entitlementName, reason string) error

Reject rejects a pending entitlement with the given reason.

func (*EntitlementManager) Suspend

func (m *EntitlementManager) Suspend(ctx context.Context, entitlementName, reason string) error

Suspend suspends an active entitlement with the given reason.

type EntitlementState

type EntitlementState string

EntitlementState represents the lifecycle state of an entitlement.

const (
	EntitlementPending             EntitlementState = "ENTITLEMENT_PENDING"
	EntitlementActive              EntitlementState = "ENTITLEMENT_ACTIVE"
	EntitlementSuspended           EntitlementState = "ENTITLEMENT_SUSPENDED"
	EntitlementCancellationPending EntitlementState = "ENTITLEMENT_CANCELLATION_PENDING"
	EntitlementCancelled           EntitlementState = "ENTITLEMENT_CANCELLED"
)

type EntitlementStore

type EntitlementStore interface {
	Put(ctx context.Context, ent LocalEntitlement) error
	Get(ctx context.Context, entitlementName string) (*LocalEntitlement, error)
	List(ctx context.Context, accountName string) ([]LocalEntitlement, error)
	Delete(ctx context.Context, entitlementName string) error
}

EntitlementStore persists local entitlement state.

type LocalEntitlement

type LocalEntitlement struct {
	Name       string           `json:"name"`
	Account    string           `json:"account"`
	Product    string           `json:"product"`
	Plan       string           `json:"plan"`
	State      EntitlementState `json:"state"`
	CreateTime time.Time        `json:"createTime"`
	UpdateTime time.Time        `json:"updateTime"`
}

LocalEntitlement tracks the local state of a GCP Marketplace entitlement.

type MemoryEntitlementStore

type MemoryEntitlementStore struct {
	// contains filtered or unexported fields
}

MemoryEntitlementStore is an in-memory EntitlementStore for testing.

func NewMemoryEntitlementStore

func NewMemoryEntitlementStore() *MemoryEntitlementStore

NewMemoryEntitlementStore creates a new in-memory entitlement store.

func (*MemoryEntitlementStore) Delete

func (s *MemoryEntitlementStore) Delete(_ context.Context, entitlementName string) error

Delete removes a local entitlement.

func (*MemoryEntitlementStore) Get

func (s *MemoryEntitlementStore) Get(_ context.Context, entitlementName string) (*LocalEntitlement, error)

Get retrieves a local entitlement by name.

func (*MemoryEntitlementStore) List

func (s *MemoryEntitlementStore) List(_ context.Context, accountName string) ([]LocalEntitlement, error)

List returns all entitlements for an account.

func (*MemoryEntitlementStore) Put

Put stores a local entitlement.

type MetricValue

type MetricValue struct {
	Int64Value *int64            `json:"int64Value,omitempty"`
	Labels     map[string]string `json:"labels,omitempty"`
}

MetricValue represents a single metric measurement.

type MetricValueSet

type MetricValueSet struct {
	MetricName   string        `json:"metricName"`
	MetricValues []MetricValue `json:"metricValues"`
}

MetricValueSet represents a set of metric values for a single metric.

type Operation

type Operation struct {
	OperationID   string            `json:"operationId"`
	OperationName string            `json:"operationName"`
	ConsumerID    string            `json:"consumerId"`
	StartTime     time.Time         `json:"startTime"`
	EndTime       time.Time         `json:"endTime"`
	MetricValues  []MetricValueSet  `json:"metricValueSets"`
	Labels        map[string]string `json:"labels,omitempty"`
}

Operation represents a single usage operation for the Service Control API.

type ProcurementAPI

type ProcurementAPI interface {
	// GetAccount retrieves a procurement account by name.
	GetAccount(ctx context.Context, name string) (*Account, error)

	// ListAccounts lists procurement accounts for the provider.
	ListAccounts(ctx context.Context, parent string) ([]Account, error)

	// GetEntitlement retrieves an entitlement by name.
	GetEntitlement(ctx context.Context, name string) (*ProcurementEntitlement, error)

	// ListEntitlements lists entitlements for the provider.
	ListEntitlements(ctx context.Context, parent string) ([]ProcurementEntitlement, error)

	// ApproveEntitlement approves a pending entitlement.
	ApproveEntitlement(ctx context.Context, name string) error

	// RejectEntitlement rejects a pending entitlement with the given reason.
	RejectEntitlement(ctx context.Context, name, reason string) error

	// SuspendEntitlement suspends an active entitlement with the given reason.
	SuspendEntitlement(ctx context.Context, name, reason string) error

	// ReinstateEntitlement reinstates a suspended entitlement.
	ReinstateEntitlement(ctx context.Context, name string) error
}

ProcurementAPI abstracts GCP Cloud Commerce Partner Procurement API calls.

type ProcurementClient

type ProcurementClient struct {
	// Endpoint is the Partner Procurement API base URL.
	Endpoint string
	// TokenSource provides access tokens for API authentication.
	TokenSource TokenSource
	// HTTPClient is the HTTP client used for API calls.
	HTTPClient *http.Client
}

ProcurementClient implements ProcurementAPI by calling the GCP Cloud Commerce Partner Procurement REST API.

func NewProcurementClient

func NewProcurementClient(tokenSource TokenSource) *ProcurementClient

NewProcurementClient creates a new ProcurementClient with the given token source.

func (*ProcurementClient) ApproveEntitlement

func (c *ProcurementClient) ApproveEntitlement(ctx context.Context, name string) error

ApproveEntitlement approves a pending entitlement.

func (*ProcurementClient) GetAccount

func (c *ProcurementClient) GetAccount(ctx context.Context, name string) (*Account, error)

GetAccount retrieves a procurement account by name.

func (*ProcurementClient) GetEntitlement

func (c *ProcurementClient) GetEntitlement(ctx context.Context, name string) (*ProcurementEntitlement, error)

GetEntitlement retrieves an entitlement by name.

func (*ProcurementClient) ListAccounts

func (c *ProcurementClient) ListAccounts(ctx context.Context, parent string) ([]Account, error)

ListAccounts lists procurement accounts for the provider.

func (*ProcurementClient) ListEntitlements

func (c *ProcurementClient) ListEntitlements(ctx context.Context, parent string) ([]ProcurementEntitlement, error)

ListEntitlements lists entitlements for the provider.

func (*ProcurementClient) ReinstateEntitlement

func (c *ProcurementClient) ReinstateEntitlement(ctx context.Context, name string) error

ReinstateEntitlement reinstates a suspended entitlement.

func (*ProcurementClient) RejectEntitlement

func (c *ProcurementClient) RejectEntitlement(ctx context.Context, name, reason string) error

RejectEntitlement rejects a pending entitlement with the given reason.

func (*ProcurementClient) SuspendEntitlement

func (c *ProcurementClient) SuspendEntitlement(ctx context.Context, name, reason string) error

SuspendEntitlement suspends an active entitlement with the given reason.

type ProcurementEntitlement

type ProcurementEntitlement struct {
	Name           string           `json:"name"`
	Account        string           `json:"account"`
	Provider       string           `json:"provider"`
	Product        string           `json:"product"`
	Plan           string           `json:"plan"`
	State          EntitlementState `json:"state"`
	NewPendingPlan string           `json:"newPendingPlan,omitempty"`
	CreateTime     time.Time        `json:"createTime"`
	UpdateTime     time.Time        `json:"updateTime"`
}

ProcurementEntitlement represents a GCP Marketplace entitlement from the Partner Procurement API.

type ReportError

type ReportError struct {
	OperationID string `json:"operationId"`
	Status      Status `json:"status"`
}

ReportError describes an error for a specific operation in a report request.

type ReportRequest

type ReportRequest struct {
	Operations []Operation `json:"operations"`
}

ReportRequest is the request body for the Service Control Report API.

type ReportResponse

type ReportResponse struct {
	ReportErrors []ReportError `json:"reportErrors,omitempty"`
}

ReportResponse is the response from the Service Control Report API.

type ServiceControlAPI

type ServiceControlAPI interface {
	// Report submits usage operations to the Service Control API.
	Report(ctx context.Context, serviceName string, ops []Operation) error
}

ServiceControlAPI abstracts GCP Service Control API calls for usage reporting.

type ServiceControlClient

type ServiceControlClient struct {
	// Endpoint is the Service Control API base URL.
	Endpoint string
	// TokenSource provides access tokens for API authentication.
	TokenSource TokenSource
	// HTTPClient is the HTTP client used for API calls.
	HTTPClient *http.Client
	// Retry configures exponential backoff for metering calls.
	// Zero value disables retry.
	Retry marketplace.RetryConfig
}

ServiceControlClient implements ServiceControlAPI by calling the GCP Service Control v1 REST API.

func NewServiceControlClient

func NewServiceControlClient(tokenSource TokenSource) *ServiceControlClient

NewServiceControlClient creates a new ServiceControlClient with the given token source.

func (*ServiceControlClient) Report

func (c *ServiceControlClient) Report(ctx context.Context, serviceName string, ops []Operation) error

Report submits usage operations to the Service Control API.

type Status

type Status struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

Status represents a gRPC-style status in JSON responses.

type TokenBillingTracker

type TokenBillingTracker struct {
	// contains filtered or unexported fields
}

TokenBillingTracker accumulates token usage per consumer and flushes metering records to GCP via the Service Control API.

func NewTokenBillingTracker

func NewTokenBillingTracker(serviceName string, metering ServiceControlAPI) *TokenBillingTracker

NewTokenBillingTracker creates a new TokenBillingTracker for the given service.

func (*TokenBillingTracker) Flush

func (t *TokenBillingTracker) Flush(ctx context.Context) (int, error)

Flush sends accumulated usage to GCP via the Service Control API and resets the accumulators. Usage is billed per 1M tokens.

func (*TokenBillingTracker) RecordUsage

func (t *TokenBillingTracker) RecordUsage(consumerID string, inputTokens, outputTokens int64)

RecordUsage adds token usage for a consumer.

func (*TokenBillingTracker) Snapshot

func (t *TokenBillingTracker) Snapshot() []TokenUsageRecord

Snapshot returns the current accumulated usage per consumer without resetting the accumulators.

type TokenSource

type TokenSource interface {
	Token(ctx context.Context) (string, error)
}

TokenSource provides OAuth2 access tokens for authenticating GCP API requests.

type TokenUsageRecord

type TokenUsageRecord struct {
	ConsumerID   string    `json:"consumerId"`
	InputTokens  int64     `json:"inputTokens"`
	OutputTokens int64     `json:"outputTokens"`
	TotalTokens  int64     `json:"totalTokens"`
	Timestamp    time.Time `json:"timestamp"`
}

TokenUsageRecord tracks token usage for a consumer within a billing period.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL