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