nerdstorage

package
v0.77.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: Apache-2.0 Imports: 7 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

Code generated by tutone: DO NOT EDIT

Example (AccountScope)
package main

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

	"github.com/mitchellh/mapstructure"

	"github.com/newrelic/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/newrelic/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/newrelic/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

View Source
var NerdStorageScopeTypes = struct {
	// Account-level storage.
	ACCOUNT NerdStorageScope
	// Actor-level storage.
	ACTOR NerdStorageScope
	// Entity-level storage.
	ENTITY NerdStorageScope
}{

	ACCOUNT: "ACCOUNT",

	ACTOR: "ACTOR",

	ENTITY: "ENTITY",
}

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) DeleteCollectionWithAccountScopeWithContext added in v0.60.0

func (e *NerdStorage) DeleteCollectionWithAccountScopeWithContext(ctx context.Context, accountID int, input DeleteCollectionInput) (bool, error)

DeleteCollectionWithAccountScopeWithContext 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) DeleteCollectionWithEntityScopeWithContext added in v0.60.0

func (e *NerdStorage) DeleteCollectionWithEntityScopeWithContext(ctx context.Context, entityGUID string, input DeleteCollectionInput) (bool, error)

DeleteCollectionWithEntityScopeWithContext 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) DeleteCollectionWithUserScopeWithContext added in v0.60.0

func (e *NerdStorage) DeleteCollectionWithUserScopeWithContext(ctx context.Context, input DeleteCollectionInput) (bool, error)

DeleteCollectionWithUserScopeWithContext 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) DeleteDocumentWithAccountScopeWithContext added in v0.60.0

func (e *NerdStorage) DeleteDocumentWithAccountScopeWithContext(ctx context.Context, accountID int, input DeleteDocumentInput) (bool, error)

DeleteDocumentWithAccountScopeWithContext 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) DeleteDocumentWithEntityScopeWithContext added in v0.60.0

func (e *NerdStorage) DeleteDocumentWithEntityScopeWithContext(ctx context.Context, entityGUID string, input DeleteDocumentInput) (bool, error)

DeleteDocumentWithEntityScopeWithContext 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) DeleteDocumentWithUserScopeWithContext added in v0.60.0

func (e *NerdStorage) DeleteDocumentWithUserScopeWithContext(ctx context.Context, input DeleteDocumentInput) (bool, error)

DeleteDocumentWithUserScopeWithContext 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) GetCollectionWithAccountScopeWithContext added in v0.60.0

func (e *NerdStorage) GetCollectionWithAccountScopeWithContext(ctx context.Context, accountID int, input GetCollectionInput) ([]interface{}, error)

GetCollectionWithAccountScopeWithContext 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) GetCollectionWithEntityScopeWithContext added in v0.60.0

func (e *NerdStorage) GetCollectionWithEntityScopeWithContext(ctx context.Context, entityGUID string, input GetCollectionInput) ([]interface{}, error)

GetCollectionWithEntityScopeWithContext 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) GetCollectionWithUserScopeWithContext added in v0.60.0

func (e *NerdStorage) GetCollectionWithUserScopeWithContext(ctx context.Context, input GetCollectionInput) ([]interface{}, error)

GetCollectionWithUserScopeWithContext 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) GetDocumentWithAccountScopeWithContext added in v0.60.0

func (e *NerdStorage) GetDocumentWithAccountScopeWithContext(ctx context.Context, accountID int, input GetDocumentInput) (interface{}, error)

GetDocumentWithAccountScopeWithContext 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) GetDocumentWithEntityScopeWithContext added in v0.60.0

func (e *NerdStorage) GetDocumentWithEntityScopeWithContext(ctx context.Context, entityGUID string, input GetDocumentInput) (interface{}, error)

GetDocumentWithEntityScopeWithContext 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) GetDocumentWithUserScopeWithContext added in v0.60.0

func (e *NerdStorage) GetDocumentWithUserScopeWithContext(ctx context.Context, input GetDocumentInput) (interface{}, error)

GetDocumentWithUserScopeWithContext 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) WriteDocumentWithAccountScopeWithContext added in v0.60.0

func (e *NerdStorage) WriteDocumentWithAccountScopeWithContext(ctx context.Context, accountID int, input WriteDocumentInput) (interface{}, error)

WriteDocumentWithAccountScopeWithContext 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) WriteDocumentWithEntityScopeWithContext added in v0.60.0

func (e *NerdStorage) WriteDocumentWithEntityScopeWithContext(ctx context.Context, entityGUID string, input WriteDocumentInput) (interface{}, error)

WriteDocumentWithEntityScopeWithContext 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.

func (*NerdStorage) WriteDocumentWithUserScopeWithContext added in v0.60.0

func (e *NerdStorage) WriteDocumentWithUserScopeWithContext(ctx context.Context, input WriteDocumentInput) (interface{}, error)

WriteDocumentWithUserScopeWithContext writes a NerdStorage document with user scope.

type NerdStorageCollectionMember added in v0.53.0

type NerdStorageCollectionMember struct {
	// The NerdStorage document.
	Document NerdStorageDocument `json:"document,omitempty"`
	// The documentId.
	ID string `json:"id,omitempty"`
}

NerdStorageCollectionMember -

type NerdStorageDocument added in v0.53.0

type NerdStorageDocument string

NerdStorageDocument - This scalar represents a NerdStorage document.

type NerdStorageEntityScope added in v0.53.0

type NerdStorageEntityScope struct {
	//
	Collection []NerdStorageCollectionMember `json:"collection,omitempty"`
	//
	Document NerdStorageDocument `json:"document,omitempty"`
}

NerdStorageEntityScope -

type NerdStorageScope added in v0.53.0

type NerdStorageScope string

NerdStorageScope - The access level of the NerdStorage data.

type NerdStorageScopeInput added in v0.53.0

type NerdStorageScopeInput struct {
	// The ID for the selected scope.
	ID string `json:"id"`
	// The NerdStorage data access level.
	Name NerdStorageScope `json:"name"`
}

NerdStorageScopeInput - The data access level and ID for the selected 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