I need to unzip a folder and update several lines in a file called "main.py". For context, this .zip file will be used to create a Lambda with Terraform.
I'm being able to unzip the folder and I'm being able to update the lines of code in "main.py" file (the changes I apply to these lines are in another file, "conditions.txt". Those changes are the ID's of some alerts in a policy of NewRelic).
My problem is that when I insert the lines back into the file they go to the end of the file. Even though I know precisely that the new lines (from 20 to 22 of them, depending on the "conditions.txt" file) should be inserted starting on line 8 of "main.py" file, I'm not being able to do so. I've tried many things but the result is always the same. I feel I'm missing something, and I can't find a solution online.
Any suggestions will be much appreciated.
Here is the python code I'm running, after that I included the code of "main.py" file:
import zipfile
# unzip the file in "temp" folder (works fine)
with zipfile.ZipFile("Archive.zip", 'r') as zip_ref:
zip_ref.extractall("temp")
# delete "payload_" lines that will later be replaced
with open("temp/main.py", "r") as f:
lines = f.readlines()
with open("temp/main.py", "w") as f:
for line in lines:
# if the line in the file does not start with "payload_" its re-writen
if not line.lstrip().startswith("payload_"):
f.write(line)
# take "conditions" file and create the text to be inserted in "main.py" inside "temp" folder
conditions = open("conditions.txt").readlines()
condition = ""
new_payload_lines = []
for index, condition in enumerate(conditions):
index = index + 1
condition = condition.replace(",", "")
condition = condition.strip()
new_payload_lines.append('payload_%s="{\\"query\\":\\"mutation {\\\\n alertsNrqlConditionStaticUpdate(id: %s, accountId: 11111111, condition: {\\\\n enabled : true\\\\n }) {\\\\n name\\\\n }\\\\n}\\\\n\\",\\"variables\\":{}}"' % (index, condition))
# This inserts the lines in "new_payload_lines" with proper format, but at the end of the file, which is wrong
with open("temp/main.py", "r+") as f:
for i, line in enumerate(f):
if line.startswith("url"):
f.write("\n")
for payload in new_payload_lines:
f.write(payload + "\n")
This is the file I'm trying to modify, "main.py". You can see the "payload_x" lines that I need to replace:
from ast import For
import requests
import json
url = "https://api.newrelic.com/graphql"
payload_1="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058756, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_2="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058757, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_3="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058758, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_4="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058759, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_5="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058760, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_6="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058761, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_7="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058762, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_8="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058763, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_9="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058764, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_10="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058765, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_11="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058766, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_12="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058767, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_13="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058768, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_14="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058769, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_15="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058770, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_16="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058772, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_17="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058773, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_18="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058774, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_19="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058775, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
payload_20="{\"query\":\"mutation {\\n alertsNrqlConditionStaticUpdate(id: 27058776, accountId: 1111111, condition: {\\n enabled : true\\n }) {\\n name\\n }\\n}\\n\",\"variables\":{}}"
headers = {
'Content-Type': 'application/json',
'API-Key': 'AAAA-xxxxxxxxxxxxxxxxxxxxxxx'
}
def lambda_handler(event, context):
payloads = [payload_1,payload_2,payload_3,payload_4,payload_5,payload_6,payload_7,payload_8,payload_9,payload_10,payload_11,payload_12,payload_13,payload_14,payload_15,payload_16,payload_17,payload_18,payload_19,payload_20]
for ids in payloads:
response = requests.post(url, headers=headers, data=ids)
return {
'statusCode': 200,
'body': json.dumps('Conditions updated!')
}