Skip to content

Commit

Permalink
Baseline implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HackAttack committed Oct 7, 2016
1 parent 59a537f commit 5f1df60
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.gradle
build
38 changes: 35 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
apply plugin: 'groovy'
plugins {
id 'groovy'
id 'maven-publish'
}

repositories {
jcenter()
Expand All @@ -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'
}
61 changes: 61 additions & 0 deletions src/main/groovy/com/mhackner/cloudflare/CloudFlareClient.groovy
Original file line number Diff line number Diff line change
@@ -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<Map> 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)
}

}

0 comments on commit 5f1df60

Please sign in to comment.