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

     1  // Package plugins provides a programmatic API for interacting with the New Relic Plugins product.
     2  package plugins
     3  
     4  import (
     5  	"fmt"
     6  
     7  	"github.com/newrelic/newrelic-client-go/internal/http"
     8  	"github.com/newrelic/newrelic-client-go/pkg/config"
     9  	"github.com/newrelic/newrelic-client-go/pkg/logging"
    10  )
    11  
    12  // Plugins is used to communicate with the New Relic Plugins product.
    13  type Plugins struct {
    14  	client http.Client
    15  	config config.Config
    16  	logger logging.Logger
    17  	pager  http.Pager
    18  }
    19  
    20  // New is used to create a new Plugins client instance.
    21  func New(config config.Config) Plugins {
    22  	client := http.NewClient(config)
    23  	client.SetAuthStrategy(&http.PersonalAPIKeyCapableV2Authorizer{})
    24  
    25  	pkg := Plugins{
    26  		client: client,
    27  		config: config,
    28  		logger: config.GetLogger(),
    29  		pager:  &http.LinkHeaderPager{},
    30  	}
    31  
    32  	return pkg
    33  }
    34  
    35  // ListPluginsParams represents a set of query string parameters
    36  // used as filters when querying New Relic plugins.
    37  type ListPluginsParams struct {
    38  	GUID     string `url:"filter[guid],omitempty"`
    39  	IDs      []int  `url:"filter[ids],omitempty,comma"`
    40  	Detailed bool   `url:"detailed,omitempty"`
    41  }
    42  
    43  // ListPlugins returns a list of Plugins associated with an account.
    44  // If the query paramater `detailed=true` is provided, the plugins
    45  // response objects will contain an additional `details` property
    46  // with metadata pertaining to each plugin.
    47  func (p *Plugins) ListPlugins(params *ListPluginsParams) ([]*Plugin, error) {
    48  	results := []*Plugin{}
    49  	nextURL := p.config.Region().RestURL("plugins.json")
    50  
    51  	for nextURL != "" {
    52  		response := pluginsResponse{}
    53  		resp, err := p.client.Get(nextURL, &params, &response)
    54  
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  
    59  		results = append(results, response.Plugins...)
    60  
    61  		paging := p.pager.Parse(resp)
    62  		nextURL = paging.Next
    63  	}
    64  
    65  	return results, nil
    66  }
    67  
    68  // GetPluginParams represents a set of query string parameters
    69  // to apply to the request.
    70  type GetPluginParams struct {
    71  	Detailed bool `url:"detailed,omitempty"`
    72  }
    73  
    74  // GetPlugin returns a plugin for a given account. If the query paramater `detailed=true`
    75  // is provided, the response will contain an additional `details` property with
    76  // metadata pertaining to the plugin.
    77  func (p *Plugins) GetPlugin(id int, params *GetPluginParams) (*Plugin, error) {
    78  	response := pluginResponse{}
    79  
    80  	url := fmt.Sprintf("/plugins/%d.json", id)
    81  	_, err := p.client.Get(p.config.Region().RestURL(url), &params, &response)
    82  
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return &response.Plugin, nil
    88  }
    89  
    90  type pluginsResponse struct {
    91  	Plugins []*Plugin `json:"plugins,omitempty"`
    92  }
    93  
    94  type pluginResponse struct {
    95  	Plugin Plugin `json:"plugin,omitempty"`
    96  }