From a40b047e340d58098638fd2a47ebb18dee87852e Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 9 Aug 2010 17:43:25 -0700 Subject: [PATCH] Issue 191: added clojure support to chef --- .../src/main/clojure/org/jclouds/chef.clj | 92 +++++++++++++++++++ .../java/org/jclouds/chef/ChefService.java | 4 +- .../chef/internal/BaseChefService.java | 4 +- 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 chef/core/src/main/clojure/org/jclouds/chef.clj diff --git a/chef/core/src/main/clojure/org/jclouds/chef.clj b/chef/core/src/main/clojure/org/jclouds/chef.clj new file mode 100644 index 0000000000..70ce3721e1 --- /dev/null +++ b/chef/core/src/main/clojure/org/jclouds/chef.clj @@ -0,0 +1,92 @@ +;; +;; Copyright (C) 2010 Cloud Conscious, LLC. +;; +;; ==================================================================== +;; 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. +;; ==================================================================== +;; + +(ns + #^{:author "Adrian Cole" + :doc "A clojure binding to the jclouds chef interface."} + org.jclouds.chef + (:use org.jclouds.core (core)) + (:import + java.util.Properties + [org.jclouds.chef ChefClient + ChefService ChefContext ChefContextFactory])) +(try + (use '[clojure.contrib.reflect :only [get-field]]) + (catch Exception e + (use '[clojure.contrib.java-utils + :only [wall-hack-field] + :rename {wall-hack-field get-field}]))) + +(defn pem + "get the pem associated with the supplied identity" + ([#^String identity] + (slurp (str (. System getProperty "user.home") "/.chef/" identity ".pem")))) + +(defn chef-service + "Create a logged in context." + ([#^String identity #^String credential & options] + (let [module-keys (set (keys module-lookup)) + ext-modules (filter #(module-keys %) options) + opts (apply hash-map (filter #(not (module-keys %)) options))] + (.. (ChefContextFactory.) + (createContext identity credential + (apply modules (concat ext-modules (opts :extensions))) + (reduce #(do (.put %1 (name (first %2)) (second %2)) %1) + (Properties.) (dissoc opts :extensions))) + (getChefService))))) + +(defn chef-context + "Returns a chef context from a chef service." + [#^ChefService chef] + (.getContext chef)) + +(defn chef-service? + [object] + (instance? ChefService object)) + +(defn chef-context? + [object] + (instance? ChefContext object)) + +(defn as-chef-service + "Tries hard to produce a chef service from its input arguments" + [& args] + (cond + (chef-service? (first args)) (first args) + (chef-context? (first args)) (.getChefService (first args)) + :else (apply chef-service args))) + +(def *chef*) + +(defmacro with-chef-service + "Specify the default chef service" + [[& chef-or-args] & body] + `(binding [*chef* (as-chef-service ~@chef-or-args)] + ~@body)) + +(defn nodes + "Retrieve the names of the existing nodes in your chef server." + ([] (nodes *chef*)) + ([#^ChefService chef] + (seq (.listNodes (.getApi (.getContext chef)))))) + +(defn nodes-with-details + "Retrieve the existing nodes in your chef server including all details." + ([] (nodes *chef*)) + ([#^ChefService chef] + (seq (.listNodesDetails chef)))) \ No newline at end of file diff --git a/chef/core/src/main/java/org/jclouds/chef/ChefService.java b/chef/core/src/main/java/org/jclouds/chef/ChefService.java index 388f608e49..cf4b44b39e 100644 --- a/chef/core/src/main/java/org/jclouds/chef/ChefService.java +++ b/chef/core/src/main/java/org/jclouds/chef/ChefService.java @@ -38,9 +38,9 @@ public interface ChefService { void deleteAllNodesInList(Iterable names); - Iterable getNodes(); + Iterable listNodesDetails(); - Iterable getNodesWithNamesMatching(Predicate nodeNameSelector); + Iterable listNodesDetailsMatching(Predicate nodeNameSelector); Iterable getNodesNamed(Iterable names); diff --git a/chef/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java b/chef/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java index b78b1c4783..07a1f6452d 100644 --- a/chef/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java +++ b/chef/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java @@ -81,12 +81,12 @@ public class BaseChefService implements ChefService { } @Override - public Iterable getNodes() { + public Iterable listNodesDetails() { return getNodes.execute(); } @Override - public Iterable getNodesWithNamesMatching(Predicate nodeNameSelector) { + public Iterable listNodesDetailsMatching(Predicate nodeNameSelector) { return getNodes.execute(nodeNameSelector); }