예제 #1
0
def lambda_handler(event, context):
#    logging.info(event)
    db = dbutils.DocmentDb(event.get("db_url"))

    if "task_started" in event.get("status"):
      event.update(
        {"status": "task_progress"}
      )
    elif "task_failed" in event.get("status"):
      db.delete_event_db(event.get("user_id"))
      return
    else:
      remove_event(
        event.get("event_name"), 
        event.get("lambda_name")
      )
      
    vmc_client = get_vmc_client(event.get("token"))
    status = task_handler(
      vmc_client.orgs.Tasks, 
      event
    )
    event.update({"status": status})
    message_handler(constant.CHECK_TASK, event)
    message_handler(constant.CHECK_TASK_WH, event)

    if "Failed" in status or "Canceled" in status or "Finished" in status:
      db.delete_event_db(event.get("user_id"))
예제 #2
0
def task_handler(task_client, event):
  resp = wait_for_task(task_client, event.get("org_id"), event.get("task_id"))
#  resp = {"status": True, "check_time": 3, "estimated_time": 3} #for test

  db = dbutils.DocmentDb(event.get("db_url"))
  
  if resp.get("status") == False:
    db.delete_event_db(event.get("user_id"))
    return "{} to {} sddc, {}".format(
      resp.get("message"), 
      event.get("command"), 
      event.get("sddc_name")
    )
  elif resp.get("status") == True and resp.has_key("check_time"):
    event["event_name"] = "{}-{}-{}".format(
      event.get("user_id"),
      event.get("task_id"),
      datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    )
    put_event(resp.get("check_time"), event)
    return "It takes around {} min".format(resp.get("estimated_time"))
  elif resp.get("status") == True:
    db.delete_event_db(event.get("user_id"))
    return "{} successfully to {} sddc, task id: {}".format(
      resp.get("message"), 
      event.get("command"),
      event.get("task_id")
    )
예제 #3
0
def lambda_handler(event, context):
#  logging.info(event)

  result = check(
    event, 
    get_vmc_client(event.get("token"))
  )
  
  event.update(
    {
      "check_result": result.get("message")
    }
  )
  message_handler(constant.CHECK_RESULT, event)
  
  db = dbutils.DocmentDb(event.get("db_url"))
  if result.get("result"):
    db.write_event_db(
      event.get("user_id"), 
      {
        "command": "create",
        "status": "creating"
      }
    )
  else:
    db.delete_event_db(event.get("user_id"))
예제 #4
0
def interactive_handler(event):
    user_id = event.get("user_id")
    
    db = dbutils.DocmentDb(event.get("db_url"))
    event_db = db.read_event_db(user_id, 5)
    
    if event_db is None:
      message_handler(constant.MAY_I, event)
      return
    
    cred_db = db.read_cred_db(event.get("user_id"))
    event.update(
        {
            "token": cred_db.get("token"),
            "org_id": cred_db.get("org_id"),
            "user_name": cred_db.get("user_name")
        }
    )
    event.update(event_db)
    
    callback_id = event.get("callback_id")
    command_handler(callback_id, event, db)
예제 #5
0
def event_handler(event):
    text = event.get("text").lower()

    db = dbutils.DocmentDb(event.get("db_url"))
    current = db.read_event_db(event.get("user_id"), 120)
    if current is not None and current.get("status") in [
            cmd_const.CREATING, cmd_const.DELETING
    ]:
        message_handler(msg_const.ASK_WAIT_TASK, event)
        return

    event_db = db.read_event_db(event.get("user_id"), 5)
    cred_db = db.read_cred_db(event.get("user_id"))

    if event_db is None:
        if cred_db is not None and text == "cancel":
            if cmd_const.REGISTERED not in cred_db.get("status"):
                command_handler(cmd_const.CANCEL_REGISTER, event, db)
        elif text in cmd_const.COMMAND_ORG:
            command_handler(cmd_const.COMMAND_ORG[text], event, db)
            return
        elif text in cmd_const.COMMAND_SDDC:
            if cred_db is not None and cmd_const.REGISTERED in cred_db.get(
                    "status"):
                event_cred_update(event, cred_db)
                command_handler(cmd_const.COMMAND_SDDC[text], event, db)
                return
            else:
                message_handler(msg_const.ASK_REGISTER_TOKEN, event)
                return
        elif cred_db is not None and cmd_const.REGISTER_ORG_ID in cred_db.get(
                "status"):
            command_handler(cmd_const.REGISTER_ORG_ID, event, db)
        elif cred_db is not None and cmd_const.REGISTER_TOKEN in cred_db.get(
                "status"):
            command_handler(cmd_const.REGISTER_TOKEN, event, db)
        elif text == "help":
            message_handler(msg_const.HELP, event)
        else:
            message_handler(msg_const.MAY_I, event)
            return
    else:
        command = event_db.get("command")
        status = event_db.get("status")
        if text == "cancel":
            if "create_sddc" in command:
                message_handler(msg_const.CANCEL_SDDC, event)
            elif "delete_sddc" in command:
                message_handler(msg_const.CANCEL_DELETE, event)
            db.delete_event_db(event.get("user_id"))
            return
        elif cmd_const.SDDC_NAME in status:
            event.update(event_db.get("max_hosts"))
            command_handler(cmd_const.SDDC_NAME, event, db)
            return
        elif cmd_const.MGMT_CIDR in status:
            event.update(event_db)
            command_handler(cmd_const.MGMT_CIDR, event, db)
            return
        elif command in ["create_sddc", "delete_sddc"]:
            message_handler(constant.ASK_SELECT_BUTTON, event)
            return
예제 #6
0
def interactive_handler(event):
    user_id = event.get("user_id")

    db = dbutils.DocmentDb(event.get("db_url"))
    result = db.read_event_db(user_id, 5)
    if result is None:
        message_handler(constant.MAY_I, event)
        return

    __cred_data = db.read_cred_db(event.get("user_id"))
    event.update({
        "token": __cred_data.get("token"),
        "org_id": __cred_data.get("org_id")
    })
    if "delete_sddc" in event.get("callback_id"):
        if "delete_sddc" in result.get("status"):
            sddc_name = event.get("response").split("+")[0]
            sddc_id = event.get("response").split("+")[1]
            event.update({"sddc_name": sddc_name})
            db.write_event_db(user_id, {
                "sddc_name": sddc_name,
                "sddc_id": sddc_id
            })
            message_handler(constant.CONFIRM_DELETE, event)
        return
    elif "delete_confirmation" in event.get("callback_id"):
        if "yes" in event.get("response"):
            message_handler(constant.START_DELETE, event)
            event.update(result)
            event.update({"user_name": __cred_data.get("user_name")})
            if check_sddc_user(event.get("token"), event.get("org_id"),
                               event.get("sddc_id"), event.get("user_name")):
                call_lambda("delete_sddc", event)
            else:
                message_handler(constant.CANT_DELETE, event)
                db.delete_event_db(user_id)
        else:
            message_handler(constant.CANCEL_DELETE, event)
            db.delete_event_db(user_id)
        return
    elif "create_sddc" in event.get("callback_id"):
        if "yes" in event.get("response"):
            event.update(
                #                {
                #                    "region_list": list_region(
                #                        event.get("token"),
                #                        event.get("org_id")
                #                    )
                #                }
                {
                    "region_list": [{
                        "text": "AP_NORTHEAST_1",  #for internal use
                        "value": "AP_NORTHEAST_1"  #for internal use
                    }]
                })
            message_handler(constant.REGION, event)
            db.write_event_db(event.get("user_id"),
                              {"status": "resource_check"})
        else:
            message_handler(constant.CANCEL_SDDC, event)
            db.delete_event_db(user_id)
        return
    elif "link_aws_sddc" in event.get("callback_id"):
        if "True" in event.get("response"):
            event.update({
                #                    "aws_account_list": list_aws_account(
                #                        event.get("token"),
                #                        event.get("org_id")
                #                    )
                "aws_account_list": [{
                    "text":
                    event.get("aws_internal_account"),  #for internal use
                    "value":
                    "{}+{}".format(
                        event.get("aws_internal_account"),
                        event.get("aws_internal_id"))  #for internal use
                }]
            })
            message_handler(constant.AWS_ACCOUNT, event)
        else:
            message_handler(constant.CIDR, event)
        db.write_event_db(
            user_id, {
                "status": "link_aws",
                "num_hosts": 1,
                "sddc_type": "1NODE",
                "link_aws": event.get("response")
            })
        return
    elif "region" in event.get("callback_id"):
        message_handler(constant.SDDC_NAME, event)
        db.write_event_db(user_id, {
            "status": "region",
            "region": event.get("response")
        })
        return
    elif "single_multi" in event.get("callback_id"):
        if "single" in event.get("response"):
            message_handler(constant.LINK_AWS, event)
            db.write_event_db(user_id, {"status": "single_multi"})
        else:
            event.update(
                {"num_hosts_list": list_num_hosts(result.get("max_hosts"))})
            message_handler(constant.NUM_HOSTS, event)
            db.write_event_db(user_id, {
                "status": "single_multi",
                "link_aws": "True"
            })
        return
    elif "num_hosts" in event.get("callback_id"):
        event.update({
            #                "aws_account_list": list_aws_account(
            #                    event.get("token"),
            #                    event.get("org_id")
            #                )
            "aws_account_list": [{
                "text":
                event.get("aws_internal_account"),  #for internal use
                "value":
                "{}+{}".format(event.get("aws_internal_account"),
                               event.get("aws_internal_id"))  #for internal use
            }]
        })
        message_handler(constant.AWS_ACCOUNT, event)
        db.write_event_db(
            user_id, {
                "status": "num_hosts",
                "num_hosts": int(event.get("response")),
                "link_aws": "True"
            })
        return
    elif "aws_account" in event.get("callback_id"):
        aws_account = event.get("response").split("+")[0]
        aws_id = event.get("response").split("+")[1]
        event.update({
            "vpc_list":
            list_vpc(event.get("token"), event.get("org_id"), aws_id,
                     result.get("region"))
        })
        message_handler(constant.AWS_VPC, event)
        db.write_event_db(
            user_id, {
                "status": "aws_account",
                "aws_account": aws_account,
                "connected_account_id": aws_id
            })
        return
    elif "vpc" in event.get("callback_id"):
        event.update({
            "subnet_list":
            list_subnet(event.get("token"), event.get("org_id"),
                        result.get("connected_account_id"),
                        result.get("region"), event.get("response"))
        })
        message_handler(constant.AWS_SUBNET, event)
        db.write_event_db(user_id, {
            "status": "vpc",
            "vpc_id": event.get("response")
        })
        return
    elif "subnet" in event.get("callback_id"):
        message_handler(constant.CIDR, event)
        db.write_event_db(user_id, {
            "status": "subnet",
            "customer_subnet_id": event.get("response")
        })
        return
    elif "confirmation" in event.get("callback_id"):
        if "yes" in event.get("response"):
            message_handler(constant.CREATE, event)
            db.write_event_db(user_id, {
                "status": "creating",
            })
            event.update(result)
            call_lambda("create_sddc", event)
        else:
            message_handler(constant.CANCEL_SDDC, event)
            db.delete_event_db(user_id)
        return
    elif "restore_sddc" in event.get("callback_id"):
        if "yes" in event.get("response"):
            message_handler(constant.CHECK_RESOURCE, event)
            event.update(result)
            call_lambda("check_resources", event)
            db.write_event_db(event.get("user_id"),
                              {"status": "check_resources"})
        else:
            message_handler(constant.CANCEL_RESTORE, event)
            db.delete_event_db(user_id)
        return
    else:
        return
예제 #7
0
def event_handler(event):
    text = event.get("text").lower()

    db = dbutils.DocmentDb(event.get("db_url"))
    current = db.read_event_db(event.get("user_id"), 120)
    if current is not None and current.get("status") == "creating":
        message_handler(constant.ASK_WAIT_TASK, event)
        return
    
    result = db.read_event_db(event.get("user_id"), 5)
    __cred_data = db.read_cred_db(event.get("user_id"))
    if result is None:
        if "create sddc on zerocloud" in text: #for internal use only
            if __cred_data is None:
                message_handler(constant.ASK_REGISTER_TOKEN, event)
            elif "registered" in __cred_data.get("status"):
                event_cred_update(event, __cred_data)
                message_handler(constant.SDDC_WIZARD, event)
                message_handler(constant.CHECK_RESOURCE, event)
                event.update(
                    {
                        "max_hosts": get_max_num_hosts(
                            event.get("token"), 
                            event.get("org_id")
                        )
                    }
                )
                if event.get("max_hosts") < 1:
                    message_handler(constant.NOT_ENOUGH, event)
                    db.delete_event_db(event.get("user_id"))
                    return
                message_handler(constant.MAX_HOSTS, event)
                db.write_event_db(
                    event.get("user_id"), 
                    {
                        "command": "create",
                        "status": "create_sddc", 
                        "max_hosts": event.get("max_hosts"),
                        "provider": "ZEROCLOUD"
                    }
                )
            return
        elif "create sddc" in text:
            if __cred_data is None:
                message_handler(constant.ASK_REGISTER_TOKEN, event)
            elif "registered" in __cred_data.get("status"):
                event_cred_update(event, __cred_data)
                message_handler(constant.SDDC_WIZARD, event)
                message_handler(constant.CHECK_RESOURCE, event)
                event.update(
                    {
                        "max_hosts": get_max_num_hosts(
                            event.get("token"), 
                            event.get("org_id")
                        )
                    }
                )
                if event.get("max_hosts") < 1:
                    message_handler(constant.NOT_ENOUGH, event)
                    db.delete_event_db(event.get("user_id"))
                    return
                message_handler(constant.MAX_HOSTS, event)
                db.write_event_db(
                    event.get("user_id"), 
                    {
                        "command": "create",
                        "status": "create_sddc", 
                        "max_hosts": event.get("max_hosts"),
                        "provider": "AWS"
                    }
                )
            return
        elif "delete sddc" in text:
            if __cred_data is None:
                message_handler(constant.ASK_REGISTER_TOKEN, event)
            elif "registered" in __cred_data.get("status"):
                event_cred_update(event, __cred_data)
                event.update(
                    {
                        "option_list": list_sddcs(
                            event.get("token"), 
                            event.get("org_id")
                        )
                    }
                )
                message_handler(constant.DELETE_SDDC, event)
                db.write_event_db(
                    event.get("user_id"), 
                    {
                        "command": "delete", 
                        "status": "delete_sddc"
                    }
                )
        elif "restore sddc" in text: #for internal use
            if __cred_data is None:
                message_handler(constant.ASK_REGISTER_TOKEN, event)
            elif "registered" in __cred_data.get("status"):
                message_handler(constant.RESTORE_WIZARD, event)
                config = db.get_backedup_sddc_config()
                event.update(config)
                event_cred_update(event, __cred_data)
                config.update(
                    {
                        "command": "restore", 
                        "status": "restore_sddc"
                    }
                )
                db.write_event_db(event.get("user_id"), config)
                message_handler(constant.RESTORE, event)
        elif "list sddcs" in text:
            if __cred_data is None:
                message_handler(constant.ASK_REGISTER_TOKEN, event)
            elif "registered" in __cred_data.get("status"):
                event_cred_update(event, __cred_data)
                vmc_client = get_vmc_client(event.get("token"))
                sddcs = vmc_client.orgs.Sddcs.list(event.get("org_id"))
                message_handler(constant.SDDCS_TXT, event)
                for sddc in sddcs:
                    event.update(
                        {
                            "sddc_name": sddc.name,
                            "user_name": sddc.user_name,
                            "created": sddc.created.isoformat(),
                            "num_hosts": len(sddc.resource_config.esx_hosts)
                        }
                    )
                    message_handler(constant.SDDCS_MSG, event)
            return

        elif "register org" in text:
            message_handler(constant.REGISTER_ORG, event)
            db.write_cred_db(
                event.get("user_id"), 
                {
                    "status": "registering"
                }
            )
            db.write_event_db(
                event.get("user_id"), 
                {
                    "command": "register org",
                    "status": "registering org"
                }
            )
            return
        elif "delete org" in text:
            message_handler(constant.DELETE_ORG, event)
            db.delete_cred_db(event["user_id"])
            return
        elif "help" in text:
            message_handler(constant.HELP, event)
            return
        elif "cancel" in text:
            if __cred_data is not None and "registering" in __cred_data.get("status"):
                message_handler(constant.CANCEL_TOKEN, event)
                db.delete_cred_db(event.get("user_id"))
            else:
                message_handler(constant.MAY_I, event)
            return
        else:
            message_handler(constant.MAY_I, event)
            return
    elif "create" in result.get("command"):
        if "create sddc" in text:
            return
        elif "cancel" in text:
            message_handler(constant.CANCEL_SDDC, event)
            db.delete_event_db(event.get("user_id"))
            return
        elif text.find(" ") != -1:
            return
        elif is_network(text):
            if result.get("status") == "link_aws" or "subnet":
                if is_valid_network(text):
                    event.update(
                        {"vpc_cidr": event.get("text")}
                    )
                    event.update(result)
                    message_handler(constant.SDDC_CONFIRM, event)
                    db.write_event_db(
                        event.get("user_id"), 
                        {
                            "status": "vpc_cidr", 
                            "vpc_cidr": event.get("text")
                        }
                    )
                else:
                    message_handler(constant.WRONG_NETWORK, event)
            return
        else:
            if result.get("status") == "region":
                if result.get("max_hosts") == 1:
                    message_handler(constant.LINK_AWS, event)
                else:
                    message_handler(constant.SINGLE_MULTI, event)
                db.write_event_db(
                    event.get("user_id"), 
                    {
                        "status": "sddc_name", 
                        "sddc_name": event.get("text")
                    }
                )
            elif result.get("status") in constant.INT_STATUS:
                message_handler(constant.ASK_SELECT_BUTTON, event)
            return
    elif "delete" in result.get("command"):
        if "cancel" in text:
            message_handler(constant.CANCEL_DELETE, event)
            db.delete_event_db(event.get("user_id"))
    elif "restore" in result.get("command"):
        if "cancel" in text:
            message_handler(constant.CANCEL_RESTORE, event)
            db.delete_event_db(event.get("user_id"))
    elif "register org" in result.get("command"):
        if "cancel" in text:
            message_handler(constant.CANCEL_ORG, event)
            db.delete_event_db(event.get("user_id"))
            db.delete_cred_db(event.get("user_id"))
        elif "registering org" in result.get("status"):
            db.write_cred_db(
                event.get("user_id"), 
                {
                    "org_id": event.get("text")
                }
            )
            db.write_event_db(
                event.get("user_id"), 
                {
                    "status": "registering token"
                }
            )
            message_handler(constant.REGISTER_TOKEN, event)
        elif "registering token" in result.get("status"):
                user_name = validate_token(event.get("text"), __cred_data.get("org_id"))
                if user_name is not None:
                    message_handler(constant.SUCCESS_TOKEN, event)
                    db.write_cred_db(
                        event.get("user_id"), 
                        {
                            "status": "registered", 
                            "token": event.get("text"), 
                            "user_name": user_name
                        }
                    )
                    db.delete_event_db(event.get("user_id"))
                else:
                    message_handler(constant.FAILED_TOKEN, event)
                    message_handler(constant.WRONG_TOKEN, event)
                    db.delete_event_db(event.get("user_id"))
                    db.delete_cred_db(event.get("user_id"))