github.com/newrelic/newrelic-client-go@v1.1.0/pkg/apm/labels.go (about)

     1  package apm
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/newrelic/newrelic-client-go/pkg/errors"
     7  )
     8  
     9  // Label represents a New Relic label.
    10  type Label struct {
    11  	Key      string     `json:"key,omitempty"`
    12  	Category string     `json:"category,omitempty"`
    13  	Name     string     `json:"name,omitempty"`
    14  	Links    LabelLinks `json:"links,omitempty"`
    15  }
    16  
    17  // LabelLinks represents external references on the Label.
    18  type LabelLinks struct {
    19  	Applications []int `json:"applications"`
    20  	Servers      []int `json:"servers"`
    21  }
    22  
    23  // ListLabels returns the labels within an account.
    24  func (a *APM) ListLabels() ([]*Label, error) {
    25  	return a.ListLabelsWithContext(context.Background())
    26  }
    27  
    28  // ListLabelsWithContext returns the labels within an account.
    29  func (a *APM) ListLabelsWithContext(ctx context.Context) ([]*Label, error) {
    30  	labels := []*Label{}
    31  	nextURL := a.config.Region().RestURL("labels.json")
    32  
    33  	for nextURL != "" {
    34  		response := labelsResponse{}
    35  		resp, err := a.client.GetWithContext(ctx, nextURL, nil, &response)
    36  
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  
    41  		labels = append(labels, response.Labels...)
    42  
    43  		paging := a.pager.Parse(resp)
    44  		nextURL = paging.Next
    45  	}
    46  
    47  	return labels, nil
    48  }
    49  
    50  // GetLabel gets a label by key. A label's key
    51  // is a string hash formatted as <Category>:<Name>.
    52  func (a *APM) GetLabel(key string) (*Label, error) {
    53  	return a.GetLabelWithContext(context.Background(), key)
    54  }
    55  
    56  // GetLabelWithContext gets a label by key. A label's key
    57  // is a string hash formatted as <Category>:<Name>.
    58  func (a *APM) GetLabelWithContext(ctx context.Context, key string) (*Label, error) {
    59  	labels, err := a.ListLabelsWithContext(ctx)
    60  
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	for _, label := range labels {
    66  		if label.Key == key {
    67  			return label, nil
    68  		}
    69  	}
    70  
    71  	return nil, errors.NewNotFoundf("no label found with key %s", key)
    72  }
    73  
    74  // CreateLabel creates a new label within an account.
    75  func (a *APM) CreateLabel(label Label) (*Label, error) {
    76  	return a.CreateLabelWithContext(context.Background(), label)
    77  }
    78  
    79  // CreateLabelWithContext creates a new label within an account.
    80  func (a *APM) CreateLabelWithContext(ctx context.Context, label Label) (*Label, error) {
    81  	reqBody := labelRequestBody{
    82  		Label: label,
    83  	}
    84  	resp := labelResponse{}
    85  
    86  	// The API currently uses a PUT request for label creation
    87  	_, err := a.client.PutWithContext(ctx, a.config.Region().RestURL("labels.json"), nil, &reqBody, &resp)
    88  
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return &resp.Label, nil
    94  }
    95  
    96  // DeleteLabel deletes a label by key. A label's key
    97  // is a string hash formatted as <Category>:<Name>.
    98  func (a *APM) DeleteLabel(key string) (*Label, error) {
    99  	return a.DeleteLabelWithContext(context.Background(), key)
   100  }
   101  
   102  // DeleteLabelWithContext deletes a label by key. A label's key
   103  // is a string hash formatted as <Category>:<Name>.
   104  func (a *APM) DeleteLabelWithContext(ctx context.Context, key string) (*Label, error) {
   105  	resp := labelResponse{}
   106  
   107  	_, err := a.client.DeleteWithContext(ctx, a.config.Region().RestURL("labels", key+".json"), nil, &resp)
   108  
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	return &resp.Label, nil
   114  }
   115  
   116  type labelsResponse struct {
   117  	Labels []*Label `json:"labels,omitempty"`
   118  }
   119  
   120  type labelResponse struct {
   121  	Label Label `json:"label,omitempty"`
   122  }
   123  
   124  type labelRequestBody struct {
   125  	Label Label `json:"label,omitempty"`
   126  }