github.com/newrelic/newrelic-client-go@v1.1.0/pkg/entities/example_entity_test.go (about)

     1  package entities
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/newrelic/newrelic-client-go/pkg/common"
     9  	"github.com/newrelic/newrelic-client-go/pkg/config"
    10  )
    11  
    12  func Example_entity() {
    13  	// Initialize the client configuration.  A Personal API key is required to
    14  	// communicate with the backend API.
    15  	cfg := config.New()
    16  	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")
    17  
    18  	// Initialize the client.
    19  	client := New(cfg)
    20  
    21  	// Search the current account for entities by name and type.
    22  	queryBuilder := EntitySearchQueryBuilder{
    23  		Name: "Example entity",
    24  		Type: EntitySearchQueryBuilderTypeTypes.APPLICATION,
    25  	}
    26  
    27  	entitySearch, err := client.GetEntitySearch(
    28  		EntitySearchOptions{},
    29  		"",
    30  		queryBuilder,
    31  		[]EntitySearchSortCriteria{},
    32  	)
    33  	if err != nil {
    34  		log.Fatal("error searching entities:", err)
    35  	}
    36  
    37  	// Get several entities by GUID.
    38  	var entityGuids []common.EntityGUID
    39  	for _, x := range entitySearch.Results.Entities {
    40  		e := x.(*GenericEntityOutline)
    41  		entityGuids = append(entityGuids, e.GUID)
    42  	}
    43  
    44  	entities, err := client.GetEntities(entityGuids)
    45  	if err != nil {
    46  		log.Fatal("error getting entities:", err)
    47  	}
    48  	fmt.Printf("GetEntities returned %d entities", len((*entities)))
    49  
    50  	// Get an entity by GUID.
    51  	entity, err := client.GetEntity(entityGuids[0])
    52  	if err != nil {
    53  		log.Fatal("error getting entity:", err)
    54  	}
    55  
    56  	// Output the entity's URL.
    57  	fmt.Printf("Entity name: %s, URL: %s\n", (*entity).(*GenericEntity).Name, (*entity).(*GenericEntity).Permalink)
    58  }