Files
threshold/internal/host/static/host_catalog.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.2 KiB
Go
Raw Normal View History

2020-08-13 14:00:15 -04:00
// Copyright (c) HashiCorp, Inc.
2023-02-02 17:15:17 -08:00
// SPDX-License-Identifier: BUSL-1.1
2020-08-13 14:00:15 -04:00
package static
import (
"context"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-13 14:00:15 -04:00
"github.com/hashicorp/boundary/internal/host/static/store"
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/types/resource"
2020-08-13 14:00:15 -04:00
"google.golang.org/protobuf/proto"
)
// A HostCatalog contains static hosts and static host sets. It is owned by
2022-07-20 16:24:46 -07:00
// a project.
2020-08-13 14:00:15 -04:00
type HostCatalog struct {
*store.HostCatalog
tableName string `gorm:"-"`
}
2022-07-20 16:24:46 -07:00
// NewHostCatalog creates a new in memory HostCatalog assigned to projectId.
2020-08-13 14:00:15 -04:00
// Name and description are the only valid options. All other options are
// ignored.
func NewHostCatalog(ctx context.Context, projectId string, opt ...Option) (*HostCatalog, error) {
2022-07-20 16:24:46 -07:00
if projectId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, "static.NewHostCatalog", "no project id")
2020-08-13 14:00:15 -04:00
}
opts := getOpts(opt...)
hc := &HostCatalog{
HostCatalog: &store.HostCatalog{
2022-07-20 16:24:46 -07:00
ProjectId: projectId,
2020-08-13 14:00:15 -04:00
Name: opts.withName,
Description: opts.withDescription,
},
}
return hc, nil
}
func (c *HostCatalog) clone() *HostCatalog {
cp := proto.Clone(c.HostCatalog)
return &HostCatalog{
HostCatalog: cp.(*store.HostCatalog),
}
}
// TableName returns the table name for the host catalog.
func (c *HostCatalog) TableName() string {
if c.tableName != "" {
return c.tableName
}
return "static_host_catalog"
}
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (c *HostCatalog) SetTableName(n string) {
c.tableName = n
}
// GetResourceType returns the resource type of the HostCatalog
func (c *HostCatalog) GetResourceType() resource.Type {
return resource.HostCatalog
}
2020-08-13 14:00:15 -04:00
func allocCatalog() *HostCatalog {
fresh := &HostCatalog{
HostCatalog: &store.HostCatalog{},
}
return fresh
}
func newCatalogMetadata(c *HostCatalog, op oplog.OpType) oplog.Metadata {
metadata := oplog.Metadata{
"resource-public-id": []string{c.GetPublicId()},
"resource-type": []string{"static host catalog"},
"op-type": []string{op.String()},
}
2022-07-20 16:24:46 -07:00
if c.ProjectId != "" {
metadata["project-id"] = []string{c.ProjectId}
2020-08-13 14:00:15 -04:00
}
return metadata
}