From 5f1df605b5309b82fa2dcceaec7ec474225a3d8b Mon Sep 17 00:00:00 2001 From: Michael Hackner Date: Fri, 7 Oct 2016 14:47:20 -0400 Subject: [PATCH] Baseline implementation --- .gitignore | 1 + build.gradle | 38 +++++++++++- .../cloudflare/CloudFlareClient.groovy | 61 +++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/main/groovy/com/mhackner/cloudflare/CloudFlareClient.groovy diff --git a/.gitignore b/.gitignore index 08a55c0..f8b92c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .gradle +build diff --git a/build.gradle b/build.gradle index e491a8b..0bf0f36 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,7 @@ -apply plugin: 'groovy' +plugins { + id 'groovy' + id 'maven-publish' +} repositories { jcenter() @@ -7,7 +10,36 @@ repositories { sourceCompatibility = 1.6 targetCompatibility = 1.6 +description = 'A Java/Groovy client for the CloudFlare v4 API' +group = 'com.mhackner' + +task sourceJar(type: Jar) { + from sourceSets.main.allSource +} + +publishing { + publications { + maven(MavenPublication) { + from components.java + + artifact sourceJar { + classifier 'sources' + } + + pom.withXml { + asNode().appendNode('description', description) + def scm = asNode().appendNode('scm') + scm.appendNode('developerConnection', 'scm:git:https://github.com/HackAttack/cloudflare-java-client.git') + scm.appendNode('url', 'https://github.com/HackAttack/cloudflare-java-client') + scm.appendNode('tag', 'HEAD') + } + } + } +} + dependencies { - compile localGroovy() - compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1' + compile 'org.codehaus.groovy:groovy:2.4.7' + compile 'org.codehaus.groovy:groovy-json:2.4.7' + compile 'org.codehaus.groovy:groovy-xml:2.4.7' + compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.2' } diff --git a/src/main/groovy/com/mhackner/cloudflare/CloudFlareClient.groovy b/src/main/groovy/com/mhackner/cloudflare/CloudFlareClient.groovy new file mode 100644 index 0000000..9ed3fe8 --- /dev/null +++ b/src/main/groovy/com/mhackner/cloudflare/CloudFlareClient.groovy @@ -0,0 +1,61 @@ +package com.mhackner.cloudflare + +import groovyx.net.http.ContentType +import groovyx.net.http.RESTClient + +class CloudFlareClient { + + private final RESTClient http + + CloudFlareClient(String apiKey, String email) { + http = new RESTClient('https://api.cloudflare.com/client/v4/', ContentType.JSON) + http.headers = ['X-Auth-Key': apiKey, 'X-Auth-Email': email] + } + + Map getZone(String domain) { + def result = http.get(path: 'zones', query: [name: domain]).data.result + assert result.size() <= 1 + result ? result.first() : null + } + + Map createZone(String domain, String orgId = null) { + def params = [name: domain, jump_start: false] + if (orgId) { + params.organization = [id: orgId] + } + http.post(path: 'zones', body: params).data.result + } + + Map updatePlanForZone(String zoneId, String planId) { + http.patch(path: "zones/$zoneId", body: [plan: [id: planId]]).data.result + } + + void deleteZone(String zoneId) { + http.delete(path: "zones/$zoneId") + } + + List getRecords(String zoneId, Map params = [:]) { + http.get(path: "zones/$zoneId/dns_records", query: params).data.result + } + + Map createRecord(String zoneId, String type, String name, String content, Integer priority = null) { + def params = [type: type, name: name, content: content] + if (priority) { + params.priority = priority + } + http.post(path: "zones/$zoneId/dns_records", body: params).data.result + } + + void deleteRecord(String zoneId, String recordId) { + http.delete(path: "zones/$zoneId/dns_records/$recordId") + } + + void deleteRecords(String zoneId, Map params = [:]) { + getRecords(zoneId, params).each { deleteRecord(zoneId, it.id) } + } + + void updateRecord(Map record) { + http.put(path: "zones/$record.zone_id/dns_records/$record.id", body: record) + } + +}