Created
September 1, 2014 16:55
-
-
Save nakaji/dd69945b9d479ebc9a06 to your computer and use it in GitHub Desktop.
C#でDozensのDNSレコードを操作する ref: http://qiita.com/nakaji/items/008090c0fc8173afce53
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net; | |
using Codeplex.Data; | |
namespace ConsoleApplication3 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// APIの仕様は下記URL参照 | |
// https://sites.google.com/a/dozens.jp/docs/ | |
string authToken; | |
// 認証 | |
using (var wc = new WebClient()) | |
{ | |
wc.Headers.Add("X-Auth-User", "{DOZENS ID}"); | |
wc.Headers.Add("X-Auth-Key", "{プロフィールに表示されるAPI KEY}"); | |
var json = wc.DownloadString("http://dozens.jp/api/authorize.json"); | |
authToken = DynamicJson.Parse(json).auth_token; | |
} | |
using (var wc = new WebClient()) | |
{ | |
wc.Headers.Add("X-Auth-Token", authToken); | |
// ゾーンの取得 | |
var json = wc.DownloadString("http://dozens.jp/api/zone.json"); | |
Console.WriteLine("---全Zone---\n{0}", json); | |
// レコードの取得 | |
json = wc.DownloadString("http://dozens.jp/api/record/nkd.jp.json"); | |
Console.WriteLine("---全Record---\n{0}", json); | |
} | |
string recordsJson; | |
// レコードの登録 | |
using (var wc = new WebClient()) | |
{ | |
wc.Headers.Add("X-Auth-Token", authToken); | |
wc.Headers.Add("Content-Type", "application/json"); | |
recordsJson = wc.UploadString("http://dozens.jp/api/record/create.json", "POST", | |
DynamicJson.Serialize( | |
new | |
{ | |
domain = "{自分の持ってるドメイン}", | |
name = "www", | |
type = "A", | |
prio = "10", | |
content = "192.168.1.100", | |
ttl = "7200" | |
})); | |
Console.WriteLine("---全Record(登録後)---\n{0}", recordsJson); | |
} | |
// 登録したレコードに対して以降の操作を行う | |
var records = (dynamic[])DynamicJson.Parse(recordsJson).record; | |
dynamic record = records.First(x => x.name == "www.{自分の持ってるドメイン}"); | |
// レコードの更新 | |
using (var wc = new WebClient()) | |
{ | |
wc.Headers.Add("X-Auth-Token", authToken); | |
wc.Headers.Add("Content-Type", "application/json"); | |
var url = string.Format("http://dozens.jp/api/record/update/{0}.json", record.id); | |
var json = wc.UploadString(url, "POST", | |
DynamicJson.Serialize( | |
new | |
{ | |
prio = "10", | |
content = "192.168.1.200", | |
ttl = "7200" | |
})); | |
Console.WriteLine("---全Record(更新後)---\n{0}", json); | |
} | |
// レコードの削除 | |
using (var wc = new WebClient()) | |
{ | |
wc.Headers.Add("X-Auth-Token", authToken); | |
var url = string.Format("http://dozens.jp/api/record/delete/{0}.json", record.id); | |
var json = wc.UploadString(url, "DELETE", ""); | |
Console.WriteLine("---全Record(削除後)---\n{0}", json); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment