def _create_rds_instance()

in footmark/rds/connection.py [0:0]


    def _create_rds_instance(self, db_engine, engine_version, db_instance_class, db_instance_storage,
                            instance_net_type, security_ip_list, pay_type, period=None,zone=None,
                            instance_description=None, used_time=None, instance_network_type=None, connection_mode=None,
                            vpc_id=None, vswitch_id=None, private_ip_address=None, allocate_public_ip=False,
                            connection_string_prefix=None, public_port=None, db_name=None, db_description=None,
                            character_set_name=None, maint_window=None, preferred_backup_time=None,
                            preferred_backup_period=None, backup_retention_period=None, db_tags=None, wait=None,
                            wait_timeout=None, client_token=None):
        """
         Create RDS Instance

         :type zone: string
         :param zone:  ID of a zone to which an instance belongs
         :type db_engine: string
         :param db_engine: The type of database
         :type engine_version: string
         :param engine_version: Version number of the database engine to use
         :type db_instance_class: string
         :param db_instance_class: The instance type of the database.
         :type db_instance_storage: integer
         :param db_instance_storage: Size in gigabytes of the initial storage for the DB instance.
         :type instance_net_type: string
         :param instance_net_type: The net type of the DB instance
         :type instance_description: string
         :param instance_description: Instance description or remarks, no more than 256 bytes.
         :type security_ip_list: string
         :param security_ip_list: List of IP addresses allowed to access all databases of an instance.
         :type pay_type: string
         :param pay_type: The pay type of the DB instance.
         :type period: string
         :param period: Period of the instance if pay_type set to prepaid.
         :type used_time: integer
         :param used_time: This parameter specifies the duration for purchase.
         :type instance_network_type: string
         :param instance_network_type: The network type of the instance.
         :type connection_mode: string
         :param connection_mode: The connection mode of the instance
         :type vpc_id: string
         :param vpc_id: The ID of the VPC
         :type vswitch_id: string
         :param vswitch_id: ID of VSwitch
         :type private_ip_address: string
         :param private_ip_address: IP address of an VPC under VSwitchId.
         :type allocate_public_ip: string
         :param allocate_public_ip: Whether to allocate public IP
         :type connection_string_prefix: string
         :param connection_string_prefix: Prefix of an Internet connection string
         :type public_port: integer
         :param public_port: The public connection port.
         :type db_name: string
         :param db_name: Name of a database to create within the instance.
         :type db_description: string
         :param db_description: Description of a database to create within the instance.
         :type character_set_name: string
         :param character_set_name: Associate the DB instance with a specified character set.
         :type maint_window: string
         :param maint_window: Maintenance window in format of ddd:hh24:mi-ddd:hh24:mi.
         :type preferred_backup_time: string
         :param preferred_backup_time: Backup time, in the format of HH:mmZ- HH:mm Z.
         :type preferred_backup_period: string
         :param preferred_backup_period: Backup period.
         :type backup_retention_period: integer
         :param backup_retention_period: Retention days of the backup
         :type db_tags: dict
         :param db_tags: A list of hash/dictionaries of db tags
         :type db_engine: string
         :param wait: Wait for the RDS instance to be 'running' before returning.
         :type wait_timeout: integer
         :param wait_timeout: how long before wait gives up, in seconds
         :return:
             changed: If instance is created successfully the changed will be set
                 to True else False
             DBInstanceId: the newly created instance id
         """
        params = {}
        results = []
        flag = False
        changed = False

        if zone:
            self.build_list_params(params, zone, 'ZoneId')
        if db_engine:
            self.build_list_params(params, db_engine, 'Engine')
        if engine_version:
            self.build_list_params(params, engine_version, 'EngineVersion')
        if db_instance_class:
            self.build_list_params(params, db_instance_class, 'DBInstanceClass')
        if db_instance_storage:
            self.build_list_params(params, db_instance_storage, 'DBInstanceStorage')
        if instance_net_type:
            self.build_list_params(params, instance_net_type, 'DBInstanceNetType')
        if instance_description:
            self.build_list_params(params, instance_description, 'DBInstanceDescription')
        if security_ip_list:
            self.build_list_params(params, security_ip_list, 'SecurityIPList')
        if pay_type:
            self.build_list_params(params, pay_type, 'PayType')
        if period:
            self.build_list_params(params, period, 'Period')
        if used_time:
            self.build_list_params(params, used_time, 'UsedTime')
        if instance_network_type:
            self.build_list_params(params, instance_network_type, 'InstanceNetworkType')
        if connection_mode:
            self.build_list_params(params, connection_mode, 'ConnectionMode')
        if vpc_id:
            self.build_list_params(params, vpc_id, 'VPCId')
        if vswitch_id:
            self.build_list_params(params, vswitch_id, 'VSwitchId')
        if private_ip_address:
            self.build_list_params(params, private_ip_address, 'PrivateIpAddress')
        if client_token:
            self.build_list_params(params, client_token, 'ClientToken')
        try:
            results_instance = self.get_status('CreateDBInstance', params)
            if results_instance:
                results.append({"DBInstanceId": results_instance["DBInstanceId"]})
            changed = True
        except Exception as ex:
            custom_msg = [
                "The API is showing None error code and error message while creating rds instance",
                "Following error occur while creating rds instance: ",
                "Failed to create rds instance with error code "
            ]
            results = results + self.rds_error_handler(ex, custom_msg)

        else:
            time.sleep(240)
            # Start newly created Instance
            # wait until instance status becomes running
            if allocate_public_ip and self.check_instance_status(results_instance['DBInstanceId']):
                if connection_string_prefix and public_port:
                    if instance_net_type == "Intranet":
                        changed_conn_str, result_conn_str = \
                            self.create_instance_public_connection_string(results_instance['DBInstanceId'],
                                                                          connection_string_prefix, public_port)
                    elif instance_net_type == "Internet":
                        changed_conn_str, result_conn_str = \
                            self.modify_instance_public_connection(results_instance['DBInstanceId'],
                                                                   results_instance['ConnectionString'],
                                                                   connection_string_prefix, public_port)

                    if 'error' in (''.join(str(result_conn_str))).lower():
                        results.append(result_conn_str)

            if self.check_instance_status(results_instance['DBInstanceId']):
                if db_name or character_set_name:
                    changed_create_db, result_create_db = self.create_database(results_instance['DBInstanceId'],
                                                                               db_name, db_description,
                                                                               character_set_name)

                    if 'error' in (''.join(str(result_create_db))).lower():
                        results.append(result_create_db)

            if maint_window and self.check_instance_status(results_instance['DBInstanceId']):
                changed_maint_window, result_maint_window = \
                    self.modify_db_instance_maint_time(results_instance['DBInstanceId']
                                                            , maint_window)
                if 'error' in (''.join(str(result_maint_window))).lower():
                    results.append(result_maint_window)

            if preferred_backup_time and preferred_backup_period:
                if self.check_instance_status(results_instance['DBInstanceId']):
                    changed_backup_policy, result_backup_policy = \
                        self.modify_backup_policy(results_instance['DBInstanceId'],
                                                  preferred_backup_time, preferred_backup_period,
                                                  backup_retention_period)
                    if 'error' in (''.join(str(result_backup_policy))).lower():
                        results.append(result_backup_policy)

            if db_tags:
                changed_tags, result_db_tags = self.bind_tags(results_instance['DBInstanceId'], db_tags)
                if 'error' in (''.join(str(result_db_tags))).lower():
                    results.append(result_db_tags)

            if str(wait).lower() in ['yes', 'true'] and wait_timeout:
                time.sleep(wait_timeout)

        return changed, results