Search in sources :

Example 1 with GloboDnsResource

use of com.globo.globodns.cloudstack.resource.GloboDnsResource in project cloudstack by apache.

the class GloboDnsElement method addGloboDnsHost.

@Override
@DB
public Host addGloboDnsHost(Long physicalNetworkId, final String username, final String password, String url) {
    if (username == null || username.trim().isEmpty()) {
        throw new InvalidParameterValueException("Invalid username: " + username);
    }
    if (password == null || password.trim().isEmpty()) {
        throw new InvalidParameterValueException("Invalid password: " + password);
    }
    if (url == null || url.trim().isEmpty()) {
        throw new InvalidParameterValueException("Invalid url: " + url);
    }
    // validate physical network and zone
    // Check if physical network exists
    PhysicalNetwork pNtwk = null;
    if (physicalNetworkId != null) {
        pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
        if (pNtwk == null) {
            throw new InvalidParameterValueException("Unable to find a physical network having the specified physical network id");
        }
    } else {
        throw new InvalidParameterValueException("Invalid physicalNetworkId: " + physicalNetworkId);
    }
    final Long zoneId = pNtwk.getDataCenterId();
    final Map<String, String> params = new HashMap<String, String>();
    params.put("guid", "globodns-" + String.valueOf(zoneId));
    params.put("zoneId", String.valueOf(zoneId));
    params.put("name", Provider.GloboDns.getName());
    params.put("url", url);
    params.put("username", username);
    params.put("password", password);
    final Map<String, Object> hostDetails = new HashMap<String, Object>();
    hostDetails.putAll(params);
    Host host = Transaction.execute(new TransactionCallbackWithException<Host, CloudRuntimeException>() {

        @Override
        public Host doInTransaction(TransactionStatus status) throws CloudRuntimeException {
            try {
                GloboDnsResource resource = new GloboDnsResource();
                resource.configure(Provider.GloboDns.getName(), hostDetails);
                Host host = _resourceMgr.addHost(zoneId, resource, resource.getType(), params);
                if (host == null) {
                    throw new CloudRuntimeException("Failed to add GloboDNS host");
                }
                // Validate username and password by logging in
                SignInCommand cmd = new SignInCommand(username, password);
                Answer answer = callCommand(cmd, zoneId);
                if (answer == null || !answer.getResult()) {
                    // Could not sign in on GloboDNS
                    throw new ConfigurationException("Could not sign in on GloboDNS. Please verify URL, username and password.");
                }
                return host;
            } catch (ConfigurationException e) {
                throw new CloudRuntimeException(e);
            }
        }
    });
    return host;
}
Also used : SignInCommand(com.globo.globodns.cloudstack.commands.SignInCommand) HashMap(java.util.HashMap) TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) GloboDnsResource(com.globo.globodns.cloudstack.resource.GloboDnsResource) Answer(com.cloud.agent.api.Answer) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PhysicalNetwork(com.cloud.network.PhysicalNetwork) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Aggregations

Answer (com.cloud.agent.api.Answer)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 Host (com.cloud.host.Host)1 PhysicalNetwork (com.cloud.network.PhysicalNetwork)1 DB (com.cloud.utils.db.DB)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 SignInCommand (com.globo.globodns.cloudstack.commands.SignInCommand)1 GloboDnsResource (com.globo.globodns.cloudstack.resource.GloboDnsResource)1 HashMap (java.util.HashMap)1 ConfigurationException (javax.naming.ConfigurationException)1