Ejemplo n.º 1
0
def lambda_function_update_config(ctx, name, config):
    tenant, token, url, tenantId = CheckAndGetConnection()
    CheckEmptyParam('name', name, "")
    CheckEmptyParam('config', config, "specify a valid path to config file")

    click.echo("Lambda Update - Function {} Config {}".format(name, config))
    funcObject = None
    name = getNameWithPrefix(name, tenant)

    # Load the json file
    with open(config) as json_file:
        json_data = json.load(json_file)
        for index, item in enumerate(json_data["LambdaFunctions"]):
            if getNameWithPrefix(item["FunctionName"], tenant) == name:
                funcObject = item
                break
    if funcObject is None:
        raise ValueError(
            'Failed to find desired function {} in config file'.format(name))

    funcObject["FunctionName"] = getNameWithPrefix(funcObject["FunctionName"],
                                                   tenant)
    # Create Function
    print("Updating lambda function {}".format(funcObject["FunctionName"]))
    updateLambdaFunctionConfig(token, url, tenantId, funcObject)
    printSuccess('Updated Lambda Function {} configuration'.format(name))
Ejemplo n.º 2
0
def lambda_delete(ctx, name):
    tenant, token, url, tenantId = CheckAndGetConnection()
    CheckEmptyParam('name', name, "")
    click.echo("Lambda - Delete Function {} ".format(name))
    name = getNameWithPrefix(name, tenant)
    deleteLambdaFunction(token, url, tenantId, name)
    printSuccess('Deleted Lambda Function {}'.format(name))
Ejemplo n.º 3
0
def apigateway_add(ctx, name):
    tenant, token, url, tenantId = CheckAndGetConnection()
    CheckEmptyParam('name', name, "")

    click.echo("Apigateway - Delete API {}".format(name))
    name = getNameWithPrefix(name, tenant)
    jsondata = {"Name": name, "State": "delete"}
    createApiGatewayApi(token, url, tenantId, jsondata)
    printSuccess(
        'Marked ApiGateway {} for delete .. It will removed in about a min'.
        format(name))
Ejemplo n.º 4
0
def apigateway_add(ctx, name, lambdaa):
    tenant, token, url, tenantId = CheckAndGetConnection()
    CheckEmptyParam('name', name, "")
    if lambdaa is None:
        lambdaa = ""

    click.echo("Apigateway - Create API {} Lambda function {}".format(
        name, lambdaa))
    name = getNameWithPrefix(name, tenant)
    jsondata = {"Name": name, "LambdaFunction": lambdaa}
    createApiGatewayApi(token, url, tenantId, jsondata)
    printSuccess(
        'Created ApiGateway {} .. It will available in about a min'.format(
            name))
Ejemplo n.º 5
0
def lambda_add_or_update(ctx, name, config, dir, update):
    tenant, token, url, tenantId = CheckAndGetConnection()
    CheckEmptyParam('name', name, "")
    CheckEmptyParam('config', config, "specify a valid path to config file")

    click.echo("Lambda Deploy - Function {} Config {}".format(name, config))
    funcObject = None
    name = getNameWithPrefix(name, tenant)

    # Load the json file
    with open(config) as json_file:
        json_data = json.load(json_file)
        for index, item in enumerate(json_data["LambdaFunctions"]):
            if getNameWithPrefix(item["FunctionName"], tenant) == name:
                funcObject = item
                break
    if funcObject is None:
        raise ValueError(
            'Failed to find desired function {} in config file'.format(name))

    funcObject["FunctionName"] = getNameWithPrefix(funcObject["FunctionName"],
                                                   tenant)
    zipFilePath = ""
    if dir.endswith(".zip"):
        zipFilePath = dir
        localFileName = os.path.basename(dir)
    else:
        fileVersion = funcObject["FunctionName"] + "-" + datetime.datetime.now(
        ).strftime("%Y-%m-%d-%H-%M-%S")
        zipFilePath = "/tmp/" + fileVersion
        print('Making a zip file {} from folder {}'.format(
            zipFilePath + ".zip", dir))
        # zip the directory
        shutil.make_archive(zipFilePath, 'zip', dir)
        zipFilePath = zipFilePath + ".zip"
        localFileName = fileVersion + ".zip"

    # check if s3 bucket exists, if not create it
    bucketName = getNameWithPrefix("lambda-packages", tenant)
    bucketName = checkAndCreateS3Bucket(bucketName, tenant, token, url,
                                        tenantId)

    # Upload the file
    uploadFileToS3(token, tenantId, url, bucketName, zipFilePath,
                   localFileName)

    if not update:
        # Create Function
        funcObject["Code"] = {
            "S3Bucket": bucketName,
            "S3Key": localFileName
        }
        print("Creating lambda function {}".format(funcObject["FunctionName"]))
        createLambdaFunction(token, url, tenantId, funcObject)
        printSuccess('Created Lambda Function {}'.format(
            funcObject["FunctionName"]))
    else:
        print("Updating lambda function code {}".format(
            funcObject["FunctionName"]))
        data = {
            "FunctionName": funcObject["FunctionName"],
            "S3Bucket": bucketName,
            "S3Key": localFileName
        }
        updateLambdaFunctionCode(token, url, tenantId, data)
        printSuccess('Updated Lambda Function {} code'.format(
            funcObject["FunctionName"]))