org.dasein.cloud.cloudstack.identity.Keypair Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dasein-cloud-cloudstack Show documentation
Show all versions of dasein-cloud-cloudstack Show documentation
Implements the Dasein Cloud API for Cloud.com Cloudstack-based public and private clouds.
/**
* Copyright (C) 2009-2015 Dell, Inc.
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.dasein.cloud.cloudstack.identity;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.OperationNotSupportedException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.Requirement;
import org.dasein.cloud.cloudstack.CSCloud;
import org.dasein.cloud.cloudstack.CSMethod;
import org.dasein.cloud.cloudstack.Param;
import org.dasein.cloud.identity.SSHKeypair;
import org.dasein.cloud.identity.ServiceAction;
import org.dasein.cloud.identity.ShellKeyCapabilities;
import org.dasein.cloud.identity.ShellKeySupport;
import org.dasein.cloud.util.APITrace;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
/**
* Implements the CSCloud 3.0 SSH keypair support
* @author George Reese
* @since 2012.02
* @version 2012.02
*/
public class Keypair implements ShellKeySupport {
private CSCloud provider;
private transient volatile KeypairCapabilities capabilities;
Keypair(@Nonnull CSCloud provider) { this.provider = provider; }
@Override
public @Nonnull SSHKeypair createKeypair(@Nonnull String name) throws InternalException, CloudException {
APITrace.begin(provider, "Keypair.createKeypair");
try {
final Document doc = new CSMethod(provider).get(
CSMethod.CREATE_KEYPAIR,
new Param("name", name)
);
NodeList matches = doc.getElementsByTagName("keypair");
for( int i=0; i list() throws InternalException, CloudException {
APITrace.begin(provider, "Keypair.list");
try {
final CSMethod method = new CSMethod(provider);
Document doc = method.get(CSMethod.LIST_KEYPAIRS);
final List keys = new ArrayList();
int numPages = 1;
NodeList nodes = doc.getElementsByTagName("count");
Node n = nodes.item(0);
if (n != null) {
String value = n.getFirstChild().getNodeValue().trim();
int count = Integer.parseInt(value);
numPages = count/500;
int remainder = count % 500;
if (remainder > 0) {
numPages++;
}
}
for (int page = 1; page <= numPages; page++) {
if (page > 1) {
String nextPage = String.valueOf(page);
doc = method.get(
CSMethod.LIST_KEYPAIRS,
new Param("pagesize", "500"),
new Param("page", nextPage)
);
}
NodeList matches = doc.getElementsByTagName("sshkeypair");
for( int i=0; i