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