Exemplo n.º 1
0
def lambda_handler(event, context):
    project_id = event["pathParameters"]["projectId"]
    logger.info(f"Got request for project {project_id}")

    configfile = f"{project_id}/config.json"
    statefile = f"{project_id}/terraform.tfstate"

    config = json.loads(read_key_or_default(configfile))
    project_name = config["name"]
    logger.info(f"Got request for {project_name} with id {project_id}")

    self_url = "https://" + event["requestContext"]["domainName"]

    # Get existing state or create new
    if event['httpMethod'] == "GET":
        logger.info("Type is GET, send state")
        config = json.loads(read_key_or_default(configfile))
        state = json.loads(read_key_or_default(statefile))
        if config == None:
            return create_response(
                f"No project exists, please visit {self_url}/project/new")
        else:
            metadata = get_tf_metadata(state)
            resources = get_tf_res(state)
            output = INFO_TEMPLATE.render(config=config,
                                          metadata=metadata,
                                          resources=resources,
                                          project_id=project_id,
                                          domain=DOMAIN,
                                          token=config["token"])
            return create_response(output, contenttype="text/html")
Exemplo n.º 2
0
def lambda_handler(event, context):

    # Get existing state or create new
    if event['httpMethod'] == "GET":
        logger.info(f"Send form for creation")
        return create_response(PROJECT_FORM,contenttype="text/html")
        
    # update
    if event['httpMethod'] == "POST":
        body_vars = {}
        body = urllib.parse.unquote(event["body"])
        for line in body.split("&"):
            line_data = line.split("=")
            body_vars[line_data[0]]=line_data[1]

        logger.info(body_vars)
        logger.info("name" in body_vars)
        logger.info("owner" in body_vars)
        logger.info("token" in body_vars)
        if not ("name" in body_vars and "owner" in body_vars and "token" in body_vars):
            return create_response("Missing field owner, name or token",code=500)

        name = body_vars["name"]
        owner = body_vars["owner"]
        token = body_vars["token"]
        project_id = randomString(48)
        config = json.dumps({"name":name, "owner":owner, "token":token})

        logger.info(f"Create project {name} with id {project_id}")
        
        write_key(f"{project_id}/config.json",config)
        write_key(f"{project_id}/terraform.tfstate",DEFAULT_STATE)
        
        return redirect(f"https://{DOMAIN}/project/{project_id}/info")
Exemplo n.º 3
0
def lambda_handler(event, context):
    project_id = event["pathParameters"]["projectId"]
    logger.info(f"Got request for project {project_id}")

    statefile = f"{project_id}/terraform.tfstate"
    self_url = "https://" + event["requestContext"]["domainName"]

    config = get_config(project_id)
    if config["name"] == "invalid":
        return create_response(
            f"No project exists, please visit {self_url}/project/new", 404)

    project_name = config["name"]
    logger.info(f"Got request for {project_name} with id {project_id}")

    # Get existing state or create new
    if event["httpMethod"] == "GET":
        logger.info("Type is GET, send state")
        data = read_key_or_default(statefile)
        return create_response(data.decode("utf-8"))

    # update
    if event["httpMethod"] == "POST":
        logger.info("Type is POST, save and send state")
        data = event["body"]
        metadata = get_tf_metadata(data, True)
        if metadata["terraform_version"] == "invalid":
            return create_response("Unable to parse", code=500)
        else:
            write_key(statefile, data)
            # todo: write the terraform.tfstate.serial
            return create_response(data)
Exemplo n.º 4
0
def lambda_handler(event, context):
    project_id = event["pathParameters"]["projectId"]
    logger.info(f"Got request for project {project_id}")

    configfile = f"{project_id}/config.json"
    statefile = f"{project_id}/terraform.tfstate"

    config = json.loads(read_key_or_default(configfile))
    project_name = config["name"]
    logger.info(f"Got request for {project_name} with id {project_id}")

    self_url = "https://" + event["requestContext"]["domainName"]

    # Get existing state or create new
    if event['httpMethod'] == "GET":
        logger.info("Type is GET, send state")
        data = read_key_or_default(statefile)
        if data == None:
            return create_response(
                f"No project exists, please visit {self_url}/project/new")
        else:
            return create_response(data.decode('utf-8'))

    # update
    if event['httpMethod'] == "POST":
        logger.info("Type is POST, save and send state")
        data = read_key_or_default(configfile)
        if data == None:
            return create_response(
                f"No project exists, please visit {self_url}/project/new")
        else:
            data = event["body"]
            write_key(statefile, data)
            return create_response(data)
Exemplo n.º 5
0
def lambda_handler(event, context):

    # Get existing state or create new
    if event["httpMethod"] == "GET":
        logger.info(f"Send form for creation")
        output = render_template("project_form.html")
        return create_response(output, contenttype="text/html")

    # update
    if event["httpMethod"] == "POST":
        body_vars = get_post_parameter(event)
        if not ("name" in body_vars and "owner" in body_vars):
            return create_response("Missing field owner, name or token",
                                   code=500)

        project_id = new_project(body_vars["name"], body_vars["owner"])

        return redirect(f"https://{DOMAIN}/project/{project_id}/info")
Exemplo n.º 6
0
def lambda_handler(event, context):
    project_id = event["pathParameters"]["projectId"]
    logger.info(f"Got request for project {project_id}")

    report = get_report(project_id)

    if not "metadata" in report:
        return create_response(
            "No report is available (yet). Please check out in a view seconds.",
            code=404,
        )

    # Get existing state or create new
    if event["httpMethod"] == "GET":
        output = render_template(
            template_file="project_info.html",
            report=report,
            project_id=project_id,
            domain=DOMAIN,
        )
        return create_response(output, contenttype="text/html")
Exemplo n.º 7
0
def lambda_handler(event, context):
    logger.info(f"Got request for listing projects")

    reports = get_reports()

    # Get existing state or create new
    if event["httpMethod"] == "GET":
        output = render_template(
            template_file="project_list.html",
            reports=reports,
            domain=DOMAIN,
        )
        return create_response(output, contenttype="text/html")
Exemplo n.º 8
0
 def test_result(self):
     result = create_response("test", code=401)
     self.assertEqual(result["body"], "test")
     self.assertEqual(result["statusCode"], 401)