github.com/hashicorp/nomad/api@v0.0.0-20240306165712-3193ac204f65/node_meta.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package api
     5  
     6  // NodeMetaApplyRequest contains the Node meta update.
     7  type NodeMetaApplyRequest struct {
     8  	NodeID string
     9  	Meta   map[string]*string
    10  }
    11  
    12  // NodeMetaResponse contains the merged Node metadata.
    13  type NodeMetaResponse struct {
    14  	// Meta is the merged static + dynamic Node metadata
    15  	Meta map[string]string
    16  
    17  	// Dynamic is the dynamic Node metadata (set via API)
    18  	Dynamic map[string]*string
    19  
    20  	// Static is the static Node metadata (set via agent configuration)
    21  	Static map[string]string
    22  }
    23  
    24  // NodeMeta is a client for manipulating dynamic Node metadata.
    25  type NodeMeta struct {
    26  	client *Client
    27  }
    28  
    29  // Meta returns a NodeMeta client.
    30  func (n *Nodes) Meta() *NodeMeta {
    31  	return &NodeMeta{client: n.client}
    32  }
    33  
    34  // Apply dynamic Node metadata updates to a Node. If NodeID is unset then Node
    35  // receiving the request is modified.
    36  func (n *NodeMeta) Apply(meta *NodeMetaApplyRequest, qo *QueryOptions) (*NodeMetaResponse, error) {
    37  	var out NodeMetaResponse
    38  	_, err := n.client.postQuery("/v1/client/metadata", meta, &out, qo)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	return &out, nil
    43  }
    44  
    45  // Read Node metadata (dynamic and static merged) from a Node directly. May
    46  // differ from Node.Info as dynamic Node metadata updates are batched and may
    47  // be delayed up to 10 seconds.
    48  //
    49  // If nodeID is empty then the metadata for the Node receiving the request is
    50  // returned.
    51  func (n *NodeMeta) Read(nodeID string, qo *QueryOptions) (*NodeMetaResponse, error) {
    52  	if qo == nil {
    53  		qo = &QueryOptions{}
    54  	}
    55  
    56  	if qo.Params == nil {
    57  		qo.Params = make(map[string]string)
    58  	}
    59  
    60  	if nodeID != "" {
    61  		qo.Params["node_id"] = nodeID
    62  	}
    63  
    64  	var out NodeMetaResponse
    65  	_, err := n.client.query("/v1/client/metadata", &out, qo)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return &out, nil
    71  }