rdsAD

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.0 kB
2
Indexable
Never
from decimal import Decimal
import json
import boto3
import boto3.session
from botocore.client import Config
import logging
import os
import sys
from ast import literal_eval
from random import *
import string
import uuid
import mysql.connector
# from psycopg2 import connect
# #import mysql.connector 
import time
from datetime import datetime
import base64
from botocore.exceptions import ClientError
logger = logging.getLogger()
logger.setLevel(logging.INFO)
inProgressState = "in_progress"
inProgress_transition_id = '31'
Progress_Error_transition_id = '61'
Approved_Error_transition_id = '41'
REGION = "ap-south-1"
# invokeLam = boto3.client("lambda", region_name=os.environ['LAMBDA_REGION'])

config = Config(read_timeout=600, connect_timeout=600, retries={
                'max_attempts': 2}, signature_version='s3v4')
session = boto3.session.Session(region_name='ap-south-1',profile_name="vishalaws")
dynamodb = session.resource('dynamodb',
                            # endpoint_url=HOST,
                            config=config,
                            # region_name='us-west-1',
                            # aws_access_key_id="foo",
                            # aws_secret_access_key="bar",
                            # use_ssl=False,
                            )


class JiraException(Exception):
    pass

def load_record(event, context):
    try:
        statement= "CREATE USER 'testuser'@'%' IDENTIFIED BY 'auth_pam'"
        payload = {
        "db_statement": statement
        }
        execute()
    except Exception as e:
        e = str(e)
        
        # e = e.replace("'", "")
        # e = e.replace('"', "")
        # payload = {
        #     "ticket_id": ticket_id,
        #     "transition_id": Progress_Error_transition_id,
        #     "msg": e
        # }
        # raise JiraException(payload)


# Execute Function
def execute(event):
    db_statement = event['db_statement']
    host = "demodb.c0vrpfvrlu10.ap-south-1.rds.amazonaws.com"
    #os.environ['POSTGRES_HOST']
    rds_user = "admin"
    #os.environ['RDS_USER']
    rds_pwd = "123456789"
    #os.environ['RDS_PASSWORD']
    print(host)

    # session = boto3.Session(profile_name='default')
    # client = session.client('rds')

    

    conn = mysql.connector.connect(
        host=host,
        user=rds_user,
        passwd=rds_pwd,
        port="3306",
        database="test"
        
    )
    print(db_statement)
    cursor = conn.cursor()
    try:
        resp = cursor.execute(db_statement)
        cursor.close()
        conn.commit()
        conn.close()
        print("created user")
        return {
            'statusCode': 200,
            'body': 'success'
        }
    except Exception as e:
        conn.close()
        logger.error("Exception in postgres operation:" + str(e))
        return {
            'statusCode': 400,
            'body': str(e)
        }