如何通过Python以编程方式暂停mongodbatlas集群,无论是使用API还是Python包?

问题描述 投票:0回答:1

我想自动暂停 Mongodb-Atlas 集群。我怎样才能做到这一点? 通过Atlas Cli,我们有这个命令

atlas 集群暂停 [选项]

但我想通过 python 运行这个命令。 请告诉我怎么做。 显然,Pymongo 不允许这样做。

我试过了

import os
os.system("atlas clusters pause Cluster0 --projectId 662b391615a47202d49f2fa0 --output json")

import requests
import json

# Atlas API configuration
base_url = "https://cloud.mongodb.com/api/atlas/v1.0"
project_id = "662b391615a47202d49f2fa0"
cluster_name = "Cluster0"

# Your Atlas API credentials
public_key = "YOUR_PUBLIC_KEY"
#private_key = "YOUR_PRIVATE_KEY"

# Endpoint for pausing a cluster
endpoint = f"{base_url}/groups/{project_id}/clusters/{cluster_name}"

# Request headers
headers = {
    "Content-Type": "application/json",
    "Accept": "application/json"
}

# Request payload
payload = {
    "paused": True
}

# Send the PATCH request
response = requests.patch(
    endpoint,
    auth=(public_key),
    headers=headers,
    data=json.dumps(payload)
)

# Check the response
if response.status_code == 200:
    print(f"Cluster {cluster_name} paused successfully.")
else:
    print(f"Failed to pause cluster. Status code: {response.status_code}")
    print(response.text)

但是两者都不起作用。

mongodb cluster-computing mongodb-atlas pause
1个回答
0
投票

我相信,如果您将有效负载更改为以下内容,它将起作用:

# Request payload
payload = {
    "paused": "true"
}
© www.soinside.com 2019 - 2024. All rights reserved.