Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions managedkafka/snippets/clusters/create_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_create_cluster]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import managedkafka_v1


def create_cluster(
project_id: str,
Expand All @@ -40,6 +36,17 @@ def create_cluster(
This method will raise the exception if the operation errors or
the timeout before the operation completes is reached.
"""
# [START managedkafka_create_cluster]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"
# subnet = "projects/my-project-id/regions/us-central1/subnetworks/default"
# cpu = 3
# memory_bytes = 3221225472

client = managedkafka_v1.ManagedKafkaClient()

Expand Down Expand Up @@ -67,7 +74,6 @@ def create_cluster(
response = operation.result()
print("Created cluster:", response)
except GoogleAPICallError:
print(operation.operation.error)

print("The operation failed with error:", operation.operation.error)

# [END managedkafka_create_cluster]
# [END managedkafka_create_cluster]
17 changes: 10 additions & 7 deletions managedkafka/snippets/clusters/delete_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_delete_cluster]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import managedkafka_v1


def delete_cluster(
project_id: str,
Expand All @@ -34,6 +30,14 @@ def delete_cluster(
This method will raise the exception if the operation errors or
the timeout before the operation completes is reached.
"""
# [START managedkafka_delete_cluster]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"

client = managedkafka_v1.ManagedKafkaClient()

Expand All @@ -46,7 +50,6 @@ def delete_cluster(
operation.result()
print("Deleted cluster")
except GoogleAPICallError:
print(operation.operation.error)

print("The operation failed with error:", operation.operation.error)

# [END managedkafka_delete_cluster]
# [END managedkafka_delete_cluster]
15 changes: 9 additions & 6 deletions managedkafka/snippets/clusters/get_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_get_cluster]
from google.cloud import managedkafka_v1


def get_cluster(
project_id: str,
region: str,
cluster_id: str,
) -> managedkafka_v1.Cluster:
):
"""
Get a Kafka cluster.

Expand All @@ -29,6 +26,13 @@ def get_cluster(
region: Cloud region.
cluster_id: ID of the Kafka cluster.
"""
# [START managedkafka_get_cluster]
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"

client = managedkafka_v1.ManagedKafkaClient()

Expand All @@ -42,5 +46,4 @@ def get_cluster(

return cluster


# [END managedkafka_get_cluster]
# [END managedkafka_get_cluster]
16 changes: 8 additions & 8 deletions managedkafka/snippets/clusters/list_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_list_clusters]
from typing import List

from google.cloud import managedkafka_v1


def list_clusters(
project_id: str,
region: str,
) -> List[str]:
):
"""
List Kafka clusters in a given project ID and region.

Args:
project_id: Google Cloud project ID.
region: Cloud region.
"""
# [START managedkafka_list_clusters]
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"

client = managedkafka_v1.ManagedKafkaClient()

Expand All @@ -42,5 +43,4 @@ def list_clusters(

return [cluster.name for cluster in response]


# [END managedkafka_list_clusters]
# [END managedkafka_list_clusters]
23 changes: 14 additions & 9 deletions managedkafka/snippets/clusters/update_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_update_cluster]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import managedkafka_v1
from google.protobuf import field_mask_pb2


def update_cluster(
project_id: str, region: str, cluster_id: str, memory_bytes: int
) -> None:
"""
Update a Kafka cluster. For a list of editable fields, one can check https://cloud.google.com/managed-kafka/docs/create-cluster#properties.
Update a Kafka cluster.

Args:
project_id: Google Cloud project ID.
Expand All @@ -34,6 +29,16 @@ def update_cluster(
This method will raise the exception if the operation errors or
the timeout before the operation completes is reached.
"""
# [START managedkafka_update_cluster]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import managedkafka_v1
from google.protobuf import field_mask_pb2

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"
# memory_bytes = 4295000000

client = managedkafka_v1.ManagedKafkaClient()

Expand All @@ -43,6 +48,7 @@ def update_cluster(
update_mask = field_mask_pb2.FieldMask()
update_mask.paths.append("capacity_config.memory_bytes")

# For a list of editable fields, one can check https://cloud.google.com/managed-kafka/docs/create-cluster#properties.
request = managedkafka_v1.UpdateClusterRequest(
update_mask=update_mask,
cluster=cluster,
Expand All @@ -53,7 +59,6 @@ def update_cluster(
response = operation.result()
print("Updated cluster:", response)
except GoogleAPICallError:
print(operation.operation.error)

print("The operation failed with error:", operation.operation.error)

# [END managedkafka_update_cluster]
# [END managedkafka_update_cluster]
16 changes: 10 additions & 6 deletions managedkafka/snippets/consumergroups/delete_consumer_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_delete_consumergroup]
from google.api_core.exceptions import NotFound
from google.cloud import managedkafka_v1


def delete_consumer_group(
project_id: str,
Expand All @@ -35,6 +31,15 @@ def delete_consumer_group(
Raises:
This method will raise the exception if the consumer group is not found.
"""
# [START managedkafka_delete_consumergroup]
from google.api_core.exceptions import NotFound
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"
# consumer_group_id = "my-consumer-group"

client = managedkafka_v1.ManagedKafkaClient()

Expand All @@ -51,5 +56,4 @@ def delete_consumer_group(
except NotFound:
print(f"Consumer group {consumer_group_path} not found")


# [END managedkafka_delete_consumergroup]
# [END managedkafka_delete_consumergroup]
16 changes: 10 additions & 6 deletions managedkafka/snippets/consumergroups/get_consumer_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_get_consumergroup]
from google.cloud import managedkafka_v1


def get_consumer_group(
project_id: str,
region: str,
cluster_id: str,
consumer_group_id: str,
) -> managedkafka_v1.ConsumerGroup:
):
"""
Get a Kafka consumer group.

Expand All @@ -31,6 +28,14 @@ def get_consumer_group(
cluster_id: ID of the Kafka cluster.
consumer_group_id: ID of the Kafka consumer group.
"""
# [START managedkafka_get_consumergroup]
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"
# consumer_group_id = "my-consumer-group"

client = managedkafka_v1.ManagedKafkaClient()

Expand All @@ -46,5 +51,4 @@ def get_consumer_group(

return consumer_group


# [END managedkafka_get_consumergroup]
# [END managedkafka_get_consumergroup]
16 changes: 9 additions & 7 deletions managedkafka/snippets/consumergroups/list_consumer_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_list_consumergroups]
from typing import List

from google.cloud import managedkafka_v1


def list_consumer_groups(
project_id: str,
region: str,
cluster_id: str,
) -> List[str]:
):
"""
List Kafka consumer groups in a cluster.

Expand All @@ -31,6 +26,13 @@ def list_consumer_groups(
region: Cloud region.
cluster_id: ID of the Kafka cluster.
"""
# [START managedkafka_list_consumergroups]
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"

client = managedkafka_v1.ManagedKafkaClient()

Expand All @@ -44,4 +46,4 @@ def list_consumer_groups(

return [consumer_group.name for consumer_group in response]

# [END managedkafka_list_consumergroups]
# [END managedkafka_list_consumergroups]
20 changes: 13 additions & 7 deletions managedkafka/snippets/consumergroups/update_consumer_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START managedkafka_update_consumergroup]
from google.api_core.exceptions import NotFound
from google.cloud import managedkafka_v1
from google.protobuf import field_mask_pb2


def update_consumer_group(
project_id: str,
Expand All @@ -40,6 +35,18 @@ def update_consumer_group(
Raises:
This method will raise the exception if the consumer group is not found.
"""
# [START managedkafka_update_consumergroup]
from google.api_core.exceptions import NotFound
from google.cloud import managedkafka_v1
from google.protobuf import field_mask_pb2

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"
# consumer_group_id = "my-consumer-group"
# topic_path = "my-topic-path"
# partition_offsets = {10: 10}

client = managedkafka_v1.ManagedKafkaClient()

Expand Down Expand Up @@ -70,5 +77,4 @@ def update_consumer_group(
except NotFound:
print(f"Consumer group {consumer_group.name} not found")


# [END managedkafka_update_consumergroup]
# [END managedkafka_update_consumergroup]
4 changes: 4 additions & 0 deletions managedkafka/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
protobuf==5.27.2
pytest==8.2.2
google-api-core==2.23.0
google-auth==2.36.0
google-cloud-managedkafka==0.1.4
googleapis-common-protos==1.65.0
Loading