def newrelic_registration()

in functions/source/register/register.py [0:0]


def newrelic_registration(aws_account_id, access_key, newrelic_account_id, newrelic_integration_list):
    role_arn =  'arn:aws:iam::{}:role/NewRelicIntegrationRole_{}'.format(aws_account_id, newrelic_account_id)
    nerdGraphEndPoint = os.environ['nerdGraphEndPoint']
    
    link_payload = '''
    mutation 
    {{
        cloudLinkAccount(accountId: {0}, accounts: 
        {{
            aws: [
            {{
                name: "{1}", 
                arn: "{2}"
            }}]
        }}) 
        {{
            linkedAccounts 
            {{
                id name authLabel
            }}
            errors 
            {{
                type
                message
                linkedAccountId
            }}
        }}
    }}
    '''.format(newrelic_account_id, aws_account_id, role_arn)
    logger.debug('NerdGraph link account payload : {}'.format(json.dumps(link_payload)))
    
    response = requests.post(nerdGraphEndPoint, headers={'API-Key': access_key}, verify=True, data=link_payload)
    logger.info('NerdGraph response code : {}'.format(response.status_code))
    logger.info('NerdGraph response : {}'.format(response.text))
    if response.status_code == 200:
        link_response = json.loads(response.text)
        
        try:
            link_accound_id = link_response['data']['cloudLinkAccount']['linkedAccounts'][0]['id']
            service_payload = []
            for service in newrelic_integration_list:
                service_payload.append('{0}: [{{ linkedAccountId: {1} }}]'.format(service, link_accound_id))
            
            integration_payload = '''
            mutation 
            {{
              cloudConfigureIntegration (
                accountId: {0},
                integrations: 
                {{
                  aws: 
                  {{
                    {1}
                  }}
                }} 
              ) 
              {{
                integrations 
                {{
                  id
                  name
                  service 
                  {{
                    id 
                    name
                  }}
                }}
                errors 
                {{
                  type
                  message
                }}
              }}
            }}
            '''.format(newrelic_account_id, '\n'.join(service_payload))
            logger.debug('NerdGraph integration payload : {}'.format(json.dumps(integration_payload)))
            integration_response = requests.post(nerdGraphEndPoint, headers={'API-Key': access_key}, verify=True, data=integration_payload)
            logger.info('NerdGraph integration response code : {}'.format(integration_response.status_code))
            logger.info('NerdGraph integration response : {}'.format(integration_response.text))
            
        except Exception as create_exception:
            if len(link_response['data']['cloudLinkAccount']['errors']) > 0:
                logger.warning('NerdGraph error messages : {}'.format(link_response['data']['cloudLinkAccount']['errors']))    
                for error in link_response['data']['cloudLinkAccount']['errors']:
                    if 'AWS account is already linked ' in error['message']:
                        logger.warning('AWS Account {} already linked, skipping'.format(aws_account_id))
            else:
                logger.error('Exception {}'.format(create_exception))