github.com/newrelic/newrelic-client-go@v1.1.0/pkg/cloud/example_cloud_account_test.go (about)

     1  package cloud
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/newrelic/newrelic-client-go/pkg/config"
     9  )
    10  
    11  func Example_cloudAccounts() {
    12  	// Initialize the client configuration.  A Personal API key is required to
    13  	// communicate with the backend API.
    14  	cfg := config.New()
    15  	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")
    16  
    17  	// Initialize the client.
    18  	client := New(cfg)
    19  
    20  	envAccountID := os.Getenv("NEW_RELIC_ACCOUNT_ID")
    21  	accountID, err := strconv.Atoi(envAccountID)
    22  	if err != nil {
    23  		log.Fatal("must set NEW_RELIC_ACCOUNT_ID")
    24  	}
    25  
    26  	// Get the linked cloud accounts
    27  	linkedAccounts, err := client.GetLinkedAccounts("aws")
    28  	if err != nil {
    29  		log.Fatal("error retrieving linked accounts:", err)
    30  	}
    31  
    32  	log.Printf("linked accounts count: %d", len(*linkedAccounts))
    33  
    34  	// Link a cloud account
    35  	linkResponse, err := client.CloudLinkAccount(accountID, CloudLinkCloudAccountsInput{
    36  		Aws: []CloudAwsLinkAccountInput{
    37  			{
    38  				Arn:  "arn:aws:iam::12345678:role/MyAWSARN",
    39  				Name: "My Linked AWS Account",
    40  			},
    41  		},
    42  	})
    43  	if err != nil || len(linkResponse.LinkedAccounts) != 1 {
    44  		log.Fatal("error linking cloud account:", err)
    45  	}
    46  
    47  	linkedAccountID := linkResponse.LinkedAccounts[0].ID
    48  
    49  	// Rename a linked account
    50  	_, err = client.CloudRenameAccount(accountID, []CloudRenameAccountsInput{
    51  		{
    52  			LinkedAccountId: linkedAccountID,
    53  			Name:            "My Renamed Linked AWS Account",
    54  		},
    55  	})
    56  	if err != nil {
    57  		log.Fatal("error renaming linked cloud account:", err)
    58  	}
    59  
    60  	// Unlink a linked account
    61  	_, err = client.CloudUnlinkAccount(accountID, []CloudUnlinkAccountsInput{{linkedAccountID}})
    62  	if err != nil {
    63  		log.Fatal("error unlinking linked cloud account:", err)
    64  	}
    65  }