goops

package module
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 License: Apache-2.0 Imports: 9 Imported by: 6

README

goops

Develop reliable, simple, and fast Juju Charms in Go

goops is a Go library for developing simple and robust Juju charms. Write, test, build, and deploy your charm in minutes with goops.

Get Started Now!


package charm

import (
	"fmt"

	"github.com/gruyaume/goops"
)

func Configure() error {
	isLeader, err := goops.IsLeader()
	if err != nil {
		return fmt.Errorf("could not check if unit is leader: %w", err)
	}

	if !isLeader {
		goops.SetUnitStatus(goops.StatusBlocked, "Unit is not leader")
		return nil
	}

	err = goops.SetPorts([]*goops.Port{
		{
			Port:     2111,
			Protocol: "tcp",
		},
	})
	if err != nil {
		return fmt.Errorf("could not set ports: %w", err)
	}

	goops.SetUnitStatus(goops.StatusActive, fmt.Sprintf("Port %d/tcp is set", 2111))

	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActionLogf added in v0.0.19

func ActionLogf(format string, args ...any) error

ActionLogf records a progress message for the current action. This functionality only works when the charm is running in an action hook.

func AddSecret added in v0.0.13

func AddSecret(opts *AddSecretOptions) (string, error)

AddSecret adds a new secret with the provided options.

func AddStorage added in v0.0.13

func AddStorage(name string, count int) error

AddStorage adds a storage instance to the unit.

func ClosePort added in v0.0.13

func ClosePort(port int, protocol Protocol) error

ClosePort registers a request to close the specified port. The port must be between 0 and 65535, and the protocol must be one of tcp, udp, or icmp. If the protocol is icmp, the port argument is ignored.

func DeleteState added in v0.0.13

func DeleteState(key string) error

DeleteState deletes a state key.

func FailActionf added in v0.0.13

func FailActionf(format string, args ...any) error

FailActionf fails the current action with a formatted message. This functionality only works when the charm is running in an action hook.

func GetActionParams added in v0.0.19

func GetActionParams(params any) error

GetActionParams retrieves the parameters for the current action and unmarshals them into the provided params struct. This functionality only works when the charm is running in an action hook.

func GetAppRelationData added in v0.0.13

func GetAppRelationData(id string, unitID string) (map[string]string, error)

GetUnitRelationData retrieves the relation data for a specific app in a relation by its ID. unitID can either be: - The remote unit ID which can be retrieved via goops.ListRelationUnits() - The local unit ID which you can retrieve via goops.ReadEnv()

func GetConfig added in v0.0.13

func GetConfig(config any) error

GetConfig retrieves the Juju configuration options and unmarshals them into the provided config struct.

func GetCredential added in v0.0.13

func GetCredential() (map[string]string, error)

GetCredential retrieves cloud credentials.

func GetRelationApp added in v0.0.14

func GetRelationApp(id string) (string, error)

GetRelationApp retrieves the remote application name for a relation by its ID.

func GetRelationIDs added in v0.0.13

func GetRelationIDs(name string) ([]string, error)

GetRelationIDs retrieves the IDs of all relations for a given endpoint. The output is useful as input to: - ListRelationUnits - GetAppRelationData - GetUnitRelationData - SetUnitRelationData - SetAppRelationData - GetRelationModel

func GetRelationModelUUID added in v0.0.22

func GetRelationModelUUID(id string) (string, error)

GetRelationModel retrieves the relation model UUID for a relation by its ID.

func GetResource added in v0.0.13

func GetResource(name string) (string, error)

GetResource retrieves the local path to a resource file for the given resource name.

func GetSecretByID added in v0.0.13

func GetSecretByID(id string, peek bool, refresh bool) (map[string]string, error)

GetSecretByID retrieves the secret content by its ID.

func GetSecretByLabel added in v0.0.13

func GetSecretByLabel(label string, peek bool, refresh bool) (map[string]string, error)

GetSecretByLabel retrieves the secret content by its label.

func GetSecretIDs added in v0.0.13

func GetSecretIDs() ([]string, error)

GetSecretIDs retrieves the IDs for secrets owned by the application.

func GetSecretInfoByID added in v0.0.13

func GetSecretInfoByID(id string) (map[string]SecretInfo, error)

GetSecretInfoByID retrieves a secret metadata info by its ID.

func GetSecretInfoByLabel added in v0.0.13

func GetSecretInfoByLabel(label string) (map[string]SecretInfo, error)

GetSecretInfoByLabel retrieves a secret metadata info by its label.

func GetState added in v0.0.13

func GetState(key string) (string, error)

GetState retrieves the value of a state key.

func GetUnitPrivateAddress added in v0.0.13

func GetUnitPrivateAddress() (string, error)

GetUnitPrivateAddress returns the private IP address of the unit.

func GetUnitPublicAddress added in v0.0.13

func GetUnitPublicAddress() (string, error)

GetUnitName returns the public IP address of the unit.

func GetUnitRelationData added in v0.0.13

func GetUnitRelationData(id string, unitID string) (map[string]string, error)

GetUnitRelationData retrieves the relation data for a specific unit in a relation by its ID. unitID can either be: - The remote unit ID which can be retrieved via goops.ListRelationUnits() - The local unit ID which you can retrieve via goops.ReadEnv()

func GrantSecretToRelation added in v0.0.13

func GrantSecretToRelation(id string, relation string) error

GrantSecretToRelation grants a secret to a specific relation. All units of the related application are granted access

func GrantSecretToUnit added in v0.0.13

func GrantSecretToUnit(id string, relation string, unit string) error

GrantSecretToUnit grants a secret to a specific unit in a relation.

func IsLeader added in v0.0.13

func IsLeader() (bool, error)

IsLeader retrieves the unit's leadership status.

func ListRelationUnits added in v0.0.14

func ListRelationUnits(id string) ([]string, error)

ListRelationUnits lists all remote units in a relation by its ID.

func ListStorage added in v0.0.13

func ListStorage(name string) ([]string, error)

ListStorage lists all storage IDs for a given storage name.

func LogDebugf added in v0.0.13

func LogDebugf(format string, args ...any)

LogDebugf logs a debug message. Log messages can be read using `juju debug-log`.

func LogErrorf added in v0.0.13

func LogErrorf(format string, args ...any)

LogErrorf logs an error message. Log messages can be read using `juju debug-log`.

func LogInfof added in v0.0.13

func LogInfof(format string, args ...any)

LogInfof logs an informational message. Log messages can be read using `juju debug-log`.

func LogWarningf added in v0.0.13

func LogWarningf(format string, args ...any)

LogWarningf logs a warning message. Log messages can be read using `juju debug-log`.

func OpenPort added in v0.0.13

func OpenPort(port int, protocol Protocol) error

OpenPort registers a request to open the specified port. The port must be between 0 and 65535, and the protocol must be one of tcp, udp, or icmp. If the protocol is icmp, the port argument is ignored.

func Reboot added in v0.0.13

func Reboot(now bool) error

Reboot causes the host machine to reboot, after stopping all containers hosted on the machine.

func RemoveSecret added in v0.0.13

func RemoveSecret(id string) error

RemoveSecret removes a secret by its ID.

func RevokeSecret added in v0.0.13

func RevokeSecret(id string) error

RevokeSecret revokes a secret by its ID.

func RevokeSecretFromApp added in v0.0.13

func RevokeSecretFromApp(id string, app string) error

RevokeSecretFromApp revokes a secret from a specific application.

func RevokeSecretFromRelation added in v0.0.13

func RevokeSecretFromRelation(id string, relation string) error

RevokeSecretFromRelation revokes a secret from a specific relation.

func RevokeSecretFromUnit added in v0.0.13

func RevokeSecretFromUnit(id string, unit string) error

RevokeSecretFromApp revokes a secret from a specific application.

func SetActionResults added in v0.0.13

func SetActionResults(results map[string]string) error

SetActionResults sets action results. This functionality only works when the charm is running in an action hook.

func SetAppRelationData added in v0.0.13

func SetAppRelationData(id string, data map[string]string) error

SetAppRelationData sets the local application relation data in a relation by its ID.

func SetAppStatus added in v0.0.14

func SetAppStatus(status StatusName, message ...string) error

SetAppStatus sets the application status. Only the leader unit can set the application status.

func SetAppVersion added in v0.0.19

func SetAppVersion(version string) error

SetAppVersion sets the application version. The version set will be displayed in “juju status” output for the application.

func SetCommandRunner added in v0.0.19

func SetCommandRunner(runner CommandRunner)

func SetEnvGetter added in v0.0.19

func SetEnvGetter(envGetter EnvironmentGetter)

func SetPebbleGetter added in v0.0.20

func SetPebbleGetter(getter PebbleGetter)

func SetPorts added in v0.0.13

func SetPorts(ports []*Port) error

SetPorts sets the desired ports for the unit. It opens ports that are desired but not currently opened, and closes ports that are currently opened but not desired.

func SetSecret added in v0.0.19

func SetSecret(opts *SetSecretOptions) error

SetSecret updates an existing secret with new content and options.

func SetState added in v0.0.13

func SetState(key string, value string) error

SetState sets a state key to a value.

func SetUnitRelationData added in v0.0.13

func SetUnitRelationData(id string, data map[string]string) error

SetUnitRelationData sets the local unit relation data in a relation by its ID.

func SetUnitStatus added in v0.0.13

func SetUnitStatus(status StatusName, message ...string) error

SetUnitStatus sets the unit status.

Types

type AddSecretOptions added in v0.0.13

type AddSecretOptions struct {
	Content     map[string]string
	Description string
	Expire      time.Time
	Label       string
	Owner       SecretOwner
	Rotate      SecretRotate
}

type Address added in v0.0.13

type Address struct {
	Value string `json:"value"`
	CIDR  string `json:"cidr"`
}

type AppStatus added in v0.0.22

type AppStatus struct {
	Name    StatusName            `json:"status"`
	Message string                `json:"message"`
	Units   map[string]UnitStatus `json:"units"`
}

func GetAppStatus added in v0.0.19

func GetAppStatus() (*AppStatus, error)

GetAppStatus returns the application status information. Only the leader unit can retrieve the application status.

type BindAddress added in v0.0.13

type BindAddress struct {
	InterfaceName string    `json:"interface-name"`
	Addresses     []Address `json:"addresses"`
}

type CommandRunner added in v0.0.13

type CommandRunner interface {
	Run(name string, args ...string) ([]byte, error)
}

CommandRunner is an interface for running commands. It allows for mocking in tests.

func GetCommandRunner added in v0.0.19

func GetCommandRunner() CommandRunner

type Container added in v0.0.13

type Container struct {
	Mounts   []Mount `yaml:"mounts"`
	Resource string  `yaml:"resource"`
}

type Environment added in v0.0.13

type Environment struct {
	ActionName         string
	AgentSocketAddress string
	AgentSocketNetwork string
	APIAddresses       string
	AvailabilityZone   string
	CharmDir           string
	CharmFTPProxy      string
	CharmHTTPProxy     string
	CharmHTTPSProxy    string
	CharmNoProxy       string
	CloudAPIVersion    string
	ContextID          string
	HookName           string
	MachineID          string
	ModelName          string
	ModelUUID          string
	Path               string
	PrincipalUnit      string
	UnitName           string
	Version            string
}

func ReadEnv added in v0.0.13

func ReadEnv() Environment

ReadEnv reads the Juju related environment variables and returns an Environment struct

type EnvironmentGetter added in v0.0.13

type EnvironmentGetter interface {
	Get(name string) string
	ReadFile(name string) ([]byte, error)
}

func GetEnvGetter added in v0.0.19

func GetEnvGetter() EnvironmentGetter

type GoalState added in v0.0.13

type GoalState struct {
	Units     UnitsGoalStateContents            `json:"units"`
	Relations map[string]UnitsGoalStateContents `json:"relations"`
}

func GetGoalState added in v0.0.13

func GetGoalState() (*GoalState, error)

GetGoalState retrieves the status of the charm's peers and related units.

type GoalStateStatusContents added in v0.0.22

type GoalStateStatusContents struct {
	Status StatusName `json:"status"`
	Since  string     `json:"since,omitempty"`
}

type Integration added in v0.0.13

type Integration struct {
	Interface string `yaml:"interface"`
}

type JujuEnvironment added in v0.0.13

type JujuEnvironment struct {
	Getter EnvironmentGetter
}

type Level added in v0.0.13

type Level int
const (
	Debug Level = iota
	Info
	Warning
	Error
)

type Metadata added in v0.0.13

type Metadata struct {
	Containers  map[string]Container   `yaml:"containers"`
	Description string                 `yaml:"description"`
	Name        string                 `yaml:"name"`
	Provides    map[string]Integration `yaml:"provides"`
	Resources   map[string]Resource    `yaml:"resources"`
	Storage     map[string]Storage     `yaml:"storage"`
	Summary     string                 `yaml:"summary"`
}

func ReadMetadata added in v0.0.13

func ReadMetadata() (*Metadata, error)

ReadMetadata reads the metadata.yaml file from the charm directory and unmarshals it into a Metadata struct.

type Mount added in v0.0.13

type Mount struct {
	Location string `yaml:"location"`
	Storage  string `yaml:"storage"`
}

type Network added in v0.0.13

type Network struct {
	BindAddresses    []BindAddress `json:"bind-addresses"`
	IngressAddresses []string      `json:"ingress-addresses"`
	EgressSubnets    []string      `json:"egress-subnets"`
}

func GetNetwork added in v0.0.13

func GetNetwork(bindingName string) (*Network, error)

GetNetwork retrieves the network configuration for a given binding name.

type PebbleClient added in v0.0.20

type PebbleClient interface {
	Stop(opts *client.ServiceOptions) (changeID string, err error)
	WaitChange(changeID string, options *client.WaitChangeOptions) (*client.Change, error)
	Exec(opts *client.ExecOptions) (PebbleExecProcess, error)
	SysInfo() (*client.SysInfo, error)
	Push(opts *client.PushOptions) error
	Pull(opts *client.PullOptions) error
	AddLayer(opts *client.AddLayerOptions) error
	Restart(opts *client.ServiceOptions) (changeID string, err error)
	Start(opts *client.ServiceOptions) (changeID string, err error)
	Replan(opts *client.ServiceOptions) (changeID string, err error)
	Services(opts *client.ServicesOptions) ([]*client.ServiceInfo, error)
	PlanBytes(_ *client.PlanOptions) (data []byte, err error)
}

func Pebble added in v0.0.20

func Pebble(container string) PebbleClient

Pebble returns a PebbleClient for the specified container.

type PebbleExecProcess added in v0.0.20

type PebbleExecProcess interface {
	Wait() error
	SendResize(width int, height int) error
	SendSignal(signal string) error
}

type PebbleGetter added in v0.0.20

type PebbleGetter interface {
	Pebble(container string) PebbleClient
}

type Port added in v0.0.13

type Port struct {
	Port     int
	Protocol Protocol
}

func OpenedPorts added in v0.0.13

func OpenedPorts() ([]*Port, error)

List all ports opened by the unit.

type Protocol added in v0.0.22

type Protocol string
const (
	ProtocolTCP  Protocol = "tcp"
	ProtocolUDP  Protocol = "udp"
	ProtocolICMP Protocol = "icmp"
)

type RelationModel added in v0.0.13

type RelationModel struct {
	UUID string `json:"uuid"`
}

type Resource added in v0.0.13

type Resource struct {
	Description    string `yaml:"description"`
	Type           string `yaml:"type"`
	UpstreamSource string `yaml:"upstream-source"`
}

type SecretInfo added in v0.0.13

type SecretInfo struct {
	Revision int    `json:"revision"`
	Label    string `json:"label"`
	Owner    string `json:"owner"`
	Rotation string `json:"rotation"`
}

type SecretOwner added in v0.0.22

type SecretOwner string
const (
	OwnerApplication SecretOwner = "application"
	OwnerUnit        SecretOwner = "unit"
)

type SecretRotate added in v0.0.19

type SecretRotate string
const (
	RotateHourly  SecretRotate = "hourly"
	RotateDaily   SecretRotate = "daily"
	RotateMonthly SecretRotate = "monthly"
	RotateNever   SecretRotate = "never"
)

type SetSecretOptions added in v0.0.13

type SetSecretOptions struct {
	ID          string
	Content     map[string]string
	Description string
	Expire      time.Time
	Label       string
	Owner       SecretOwner
	Rotate      SecretRotate
}

type StatusName added in v0.0.22

type StatusName string
const (
	StatusActive      StatusName = "active"
	StatusBlocked     StatusName = "blocked"
	StatusWaiting     StatusName = "waiting"
	StatusMaintenance StatusName = "maintenance"
	StatusUnknown     StatusName = "unknown"
)

type Storage added in v0.0.13

type Storage struct {
	MinimumSize string `yaml:"minimum-size"`
	Type        string `yaml:"type"`
}

type StorageInfo added in v0.0.22

type StorageInfo struct {
	Kind     string `json:"kind"`
	Location string `json:"location"`
}

func GetStorageByID added in v0.0.13

func GetStorageByID(id string) (*StorageInfo, error)

GetStorageByID retrieves storage information by its ID.

type UnitStatus added in v0.0.13

type UnitStatus struct {
	Name    StatusName `json:"status"`
	Message string     `json:"message"`
}

func GetUnitStatus added in v0.0.19

func GetUnitStatus() (*UnitStatus, error)

GetUnitStatus returns the unit status information.

type UnitsGoalStateContents added in v0.0.22

type UnitsGoalStateContents map[string]GoalStateStatusContents

Directories

Path Synopsis
cmd
example command
integration
internal

Jump to

Keyboard shortcuts

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