python 使用API在OVH中添加新的域分区

3mpgtkmj  于 2023-02-21  发布在  Python
关注(0)|答案(1)|浏览(108)

我想使用OVH API在OVH with Python中添加一个新的DNS区域。
我写了一个脚本,包含以下步骤:

  • 创建新购物车
  • 将新DNS区域添加到购物车
  • 查看购物车内容
  • 确认订单
  • 查看订单状态

我是否忘记了某个步骤或者哪里出错了?因为当我查看GET订单时,我没有看到新订单,而且它们也没有出现在GUI中。

cart = client.post('/order/cart', ovhSubsidiary='PL')

#Get the cart ID
cart_id = cart.get('cartId')

#Set data for the new DNS zone
zone_name = 'testttt.pl' # DNS zone name

#Add the new DNS zone to the cart
result = client.post(f'/order/cart/{cart_id}/dns',
domain=zone_name,
duration="P1Y",
planCode="zone",
pricingMode="default",
quantity=1)

#Check if the operation was successful
if 'itemId' in result:
    print(f'DNS zone {zone_name} was added to the cart.')
else:
    print('Error while adding DNS zone to the cart.')

#Display the cart contents
cart_info = client.get(f'/order/cart/{cart_id}')
print(f'Cart contents:\n{json.dumps(cart_info, indent=4)}')

#Make sure the cart is ready to order
order = client.post(f'/order/cart/{cart_id}/checkout', autoPayWithPreferredPaymentMethod=True)
print(f'Order {order.get("orderId")} has been placed.')

order_id = cart_info['orders'][-1]

#Check the status of the order
order_status = client.get(f'/me/order/{order_id}/status')
print(f'Order {order_id} status: {order_status}')```
nnsrf1az

nnsrf1az1#

创建购物车后,在将您的DNS区域(或任何其他产品)添加到此购物车之前,您需要将此购物车 * 分配 * 给自己。
可以通过以下途径完成:

# Assign a shopping cart to an loggedin client
POST /order/cart/{cartId}/assign

因此,您的脚本应如下所示:

import ovh

# For these 3 keys, refer to the documentation:
# - https://github.com/ovh/python-ovh#use-the-api-on-behalf-of-a-user
# - Generate the keys easily via https://www.ovh.com/auth/api/createToken

client = ovh.Client(
    endpoint='ovh-eu',
    application_key='<app_key>',
    application_secret='<app_secret>',
    consumer_key='<consumer_key>'
)
# Initiate a new cart
cart = client.post(
    '/order/cart',
    ovhSubsidiary='PL'
)
cart_id = cart.get('cartId')

# Assign this cart to the currently logged-in user
assign = client.post(
    f'/order/cart/{cart_id}/assign'
)

zones_to_order = ['domain-1.ovh', 'domain-2.ovh', 'domain-3.ovh']
for domain in zones_to_order:
    # Add a new DNS zone to the cart
    result = client.post(
        f'/order/cart/{ cart_id }/dns',
        duration="P1Y",
        planCode="zone",
        pricingMode="default"
    )
    itemId = result.get("idemId")
    # Configure the DNS zone in the cart
    client.post(
        f'/order/cart/{cart_id}/item/{ itemId }/configuration',
        label="zone",
        value=domain
    )

# Finalyze your order
checkout = client.post(
    f'/order/cart/{cart_id}/checkout'
)

相关问题