-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,82 @@ | ||
# Copyright Materialize, Inc. and contributors. All rights reserved. | ||
# | ||
# Use of this software is governed by the Business Source License | ||
# included in the LICENSE file at the root of this repository. | ||
# | ||
# As of the Change Date specified in that file, in accordance with | ||
# the Business Source License, use of this software will be governed | ||
# by the Apache License, Version 2.0. | ||
|
||
import os | ||
import time | ||
from typing import Any | ||
|
||
import requests | ||
|
||
|
||
class RedpandaCloud: | ||
def __init__(self) -> None: | ||
client_id = os.environ["REDPANDA_CLOUD_CLIENT_ID"] | ||
client_secret = os.environ["REDPANDA_CLOUD_CLIENT_SECRET"] | ||
|
||
response = requests.post( | ||
"https://auth.prd.cloud.redpanda.com/oauth/token", | ||
json={ | ||
"client_id": client_id, | ||
"client_secret": client_secret, | ||
"audience": "cloudv2-production.redpanda.cloud", | ||
"grant_type": "client_credentials", | ||
}, | ||
) | ||
if response.status_code != 200: | ||
print(response.text) | ||
raise ValueError(response) | ||
result = response.json() | ||
assert result["expires_in"] >= 3600, result | ||
self.token = result["access_token"] | ||
self.url_prefix = "https://api.redpanda.com/v1beta2" | ||
|
||
def wait(self, result: dict[str, Any]) -> str: | ||
while True: | ||
operation_id = result["operation"]["id"] | ||
response = requests.get( | ||
f"{self.url_prefix}/operations/{operation_id}", | ||
headers={"Authorization": f"Bearer {self.token}"}, | ||
) | ||
if response.status_code not in (200, 202): | ||
print(response.text) | ||
raise ValueError(response) | ||
result = response.json() | ||
print(result) | ||
if result["operation"]["state"] == "STATE_COMPLETED": | ||
return result["operation"].get("resource_id") | ||
if result["operation"]["state"] == "STATE_FAILED": | ||
raise ValueError(result) | ||
if result["operation"]["state"] != "STATE_IN_PROGRESS": | ||
raise ValueError(result) | ||
time.sleep(10) | ||
|
||
def create(self, object: str, json: dict[str, Any] | None) -> str: | ||
response = requests.post( | ||
f"{self.url_prefix}/{object}", | ||
json=json, | ||
headers={"Authorization": f"Bearer {self.token}"}, | ||
) | ||
if response.status_code not in (200, 202): | ||
print(response.text) | ||
raise ValueError(response) | ||
result = response.json() | ||
print(result) | ||
return result | ||
|
||
def delete(self, object: str, id: str) -> dict[str, Any]: | ||
response = requests.delete( | ||
f"{self.url_prefix}/{object}/{id}", | ||
headers={"Authorization": f"Bearer {self.token}"}, | ||
) | ||
if response.status_code not in (200, 202): | ||
print(response.text) | ||
raise ValueError(response) | ||
result = response.json() | ||
print(result) | ||
return result |
This file contains 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