Signed-off-by: Sonia Appasamy <sonia@tailscale.com>
Consolidates the node display name logic from each of the clients into
tailcfg.Node. UI clients can use these names directly, rather than computing
them independently.
// DisplayName is the title to show for the node in client
// UIs. This field is assigned by default in controlclient,
// but can be overriden by providing this field non-empty
// in a MapResponse.
DisplayNamestring`json:",omitempty"`
// User is the user who created the node. If ACL tags are in
// use for the node then it doesn't reflect the ACL identity
// that the node is running as.
@ -190,21 +184,98 @@ type Node struct {
KeepAlivebool`json:",omitempty"`// open and keep open a connection to this peer
MachineAuthorizedbool`json:",omitempty"`// TODO(crawshaw): replace with MachineStatus
// The following three computed fields hold the various names that can
// be used for this node in UIs. They are populated from controlclient
// (not from control) by calling node.InitDisplayNames. These can be
// used directly or accessed via node.DisplayName or node.DisplayNames.
ComputedNamestring`json:",omitempty"`// MagicDNS base name (for normal non-shared-in nodes), FQDN (without trailing dot, for shared-in nodes), or Hostname (if no MagicDNS)
computedHostIfDifferentstring// hostname, if different than ComputedName, otherwise empty
ComputedNameWithHoststring`json:",omitempty"`// either "ComputedName" or "ComputedName (computedHostIfDifferent)", if computedHostIfDifferent is set
}
// DefaultDisplayName returns a value suitable
// for using as the default value for n.DisplayName.
func(n*Node)DefaultDisplayName()string{
ifn.Name!=""{
// Use the Magic DNS prefix as the default display name.
returndnsname.ToBaseName(n.Name)
// DisplayName returns the user-facing name for a node which should
// be shown in client UIs.
//
// Parameter forOwner specifies whether the name is requested by
// the owner of the node. When forOwner is false, the hostname is
// never included in the return value.
//
// Return value is either either "Name" or "Name (Hostname)", where
// Name is the node's MagicDNS base name (for normal non-shared-in
// nodes), FQDN (without trailing dot, for shared-in nodes), or
// Hostname (if no MagicDNS). Hostname is only included in the
// return value if it varies from Name and forOwner is provided true.
//
// DisplayName is only valid if InitDisplayNames has been called.
func(n*Node)DisplayName(forOwnerbool)string{
ifforOwner{
returnn.ComputedNameWithHost
}
ifn.Hostinfo.Hostname!=""{
// When no Magic DNS name is present, use the hostname.
returnn.Hostinfo.Hostname
returnn.ComputedName
}
// DisplayName returns the decomposed user-facing name for a node.
//
// Parameter forOwner specifies whether the name is requested by
// the owner of the node. When forOwner is false, hostIfDifferent
// is always returned empty.
//
// Return value name is the node's primary name, populated with the
// node's MagicDNS base name (for normal non-shared-in nodes), FQDN
// (without trailing dot, for shared-in nodes), or Hostname (if no
// MagicDNS).
//
// Return value hostIfDifferent, when non-empty, is the node's
// hostname. hostIfDifferent is only populated when the hostname
// varies from name and forOwner is provided as true.
//
// DisplayNames is only valid if InitDisplayNames has been called.