static

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2022 License: MPL-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package static provides a host, a host catalog, and a host set suitable for hosts with a static address.

A host catalog contains a collection of hosts with static addresses. These hosts can be grouped into host sets allowing them to be used in a target. Hosts and host sets are owned by a single host catalog. If a host catalog is deleted, all hosts and host sets owned by it are also deleted. A host set contains references to zero or more hosts but does not own them. Deleting a host set does not effect any of the hosts it referenced. A host set can only reference hosts from the host catalog that owns it. Host addresses must be unique within a host catalog.

Repository

A repository provides methods for creating, updating, retrieving, and deleting host catalogs, host sets, and hosts. A new repository should be created for each transaction. For example:

var wrapper wrapping.Wrapper
... init wrapper...

// db implements both the reader and writer interfaces.
db, _ := db.Open(db.Postgres, url)

var repo *static.Repository

repo, _ = static.NewRepository(db, db, wrapper)
catalog, _ := repo.LookupCatalog(ctx, catalogId)

catalog.Name = "new name"

repo, _ = static.NewRepository(db, db, wrapper)
catalog, _ := repo.UpdateCatalog(ctx, catalog, []string{"Name"})

Index

Examples

Constants

View Source
const (
	MinHostAddressLength = 3
	MaxHostAddressLength = 255
)
View Source
const (
	HostCatalogPrefix = "hcst"
	HostSetPrefix     = "hsst"
	HostPrefix        = "hst"

	Subtype = subtypes.Subtype("static")
)

PublicId prefixes for the resources in the static package.

Variables

This section is empty.

Functions

This section is empty.

Types

type Host

type Host struct {
	*store.Host
	SetIds []string `gorm:"-"`
	// contains filtered or unexported fields
}

A Host contains a static address.

func NewHost

func NewHost(catalogId string, opt ...Option) (*Host, error)

NewHost creates a new in memory Host for address assigned to catalogId. Name and description are the only valid options. All other options are ignored.

Example
package main

import (
	"fmt"

	"github.com/hashicorp/boundary/internal/host/static"
)

func main() {
	catalogPublicId := "hcst_1234"
	host, _ := static.NewHost(catalogPublicId, static.WithAddress("127.0.0.1"))
	fmt.Println(host.Address)
}
Output:

127.0.0.1

func TestHosts

func TestHosts(t testing.TB, conn *db.DB, catalogId string, count int) []*Host

TestHosts creates count number of static hosts to the provided DB with the provided catalog id. The catalog must have been created previously. If any errors are encountered during the creation of the host, the test will fail.

func (*Host) GetDnsNames added in v0.7.0

func (h *Host) GetDnsNames() []string

For compatibility with the general Host type

func (*Host) GetIpAddresses added in v0.7.0

func (h *Host) GetIpAddresses() []string

For compatibility with the general Host type

func (*Host) GetSetIds added in v0.7.5

func (h *Host) GetSetIds() []string

GetSetIds returns host set ids

func (*Host) SetTableName

func (h *Host) SetTableName(n string)

SetTableName sets the table name. If the caller attempts to set the name to "" the name will be reset to the default name.

func (*Host) TableName

func (h *Host) TableName() string

TableName returns the table name for the host.

type HostCatalog

type HostCatalog struct {
	*store.HostCatalog
	// contains filtered or unexported fields
}

A HostCatalog contains static hosts and static host sets. It is owned by a project.

func NewHostCatalog

func NewHostCatalog(projectId string, opt ...Option) (*HostCatalog, error)

NewHostCatalog creates a new in memory HostCatalog assigned to projectId. Name and description are the only valid options. All other options are ignored.

Example
package main

import (
	"fmt"

	"github.com/hashicorp/boundary/internal/host/static"
)

func main() {
	projectPublicId := "p_1234"
	catalog, _ := static.NewHostCatalog(projectPublicId, static.WithName("my catalog"))
	fmt.Println(catalog.Name)
}
Output:

my catalog

func TestCatalogs

func TestCatalogs(t testing.TB, conn *db.DB, projectId string, count int) []*HostCatalog

TestCatalogs creates count number of static host catalogs to the provided DB with the provided project id. If any errors are encountered during the creation of the host catalog, the test will fail.

func (*HostCatalog) SetTableName

func (c *HostCatalog) SetTableName(n string)

SetTableName sets the table name. If the caller attempts to set the name to "" the name will be reset to the default name.

func (*HostCatalog) TableName

func (c *HostCatalog) TableName() string

TableName returns the table name for the host catalog.

type HostSet

type HostSet struct {
	*store.HostSet
	// contains filtered or unexported fields
}

A HostSet is a collection of hosts from the set's catalog.

func NewHostSet

func NewHostSet(catalogId string, opt ...Option) (*HostSet, error)

NewHostSet creates a new in memory HostSet assigned to catalogId. Name and description are the only valid options. All other options are ignored.

Example
package main

import (
	"fmt"

	"github.com/hashicorp/boundary/internal/host/static"
)

func main() {
	catalogPublicId := "hcst_1234"
	set, _ := static.NewHostSet(catalogPublicId, static.WithName("my host set"))
	fmt.Println(set.Name)
}
Output:

my host set

func TestSets

func TestSets(t testing.TB, conn *db.DB, catalogId string, count int) []*HostSet

TestSets creates count number of static host sets in the provided DB with the provided catalog id. The catalog must have been created previously. The test will fail if any errors are encountered.

func (*HostSet) SetTableName

func (s *HostSet) SetTableName(n string)

SetTableName sets the table name. If the caller attempts to set the name to "" the name will be reset to the default name.

func (*HostSet) TableName

func (s *HostSet) TableName() string

TableName returns the table name for the host set.

type HostSetMember

type HostSetMember struct {
	*store.HostSetMember
	// contains filtered or unexported fields
}

A HostSetMember represents the membership of a host in a host set.

func NewHostSetMember

func NewHostSetMember(hostSetId, hostId string, opt ...Option) (*HostSetMember, error)

NewHostSetMember creates a new in memory HostSetMember representing the membership of hostId in hostSetId.

Example
package main

import (
	"fmt"

	"github.com/hashicorp/boundary/internal/host/static"
)

func main() {
	setPublicId := "hsst_11111"
	hostPublicId := "hst_22222"
	member, _ := static.NewHostSetMember(setPublicId, hostPublicId)
	fmt.Println(member.SetId)
	fmt.Println(member.HostId)
}
Output:

func TestSetMembers

func TestSetMembers(t testing.TB, conn *db.DB, setId string, hosts []*Host) []*HostSetMember

TestSetMembers adds hosts to the specified setId in the provided DB. The set and hosts must have been created previously and belong to the same catalog. The test will fail if any errors are encountered.

func (*HostSetMember) SetTableName

func (m *HostSetMember) SetTableName(n string)

SetTableName sets the table name. If the caller attempts to set the name to "" the name will be reset to the default name.

func (*HostSetMember) TableName

func (m *HostSetMember) TableName() string

TableName returns the table name for the host set.

type Option

type Option func(*options)

Option - how Options are passed as arguments.

func WithAddress

func WithAddress(address string) Option

WithAddress provides an optional address.

func WithDescription

func WithDescription(desc string) Option

WithDescription provides an optional description.

func WithLimit

func WithLimit(l int) Option

WithLimit provides an option to provide a limit. Intentionally allowing negative integers. If WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then default limits are used for results.

func WithName

func WithName(name string) Option

WithName provides an optional name.

func WithPublicId

func WithPublicId(id string) Option

WithPublicId provides an optional public id

type Repository

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

A Repository stores and retrieves the persistent types in the static package. It is not safe to use a repository concurrently.

func NewRepository

func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error)

NewRepository creates a new Repository. The returned repository should only be used for one transaction and it is not safe for concurrent go routines to access it. WithLimit option is used as a repo wide default limit applied to all ListX methods.

func (*Repository) AddSetMembers

func (r *Repository) AddSetMembers(ctx context.Context, projectId string, setId string, version uint32, hostIds []string, opt ...Option) ([]*Host, error)

AddSetMembers adds hostIds to setId in the repository. It returns a slice of all hosts in setId. A host must belong to the same catalog as the set to be added. The version must match the current version of the setId in the repository.

func (*Repository) CreateCatalog

func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...Option) (*HostCatalog, error)

CreateCatalog inserts c into the repository and returns a new HostCatalog containing the catalog's PublicId. c is not changed. c must contain a valid ProjectID. c must not contain a PublicId. The PublicId is generated and assigned by this method. opt is ignored.

Both c.Name and c.Description are optional. If c.Name is set, it must be unique within c.ProjectId.

Both c.CreateTime and c.UpdateTime are ignored.

func (*Repository) CreateHost

func (r *Repository) CreateHost(ctx context.Context, projectId string, h *Host, opt ...Option) (*Host, error)

CreateHost inserts h into the repository and returns a new Host containing the host's PublicId. h is not changed. h must contain a valid CatalogId. h must not contain a PublicId. The PublicId is generated and assigned by this method. opt is ignored.

h must contain a valid Address.

Both h.Name and h.Description are optional. If h.Name is set, it must be unique within h.CatalogId.

func (*Repository) CreateSet

func (r *Repository) CreateSet(ctx context.Context, projectId string, s *HostSet, opt ...Option) (*HostSet, error)

CreateSet inserts s into the repository and returns a new HostSet containing the host set's PublicId. s is not changed. s must contain a valid CatalogId. s must not contain a PublicId. The PublicId is generated and assigned by this method. opt is ignored.

Both s.Name and s.Description are optional. If s.Name is set, it must be unique within s.CatalogId.

func (*Repository) DeleteCatalog

func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option) (int, error)

DeleteCatalog deletes id from the repository returning a count of the number of records deleted.

func (*Repository) DeleteHost

func (r *Repository) DeleteHost(ctx context.Context, projectId string, publicId string, opt ...Option) (int, error)

DeleteHost deletes the host for the provided id from the repository returning a count of the number of records deleted. All options are ignored.

func (*Repository) DeleteSet

func (r *Repository) DeleteSet(ctx context.Context, projectId string, publicId string, opt ...Option) (int, error)

DeleteSet deletes the host set for the provided id from the repository returning a count of the number of records deleted. All options are ignored.

func (*Repository) DeleteSetMembers

func (r *Repository) DeleteSetMembers(ctx context.Context, projectId string, setId string, version uint32, hostIds []string, opt ...Option) (int, error)

DeleteSetMembers deletes hostIds from setId in the repository. It returns the number of hosts deleted from the set. The version must match the current version of the setId in the repository.

func (*Repository) Endpoints added in v0.7.0

func (r *Repository) Endpoints(ctx context.Context, setId string) ([]*host.Endpoint, error)

Endpoints returns a slice of host.Endpoint for the provided set id. If there are no hosts in the provided set id the slice is empty. If the set does not exist an error is returned.

func (*Repository) ListCatalogs

func (r *Repository) ListCatalogs(ctx context.Context, projectIds []string, opt ...Option) ([]*HostCatalog, error)

ListCatalogs returns a slice of HostCatalogs for the project IDs. WithLimit is the only option supported.

func (*Repository) ListHosts

func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Option) ([]*Host, error)

ListHosts returns a slice of Hosts for the catalogId. WithLimit is the only option supported.

func (*Repository) ListSets

func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...Option) ([]*HostSet, error)

ListSets returns a slice of HostSets for the catalogId. WithLimit is the only option supported.

func (*Repository) LookupCatalog

func (r *Repository) LookupCatalog(ctx context.Context, id string, opt ...Option) (*HostCatalog, error)

LookupCatalog returns the HostCatalog for id. Returns nil, nil if no HostCatalog is found for id.

func (*Repository) LookupHost

func (r *Repository) LookupHost(ctx context.Context, publicId string, opt ...Option) (*Host, error)

LookupHost will look up a host in the repository. If the host is not found, it will return nil, nil. All options are ignored.

func (*Repository) LookupSet

func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Option) (*HostSet, []*Host, error)

LookupSet will look up a host set in the repository and return the host set and the hosts assigned to the host set. If the host set is not found, it will return nil, nil, nil. The WithLimit option can be used to limit the number of hosts returned. All other options are ignored.

func (*Repository) SetSetMembers

func (r *Repository) SetSetMembers(ctx context.Context, projectId string, setId string, version uint32, hostIds []string, opt ...Option) ([]*Host, int, error)

SetSetMembers replaces the hosts in setId with hostIds in the repository. It returns a slice of all hosts in setId and a count of hosts added or deleted. A host must belong to the same catalog as the set to be added. The version must match the current version of the setId in the repository. If hostIds is empty, all hosts will be removed setId.

func (*Repository) UpdateCatalog

func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version uint32, fieldMask []string, opt ...Option) (*HostCatalog, int, error)

UpdateCatalog updates the repository entry for c.PublicId with the values in c for the fields listed in fieldMask. It returns a new HostCatalog containing the updated values and a count of the number of records updated. c is not changed.

c must contain a valid PublicId. Only c.Name and c.Description can be updated. If c.Name is set to a non-empty string, it must be unique within c.ProjectId.

An attribute of c will be set to NULL in the database if the attribute in c is the zero value and it is included in fieldMask.

func (*Repository) UpdateHost

func (r *Repository) UpdateHost(ctx context.Context, projectId string, h *Host, version uint32, fieldMaskPaths []string, opt ...Option) (*Host, int, error)

UpdateHost updates the repository entry for h.PublicId with the values in h for the fields listed in fieldMaskPaths. It returns a new Host containing the updated values and a count of the number of records updated. h is not changed.

h must contain a valid PublicId. Only h.Name, h.Description, and h.Address can be updated. If h.Name is set to a non-empty string, it must be unique within h.CatalogId. If h.Address is set, it must contain a valid address.

An attribute of h will be set to NULL in the database if the attribute in h is the zero value and it is included in fieldMaskPaths.

func (*Repository) UpdateSet

func (r *Repository) UpdateSet(ctx context.Context, projectId string, s *HostSet, version uint32, fieldMaskPaths []string, opt ...Option) (*HostSet, []*Host, int, error)

UpdateSet updates the repository entry for s.PublicId with the values in s for the fields listed in fieldMaskPaths. It returns a new HostSet containing the updated values, the hosts assigned to the host set, and a count of the number of records updated. s is not changed.

s must contain a valid PublicId. Only s.Name and s.Description can be updated. If s.Name is set to a non-empty string, it must be unique within s.CatalogId.

An attribute of s will be set to NULL in the database if the attribute in s is the zero value and it is included in fieldMaskPaths.

The WithLimit option can be used to limit the number of hosts returned. All other options are ignored.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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