nerdstorage

package
v0.47.2-0...-23ee662 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package nerdstorage provides a programmatic API for interacting with the New Relic One NerdStorage document store. It can be used to write NerdStorage documents into collections and to delete documents and collections.

More information about the NerdStorage document store can be found in the online documentation:

https://developer.newrelic.com/build-tools/new-relic-one-applications/nerdstorage

Authentication

You will need a valid Personal API key to communicate with the backend New Relic API that provides this functionality. See the API key documentation below for more information on how to locate this key:

https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys

Example (AccountScope)
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"strconv"

	"github.com/mitchellh/mapstructure"

	"github.com/joeyparsons/newrelic-client-go/pkg/config"
)

type AccountScopedDoc struct {
	MyField string
}

func main() {
	// Initialize the client configuration.  A Personal API key is required to
	// communicate with the backend API.
	cfg := config.New()
	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

	// Initialize the client.
	client := New(cfg)

	accountID, err := strconv.Atoi(os.Getenv("NEW_RELIC_ACCOUNT_ID"))
	if err != nil {
		log.Fatal("error parsing account ID", err)
	}

	packageID := "ecaeb28c-7b3f-4932-9e33-7385980efa1c"

	// Write a NerdStorage document with account scope.
	writeDocumentInput := WriteDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
		Document: AccountScopedDoc{
			MyField: "myValue",
		},
	}

	_, err = client.WriteDocumentWithAccountScope(accountID, writeDocumentInput)
	if err != nil {
		log.Fatal("error writing NerdStorage document:", err)
	}

	// Write a second NerdStorage document to the same collection with account scope.
	writeAlternateDocumentInput := writeDocumentInput
	writeAlternateDocumentInput.DocumentID = "myOtherDoc"

	_, err = client.WriteDocumentWithAccountScope(accountID, writeAlternateDocumentInput)
	if err != nil {
		log.Fatal("error writing NerdStorage document:", err)
	}

	// Get a NerdStorage collection with account scope.
	getCollectionInput := GetCollectionInput{
		PackageID:  packageID,
		Collection: "myCol",
	}

	collection, err := client.GetCollectionWithAccountScope(accountID, getCollectionInput)
	if err != nil {
		log.Fatal("error retrieving NerdStorage collection:", err)
	}

	fmt.Printf("Collection length: %v\n", len(collection))

	// Get a NerdStorage document with account scope.
	getDocumentInput := GetDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
	}

	rawDoc, err := client.GetDocumentWithAccountScope(accountID, getDocumentInput)
	if err != nil {
		log.Fatal("error retrieving NerdStorage document:", err)
	}

	// Convert the document to a struct.
	var myDoc AccountScopedDoc

	// Method 1:
	marshalled, err := json.Marshal(rawDoc)
	if err != nil {
		log.Fatal("error marshalling NerdStorage document to json:", err)
	}

	err = json.Unmarshal(marshalled, &myDoc)
	if err != nil {
		log.Fatal("error unmarshalling NerdStorage document to struct:", err)
	}

	fmt.Printf("Document: %v\n", myDoc)

	// Method 2:
	err = mapstructure.Decode(rawDoc, &myDoc)
	if err != nil {
		log.Fatal("error converting NerdStorage document to struct:", err)
	}

	fmt.Printf("Document: %v\n", myDoc)

	// Delete a NerdStorage document with account scope.
	deleteDocumentInput := DeleteDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
	}

	ok, err := client.DeleteDocumentWithAccountScope(accountID, deleteDocumentInput)

	if !ok || err != nil {
		log.Fatal("error deleting NerdStorage document:", err)
	}

	// Delete a NerdStorage collection with account scope.
	deleteCollectionInput := DeleteCollectionInput{
		PackageID:  packageID,
		Collection: "myCol",
	}

	deleted, err := client.DeleteCollectionWithAccountScope(accountID, deleteCollectionInput)
	if err != nil {
		log.Fatal("error deleting NerdStorage collection:", err)
	}

	if !deleted {
		// NerdStorage collections are auto-deleted when their last remaining document is deleted.
		log.Println("deletion was not necessary, collection might have already been deleted", err)
	}
}
Output:

Example (EntityScope)
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/mitchellh/mapstructure"

	"github.com/joeyparsons/newrelic-client-go/pkg/config"
)

type EntityScopedDoc struct {
	MyField string
}

func main() {
	// Initialize the client configuration.  A Personal API key is required to
	// communicate with the backend API.
	cfg := config.New()
	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

	// Initialize the client.
	client := New(cfg)

	entityID := "MjUyMDUyOHxFUE18QVBQTElDQVRJT058MjE1MDM3Nzk1"
	packageID := "ecaeb28c-7b3f-4932-9e33-7385980efa1c"

	// Write a NerdStorage document with account scope.
	writeDocumentInput := WriteDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
		Document: AccountScopedDoc{
			MyField: "myValue",
		},
	}

	_, err := client.WriteDocumentWithEntityScope(entityID, writeDocumentInput)
	if err != nil {
		log.Fatal("error writing NerdStorage document:", err)
	}

	// Write a second NerdStorage document to the same collection with account scope.
	writeAlternateDocumentInput := writeDocumentInput
	writeAlternateDocumentInput.DocumentID = "myOtherDoc"

	_, err = client.WriteDocumentWithEntityScope(entityID, writeAlternateDocumentInput)
	if err != nil {
		log.Fatal("error writing NerdStorage document:", err)
	}

	// Get a NerdStorage collection with account scope.
	getCollectionInput := GetCollectionInput{
		PackageID:  packageID,
		Collection: "myCol",
	}

	collection, err := client.GetCollectionWithEntityScope(entityID, getCollectionInput)
	if err != nil {
		log.Fatal("error retrieving NerdStorage collection:", err)
	}

	fmt.Printf("Collection length: %v\n", len(collection))

	// Get a NerdStorage document with account scope.
	getDocumentInput := GetDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
	}

	rawDoc, err := client.GetDocumentWithEntityScope(entityID, getDocumentInput)
	if err != nil {
		log.Fatal("error retrieving NerdStorage document:", err)
	}

	// Convert the document to a struct.
	var myDoc EntityScopedDoc

	// Method 1:
	marshalled, err := json.Marshal(rawDoc)
	if err != nil {
		log.Fatal("error marshalling NerdStorage document to json:", err)
	}

	err = json.Unmarshal(marshalled, &myDoc)
	if err != nil {
		log.Fatal("error unmarshalling NerdStorage document to struct:", err)
	}

	fmt.Printf("Document: %v\n", myDoc)

	// Method 2:
	err = mapstructure.Decode(rawDoc, &myDoc)
	if err != nil {
		log.Fatal("error converting NerdStorage document to struct:", err)
	}

	fmt.Printf("Document: %v\n", myDoc)

	// Delete a NerdStorage document with account scope.
	deleteDocumentInput := DeleteDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
	}

	ok, err := client.DeleteDocumentWithEntityScope(entityID, deleteDocumentInput)

	if !ok || err != nil {
		log.Fatal("error deleting NerdStorage document:", err)
	}

	// Delete a NerdStorage collection with account scope.
	deleteCollectionInput := DeleteCollectionInput{
		PackageID:  packageID,
		Collection: "myCol",
	}

	deleted, err := client.DeleteCollectionWithEntityScope(entityID, deleteCollectionInput)
	if err != nil {
		log.Fatal("error deleting NerdStorage collection:", err)
	}

	if !deleted {
		// NerdStorage collections are auto-deleted when their last remaining document is deleted.
		log.Println("deletion was not necessary, collection might have already been deleted", err)
	}
}
Output:

Example (UserScope)
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/mitchellh/mapstructure"

	"github.com/joeyparsons/newrelic-client-go/pkg/config"
)

type UserScopedDoc struct {
	MyField string
}

func main() {
	// Initialize the client configuration.  A Personal API key is required to
	// communicate with the backend API.
	cfg := config.New()
	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

	// Initialize the client.
	client := New(cfg)

	packageID := "ecaeb28c-7b3f-4932-9e33-7385980efa1c"

	// Write a NerdStorage document with account scope.
	writeDocumentInput := WriteDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
		Document: UserScopedDoc{
			MyField: "myValue",
		},
	}

	_, err := client.WriteDocumentWithUserScope(writeDocumentInput)
	if err != nil {
		log.Fatal("error writing NerdStorage document:", err)
	}

	// Write a second NerdStorage document to the same collection with account scope.
	writeAlternateDocumentInput := writeDocumentInput
	writeAlternateDocumentInput.DocumentID = "myOtherDoc"

	_, err = client.WriteDocumentWithUserScope(writeAlternateDocumentInput)
	if err != nil {
		log.Fatal("error writing NerdStorage document:", err)
	}

	// Get a NerdStorage collection with account scope.
	getCollectionInput := GetCollectionInput{
		PackageID:  packageID,
		Collection: "myCol",
	}

	collection, err := client.GetCollectionWithUserScope(getCollectionInput)
	if err != nil {
		log.Fatal("error retrieving NerdStorage collection:", err)
	}

	fmt.Printf("Collection length: %v\n", len(collection))

	// Get a NerdStorage document with account scope.
	getDocumentInput := GetDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
	}

	rawDoc, err := client.GetDocumentWithUserScope(getDocumentInput)
	if err != nil {
		log.Fatal("error retrieving NerdStorage document:", err)
	}

	// Convert the document to a struct.
	var myDoc UserScopedDoc

	// Method 1:
	marshalled, err := json.Marshal(rawDoc)
	if err != nil {
		log.Fatal("error marshalling NerdStorage document to json:", err)
	}

	err = json.Unmarshal(marshalled, &myDoc)
	if err != nil {
		log.Fatal("error unmarshalling NerdStorage document to struct:", err)
	}

	fmt.Printf("Document: %v\n", myDoc)

	// Method 2:
	err = mapstructure.Decode(rawDoc, &myDoc)
	if err != nil {
		log.Fatal("error converting NerdStorage document to struct:", err)
	}

	fmt.Printf("Document: %v\n", myDoc)

	// Delete a NerdStorage document with account scope.
	deleteDocumentInput := DeleteDocumentInput{
		PackageID:  packageID,
		Collection: "myCol",
		DocumentID: "myDoc",
	}

	ok, err := client.DeleteDocumentWithUserScope(deleteDocumentInput)

	if !ok || err != nil {
		log.Fatal("error deleting NerdStorage document:", err)
	}

	// Delete a NerdStorage collection with account scope.
	deleteCollectionInput := DeleteCollectionInput{
		PackageID:  packageID,
		Collection: "myCol",
	}

	deleted, err := client.DeleteCollectionWithUserScope(deleteCollectionInput)
	if err != nil {
		log.Fatal("error deleting NerdStorage collection:", err)
	}

	if !deleted {
		// NerdStorage collections are auto-deleted when their last remaining document is deleted.
		log.Println("deletion was not necessary, collection might have already been deleted", err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeleteCollectionInput

type DeleteCollectionInput struct {
	Collection string
	PackageID  string
}

DeleteCollectionInput represents the input data required for the DeleteCollection mutation.

type DeleteDocumentInput

type DeleteDocumentInput struct {
	Collection string
	DocumentID string
	PackageID  string
}

DeleteDocumentInput represents the input data required for the DeleteDocument mutation.

type GetCollectionInput

type GetCollectionInput struct {
	Collection string
	PackageID  string
}

GetCollectionInput represents the input data required for retrieving a NerdStorage collection.

type GetDocumentInput

type GetDocumentInput struct {
	Collection string
	DocumentID string
	PackageID  string
}

GetDocumentInput represents the input data required for retrieving a NerdStorage document.

type NerdStorage

type NerdStorage struct {
	// contains filtered or unexported fields
}

NerdStorage is used to communicate with the New Relic Workloads product.

func New

func New(config config.Config) NerdStorage

New returns a new client for interacting with the New Relic One NerdStorage document store.

func (*NerdStorage) DeleteCollectionWithAccountScope

func (e *NerdStorage) DeleteCollectionWithAccountScope(accountID int, input DeleteCollectionInput) (bool, error)

DeleteCollectionWithAccountScope deletes a NerdStorage collection with account scope.

func (*NerdStorage) DeleteCollectionWithEntityScope

func (e *NerdStorage) DeleteCollectionWithEntityScope(entityGUID string, input DeleteCollectionInput) (bool, error)

DeleteCollectionWithEntityScope deletes a NerdStorage collection with entity scope.

func (*NerdStorage) DeleteCollectionWithUserScope

func (e *NerdStorage) DeleteCollectionWithUserScope(input DeleteCollectionInput) (bool, error)

DeleteCollectionWithUserScope deletes a NerdStorage collection with user scope.

func (*NerdStorage) DeleteDocumentWithAccountScope

func (e *NerdStorage) DeleteDocumentWithAccountScope(accountID int, input DeleteDocumentInput) (bool, error)

DeleteDocumentWithAccountScope deletes a NerdStorage document with account scope.

func (*NerdStorage) DeleteDocumentWithEntityScope

func (e *NerdStorage) DeleteDocumentWithEntityScope(entityGUID string, input DeleteDocumentInput) (bool, error)

DeleteDocumentWithEntityScope deletes a NerdStorage document with entity scope.

func (*NerdStorage) DeleteDocumentWithUserScope

func (e *NerdStorage) DeleteDocumentWithUserScope(input DeleteDocumentInput) (bool, error)

DeleteDocumentWithUserScope deletes a NerdStorage document with user scope.

func (*NerdStorage) GetCollectionWithAccountScope

func (e *NerdStorage) GetCollectionWithAccountScope(accountID int, input GetCollectionInput) ([]interface{}, error)

GetCollectionWithAccountScope retrieves a NerdStorage collection with account scope.

func (*NerdStorage) GetCollectionWithEntityScope

func (e *NerdStorage) GetCollectionWithEntityScope(entityGUID string, input GetCollectionInput) ([]interface{}, error)

GetCollectionWithEntityScope wretrieves a NerdStorage collection with entity scope.

func (*NerdStorage) GetCollectionWithUserScope

func (e *NerdStorage) GetCollectionWithUserScope(input GetCollectionInput) ([]interface{}, error)

GetCollectionWithUserScope retrieves a NerdStorage collection with user scope.

func (*NerdStorage) GetDocumentWithAccountScope

func (e *NerdStorage) GetDocumentWithAccountScope(accountID int, input GetDocumentInput) (interface{}, error)

GetDocumentWithAccountScope retrieves a NerdStorage document with account scope.

func (*NerdStorage) GetDocumentWithEntityScope

func (e *NerdStorage) GetDocumentWithEntityScope(entityGUID string, input GetDocumentInput) (interface{}, error)

GetDocumentWithEntityScope retrieves a NerdStorage document with entity scope.

func (*NerdStorage) GetDocumentWithUserScope

func (e *NerdStorage) GetDocumentWithUserScope(input GetDocumentInput) (interface{}, error)

GetDocumentWithUserScope retrieves a NerdStorage document with user scope.

func (*NerdStorage) WriteDocumentWithAccountScope

func (e *NerdStorage) WriteDocumentWithAccountScope(accountID int, input WriteDocumentInput) (interface{}, error)

WriteDocumentWithAccountScope writes a NerdStorage document with account scope.

func (*NerdStorage) WriteDocumentWithEntityScope

func (e *NerdStorage) WriteDocumentWithEntityScope(entityGUID string, input WriteDocumentInput) (interface{}, error)

WriteDocumentWithEntityScope writes a NerdStorage document with entity scope.

func (*NerdStorage) WriteDocumentWithUserScope

func (e *NerdStorage) WriteDocumentWithUserScope(input WriteDocumentInput) (interface{}, error)

WriteDocumentWithUserScope writes a NerdStorage document with user scope.

type WriteDocumentInput

type WriteDocumentInput struct {
	Collection string
	Document   interface{}
	DocumentID string
	PackageID  string
}

WriteDocumentInput represents the input data required for the WriteDocument mutation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL