github.com/newrelic/newrelic-client-go@v1.1.0/pkg/nerdstorage/example_user_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 UserScopedDoc struct {
    15  	MyField string
    16  }
    17  
    18  func Example_userScope() {
    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  	packageID := "ecaeb28c-7b3f-4932-9e33-7385980efa1c"
    28  
    29  	// Write a NerdStorage document with account scope.
    30  	writeDocumentInput := WriteDocumentInput{
    31  		PackageID:  packageID,
    32  		Collection: "myCol",
    33  		DocumentID: "myDoc",
    34  		Document: UserScopedDoc{
    35  			MyField: "myValue",
    36  		},
    37  	}
    38  
    39  	_, err := client.WriteDocumentWithUserScope(writeDocumentInput)
    40  	if err != nil {
    41  		log.Fatal("error writing NerdStorage document:", err)
    42  	}
    43  
    44  	// Write a second NerdStorage document to the same collection with account scope.
    45  	writeAlternateDocumentInput := writeDocumentInput
    46  	writeAlternateDocumentInput.DocumentID = "myOtherDoc"
    47  
    48  	_, err = client.WriteDocumentWithUserScope(writeAlternateDocumentInput)
    49  	if err != nil {
    50  		log.Fatal("error writing NerdStorage document:", err)
    51  	}
    52  
    53  	// Get a NerdStorage collection with account scope.
    54  	getCollectionInput := GetCollectionInput{
    55  		PackageID:  packageID,
    56  		Collection: "myCol",
    57  	}
    58  
    59  	collection, err := client.GetCollectionWithUserScope(getCollectionInput)
    60  	if err != nil {
    61  		log.Fatal("error retrieving NerdStorage collection:", err)
    62  	}
    63  
    64  	fmt.Printf("Collection length: %v\n", len(collection))
    65  
    66  	// Get a NerdStorage document with account scope.
    67  	getDocumentInput := GetDocumentInput{
    68  		PackageID:  packageID,
    69  		Collection: "myCol",
    70  		DocumentID: "myDoc",
    71  	}
    72  
    73  	rawDoc, err := client.GetDocumentWithUserScope(getDocumentInput)
    74  	if err != nil {
    75  		log.Fatal("error retrieving NerdStorage document:", err)
    76  	}
    77  
    78  	// Convert the document to a struct.
    79  	var myDoc UserScopedDoc
    80  
    81  	// Method 1:
    82  	marshalled, err := json.Marshal(rawDoc)
    83  	if err != nil {
    84  		log.Fatal("error marshalling NerdStorage document to json:", err)
    85  	}
    86  
    87  	err = json.Unmarshal(marshalled, &myDoc)
    88  	if err != nil {
    89  		log.Fatal("error unmarshalling NerdStorage document to struct:", err)
    90  	}
    91  
    92  	fmt.Printf("Document: %v\n", myDoc)
    93  
    94  	// Method 2:
    95  	err = mapstructure.Decode(rawDoc, &myDoc)
    96  	if err != nil {
    97  		log.Fatal("error converting NerdStorage document to struct:", err)
    98  	}
    99  
   100  	fmt.Printf("Document: %v\n", myDoc)
   101  
   102  	// Delete a NerdStorage document with account scope.
   103  	deleteDocumentInput := DeleteDocumentInput{
   104  		PackageID:  packageID,
   105  		Collection: "myCol",
   106  		DocumentID: "myDoc",
   107  	}
   108  
   109  	ok, err := client.DeleteDocumentWithUserScope(deleteDocumentInput)
   110  
   111  	if !ok || err != nil {
   112  		log.Fatal("error deleting NerdStorage document:", err)
   113  	}
   114  
   115  	// Delete a NerdStorage collection with account scope.
   116  	deleteCollectionInput := DeleteCollectionInput{
   117  		PackageID:  packageID,
   118  		Collection: "myCol",
   119  	}
   120  
   121  	deleted, err := client.DeleteCollectionWithUserScope(deleteCollectionInput)
   122  	if err != nil {
   123  		log.Fatal("error deleting NerdStorage collection:", err)
   124  	}
   125  
   126  	if !deleted {
   127  		// NerdStorage collections are auto-deleted when their last remaining document is deleted.
   128  		log.Println("deletion was not necessary, collection might have already been deleted", err)
   129  	}
   130  }