github.com/aiven/aiven-go-client@v1.36.0/examples/kafka_connectors/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"time"
     7  
     8  	client "github.com/aiven/aiven-go-client"
     9  )
    10  
    11  func main() {
    12  	// Create new user client
    13  	c, err := client.NewUserClient(
    14  		os.Getenv("AIVEN_USERNAME"),
    15  		os.Getenv("AIVEN_PASSWORD"), "aiven-go-client-test/"+client.Version())
    16  	if err != nil {
    17  		log.Fatalf("user authentication error: %s", err)
    18  	}
    19  
    20  	// Create new project
    21  	project, err := c.Projects.Create(client.CreateProjectRequest{
    22  		CardID:  client.ToStringPointer(os.Getenv("AIVEN_CARD_ID")),
    23  		Cloud:   client.ToStringPointer("google-europe-west1"),
    24  		Project: "test-kafka-con1",
    25  	})
    26  	if err != nil {
    27  		log.Fatalf("project creation error: %s", err)
    28  	}
    29  
    30  	// Create new Kafka service inside the project
    31  	userConfig := make(map[string]interface{})
    32  	userConfig["kafka_version"] = "2.4"
    33  	userConfig["kafka_connect"] = true
    34  
    35  	kService, err := c.Services.Create(project.Name, client.CreateServiceRequest{
    36  		Cloud:                 "google-europe-west1",
    37  		GroupName:             "default",
    38  		MaintenanceWindow:     nil,
    39  		Plan:                  "business-4",
    40  		ProjectVPCID:          nil,
    41  		ServiceName:           "my-test-kafka",
    42  		ServiceType:           "kafka",
    43  		TerminationProtection: false,
    44  		UserConfig:            userConfig,
    45  		ServiceIntegrations:   nil,
    46  	})
    47  	if err != nil {
    48  		log.Fatalf("cannot create new Kafka service, error: %s", err)
    49  	}
    50  
    51  	// Create new Elasticsearch service inside the project
    52  	userConfig = make(map[string]interface{})
    53  	userConfig["elasticsearch_version"] = "7"
    54  
    55  	esService, err := c.Services.Create(project.Name, client.CreateServiceRequest{
    56  		Cloud:                 "google-europe-west1",
    57  		GroupName:             "default",
    58  		MaintenanceWindow:     nil,
    59  		Plan:                  "startup-4",
    60  		ProjectVPCID:          nil,
    61  		ServiceName:           "my-test-elasticsearch",
    62  		ServiceType:           "elasticsearch",
    63  		TerminationProtection: false,
    64  		UserConfig:            userConfig,
    65  		ServiceIntegrations:   nil,
    66  	})
    67  	if err != nil {
    68  		log.Fatalf("cannot create new Elasticsearch service, error: %s", err)
    69  	}
    70  
    71  	for {
    72  		err = c.KafkaConnectors.Create(project.Name, kService.Name, client.KafkaConnectorConfig{
    73  			"topics":              "TestT1",
    74  			"connection.username": esService.URIParams["user"],
    75  			"name":                "es-connector",
    76  			"connection.password": esService.URIParams["password"],
    77  			"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
    78  			"type.name":           "es-connector",
    79  			"connection.url":      " https://" + esService.URIParams["host"] + ":" + esService.URIParams["port"],
    80  		})
    81  
    82  		if err != nil {
    83  			if err.(client.Error).Status == 501 {
    84  				log.Println("Kafka service is not fully up and running, wait 10 seconds and try again ...")
    85  				time.Sleep(10 * time.Second)
    86  				continue
    87  			}
    88  
    89  			if err.(client.Error).Status == 503 {
    90  				log.Println("Kafka service is not available, try again in 10 seconds ...")
    91  				time.Sleep(10 * time.Second)
    92  				continue
    93  			}
    94  
    95  			if client.IsNotFound(err) {
    96  				log.Println("Kafka service is not found by some reason, try again in 10 seconds ...")
    97  				time.Sleep(10 * time.Second)
    98  				continue
    99  			}
   100  
   101  			log.Fatalf("cannot create new Kafka connector, error: %s", err)
   102  		}
   103  
   104  		break
   105  	}
   106  
   107  	listCon, err := c.KafkaConnectors.List(project.Name, kService.Name)
   108  	if err != nil {
   109  		log.Fatalf("cannot get a Kafka Connectors list, error: %s", err)
   110  	}
   111  
   112  	for _, c := range listCon.Connectors {
   113  		log.Printf("Kafka connector: %v", c)
   114  	}
   115  }