• CLI

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

DNS Java 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 Public Zone

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

public void CreateZones() {     
    ZoneBuilder builder = Builders.zone();     
    Zone zone = builder.name(Name).description(Description).email(Email).ttl(TTL).type(zone_type).build();     
    Zone zoneResult = osclient.dns().zones().create(zone);       
}
Table 1 Parameters

Parameter

Description

Example Value

Name

Specifies the domain name registered with the domain name registrar.

example.com

Email

This parameter is optional.

Specifies the email address of the administrator managing the public zone. It is recommended that you set the email address to HOSTMASTER@Domain name.

HOSTMASTER@example.com

Description

This parameter is optional.

Provides supplementary information about the zone.

The value consists of at most 255 characters.

This is a zone example.

zone_type

Specifies the type of the zone, which can be a public or private zone.

  • public: specifies the zone that is accessible to hosts on the Internet.
  • private: specifies the zone that is accessible to the hosts only in the specified VPC.

    If the parameter is left blank, the system will create a public zone.

public

ttl

Specifies the caching period of the record set (in seconds).

The default value is 300.

The value ranges from 300 to 2147483647.

5 min by default

Deleting a Public Zone

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

public void DeleteZones() {     
    Zone deletedZone = osclient.dns().zones().delete(publicZone.getId());         
    logger.info("Delete zone: {}", deletedZone);     
    if (osclient.dns().zones().get(publicZone.getId()) == null) {    
        System.out.println("Confirmed delete zone");     
    }  
}

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. OpenStack4j allows creation of only one private zone. The creation procedure is as follows:

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

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

public void CreatePrivateZones() {     
    Router vpv = this.getFirstRouter();
    DesignateZone.Router router = new DesignateZone.Router(vpc.getId(),REGION, Status); 
    ZoneBuilder builder = Builders.zone();     
    Zone sourceZone = builder.name(Name).description(Description).email(Email).ttl(TTL).type(zone_type).router(router).build();     
    Zone zoneResult = osclient.dns().zones().create(sourceZone);         logger.info("Create zone: {}", zoneResult);  
}
Table 2 Parameters

Parameter

Description

Example Value

ROUTER_ID

Specifies the VPC to be associated with the private zone.

cd27d47c-ad5a-40a7-8b54-3504a5885d55

REGION

Specifies the region of the VPC to be associated.

eu-de

Status

Specifies the VPC status.

The value can be PENDING_CREATEACTIVEPENDING_DELETE, or ERROR.

N/A

Name

Specifies the domain name registered with the domain name registrar.

example.com

Email

This parameter is optional.

Specifies the email address of the administrator managing the private zone. It is recommended that you set the email address to HOSTMASTER@Domain name.

HOSTMASTER@example.com

Description

This parameter is optional.

Provides supplementary information about the zone.

The value consists of at most 255 characters.

This a public zone.

zone_type

Specifies the type of the zone, which can be a public or private zone.

  • public: specifies the zone that is accessible to hosts on the Internet.
  • private: specifies the zone that is accessible to the hosts only in the specified VPC.

If the parameter is left blank, the system will create a public zone.

private

ttl

Specifies the caching period of the record set (in seconds).

The default value is 300.

The value ranges from 300 to 2147483647.

5 min by default

Associating a VPC

You can use OpenStack4j 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.
public void AssociateRouter() {     
    router2 = osclient.networking().router().list().get(1);
    DesignateZone.Router router = new DesignateZone.Router(router2.getId(),REGION, Status);     
    DesignateZone.Router routerResult = osclient.dns().zones().associateRouter(privateZone.getId(),router);     
    logger.info("Associate router: {}", routerResult);
}

Disassociating a VPC

You can use OpenStack4j to disassociate a private zone from a VPC. The code is as follows:

public void DisassociateRouter() {      
    getZone();
    DesignateZone.Router router = new DesignateZone.Router(this.getFirstRouter().getId(), REGION, null);      
    DesignateZone.Router routerResult = osclient.dns().zones().disassociateRouter(privateZone.getId(), router);      
    logger.info("Disassociate router: {}", routerResult);     
}

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:

public void DeleteZones() {     
    Zone deletedZone = osclient.dns().zones().delete(privateZone.getId());         
    logger.info("Delete zone: {}", deletedZone);     
    if (osclient.dns().zones().get(privateZone.getId()) == null) {   
        System.out.println("Confirmed delete zone");     
    }  
}