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

     1  package nerdstorage
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  
     9  	"github.com/mitchellh/mapstructure"
    10  
    11  	"github.com/newrelic/newrelic-client-go/pkg/config"
    12  )
    13  
    14  type EntityScopedDoc struct {
    15  	MyField string
    16  }
    17  
    18  func Example_entityScope() {
    19  	// Initialize the client configuration.  A Personal API key is required to
    20  	// communicate with the backend API.
    21  	cfg := config.New()
    22  	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")
    23  
    24  	// Initialize the client.
    25  	client := New(cfg)
    26  
    27  	entityID := "MjUyMDUyOHxFUE18QVBQTElDQVRJT058MjE1MDM3Nzk1"
    28  	packageID := "ecaeb28c-7b3f-4932-9e33-7385980efa1c"
    29  
    30  	// Write a NerdStorage document with account scope.
    31  	writeDocumentInput := WriteDocumentInput{
    32  		PackageID:  packageID,
    33  		Collection: "myCol",
    34  		DocumentID: "myDoc",
    35  		Document: AccountScopedDoc{
    36  			MyField: "myValue",
    37  		},
    38  	}
    39  
    40  	_, err := client.WriteDocumentWithEntityScope(entityID, writeDocumentInput)
    41  	if err != nil {
    42  		log.Fatal("error writing NerdStorage document:", err)
    43  	}
    44  
    45  	// Write a second NerdStorage document to the same collection with account scope.
    46  	writeAlternateDocumentInput := writeDocumentInput
    47  	writeAlternateDocumentInput.DocumentID = "myOtherDoc"
    48  
    49  	_, err = client.WriteDocumentWithEntityScope(entityID, writeAlternateDocumentInput)
    50  	if err != nil {
    51  		log.Fatal("error writing NerdStorage document:", err)
    52  	}
    53  
    54  	// Get a NerdStorage collection with account scope.
    55  	getCollectionInput := GetCollectionInput{
    56  		PackageID:  packageID,
    57  		Collection: "myCol",
    58  	}
    59  
    60  	collection, err := client.GetCollectionWithEntityScope(entityID, getCollectionInput)
    61  	if err != nil {
    62  		log.Fatal("error retrieving NerdStorage collection:", err)
    63  	}
    64  
    65  	fmt.Printf("Collection length: %v\n", len(collection))
    66  
    67  	// Get a NerdStorage document with account scope.
    68  	getDocumentInput := GetDocumentInput{
    69  		PackageID:  packageID,
    70  		Collection: "myCol",
    71  		DocumentID: "myDoc",
    72  	}
    73  
    74  	rawDoc, err := client.GetDocumentWithEntityScope(entityID, getDocumentInput)
    75  	if err != nil {
    76  		log.Fatal("error retrieving NerdStorage document:", err)
    77  	}
    78  
    79  	// Convert the document to a struct.
    80  	var myDoc EntityScopedDoc
    81  
    82  	// Method 1:
    83  	marshalled, err := json.Marshal(rawDoc)
    84  	if err != nil {
    85  		log.Fatal("error marshalling NerdStorage document to json:", err)
    86  	}
    87  
    88  	err = json.Unmarshal(marshalled, &myDoc)
    89  	if err != nil {
    90  		log.Fatal("error unmarshalling NerdStorage document to struct:", err)
    91  	}
    92  
    93  	fmt.Printf("Document: %v\n", myDoc)
    94  
    95  	// Method 2:
    96  	err = mapstructure.Decode(rawDoc, &myDoc)
    97  	if err != nil {
    98  		log.Fatal("error converting NerdStorage document to struct:", err)
    99  	}
   100  
   101  	fmt.Printf("Document: %v\n", myDoc)
   102  
   103  	// Delete a NerdStorage document with account scope.
   104  	deleteDocumentInput := DeleteDocumentInput{
   105  		PackageID:  packageID,
   106  		Collection: "myCol",
   107  		DocumentID: "myDoc",
   108  	}
   109  
   110  	ok, err := client.DeleteDocumentWithEntityScope(entityID, deleteDocumentInput)
   111  
   112  	if !ok || err != nil {
   113  		log.Fatal("error deleting NerdStorage document:", err)
   114  	}
   115  
   116  	// Delete a NerdStorage collection with account scope.
   117  	deleteCollectionInput := DeleteCollectionInput{
   118  		PackageID:  packageID,
   119  		Collection: "myCol",
   120  	}
   121  
   122  	deleted, err := client.DeleteCollectionWithEntityScope(entityID, deleteCollectionInput)
   123  	if err != nil {
   124  		log.Fatal("error deleting NerdStorage collection:", err)
   125  	}
   126  
   127  	if !deleted {
   128  		// NerdStorage collections are auto-deleted when their last remaining document is deleted.
   129  		log.Println("deletion was not necessary, collection might have already been deleted", err)
   130  	}
   131  }