feat(host/plugin): Support External Name field

The new `ExternalName` field is an optional field that is used to store
the cloud providers' human-identifiable name for a given compute
instance. This field is not user-editable, but it is kept in sync with
the cloud provider's through Boundary's `SetSyncJob`.
This commit is contained in:
Hugo Vieira authored and Hugo committed 2023-04-04 18:51:04 +01:00
1 parent 26ebaefd70
commit 2ff9b8efac
13 files changed
+340 -132

No files matched your search

+38 -18
View File
@@ -5,12 +5,15 @@ package plugin
import (
"context"
"fmt"
"sort"
"strings"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/host/plugin/store"
"github.com/hashicorp/boundary/internal/observability/event"
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/go-secure-stdlib/strutil"
"google.golang.org/protobuf/proto"
)
@@ -29,16 +32,31 @@ type Host struct {
// Supported options: WithName, WithDescription, WithIpAddresses, WithDnsNames,
// WithPluginId, WithPublicId. Others ignored.
func NewHost(ctx context.Context, catalogId, externalId string, opt ...Option) *Host {
const op = "plugin.NewHost"
opts := getOpts(opt...)
// This check is the logical counterpart of the database constraints on the
// external_name field. By replicating the checks as closely as possible in
// code, we reduce the risk of SetSyncJob failing due to a bad external
// name.
if !strutil.Printable(opts.withExternalName) || len(opts.withExternalName) > 256 {
event.WriteError(ctx, op,
fmt.Errorf("ignoring host id %q external name %q due to its length (greater than 256 characters) or the presence of unsupported unicode characters",
opts.withPublicId,
opts.withExternalName),
)
opts.withExternalName = ""
}
h := &Host{
PluginId: opts.withPluginId,
Host: &store.Host{
PublicId: opts.withPublicId,
CatalogId: catalogId,
ExternalId: externalId,
Name: opts.withName,
Description: opts.withDescription,
PublicId: opts.withPublicId,
CatalogId: catalogId,
ExternalId: externalId,
ExternalName: opts.withExternalName,
Name: opts.withName,
Description: opts.withDescription,
},
}
if len(opts.withIpAddresses) > 0 {
@@ -115,19 +133,20 @@ func (h *Host) GetSetIds() []string {
// hostAgg is a view that aggregates the host's value objects in to
// string fields delimited with the aggregateDelimiter of "|"
type hostAgg struct {
PublicId string `gorm:"primary_key"`
CatalogId string
ProjectId string
ExternalId string
PluginId string
Name string
Description string
CreateTime *timestamp.Timestamp
UpdateTime *timestamp.Timestamp
Version uint32
IpAddresses string
DnsNames string
SetIds string
PublicId string `gorm:"primary_key"`
CatalogId string
ProjectId string
ExternalId string
ExternalName string
PluginId string
Name string
Description string
CreateTime *timestamp.Timestamp
UpdateTime *timestamp.Timestamp
Version uint32
IpAddresses string
DnsNames string
SetIds string
}
func (agg *hostAgg) toHost() *Host {
@@ -136,6 +155,7 @@ func (agg *hostAgg) toHost() *Host {
h.PublicId = agg.PublicId
h.CatalogId = agg.CatalogId
h.ExternalId = agg.ExternalId
h.ExternalName = agg.ExternalName
h.PluginId = agg.PluginId
h.Name = agg.Name
h.Description = agg.Description