Skip to content

Commit

Permalink
Merge pull request #158 from newrelic/mrickard/lambda-1345/metricStre…
Browse files Browse the repository at this point in the history
…ams-awareness

fix: Skip creating integration when MS or polling integrations exist
  • Loading branch information
mrickard authored Nov 29, 2021
2 parents 35b8c9b + 173732b commit c898e9e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const nerdgraphFetch = async (
const gqlUrl =
region === "eu"
? "https://api.eu.newrelic.com/graphql"
: region === "staging"
? "https://staging-api.newrelic.com/graphql"
: "https://api.newrelic.com/graphql";

const agent =
Expand Down
63 changes: 35 additions & 28 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ export default class Integration {
nrRegion,
proxy
} = this.config;
const {
linkedAccount = `New Relic Lambda Integration - ${accountId}`
} = this.config;

const integrationData = await nerdgraphFetch(
apiKey,
Expand All @@ -55,7 +52,6 @@ export default class Integration {

const match = linkedAccounts.filter(account => {
return (
account.name === linkedAccount &&
account.externalId === externalId &&
account.nrAccountId === parseInt(accountId, 10)
);
Expand All @@ -66,7 +62,7 @@ export default class Integration {
"No New Relic AWS Lambda integration found for this New Relic linked account and aws account."
);

if (enableIntegration) {
if (enableIntegration && enableIntegration !== "false") {
this.enable(externalId);
return;
}
Expand Down Expand Up @@ -251,6 +247,19 @@ export default class Integration {
}
}

private async requestRoleArn(RoleName: string) {
const params = {
RoleName
};
const response = await this.awsProvider.request("IAM", "getRole", params);

const {
Role: { Arn }
} = response;

return Arn;
}

private async checkAwsIntegrationRole(externalId: string) {
const { accountId } = this.config;
if (!accountId) {
Expand All @@ -261,31 +270,29 @@ export default class Integration {
}

try {
const params = {
RoleName: `NewRelicLambdaIntegrationRole_${accountId}`
};

const {
Role: { Arn }
} = await this.awsProvider.request("IAM", "getRole", params);

return Arn;
return this.requestRoleArn(`NewRelicLambdaIntegrationRole_${accountId}`);
} catch (err) {
this.serverless.cli.log(
"The required NewRelicLambdaIntegrationRole cannot be found; Creating Stack with NewRelicLambdaIntegrationRole."
);
const stackId = await this.createCFStack(accountId);
waitForStatus(
{
awsMethod: "describeStacks",
callbackMethod: () => this.enable(externalId),
methodParams: {
StackName: stackId
// We're limited to one rolename and a 64-character regex, so allowing for streams means a second query
try {
return this.requestRoleArn(`NewRelicInfrastructure-Integrations`);
} catch (fallbackErr) {
this.serverless.cli.log(
`Neither NewRelicLambdaIntegrationRole_${accountId} nor NewRelicInfrastructure-Integrations can be found.
Creating Stack with NewRelicLambdaIntegrationRole.`
);
const stackId = await this.createCFStack(accountId);
waitForStatus(
{
awsMethod: "describeStacks",
callbackMethod: () => this.enable(externalId),
methodParams: {
StackName: stackId
},
statusPath: "Stacks[0].StackStatus"
},
statusPath: "Stacks[0].StackStatus"
},
this
);
this
);
}
}
}

Expand Down

0 comments on commit c898e9e

Please sign in to comment.