さくらのクラウドAPI1.1をJavaで使う
Javaでさくらのクラウド環境を操作してみました。
APIのドキュメント→さくらのクラウドAPI1.1
- メモ
さくら公式のNode.jsのモジュールnode-sacloudがある。
環境
Vine Linuxです。
$ cat /etc/vine-release Vine Linux 6.2 (Haut Bailly) $ java -version java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.1) (vine-4.b22vl6-i386) OpenJDK Server VM (build 19.0-b09, mixed mode)
ソースコード
SSHキー関連のAPIのGET, POST, PUT, DELETEをテストです。
API_URL_PRE
, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
は各自の値を設定。
package com.example.SakuraCloudApiTest; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.PasswordAuthentication; import java.net.URL; public class Main { // 石狩第1ゾーン // private static final String API_URL_PRE = "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/"; // 石狩第2ゾーン public static final String API_URL_PRE = "https://secure.sakura.ad.jp/cloud/zone/is1b/api/cloud/1.1/"; public static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; public static final String ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET"; public static void main(String[] args) { try { // SSHキー一覧を取得 sshkeyGet(null); // 該当IDのSSHキー情報を取得 // sshkeyGet(your sshkeyid); // SSHキーを作成 // sshkeyPost("testkey1", "description1", "ssh-rsa test key1"); // SSHキーを更新 // sshkeyPut(your sshkeyid, "TESTKEY1", "DESCRIPTION1", "ssh-rsa TEST KEY1"); // 該当IDのSSHキーを削除 // sshkeyDelete(your sshkeyid); } catch (Exception e) { e.printStackTrace(); } } public static class BasicAuthenticator extends Authenticator { private String password; private String user; public BasicAuthenticator(String user, String password) { this.password = password; this.user = user; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password.toCharArray()); } } private static String getSshkeyJson(String name, String description, String publicKey) { StringBuilder sb = new StringBuilder(); sb.append("{"); sb.append("SSHKey:{"); sb.append("Name:"); sb.append("\"" + name + "\""); sb.append(","); sb.append("Description:"); sb.append("\"" + description + "\""); sb.append(","); sb.append("PublicKey:"); sb.append("\"" + publicKey + "\""); sb.append("}"); // SSHKey: { sb.append("}"); return sb.toString(); } public static void sshkeyGet(String sshkeyId) throws Exception { String path = "sshkey"; if (sshkeyId != null) { path += "/" + sshkeyId; } _http("GET", path, null); } public static void sshkeyPost(String name, String description, String publicKey) throws Exception { _http("POST", "sshkey", getSshkeyJson(name, description, publicKey)); } public static void sshkeyPut(String sshkeyid, String name, String description, String publicKey) throws Exception { _http("PUT", "sshkey" + "/" + sshkeyid, getSshkeyJson(name, description, publicKey)); } public static void sshkeyDelete(String sshkeyid) throws Exception { _http("DELETE", "sshkey" + "/" + sshkeyid, null); } private static void _http(String method, String path, String body) throws Exception { HttpURLConnection conn = null; InputStream in = null; OutputStream out = null; try { URL url = new URL(API_URL_PRE + path); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod(method); // conn.setRequestProperty("X-Sakura-API-Beautify", "1"); Authenticator authenticator = new BasicAuthenticator(ACCESS_TOKEN, ACCESS_TOKEN_SECRET); Authenticator.setDefault(authenticator); if (body != null) { out = conn.getOutputStream(); byte[] b = body.getBytes("UTF-8"); out.write(b); } conn.connect(); in = conn.getInputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); int d = -1; while ((d = in.read()) > -1) { bout.write(d); } System.out.println(bout.toString()); } catch (Exception e) { throw e; } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } if (conn != null) { conn.disconnect(); } } } }