|
| 1 | +package org.apache.cloudstack.api.command.user.dns; |
| 2 | + |
| 3 | +import javax.inject.Inject; |
| 4 | + |
| 5 | +import org.apache.cloudstack.api.APICommand; |
| 6 | +import org.apache.cloudstack.api.ApiConstants; |
| 7 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 8 | +import org.apache.cloudstack.api.BaseAsyncCreateCmd; |
| 9 | +import org.apache.cloudstack.api.Parameter; |
| 10 | +import org.apache.cloudstack.api.ServerApiException; |
| 11 | +import org.apache.cloudstack.api.response.DnsServerResponse; |
| 12 | +import org.apache.cloudstack.api.response.DnsZoneResponse; |
| 13 | +import org.apache.cloudstack.api.response.NetworkResponse; |
| 14 | +import org.apache.cloudstack.context.CallContext; |
| 15 | +import org.apache.cloudstack.dns.DnsProviderManager; |
| 16 | +import org.apache.cloudstack.dns.DnsZone; |
| 17 | + |
| 18 | +import com.cloud.event.EventTypes; |
| 19 | +import com.cloud.exception.ResourceAllocationException; |
| 20 | + |
| 21 | +@APICommand(name = "createDnsZone", description = "Creates a new DNS Zone on a specific server", |
| 22 | + responseObject = DnsZoneResponse.class, |
| 23 | + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) |
| 24 | +public class CreateDnsZoneCmd extends BaseAsyncCreateCmd { |
| 25 | + |
| 26 | + private static final String s_name = "creatednszoneresponse"; |
| 27 | + |
| 28 | + @Inject |
| 29 | + DnsProviderManager dnsProviderManager; |
| 30 | + |
| 31 | + ///////////////////////////////////////////////////// |
| 32 | + //////////////// API Parameters ///////////////////// |
| 33 | + ///////////////////////////////////////////////////// |
| 34 | + |
| 35 | + @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, |
| 36 | + description = "The name of the DNS zone (e.g. example.com)") |
| 37 | + private String name; |
| 38 | + |
| 39 | + @Parameter(name = "dnsserverid", type = CommandType.UUID, entityType = DnsServerResponse.class, |
| 40 | + required = true, description = "The ID of the DNS server to host this zone") |
| 41 | + private Long dnsServerId; |
| 42 | + |
| 43 | + @Parameter(name = ApiConstants.NETWORK_ID, type = CommandType.UUID, entityType = NetworkResponse.class, |
| 44 | + description = "Optional: The Guest Network to associate with this zone for auto-registration") |
| 45 | + private Long networkId; |
| 46 | + |
| 47 | + @Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, |
| 48 | + description = "The type of zone (Public, Private). Defaults to Public.") |
| 49 | + private String type; |
| 50 | + |
| 51 | + // Standard CloudStack ownership parameters (account/domain) are handled |
| 52 | + // automatically by the BaseCmd parent if we access them via getEntityOwnerId() |
| 53 | + |
| 54 | + ///////////////////////////////////////////////////// |
| 55 | + /////////////////// Accessors /////////////////////// |
| 56 | + ///////////////////////////////////////////////////// |
| 57 | + |
| 58 | + public String getName() { |
| 59 | + return name; |
| 60 | + } |
| 61 | + |
| 62 | + public Long getDnsServerId() { |
| 63 | + return dnsServerId; |
| 64 | + } |
| 65 | + |
| 66 | + public Long getNetworkId() { |
| 67 | + return networkId; |
| 68 | + } |
| 69 | + |
| 70 | + public String getType() { |
| 71 | + return type; |
| 72 | + } |
| 73 | + |
| 74 | + ///////////////////////////////////////////////////// |
| 75 | + /////////////// Implementation ////////////////////// |
| 76 | + ///////////////////////////////////////////////////// |
| 77 | + |
| 78 | + @Override |
| 79 | + public void create() throws ResourceAllocationException { |
| 80 | + // Phase 1: DB Persist |
| 81 | + // The manager should create the DnsZoneVO in 'Allocating' state |
| 82 | + try { |
| 83 | + DnsZone zone = dnsProviderManager.allocDnsZone(this); |
| 84 | + if (zone != null) { |
| 85 | + setEntityId(zone.getId()); |
| 86 | + setEntityUuid(zone.getUuid()); |
| 87 | + } else { |
| 88 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create DNS Zone entity"); |
| 89 | + } |
| 90 | + } catch (Exception e) { |
| 91 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to allocate DNS Zone: " + e.getMessage()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void execute() { |
| 97 | + // Phase 2: Action (Call Plugin) |
| 98 | + // The manager should retrieve the zone by ID, call the plugin, and update state to 'Ready' |
| 99 | + try { |
| 100 | + // Note: We use getEntityId() which was set in the create() phase |
| 101 | + DnsZone result = dnsProviderManager.provisionDnsZone(getEntityId()); |
| 102 | + |
| 103 | + if (result != null) { |
| 104 | + DnsZoneResponse response = dnsProviderManager.createDnsZoneResponse(result); |
| 105 | + response.setResponseName(getCommandName()); |
| 106 | + setResponseObject(response); |
| 107 | + } else { |
| 108 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to provision DNS Zone on external provider"); |
| 109 | + } |
| 110 | + } catch (Exception e) { |
| 111 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to provision DNS Zone: " + e.getMessage()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String getCommandName() { |
| 117 | + return s_name; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public long getEntityOwnerId() { |
| 122 | + return CallContext.current().getCallingAccount().getId(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String getEventType() { |
| 127 | + return EventTypes.EVENT_DNS_ZONE_CREATE; // You must add this constant to EventTypes.java |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public String getEventDescription() { |
| 132 | + return "creating DNS zone: " + getName(); |
| 133 | + } |
| 134 | +} |
0 commit comments