I want to write a python tool to create Synthetic Monitors in NewRelic, I have created a template
mutation {
syntheticsCreateScriptApiMonitor (
accountId: {account_id},
monitor: {
locations: { public: {locations} },
name: "{monitor_name}",
period: {period},
runtime: {
runtimeType: "{runtime_type}",
runtimeTypeVersion: "{runtime_version}",
scriptLanguage: "{script_language}"
},
script: "{script_content}",
status: "{status}",
advancedOptions: { enableScreenshotOnFailureAndScript: {enable_screenshot} },
apdexTarget: {apdex_target}
}
) {
errors {
description
type
}
}
}
which I want to load into my code and fill it with the required values for account_id
, locations
and so on.
I wrote this
import yaml
from gql import gql
def load_config(filename):
""" Load configuration from YAML file """
with open(filename, 'r') as file:
return yaml.safe_load(file)
def load_graphql_template(filename):
""" Read GraphQL template from a specified file """
with open(filename, 'r') as file:
return file.read()
def load_script_content(filename):
""" Load script content from file (for scripted monitors) """
with open(filename, 'r') as file:
return file.read()
# Generate the mutation with the provided configuration and monitor type
def generate_mutation(config):
monitor_type = config.get('MONITOR_TYPE', 'scripted') # Default to 'scripted' if not specified
# Determine the template file name based on monitor type
template_filename = f'synthetic-monitor-templates/{monitor_type}_monitor_template.graphql'
# Load the GraphQL template from the specified file
template = load_graphql_template(template_filename)
script_content = load_script_content(config['SCRIPT_FILE_PATH']).replace('n', '\n') # Escape newlines in the script content
if monitor_type == 'scripted_browser':
mutation = template.format(
account_id=config['ACCOUNT_ID'],
monitor_name=config['MONITOR_NAME'],
period=config['PERIOD'],
runtime_type='BROWSER',
runtime_version=config['RUNTIME_VERSION'],
script_language=config['SCRIPT_LANGUAGE'],
script_content=script_content,
status=config['STATUS'],
enable_screenshot=str(config['ENABLE_SCREENSHOT']).lower(),
apdex_target=config['APDEX_TARGET'],
locations=str(config['LOCATIONS']).replace("'", '"') # Convert list to JSON array format
)
elif monitor_type == 'scripted_api':
mutation = template.format(
account_id=config['ACCOUNT_ID'],
monitor_name=config['MONITOR_NAME'],
period=config['PERIOD'],
runtime_type='NODE_API',
runtime_version=config['RUNTIME_VERSION'],
script_language=config['SCRIPT_LANGUAGE'],
script_content=script_content,
status=config['STATUS'],
enable_screenshot=str(config['ENABLE_SCREENSHOT']).lower(),
apdex_target=config['APDEX_TARGET'],
locations=str(config['LOCATIONS']).replace("'", '"') # Convert list to JSON array format
)
elif monitor_type == 'scripted':
mutation = template.format(
account_id=config['ACCOUNT_ID'],
monitor_name=config['MONITOR_NAME'],
period=config['PERIOD'],
runtime_type=config['RUNTIME_TYPE'],
runtime_version=config['RUNTIME_VERSION'],
script_language=config['SCRIPT_LANGUAGE'],
script_content=script_content,
status=config['STATUS'],
enable_screenshot=str(config['ENABLE_SCREENSHOT']).lower(),
apdex_target=config['APDEX_TARGET'],
locations=str(config['LOCATIONS']).replace("'", '"') # Convert list to JSON array format
)
elif monitor_type == 'ping':
mutation = template.format(
account_id=config['ACCOUNT_ID'],
monitor_name=config['MONITOR_NAME'],
period=config['PERIOD'],
url=config['URL'],
status=config['STATUS'],
enable_screenshot=str(config['ENABLE_SCREENSHOT']).lower(),
locations=str(config['LOCATIONS']).replace("'", '"') # Convert list to JSON array format
)
else:
raise ValueError(f"Unsupported monitor type: {monitor_type}")
return gql(mutation)
But I keep getting key error because the graphql template is not being loaded correctly, with spaces, or newlines causing issues. There is no standard lib for graphql like yaml. So what could be a good way around this??
I have tried cleaning up the graphql template so that it would load correctly