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

     1  package apm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/newrelic/newrelic-client-go/internal/http"
     9  )
    10  
    11  // Deployment represents information about a New Relic application deployment.
    12  type Deployment struct {
    13  	Links       *DeploymentLinks `json:"links,omitempty"`
    14  	ID          int              `json:"id,omitempty"`
    15  	Revision    string           `json:"revision"`
    16  	Changelog   string           `json:"changelog,omitempty"`
    17  	Description string           `json:"description,omitempty"`
    18  	User        string           `json:"user,omitempty"`
    19  	Timestamp   string           `json:"timestamp,omitempty"`
    20  }
    21  
    22  // DeploymentLinks contain the application ID for the deployment.
    23  type DeploymentLinks struct {
    24  	ApplicationID int `json:"application,omitempty"`
    25  }
    26  
    27  // ListDeployments returns deployments for an application.
    28  func (a *APM) ListDeployments(applicationID int) ([]*Deployment, error) {
    29  	return a.ListDeploymentsWithContext(context.Background(), applicationID)
    30  }
    31  
    32  // ListDeploymentsWithContext returns deployments for an application.
    33  func (a *APM) ListDeploymentsWithContext(ctx context.Context, applicationID int) ([]*Deployment, error) {
    34  	deployments := []*Deployment{}
    35  	nextURL := a.config.Region().RestURL("applications", strconv.Itoa(applicationID), "deployments.json")
    36  
    37  	for nextURL != "" {
    38  		response := deploymentsResponse{}
    39  		req, err := a.client.NewRequest("GET", nextURL, nil, nil, &response)
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  
    44  		req.WithContext(ctx)
    45  		req.SetAuthStrategy(&http.PersonalAPIKeyCapableV2Authorizer{})
    46  
    47  		resp, err := a.client.Do(req)
    48  
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  
    53  		deployments = append(deployments, response.Deployments...)
    54  
    55  		paging := a.pager.Parse(resp)
    56  		nextURL = paging.Next
    57  	}
    58  
    59  	return deployments, nil
    60  }
    61  
    62  // CreateDeployment creates a deployment marker for an application.
    63  func (a *APM) CreateDeployment(applicationID int, deployment Deployment) (*Deployment, error) {
    64  	return a.CreateDeploymentWithContext(context.Background(), applicationID, deployment)
    65  }
    66  
    67  // CreateDeploymentWithContext creates a deployment marker for an application.
    68  func (a *APM) CreateDeploymentWithContext(ctx context.Context, applicationID int, deployment Deployment) (*Deployment, error) {
    69  	reqBody := deploymentRequestBody{
    70  		Deployment: deployment,
    71  	}
    72  	resp := deploymentResponse{}
    73  
    74  	url := a.config.Region().RestURL("applications", strconv.Itoa(applicationID), "deployments.json")
    75  	req, err := a.client.NewRequest("POST", url, nil, &reqBody, &resp)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	req.WithContext(ctx)
    81  	req.SetAuthStrategy(&http.PersonalAPIKeyCapableV2Authorizer{})
    82  
    83  	_, err = a.client.Do(req)
    84  
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	return &resp.Deployment, nil
    90  }
    91  
    92  // DeleteDeployment deletes a deployment marker for an application.
    93  func (a *APM) DeleteDeployment(applicationID int, deploymentID int) (*Deployment, error) {
    94  	return a.DeleteDeploymentWithContext(context.Background(), applicationID, deploymentID)
    95  }
    96  
    97  // DeleteDeploymentWithContext deletes a deployment marker for an application.
    98  func (a *APM) DeleteDeploymentWithContext(ctx context.Context, applicationID int, deploymentID int) (*Deployment, error) {
    99  	resp := deploymentResponse{}
   100  	url := a.config.Region().RestURL("applications", strconv.Itoa(applicationID), "deployments", fmt.Sprintf("%d.json", deploymentID))
   101  
   102  	req, err := a.client.NewRequest("DELETE", url, nil, nil, &resp)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	req.WithContext(ctx)
   108  	req.SetAuthStrategy(&http.PersonalAPIKeyCapableV2Authorizer{})
   109  
   110  	_, err = a.client.Do(req)
   111  
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	return &resp.Deployment, nil
   117  }
   118  
   119  type deploymentsResponse struct {
   120  	Deployments []*Deployment `json:"deployments,omitempty"`
   121  }
   122  
   123  type deploymentResponse struct {
   124  	Deployment Deployment `json:"deployment,omitempty"`
   125  }
   126  
   127  type deploymentRequestBody struct {
   128  	Deployment Deployment `json:"deployment,omitempty"`
   129  }