• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. Python
  6. DNS Python SDK Demo

DNS Python SDK Demo

Service Description

Domain Name Service (DNS) provides highly available and scalable authoritative DNS resolution services and domain name management services. It translates domain names or application resources into IP addresses required for network connection. By doing so, visitors' access requests are directed to the desired resources.

Creating a Private Zone

To use the DNS service to manage domain names in VPCs, you need to configure private zones on the DNS console. You can use the Python OpenStack SDK to create a private zone as follows:

  1. Specify the VPC to be associated.
  2. Create a private zone.

You can create a private zone using the Python OpenStack SDK based on the following code. After the private zone is created, it will be displayed on the private zone page of the DNS console.

def setUpClass(cls):      
    super(TestZone, cls).setUpClass()      
    # get a router      
    routers = cls.conn.network.routers(limit=2)      
    idx = 0      
    for _router in routers:          
        idx += 1          
        print _router          
        if idx == 1:              
            cls.router = _router          
        if idx == 2:              
            cls.router2 = _router              
            break        
    # create zone       
    cls.zone = auto_create_private_zone(cls.conn, cls.NAME, cls.router.id,region)

Associating a VPC

You can use the Python OpenStack SDK to associate a private zone with a VPC on the cloud platform. The association procedure is as follows:

  1. Specify the VPC to be associated.
  2. Select the target private zone to associate with the VPC.

You can associate a private zone with a VPC using the Python OpenStack SDK based on the following code:

def add_router_to_zone(self):
    # Designate a router  
    resource2.wait_for_status(self.conn.dns._session, self.zone, "ACTIVE", interval=5, failures=["ERROR"])
    # Associate the private zone to the router  
    result = self.conn.dns.add_router_to_zone(self.zone, **{"router_id": self.router2.id,"router_region": region})  
    self.assertEqual(result.router_id, self.router2.id)  
    self.assertEqual(result.router_region, region)  
    zone = self.conn.dns.get_zone(self.zone)  
    self.assertEqual(2, len(zone.routers))  
    router_ids = [_router["router_id"] for _router in zone.routers]      self.assertIn(self.router.id, router_ids)

Disassociating a VPC

You can use the Python OpenStack SDK to disassociate a private zone from a VPC on the cloud platform. The code is as follows:

def remove_router_of_zone(self):      
    resource2.wait_for_status(self.conn.dns._session, self.zone, "ACTIVE", interval=5, failures=["ERROR"])      
    result = self.conn.dns.remove_router_from_zone(self.zone, **{          "router_id": self.router.id,          
      "router_region": region     
    })      
    self.assertEqual(result.router_id, self.router.id)            
    self.assertEqual(result.router_region, region)

Deleting a Private Zone

You can delete a private zone that you do not need to manage using the DNS service. After the deletion, domain names included in this zone cannot be resolved.

Before deleting a private zone, ensure that all record sets in this zone have been backed up. The code is as follows:

def tearDownClass(cls):      
    # delete zone      
    cls.conn.dns.delete_zone(cls.zone)