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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package api
     5  
     6  // Recommendations is used to query the recommendations endpoints.
     7  type Recommendations struct {
     8  	client *Client
     9  }
    10  
    11  // Recommendations returns a new handle on the recommendations endpoints.
    12  func (c *Client) Recommendations() *Recommendations {
    13  	return &Recommendations{client: c}
    14  }
    15  
    16  // List is used to dump all of the recommendations in the cluster
    17  func (r *Recommendations) List(q *QueryOptions) ([]*Recommendation, *QueryMeta, error) {
    18  	var resp []*Recommendation
    19  	qm, err := r.client.query("/v1/recommendations", &resp, q)
    20  	if err != nil {
    21  		return nil, qm, err
    22  	}
    23  	return resp, qm, nil
    24  }
    25  
    26  // Info is used to return information on a single recommendation
    27  func (r *Recommendations) Info(id string, q *QueryOptions) (*Recommendation, *QueryMeta, error) {
    28  	var resp Recommendation
    29  	qm, err := r.client.query("/v1/recommendation/"+id, &resp, q)
    30  	if err != nil {
    31  		return nil, nil, err
    32  	}
    33  	return &resp, qm, nil
    34  }
    35  
    36  // Upsert is used to create or update a recommendation
    37  func (r *Recommendations) Upsert(rec *Recommendation, q *WriteOptions) (*Recommendation, *WriteMeta, error) {
    38  	var resp Recommendation
    39  	wm, err := r.client.put("/v1/recommendation", rec, &resp, q)
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  	return &resp, wm, nil
    44  }
    45  
    46  // Delete is used to delete a list of recommendations
    47  func (r *Recommendations) Delete(ids []string, q *WriteOptions) (*WriteMeta, error) {
    48  	req := &RecommendationApplyRequest{
    49  		Apply:   []string{},
    50  		Dismiss: ids,
    51  	}
    52  	wm, err := r.client.put("/v1/recommendations/apply", req, nil, q)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return wm, nil
    57  }
    58  
    59  // Apply is used to apply a set of recommendations
    60  func (r *Recommendations) Apply(ids []string, policyOverride bool) (
    61  	*RecommendationApplyResponse, *WriteMeta, error) {
    62  	req := &RecommendationApplyRequest{
    63  		Apply:          ids,
    64  		PolicyOverride: policyOverride,
    65  	}
    66  	var resp RecommendationApplyResponse
    67  	wm, err := r.client.put("/v1/recommendations/apply", req, &resp, nil)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  	resp.WriteMeta = *wm
    72  	return &resp, wm, nil
    73  }
    74  
    75  // Recommendation is used to serialize a recommendation.
    76  type Recommendation struct {
    77  	ID             string
    78  	Region         string
    79  	Namespace      string
    80  	JobID          string
    81  	JobVersion     uint64
    82  	Group          string
    83  	Task           string
    84  	Resource       string
    85  	Value          int
    86  	Current        int
    87  	Meta           map[string]interface{}
    88  	Stats          map[string]float64
    89  	EnforceVersion bool
    90  
    91  	SubmitTime int64
    92  
    93  	CreateIndex uint64
    94  	ModifyIndex uint64
    95  }
    96  
    97  // RecommendationApplyRequest is used to apply and/or dismiss a set of recommendations
    98  type RecommendationApplyRequest struct {
    99  	Apply          []string
   100  	Dismiss        []string
   101  	PolicyOverride bool
   102  }
   103  
   104  // RecommendationApplyResponse is used to apply a set of recommendations
   105  type RecommendationApplyResponse struct {
   106  	UpdatedJobs []*SingleRecommendationApplyResult
   107  	Errors      []*SingleRecommendationApplyError
   108  	WriteMeta
   109  }
   110  
   111  type SingleRecommendationApplyResult struct {
   112  	Namespace       string
   113  	JobID           string
   114  	JobModifyIndex  uint64
   115  	EvalID          string
   116  	EvalCreateIndex uint64
   117  	Warnings        string
   118  	Recommendations []string
   119  }
   120  
   121  type SingleRecommendationApplyError struct {
   122  	Namespace       string
   123  	JobID           string
   124  	Recommendations []string
   125  	Error           string
   126  }