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

     1  package apm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  )
     7  
     8  // applicationsREST implements fetching Applications from the RESTv2 API
     9  type applicationsREST struct {
    10  	parent *APM
    11  }
    12  
    13  // list is used to retrieve New Relic applications.
    14  func (a *applicationsREST) list(ctx context.Context, accountID int, params *ListApplicationsParams) ([]*Application, error) {
    15  	apps := []*Application{}
    16  	nextURL := a.parent.config.Region().RestURL("applications.json")
    17  
    18  	for nextURL != "" {
    19  		response := applicationsResponse{}
    20  		resp, err := a.parent.client.GetWithContext(ctx, nextURL, &params, &response)
    21  
    22  		if err != nil {
    23  			return nil, err
    24  		}
    25  
    26  		apps = append(apps, response.Applications...)
    27  
    28  		paging := a.parent.pager.Parse(resp)
    29  		nextURL = paging.Next
    30  	}
    31  
    32  	return apps, nil
    33  }
    34  
    35  // get is used to retrieve a single New Relic application.
    36  func (a *applicationsREST) get(ctx context.Context, accountID int, applicationID int) (*Application, error) {
    37  	response := applicationResponse{}
    38  	url := fmt.Sprintf("/applications/%d.json", applicationID)
    39  
    40  	_, err := a.parent.client.GetWithContext(ctx, a.parent.config.Region().RestURL(url), nil, &response)
    41  
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return &response.Application, nil
    47  }
    48  
    49  // update is used to update a New Relic application's name and/or settings.
    50  func (a *applicationsREST) update(ctx context.Context, accountID int, applicationID int, params UpdateApplicationParams) (*Application, error) {
    51  	response := applicationResponse{}
    52  	reqBody := updateApplicationRequest{
    53  		Fields: updateApplicationFields(params),
    54  	}
    55  	url := fmt.Sprintf("/applications/%d.json", applicationID)
    56  
    57  	_, err := a.parent.client.PutWithContext(ctx, a.parent.config.Region().RestURL(url), nil, &reqBody, &response)
    58  
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return &response.Application, nil
    64  }
    65  
    66  // remove is used to delete a New Relic application.
    67  // This process will only succeed if the application is no longer reporting data.
    68  func (a *applicationsREST) remove(ctx context.Context, accountID int, applicationID int) (*Application, error) {
    69  	response := applicationResponse{}
    70  	url := fmt.Sprintf("/applications/%d.json", applicationID)
    71  
    72  	_, err := a.parent.client.DeleteWithContext(ctx, a.parent.config.Region().RestURL(url), nil, &response)
    73  
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return &response.Application, nil
    79  }
    80  
    81  type applicationsResponse struct {
    82  	Applications []*Application `json:"applications,omitempty"`
    83  }
    84  
    85  type applicationResponse struct {
    86  	Application Application `json:"application,omitempty"`
    87  }
    88  
    89  type updateApplicationRequest struct {
    90  	Fields updateApplicationFields `json:"application"`
    91  }
    92  
    93  type updateApplicationFields struct {
    94  	Name     string              `json:"name,omitempty"`
    95  	Settings ApplicationSettings `json:"settings,omitempty"`
    96  }