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

     1  package apiaccess
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  type InsightsKey struct {
    10  	ID        int       `json:"id"`
    11  	AccountID int       `json:"account_id"`
    12  	Key       string    `json:"key"`
    13  	Notes     string    `json:"notes"`
    14  	IsEnabled bool      `json:"is_enabled"`
    15  	CreatedAt time.Time `json:"created_at"`
    16  	UpdatedAt time.Time `json:"updated_at"`
    17  }
    18  
    19  func (a *APIAccess) ListInsightsInsertKeys(accountID int) ([]InsightsKey, error) {
    20  	return a.ListInsightsInsertKeysWithContext(context.Background(), accountID)
    21  }
    22  
    23  func (a *APIAccess) ListInsightsInsertKeysWithContext(ctx context.Context, accountID int) ([]InsightsKey, error) {
    24  	keys := []InsightsKey{}
    25  	url := a.config.Region().InsightsKeysURL(accountID, "insert_keys?format=json")
    26  	_, err := a.insightsKeysClient.GetWithContext(ctx, url, nil, &keys)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return keys, nil
    32  }
    33  
    34  func (a *APIAccess) CreateInsightsInsertKey(accountID int) (*InsightsKey, error) {
    35  	return a.CreateInsightsInsertKeyWithContext(context.Background(), accountID)
    36  }
    37  
    38  func (a *APIAccess) CreateInsightsInsertKeyWithContext(ctx context.Context, accountID int) (*InsightsKey, error) {
    39  	key := InsightsKey{}
    40  	url := a.config.Region().InsightsKeysURL(accountID, "insert_keys?format=json")
    41  	_, err := a.insightsKeysClient.PostWithContext(ctx, url, nil, nil, &key)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return &key, nil
    47  }
    48  
    49  func (a *APIAccess) GetInsightsInsertKey(accountID int, keyID int) (*InsightsKey, error) {
    50  	return a.GetInsightsInsertKeyWithContext(context.Background(), accountID, keyID)
    51  }
    52  
    53  func (a *APIAccess) GetInsightsInsertKeyWithContext(ctx context.Context, accountID int, keyID int) (*InsightsKey, error) {
    54  	key := InsightsKey{}
    55  	url := a.config.Region().InsightsKeysURL(accountID, fmt.Sprintf("insert_keys/%d?format=json", keyID))
    56  	_, err := a.insightsKeysClient.GetWithContext(ctx, url, nil, &key)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return &key, nil
    62  }
    63  
    64  func (a *APIAccess) DeleteInsightsInsertKey(accountID int, keyID int) (*InsightsKey, error) {
    65  	return a.DeleteInsightsInsertKeyWithContext(context.Background(), accountID, keyID)
    66  }
    67  
    68  func (a *APIAccess) DeleteInsightsInsertKeyWithContext(ctx context.Context, accountID int, keyID int) (*InsightsKey, error) {
    69  	key := InsightsKey{}
    70  	url := a.config.Region().InsightsKeysURL(accountID, fmt.Sprintf("insert_keys/%d?format=json", keyID))
    71  	_, err := a.insightsKeysClient.DeleteWithContext(ctx, url, nil, &key)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	return &key, nil
    77  }
    78  
    79  func (a *APIAccess) ListInsightsQueryKeys(accountID int) ([]InsightsKey, error) {
    80  	return a.ListInsightsQueryKeysWithContext(context.Background(), accountID)
    81  }
    82  
    83  func (a *APIAccess) ListInsightsQueryKeysWithContext(ctx context.Context, accountID int) ([]InsightsKey, error) {
    84  	keys := []InsightsKey{}
    85  	url := a.config.Region().InsightsKeysURL(accountID, "query_keys?format=json")
    86  	_, err := a.insightsKeysClient.GetWithContext(ctx, url, nil, &keys)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	return keys, nil
    92  }
    93  
    94  func (a *APIAccess) CreateInsightsQueryKey(accountID int) (*InsightsKey, error) {
    95  	return a.CreateInsightsQueryKeyWithContext(context.Background(), accountID)
    96  }
    97  
    98  func (a *APIAccess) CreateInsightsQueryKeyWithContext(ctx context.Context, accountID int) (*InsightsKey, error) {
    99  	key := InsightsKey{}
   100  	url := a.config.Region().InsightsKeysURL(accountID, "query_keys?format=json")
   101  	_, err := a.insightsKeysClient.PostWithContext(ctx, url, nil, nil, &key)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	return &key, nil
   107  }
   108  
   109  func (a *APIAccess) GetInsightsQueryKey(accountID int, keyID int) (*InsightsKey, error) {
   110  	return a.GetInsightsQueryKeyWithContext(context.Background(), accountID, keyID)
   111  }
   112  
   113  func (a *APIAccess) GetInsightsQueryKeyWithContext(ctx context.Context, accountID int, keyID int) (*InsightsKey, error) {
   114  	key := InsightsKey{}
   115  	url := a.config.Region().InsightsKeysURL(accountID, fmt.Sprintf("query_keys/%d?format=json", keyID))
   116  	_, err := a.insightsKeysClient.GetWithContext(ctx, url, nil, &key)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	return &key, nil
   122  }
   123  
   124  func (a *APIAccess) DeleteInsightsQueryKey(accountID int, keyID int) (*InsightsKey, error) {
   125  	return a.DeleteInsightsQueryKeyWithContext(context.Background(), accountID, keyID)
   126  }
   127  
   128  func (a *APIAccess) DeleteInsightsQueryKeyWithContext(ctx context.Context, accountID int, keyID int) (*InsightsKey, error) {
   129  	key := InsightsKey{}
   130  	url := a.config.Region().InsightsKeysURL(accountID, fmt.Sprintf("query_keys/%d?format=json", keyID))
   131  	_, err := a.insightsKeysClient.DeleteWithContext(ctx, url, nil, &key)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	return &key, nil
   137  }