github.com/newrelic/newrelic-client-go@v1.1.0/pkg/cloud/cloud_api_integration_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package cloud 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers" 13 ) 14 15 func TestCloudAccount_Basic(t *testing.T) { 16 t.Parallel() 17 18 testAccountID, err := mock.GetTestAccountID() 19 if err != nil { 20 t.Skipf("%s", err) 21 } 22 23 testARN := os.Getenv("INTEGRATION_TESTING_AWS_ARN") 24 if testARN == "" { 25 t.Skip("an AWS ARN is required to run cloud account tests") 26 return 27 } 28 29 a := newIntegrationTestClient(t) 30 // Reset everything 31 getResponse, err := a.GetLinkedAccounts("aws") 32 require.NoError(t, err) 33 34 for _, linkedAccount := range *getResponse { 35 if linkedAccount.NrAccountId == testAccountID { 36 a.CloudUnlinkAccount(testAccountID, []CloudUnlinkAccountsInput{ 37 { 38 LinkedAccountId: linkedAccount.ID, 39 }, 40 }) 41 } 42 } 43 44 // Link the account 45 linkResponse, err := a.CloudLinkAccount(testAccountID, CloudLinkCloudAccountsInput{ 46 Aws: []CloudAwsLinkAccountInput{ 47 { 48 Arn: testARN, 49 Name: "DTK Integration Testing", 50 }, 51 }, 52 }) 53 require.NoError(t, err) 54 require.NotNil(t, linkResponse) 55 56 // Get the linked account 57 getResponse, err = a.GetLinkedAccounts("aws") 58 require.NoError(t, err) 59 60 var linkedAccountID int 61 for _, linkedAccount := range *getResponse { 62 if linkedAccount.NrAccountId == testAccountID { 63 linkedAccountID = linkedAccount.ID 64 break 65 } 66 } 67 require.NotZero(t, linkedAccountID) 68 69 // Rename the account 70 newName := "NEW-DTK-NAME" 71 renameResponse, err := a.CloudRenameAccount(testAccountID, []CloudRenameAccountsInput{ 72 { 73 LinkedAccountId: linkedAccountID, 74 Name: newName, 75 }, 76 }) 77 require.NoError(t, err) 78 require.NotNil(t, renameResponse) 79 80 // Unlink the account 81 unlinkResponse, err := a.CloudUnlinkAccount(testAccountID, []CloudUnlinkAccountsInput{ 82 { 83 LinkedAccountId: linkedAccountID, 84 }, 85 }) 86 require.NoError(t, err) 87 require.NotNil(t, unlinkResponse) 88 } 89 90 func TestCloudAccount_SingleLinkedAccount(t *testing.T) { 91 t.Parallel() 92 93 testAccountID, err := mock.GetTestAccountID() 94 if err != nil { 95 t.Skipf("%s", err) 96 } 97 98 testARN := os.Getenv("INTEGRATION_TESTING_AWS_ARN") 99 if testARN == "" { 100 t.Skip("an AWS ARN is required to run cloud account tests") 101 return 102 } 103 104 a := newIntegrationTestClient(t) 105 106 // Link the account 107 linkResponse, err := a.CloudLinkAccount(testAccountID, CloudLinkCloudAccountsInput{ 108 Aws: []CloudAwsLinkAccountInput{ 109 { 110 Arn: testARN, 111 Name: "DTK Integration Testing", 112 }, 113 }, 114 }) 115 require.NoError(t, err) 116 require.NotNil(t, linkResponse) 117 118 if len(linkResponse.LinkedAccounts) == 0 { 119 t.Skip("skipping TestCloudAccount_SingleLinkedAccount due to no linked accounts") 120 return 121 } 122 123 // Get the linked account 124 linkedAccountId := linkResponse.LinkedAccounts[0].ID 125 getResponse, err := a.GetLinkedAccount(testAccountID, linkedAccountId) 126 foundLinkedAccountId := getResponse.ID 127 128 require.NoError(t, err) 129 require.Equal(t, linkedAccountId, foundLinkedAccountId) 130 131 // Rename the account 132 newName := "NEW-DTK-NAME" 133 renameResponse, err := a.CloudRenameAccount(testAccountID, []CloudRenameAccountsInput{ 134 { 135 LinkedAccountId: linkedAccountId, 136 Name: newName, 137 }, 138 }) 139 require.NoError(t, err) 140 require.NotNil(t, renameResponse) 141 142 // Unlink the account 143 unlinkResponse, err := a.CloudUnlinkAccount(testAccountID, []CloudUnlinkAccountsInput{ 144 { 145 LinkedAccountId: linkedAccountId, 146 }, 147 }) 148 require.NoError(t, err) 149 require.NotNil(t, unlinkResponse) 150 } 151 152 func newIntegrationTestClient(t *testing.T) Cloud { 153 tc := mock.NewIntegrationTestConfig(t) 154 155 return New(tc) 156 }