Skip to content

example of list clusters #15

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions examples/clickhouse/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This is an example of how to list the clusters you have, and get the output in a pythonic object structure

import doublecloud


sa_key = json.loads(open('authorized_key.json').read())
sdk = doublecloud.SDK(service_account_key=sa_key)

from google.protobuf.wrappers_pb2 import Int64Value

from doublecloud.clickhouse.v1.cluster_pb2 import ClusterResources
from doublecloud.clickhouse.v1.cluster_service_pb2 import CreateClusterRequest, ListClustersRequest
from doublecloud.clickhouse.v1.cluster_service_pb2_grpc import ClusterServiceStub,ClusterService

cluster_service = sdk.client(ClusterServiceStub)

clusters = cluster_service.List(
ListClustersRequest(
project_id='<project_id>'
)
)

import json
from google.protobuf.json_format import MessageToDict

def protobuf_to_native(protobuf_message):
"""
Serialize a protobuf message to native Python objects.

Args:
protobuf_message: The protobuf message to be serialized.

Returns:
A native Python representation (nested dictionaries and lists) of the protobuf message.
"""
try:
native_data = MessageToDict(protobuf_message, preserving_proto_field_name=True)
return native_data
except Exception as e:
print(f"Error occurred while serializing to native Python objects: {e}")
return None

native_clusters = protobuf_to_native(clusters)

print(native_clusters)