mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-10-12 01:58:46 +02:00
Send full host metadata on managed startup
This commit is contained in:
1 parent
b0408e4d1c
commit
be8c068fa9
15 files changed
+456
-56
No files matched your search
@@ -226,9 +226,36 @@ type UtilityOrgRequest struct {
|
||||
|
||||
// ResolverConfigRequest contains request data for fetching resolver config.
|
||||
type ResolverConfigRequest struct {
|
||||
RawUID string
|
||||
Version string
|
||||
Metadata map[string]string
|
||||
RawUID string
|
||||
Version string
|
||||
Metadata map[string]string
|
||||
IncludeHostnameHints bool
|
||||
}
|
||||
|
||||
var hostnameHintsFn = hostnameHints
|
||||
|
||||
func resolverConfigUtilityRequest(req *ResolverConfigRequest, uid, clientID string) utilityRequest {
|
||||
var metadata map[string]string
|
||||
if req.Metadata != nil {
|
||||
metadata = make(map[string]string, len(req.Metadata))
|
||||
for key, value := range req.Metadata {
|
||||
metadata[key] = value
|
||||
}
|
||||
}
|
||||
if req.IncludeHostnameHints {
|
||||
if metadata == nil {
|
||||
metadata = make(map[string]string)
|
||||
}
|
||||
for key, value := range hostnameHintsFn() {
|
||||
metadata["hostname_"+key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return utilityRequest{
|
||||
UID: uid,
|
||||
ClientID: clientID,
|
||||
Metadata: metadata,
|
||||
}
|
||||
}
|
||||
|
||||
// LogsRequest contains request data for sending runtime logs to API.
|
||||
@@ -245,12 +272,8 @@ func FetchResolverConfig(ctx context.Context, req *ResolverConfigRequest, cdDev
|
||||
uid, clientID := ParseRawUID(req.RawUID)
|
||||
ctrld.Log(ctx, logger.Debug(), "Parsed UID: %s, ClientID: %s", uid, clientID)
|
||||
|
||||
uReq := utilityRequest{
|
||||
UID: uid,
|
||||
Metadata: req.Metadata,
|
||||
}
|
||||
uReq := resolverConfigUtilityRequest(req, uid, clientID)
|
||||
if clientID != "" {
|
||||
uReq.ClientID = clientID
|
||||
ctrld.Log(ctx, logger.Debug(), "Including client ID in request")
|
||||
}
|
||||
body, _ := json.Marshal(uReq)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package controld
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestResolverConfigUtilityRequestMetadataScope(t *testing.T) {
|
||||
oldHostnameHints := hostnameHintsFn
|
||||
t.Cleanup(func() { hostnameHintsFn = oldHostnameHints })
|
||||
hostnameHintsFn = func() map[string]string {
|
||||
return map[string]string{"ComputerName": "workstation"}
|
||||
}
|
||||
|
||||
baseMetadata := map[string]string{"os": "test-os", "username": "test-user"}
|
||||
full := resolverConfigUtilityRequest(&ResolverConfigRequest{
|
||||
Metadata: baseMetadata,
|
||||
IncludeHostnameHints: true,
|
||||
}, "uid", "client")
|
||||
if got := full.Metadata["hostname_ComputerName"]; got != "workstation" {
|
||||
t.Errorf("full metadata hostname hint = %q, want workstation", got)
|
||||
}
|
||||
if got := full.Metadata["username"]; got != "test-user" {
|
||||
t.Errorf("full metadata username = %q, want test-user", got)
|
||||
}
|
||||
if _, ok := baseMetadata["hostname_ComputerName"]; ok {
|
||||
t.Error("building the utility request mutated the caller metadata")
|
||||
}
|
||||
|
||||
runtime := resolverConfigUtilityRequest(&ResolverConfigRequest{
|
||||
Metadata: map[string]string{"os": "test-os"},
|
||||
}, "uid", "")
|
||||
if _, ok := runtime.Metadata["hostname_ComputerName"]; ok {
|
||||
t.Error("runtime metadata included hostname hints")
|
||||
}
|
||||
if _, ok := runtime.Metadata["username"]; ok {
|
||||
t.Error("runtime metadata included username")
|
||||
}
|
||||
|
||||
empty := resolverConfigUtilityRequest(&ResolverConfigRequest{}, "uid", "")
|
||||
if empty.Metadata != nil {
|
||||
t.Error("request without metadata changed from null to an observed empty object")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user