예제 #1
0
def upload(
    ctx: click.Context,
    configfile: str,
    name: str,
    dry_run: bool,
) -> int:
    """Upload the connector configuration from a file."""
    config = ctx.obj["config"]
    connect = Connect(config.connect_url)

    with open(configfile) as f:
        connect_config = json.load(f)

    # Ensure connector name is consistent
    connect_config["name"] = name

    # Validate the connector configuration only.
    if dry_run:
        validation = connect.validate(
            name=connect_config["connector.class"],
            connect_config=json.dumps(connect_config),
        )
        click.echo(validation)
        return 0

    click.echo(f"Uploading {name} connector configuration...")
    click.echo(connect.validate_and_create(name, json.dumps(connect_config)))
    return 0
def test_validate() -> None:
    connect = Connect(connect_url="http://localhost:8083")
    connect_config = InfluxConfig()
    connect_config.update_topics(["t1", "t2", "t3"])
    result = connect.validate(name="InfluxSinkConnector",
                              connect_config=connect_config.asjson())
    assert "error_count" in result
예제 #3
0
def create_s3_sink(
    ctx: click.Context,
    configfile: str,
    name: str,
    aws_access_key_id: str,
    aws_secret_access_key: str,
    dry_run: bool,
    show_status: bool,
    show_status_interval: int,
) -> int:
    """Create an instance of the S3 Sink connector.

    Use the --show-status option to output status.
    """
    # Get configuration from the parent command
    if ctx.parent:
        parent_config = ctx.parent.obj["config"]

    connect = Connect(parent_config.connect_url)

    with open(configfile) as f:
        config = json.load(f)

    if name:
        click.echo("Updating connector name.")
        config["name"] = name

    if None in (aws_access_key_id, aws_secret_access_key):
        click.echo(
            "Could not get the AWS credentials. "
            "Use the --access-key-id and --aws-secret-access-key options "
            "or set the AWS credentials using the AWS_ACCESS_KEY_ID and "
            "AWS_SECRET_ACCESS_KEY env variables.")
        return 1

    config["aws.access.key.id"] = aws_access_key_id
    config["aws.secret.access.key"] = aws_secret_access_key

    # Validate the configuration only.
    if dry_run:
        validation = connect.validate(
            name=config["connector.class"],
            connect_config=json.dumps(config),
        )
        click.echo(validation)
        return 0

    name = config["name"]
    click.echo(f"Creating the {name} connector...")
    click.echo(connect.validate_and_create(name, json.dumps(config)))
    if show_status:
        while True:
            time.sleep(int(show_status_interval) / 1000)
            try:
                click.echo(connect.status(name=name))
            except KeyboardInterrupt:
                raise click.ClickException("Interruped.")

    return 0
예제 #4
0
def create_jdbc_sink(
    ctx: click.Context,
    configfile: str,
    name: str,
    dry_run: bool,
    show_status: bool,
    show_status_interval: int,
) -> int:
    """Create an instance of the JDBC Sink connector.

    Use the --show-status option to output status.
    """
    # Get configuration from the parent command
    if ctx.parent:
        parent_config = ctx.parent.obj["config"]

    connect = Connect(parent_config.connect_url)

    with open(configfile) as f:
        config = json.load(f)

    # Override connector name in the configuration
    if name:
        config["name"] = name

    # Validate the configuration only.
    if dry_run:
        validation = connect.validate(
            name=config["connector.class"],
            connect_config=json.dumps(config),
        )
        click.echo(validation)
        return 0

    name = config["name"]
    click.echo(f"Creating the {name} connector...")
    click.echo(connect.validate_and_create(name, json.dumps(config)))
    if show_status:
        while True:
            time.sleep(int(show_status_interval) / 1000)
            try:
                click.echo(connect.status(name=name))
            except KeyboardInterrupt:
                raise click.ClickException("Interruped.")

    return 0
예제 #5
0
def test_validate() -> None:
    """Test validate method."""
    connect = Connect(connect_url="http://localhost:8083")
    connect_config = InfluxConfig(
        name="influxdb-sink",
        connect_influx_url="http://localhost:8086",
        connect_influx_db="mydb",
        tasks_max=1,
        connect_influx_username="******",
        connect_influx_password="******",
        connect_influx_error_policy="foo",
        connect_influx_max_retries="1",
        connect_influx_retry_interval="1",
        connect_progress_enabled=True,
    )
    connect_config.update_topics(["t1", "t2", "t3"])
    result = connect.validate(name="InfluxSinkConnector",
                              connect_config=connect_config.asjson())
    assert "error_count" in result
예제 #6
0
def create_influxdb_sink(
    ctx: click.Context,
    topiclist: tuple,
    name: str,
    connect_influx_url: str,
    connect_influx_db: str,
    tasks_max: str,
    connect_influx_username: str,
    connect_influx_password: str,
    topic_regex: str,
    dry_run: bool,
    auto_update: bool,
    validate: bool,
    check_interval: str,
    excluded_topic_regex: str,
    connect_influx_error_policy: str,
    connect_influx_max_retries: str,
    connect_influx_retry_interval: str,
    connect_progress_enabled: str,
    timestamp: str,
) -> int:
    """Create an instance of the InfluxDB Sink connector.

    A list of topics can be specified using the TOPICLIST argument.
    If not, topics are discovered from Kafka. Use the ``--topic-regex`` and
    ``--excluded_topics`` options to help in selecting the topics
    that you want to write to InfluxDB. To check for new topics and update
    the connector configuration use the
    ``--auto-update`` and ``--check-interval`` options.
    """
    # Get configuration from the main command
    if ctx.parent:
        config = ctx.parent.obj["config"]
    # Connector configuration
    influx_config = InfluxConfig(
        name=name,
        connect_influx_url=connect_influx_url,
        connect_influx_db=connect_influx_db,
        tasks_max=int(tasks_max),
        connect_influx_username=connect_influx_username,
        connect_influx_password=connect_influx_password,
        connect_influx_error_policy=connect_influx_error_policy,
        connect_influx_max_retries=connect_influx_max_retries,
        connect_influx_retry_interval=connect_influx_retry_interval,
        connect_progress_enabled=(connect_progress_enabled == "true"),
    )
    # The variadic argument is a tuple
    topics: List[str] = list(topiclist)
    if not topics:
        click.echo("Discoverying Kafka topics...")
        topics = Topic(config.broker_url, topic_regex,
                       excluded_topic_regex).names
        n = 0 if not topics else len(topics)
        click.echo(f"Found {n} topics.")
    connect = Connect(connect_url=config.connect_url)
    if topics:
        influx_config.update_topics(topics, timestamp)
        # --validate option
        if validate:
            click.echo(
                connect.validate(
                    name=influx_config.connector_class,
                    connect_config=influx_config.asjson(),
                ))
            return 0
        # --dry-run option returns the connector configuration
        if dry_run:
            click.echo(influx_config.asjson())
            return 0
        # Validate configuration before creating the connector
        validation = connect.validate(
            name=influx_config.connector_class,
            connect_config=influx_config.asjson(),
        )
        try:
            error_count = json.loads(validation)["error_count"]
            click.echo(f"Validation returned {error_count} error(s).")
            if error_count > 0:
                click.echo(
                    "Use the ``--validate`` option to return the validation "
                    "results.")
                return 1
        except Exception:
            click.echo(validation)
            return 1
        click.echo(f"Uploading {name} connector configuration...")
        connect.create_or_update(name=name,
                                 connect_config=influx_config.asjson())
    if auto_update:
        while True:
            time.sleep(int(check_interval) / 1000)
            try:
                # Current list of topics from Kafka
                current_topics = Topic(config.broker_url, topic_regex,
                                       excluded_topic_regex).names
                new_topics = list(set(current_topics) - set(topics))
                if new_topics:
                    click.echo("Found new topics, updating the connector...")
                    influx_config.update_topics(current_topics, timestamp)
                    connect.create_or_update(
                        name=name, connect_config=influx_config.asjson())
                    topics = current_topics
            except KeyboardInterrupt:
                raise click.ClickException("Interruped.")
    return 0
예제 #7
0
def create_mirrormaker2(
    ctx: click.Context,
    name: str,
    heartbeat_configfile: str,
    checkpoint_configfile: str,
    mirror_source_configfile: str,
    dry_run: bool,
    show_status: bool,
    show_status_interval: int,
) -> int:
    """Create an instance of the MirrorMaker 2 connectors.

    Create the heartbeat, checkpoint and mirror-source
    connectors. Use the --show-status option to output status.
    """
    # Get configuration from the main command
    if ctx.parent:
        config = ctx.parent.obj["config"]

    connect = Connect(config.connect_url)

    with open(heartbeat_configfile) as f:
        heartbeat_config = json.load(f)

    with open(checkpoint_configfile) as f:
        checkpoint_config = json.load(f)

    with open(mirror_source_configfile) as f:
        mirror_source_config = json.load(f)

    # Override connector name in the configuration
    if name:
        heartbeat_config["name"] = f"{name}-heartbeat"
        checkpoint_config["name"] = f"{name}-checkpoint"
        mirror_source_config["name"] = f"{name}-mirror-source"

    # Validate the configuration only.
    if dry_run:
        heartbeat_validation = connect.validate(
            name=heartbeat_config["connector.class"],
            connect_config=json.dumps(heartbeat_config),
        )
        click.echo(heartbeat_validation)
        checkpoint_validation = connect.validate(
            name=checkpoint_config["connector.class"],
            connect_config=json.dumps(checkpoint_config),
        )
        click.echo(checkpoint_validation)
        mirror_source_validation = connect.validate(
            name=mirror_source_config["connector.class"],
            connect_config=json.dumps(mirror_source_config),
        )
        click.echo(mirror_source_validation)
        return 0

    heartbeat_name = heartbeat_config["name"]
    click.echo(f"Creating the {heartbeat_name} connector...")
    click.echo(
        connect.validate_and_create(heartbeat_name,
                                    json.dumps(heartbeat_config)))
    checkpoint_name = checkpoint_config["name"]
    click.echo(f"Creating the {checkpoint_name} connector...")
    click.echo(
        connect.validate_and_create(checkpoint_name,
                                    json.dumps(checkpoint_config)))
    mirror_source_name = mirror_source_config["name"]
    click.echo(f"Creating the {mirror_source_name} connector...")
    click.echo(
        connect.validate_and_create(mirror_source_name,
                                    json.dumps(mirror_source_config)))

    if show_status:
        while True:
            time.sleep(int(show_status_interval) / 1000)
            try:
                click.echo(connect.status(name=heartbeat_name))
                click.echo(connect.status(name=checkpoint_name))
                click.echo(connect.status(name=mirror_source_name))
            except KeyboardInterrupt:
                raise click.ClickException("Interruped.")

    return 0
예제 #8
0
def create_s3_sink(
    ctx: click.Context,
    topiclist: tuple,
    name: str,
    s3_bucket_name: str,
    s3_region: str,
    topics_dir: str,
    flush_size: int,
    rotate_interval_ms: int,
    partition_duration_ms: int,
    path_format: str,
    tasks_max: int,
    topic_regex: str,
    dry_run: bool,
    auto_update: bool,
    validate: bool,
    check_interval: int,
    excluded_topics: str,
    locale: str,
    timezone: str,
    timestamp_extractor: str,
    timestamp_field: str,
) -> int:
    """Create an instance of the S3 Sink connector.

    A list of topics can be specified using the TOPICLIST argument.
    If not, topics are discovered from Kafka. Use the ``--topic-regex`` and
    ``--excluded_topics`` options to help in selecting the topics
    that you want to write to S3. To check for new topics and update
    the connector configuration use the
    ``--auto-update`` and ``--check-interval`` options.
    """
    # Connector configuration
    s3config = S3Config(
        name=name,
        s3_bucket_name=s3_bucket_name,
        s3_region=s3_region,
        topics_dir=topics_dir,
        flush_size=flush_size,
        rotate_interval_ms=rotate_interval_ms,
        partition_duration_ms=partition_duration_ms,
        path_format=path_format,
        tasks_max=tasks_max,
        locale=locale,
        timezone=timezone,
        timestamp_extractor=timestamp_extractor,
        timestamp_field=timestamp_field,
    )
    if ctx.parent:
        config = ctx.parent.obj["config"]
    # The variadic argument is a tuple
    topics: List[str] = list(topiclist)
    if not topics:
        click.echo("Discoverying Kafka topics...")
        topics = Topic(config.broker_url, topic_regex, excluded_topics).names
        n = 0 if not topics else len(topics)
        click.echo(f"Found {n} topics.")
    connect = Connect(connect_url=config.connect_url)
    if topics:
        s3config.update_topics(topics)
        # --validate option
        if validate:
            click.echo(
                connect.validate(
                    name=s3config.connector_class,
                    connect_config=s3config.asjson(),
                ))
            return 0
        # --dry-run option returns the connector configuration
        if dry_run:
            click.echo(s3config.asjson())
            return 0
        # Validate configuration before creating the connector
        validation = connect.validate(
            name=s3config.connector_class,
            connect_config=s3config.asjson(),
        )
        try:
            error_count = json.loads(validation)["error_count"]
            click.echo(f"Validation returned {error_count} error(s).")
            if error_count > 0:
                click.echo(
                    "Use the ``--validate`` option to return the validation "
                    "results.")
                return 0
        except Exception:
            click.echo(validation)
            return 1
        click.echo(f"Uploading {name} connector configuration...")
        connect.create_or_update(name=name, connect_config=s3config.asjson())
    if auto_update:
        while True:
            time.sleep(int(check_interval) / 1000)
            try:
                # Current list of topics from Kafka
                current_topics = Topic(config.broker_url, topic_regex,
                                       excluded_topics).names
                new_topics = list(set(current_topics) - set(topics))
                if new_topics:
                    click.echo("Found new topics, updating the connector...")
                    s3config.update_topics(current_topics)
                    connect.create_or_update(name=name,
                                             connect_config=s3config.asjson())
                    topics = current_topics
            except KeyboardInterrupt:
                raise click.ClickException("Interruped.")
    return 0