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

     1  // Package region describes the operational regions defined for New Relic
     2  //
     3  // Regions are geographical locations where the New Relic platform operates
     4  // and this package provides an abstraction layer for handling them within
     5  // the New Relic Client and underlying APIs
     6  package region
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"strings"
    12  
    13  	log "github.com/sirupsen/logrus"
    14  )
    15  
    16  // Name is the name of a New Relic region
    17  type Name string
    18  
    19  // Region represents the members of the Region enumeration.
    20  type Region struct {
    21  	name                  string
    22  	infrastructureBaseURL string
    23  	insightsBaseURL       string
    24  	logsBaseURL           string
    25  	nerdGraphBaseURL      string
    26  	restBaseURL           string
    27  	syntheticsBaseURL     string
    28  	insightsKeysBaseURL   string
    29  }
    30  
    31  // String returns a human readable value for the specified Region Name
    32  func (n Name) String() string {
    33  	return string(n)
    34  }
    35  
    36  // String returns a human readable value for the specified Region
    37  func (r *Region) String() string {
    38  	if r != nil && r.name != "" {
    39  		return r.name
    40  	}
    41  
    42  	return "(Unknown)"
    43  }
    44  
    45  //
    46  // NerdGraph - the future
    47  //
    48  
    49  // SetNerdGraphBaseURL Allows overriding the NerdGraph Base URL
    50  func (r *Region) SetNerdGraphBaseURL(url string) {
    51  	if r != nil && url != "" {
    52  		r.nerdGraphBaseURL = url
    53  	}
    54  }
    55  
    56  // NerdGraphURL returns the Full URL for Infrastructure REST API Calls, with any additional path elements appended
    57  func (r *Region) NerdGraphURL(path ...string) string {
    58  	if r == nil {
    59  		log.Errorf("call to nil region.NerdGraphURL")
    60  		return ""
    61  	}
    62  
    63  	url, err := concatURLPaths(r.nerdGraphBaseURL, path)
    64  	if err != nil {
    65  		log.Errorf("unable to make URL with error: %s", err)
    66  		return r.nerdGraphBaseURL
    67  	}
    68  
    69  	return url
    70  }
    71  
    72  //
    73  // REST
    74  //
    75  
    76  // SetRestBaseURL Allows overriding the REST Base URL
    77  func (r *Region) SetRestBaseURL(url string) {
    78  	if r != nil && url != "" {
    79  		r.restBaseURL = url
    80  	}
    81  }
    82  
    83  // RestURL returns the Full URL for REST API Calls, with any additional path elements appended
    84  func (r *Region) RestURL(path ...string) string {
    85  	if r == nil {
    86  		log.Errorf("call to nil region.RestURL")
    87  		return ""
    88  	}
    89  
    90  	url, err := concatURLPaths(r.restBaseURL, path)
    91  	if err != nil {
    92  		log.Errorf("unable to make URL with error: %s", err)
    93  		return r.restBaseURL
    94  	}
    95  
    96  	return url
    97  }
    98  
    99  //
   100  // Infrastructure
   101  //
   102  
   103  // SetInfrastructureBaseURL Allows overriding the Infrastructure Base URL
   104  func (r *Region) SetInfrastructureBaseURL(url string) {
   105  	if r != nil && url != "" {
   106  		r.infrastructureBaseURL = url
   107  	}
   108  }
   109  
   110  // InfrastructureURL returns the Full URL for Infrastructure REST API Calls, with any additional path elements appended
   111  func (r *Region) InfrastructureURL(path ...string) string {
   112  	if r == nil {
   113  		log.Errorf("call to nil region.InfrastructureURL")
   114  		return ""
   115  	}
   116  
   117  	url, err := concatURLPaths(r.infrastructureBaseURL, path)
   118  	if err != nil {
   119  		log.Errorf("unable to make URL with error: %s", err)
   120  		return r.infrastructureBaseURL
   121  	}
   122  
   123  	return url
   124  }
   125  
   126  //
   127  // Synthetics
   128  //
   129  
   130  // SetSyntheticsBaseURL Allows overriding the Synthetics Base URL
   131  func (r *Region) SetSyntheticsBaseURL(url string) {
   132  	if r != nil && url != "" {
   133  		r.syntheticsBaseURL = url
   134  	}
   135  }
   136  
   137  // SyntheticsURL returns the Full URL for Synthetics REST API Calls, with any additional path elements appended
   138  func (r *Region) SyntheticsURL(path ...string) string {
   139  	if r == nil {
   140  		log.Errorf("call to nil region.SyntheticsURL")
   141  		return ""
   142  	}
   143  
   144  	url, err := concatURLPaths(r.syntheticsBaseURL, path)
   145  	if err != nil {
   146  		log.Errorf("unable to make URL with error: %s", err)
   147  		return r.syntheticsBaseURL
   148  	}
   149  
   150  	return url
   151  }
   152  
   153  //
   154  // Insights Insert URL
   155  //
   156  
   157  func (r *Region) SetInsightsBaseURL(url string) {
   158  	if r != nil && url != "" {
   159  		r.insightsBaseURL = url
   160  	}
   161  }
   162  
   163  // InsightsURL returns the Full URL for Insights custom insert API calls
   164  func (r *Region) InsightsURL(accountID int) string {
   165  	if r == nil {
   166  		log.Errorf("call to nil region.SyntheticsURL")
   167  		return ""
   168  	}
   169  	if accountID < 1 {
   170  		log.Errorf("invalid account ID: %d", accountID)
   171  		return ""
   172  	}
   173  
   174  	return fmt.Sprintf("%s/accounts/%d/events", r.insightsBaseURL, accountID)
   175  }
   176  
   177  // concatURLPaths is a helper function for the URL builders below
   178  func concatURLPaths(host string, path []string) (string, error) {
   179  	if host == "" {
   180  		return "", errors.New("host can not be empty")
   181  	}
   182  
   183  	elements := make([]string, len(path)+1)
   184  	elements[0] = strings.TrimSuffix(host, "/")
   185  
   186  	for k, v := range path {
   187  		elements[k+1] = strings.Trim(v, "/")
   188  	}
   189  
   190  	return strings.Join(elements, "/"), nil
   191  }
   192  
   193  //
   194  // Insights Keys
   195  //
   196  
   197  func (r *Region) SetInsightsKeysBaseURL(url string) {
   198  	if r != nil && url != "" {
   199  		r.insightsKeysBaseURL = url
   200  	}
   201  }
   202  
   203  // InsightsURL returns the Full URL for Insights custom insert API calls
   204  func (r *Region) InsightsKeysURL(accountID int, path string) string {
   205  	if r == nil {
   206  		log.Errorf("call to nil region.InsightsKeysURL")
   207  		return ""
   208  	}
   209  	if accountID < 1 {
   210  		log.Errorf("invalid account ID: %d", accountID)
   211  		return ""
   212  	}
   213  
   214  	return fmt.Sprintf("%s/accounts/%d/%s", r.insightsKeysBaseURL, accountID, path)
   215  }
   216  
   217  //
   218  // Logs
   219  //
   220  
   221  // SetLogsBaseURL Allows overriding the Logs Base URL
   222  func (r *Region) SetLogsBaseURL(url string) {
   223  	if r != nil && url != "" {
   224  		r.logsBaseURL = url
   225  	}
   226  }
   227  
   228  // LogsURL returns the Full URL for the Log API
   229  func (r *Region) LogsURL() string {
   230  	if r == nil {
   231  		log.Errorf("call to nil region.LogsURL")
   232  		return ""
   233  	}
   234  
   235  	return r.logsBaseURL
   236  }