-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: Add MKC Code Examples #5359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
92f4fa7
feat: provide code examples for managedkafka
salmany 7c0f5a8
Updated headers.
salmany 8a63cc4
Reset buffer for sub-tests.
salmany ece4a29
Align code samples with TF / Python / Java samples.
salmany 88d93e5
Add comment about accessible subnets and DNS domains.
salmany eb1c642
Fix memory defaults for connect cluster example.
salmany c1cec6f
Fix name of MM2 Source sample.
salmany 280815f
Fix sample parameter for Cloud Storage Sink connector.
salmany ba822ac
Merge branch 'main' into mkc-code-examples
salmany 821ce11
Merge branch 'main' into mkc-code-examples
glasnt a09c17f
Merge branch 'main' into mkc-code-examples
salmany c0ebe16
Merge branch 'main' into mkc-code-examples
glasnt f562907
Fix linting errors.
salmany df7d665
Fix test error.
salmany 719bb76
Fix CreateBigQuerySinkConnector parameters.
salmany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package clusters | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/golang-samples/internal/managedkafka/fake" | ||
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
) | ||
|
||
const ( | ||
connectClusterPrefix = "connect-cluster" | ||
region = "us-central1" | ||
) | ||
|
||
func TestConnectClusters(t *testing.T) { | ||
tc := testutil.SystemTest(t) | ||
buf := new(bytes.Buffer) | ||
connectClusterID := fmt.Sprintf("%s-%d", connectClusterPrefix, time.Now().UnixNano()) | ||
kafkaClusterID := fmt.Sprintf("kafka-cluster-%d", time.Now().UnixNano()) | ||
options := fake.Options(t) | ||
|
||
// First create a Kafka cluster that the Connect cluster will reference | ||
kafkaClusterPath := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", tc.ProjectID, region, kafkaClusterID) | ||
|
||
t.Run("CreateConnectCluster", func(t *testing.T) { | ||
buf.Reset() | ||
if err := createConnectCluster(buf, tc.ProjectID, region, connectClusterID, kafkaClusterPath, options...); err != nil { | ||
t.Fatalf("failed to create a connect cluster: %v", err) | ||
} | ||
got := buf.String() | ||
want := "Created connect cluster" | ||
if !strings.Contains(got, want) { | ||
t.Fatalf("createConnectCluster() mismatch got: %s\nwant: %s", got, want) | ||
} | ||
}) | ||
t.Run("GetConnectCluster", func(t *testing.T) { | ||
buf.Reset() | ||
if err := getConnectCluster(buf, tc.ProjectID, region, connectClusterID, options...); err != nil { | ||
t.Fatalf("failed to get connect cluster: %v", err) | ||
} | ||
got := buf.String() | ||
want := "Got connect cluster" | ||
if !strings.Contains(got, want) { | ||
t.Fatalf("getConnectCluster() mismatch got: %s\nwant: %s", got, want) | ||
} | ||
}) | ||
t.Run("UpdateConnectCluster", func(t *testing.T) { | ||
buf.Reset() | ||
memoryBytes := int64(25769803776) // 24 GiB in bytes | ||
labels := map[string]string{"environment": "test"} | ||
if err := updateConnectCluster(buf, tc.ProjectID, region, connectClusterID, memoryBytes, labels, options...); err != nil { | ||
t.Fatalf("failed to update connect cluster: %v", err) | ||
} | ||
got := buf.String() | ||
want := "Updated connect cluster" | ||
if !strings.Contains(got, want) { | ||
t.Fatalf("updateConnectCluster() mismatch got: %s\nwant: %s", got, want) | ||
} | ||
}) | ||
t.Run("ListConnectClusters", func(t *testing.T) { | ||
buf.Reset() | ||
if err := listConnectClusters(buf, tc.ProjectID, region, options...); err != nil { | ||
t.Fatalf("failed to list connect clusters: %v", err) | ||
} | ||
got := buf.String() | ||
want := "Got connect cluster" | ||
if !strings.Contains(got, want) { | ||
t.Fatalf("listConnectClusters() mismatch got: %s\nwant: %s", got, want) | ||
} | ||
}) | ||
t.Run("DeleteConnectCluster", func(t *testing.T) { | ||
buf.Reset() | ||
if err := deleteConnectCluster(buf, tc.ProjectID, region, connectClusterID, options...); err != nil { | ||
t.Fatalf("failed to delete connect cluster: %v", err) | ||
} | ||
got := buf.String() | ||
want := "Deleted connect cluster" | ||
if !strings.Contains(got, want) { | ||
t.Fatalf("deleteConnectCluster() mismatch got: %s\nwant: %s", got, want) | ||
} | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package clusters | ||
|
||
// [START managedkafka_create_connect_cluster] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/managedkafka/apiv1/managedkafkapb" | ||
"google.golang.org/api/option" | ||
|
||
managedkafka "cloud.google.com/go/managedkafka/apiv1" | ||
) | ||
|
||
func createConnectCluster(w io.Writer, projectID, region, clusterID, kafkaCluster string, opts ...option.ClientOption) error { | ||
// projectID := "my-project-id" | ||
// region := "us-central1" | ||
// clusterID := "my-connect-cluster" | ||
// kafkaCluster := "projects/my-project-id/locations/us-central1/clusters/my-kafka-cluster" | ||
ctx := context.Background() | ||
client, err := managedkafka.NewManagedKafkaConnectClient(ctx, opts...) | ||
if err != nil { | ||
return fmt.Errorf("managedkafka.NewManagedKafkaConnectClient got err: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
locationPath := fmt.Sprintf("projects/%s/locations/%s", projectID, region) | ||
clusterPath := fmt.Sprintf("%s/connectClusters/%s", locationPath, clusterID) | ||
|
||
// Capacity configuration with 12 vCPU and 12 GiB RAM | ||
capacityConfig := &managedkafkapb.CapacityConfig{ | ||
VcpuCount: 12, | ||
MemoryBytes: 12884901888, // 12 GiB in bytes | ||
} | ||
|
||
// Optionally, you can also specify accessible subnets and resolvable DNS | ||
// domains as part of your network configuration. For example: | ||
// networkConfigs := []*managedkafkapb.ConnectNetworkConfig{ | ||
// { | ||
// PrimarySubnet: primarySubnet, | ||
// AdditionalSubnets: []string{"subnet-1", "subnet-2"}, | ||
// DnsDomainNames: []string{"domain-1", "domain-2"}, | ||
// }, | ||
// } | ||
|
||
connectCluster := &managedkafkapb.ConnectCluster{ | ||
Name: clusterPath, | ||
KafkaCluster: kafkaCluster, | ||
CapacityConfig: capacityConfig, | ||
} | ||
|
||
req := &managedkafkapb.CreateConnectClusterRequest{ | ||
Parent: locationPath, | ||
ConnectClusterId: clusterID, | ||
ConnectCluster: connectCluster, | ||
} | ||
op, err := client.CreateConnectCluster(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("client.CreateConnectCluster got err: %w", err) | ||
} | ||
// The duration of this operation can vary considerably, typically taking 5-15 minutes. | ||
resp, err := op.Wait(ctx) | ||
if err != nil { | ||
return fmt.Errorf("op.Wait got err: %w", err) | ||
} | ||
fmt.Fprintf(w, "Created connect cluster: %s\n", resp.Name) | ||
return nil | ||
} | ||
|
||
// [END managedkafka_create_connect_cluster] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package clusters | ||
|
||
// [START managedkafka_delete_connect_cluster] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/managedkafka/apiv1/managedkafkapb" | ||
"google.golang.org/api/option" | ||
|
||
managedkafka "cloud.google.com/go/managedkafka/apiv1" | ||
) | ||
|
||
func deleteConnectCluster(w io.Writer, projectID, region, clusterID string, opts ...option.ClientOption) error { | ||
// projectID := "my-project-id" | ||
// region := "us-central1" | ||
// clusterID := "my-connect-cluster" | ||
ctx := context.Background() | ||
client, err := managedkafka.NewManagedKafkaConnectClient(ctx, opts...) | ||
if err != nil { | ||
return fmt.Errorf("managedkafka.NewManagedKafkaConnectClient got err: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
clusterPath := fmt.Sprintf("projects/%s/locations/%s/connectClusters/%s", projectID, region, clusterID) | ||
req := &managedkafkapb.DeleteConnectClusterRequest{ | ||
Name: clusterPath, | ||
} | ||
op, err := client.DeleteConnectCluster(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("client.DeleteConnectCluster got err: %w", err) | ||
} | ||
err = op.Wait(ctx) | ||
if err != nil { | ||
return fmt.Errorf("op.Wait got err: %w", err) | ||
} | ||
fmt.Fprint(w, "Deleted connect cluster\n") | ||
return nil | ||
} | ||
|
||
// [END managedkafka_delete_connect_cluster] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.