From 6708106626d06c176b9443af7045c602362b94cd Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Mon, 2 May 2011 21:44:54 +0200 Subject: [PATCH] Support for regular Clojure fns as predicates to ..-matching fns in compute2.clj --- .../src/main/clojure/org/jclouds/compute2.clj | 46 ++++++++++++------- .../main/clojure/org/jclouds/predicate.clj | 32 +++++++++++++ .../clojure/org/jclouds/compute2_test.clj | 14 ++++-- 3 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 compute/src/main/clojure/org/jclouds/predicate.clj diff --git a/compute/src/main/clojure/org/jclouds/compute2.clj b/compute/src/main/clojure/org/jclouds/compute2.clj index 8ae13ffb99..40eefbc56f 100644 --- a/compute/src/main/clojure/org/jclouds/compute2.clj +++ b/compute/src/main/clojure/org/jclouds/compute2.clj @@ -57,7 +57,8 @@ Here's an example of creating and running a small linux node in the group webser See http://code.google.com/p/jclouds for details. " (:use org.jclouds.core - (clojure.contrib logging core)) + (clojure.contrib logging core) + (org.jclouds predicate)) (:require [clojure.contrib.condition :as condition]) (:import java.io.File @@ -126,6 +127,13 @@ Here's an example of creating and running a small linux node in the group webser ([#^ComputeService compute] (seq (.listNodesDetailsMatching compute (NodePredicates/all))))) +(defn nodes-with-details-matching + "List details for all nodes matching fn pred. + pred should be a fn of one argument that takes a ComputeMetadata and returns true or false. + " + ([#^ComputeService compute pred] + (seq (.listNodesDetailsMatching compute (to-predicate pred))))) + (defn nodes-in-group "list details of all the nodes in the given group." ([#^ComputeService compute #^String group] @@ -181,40 +189,44 @@ Here's an example of creating and running a small linux node in the group webser ([#^ComputeService compute id] (.getNodeMetadata compute id))) -(defn suspend-nodes-in-group - "Reboot all the nodes in the given group." - ([#^ComputeService compute #^String group] - (.suspendNodesMatching compute (NodePredicates/inGroup group)))) +(defn suspend-nodes-matching + "Suspend all nodes matching the fn pred. + pred should be a fn of one argument that takes a ComputeMetadata and returns true or false." + ([#^ComputeService compute pred] + (.suspendNodesMatching compute (to-predicate pred)))) (defn suspend-node "Suspend a node, given its id." ([id #^ComputeService compute] (.suspendNode compute id))) -(defn resume-nodes-in-group - "Suspend all the nodes in the given group." - ([#^ComputeService compute #^String group] - (.resumeNodesMatching compute (NodePredicates/inGroup group)))) +(defn resume-nodes-matching + "Suspend all the nodes in the fn pred. + pred should be a fn of one argument that takes a ComputeMetadata and returns true or false." + ([#^ComputeService compute pred] + (.resumeNodesMatching compute (to-predicate pred)))) (defn resume-node "Resume a node, given its id." ([id #^ComputeService compute] (.resumeNode compute id))) -(defn reboot-nodes-in-group - "Reboot all the nodes in the given group." - ([#^ComputeService compute #^String group] - (.rebootNodesMatching compute (NodePredicates/inGroup group)))) +(defn reboot-nodes-matching + "Reboot all the nodes in the fn pred. + pred should be a fn of one argument that takes a ComputeMetadata and returns true or false." + ([#^ComputeService compute pred] + (.rebootNodesMatching compute (to-predicate pred)))) (defn reboot-node "Reboot a node, given its id." ([id #^ComputeService compute] (.rebootNode compute id))) -(defn destroy-nodes-in-group - "Destroy all the nodes in the given group." - ([#^ComputeService compute #^String group] - (.destroyNodesMatching compute (NodePredicates/inGroup group)))) +(defn destroy-nodes-matching + "Destroy all the nodes in the fn pred. + pred should be a fn of one argument that takes a ComputeMetadata and returns true or false." + ([#^ComputeService compute pred] + (.destroyNodesMatching compute (to-predicate pred)))) (defn destroy-node "Destroy a node, given its id." diff --git a/compute/src/main/clojure/org/jclouds/predicate.clj b/compute/src/main/clojure/org/jclouds/predicate.clj new file mode 100644 index 0000000000..bf024c44e6 --- /dev/null +++ b/compute/src/main/clojure/org/jclouds/predicate.clj @@ -0,0 +1,32 @@ +; +; +; Copyright (C) 2011 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 org.jclouds.predicate) + +(defprotocol Predicate + "Protocol for making a com.google.common.base.Predicate." + (to-predicate [p])) + +(extend-protocol Predicate + clojure.lang.IFn + (to-predicate [p] + (reify com.google.common.base.Predicate + (apply [this input] (p input)))) + + nil + (to-predicate [_] nil)) \ No newline at end of file diff --git a/compute/src/test/clojure/org/jclouds/compute2_test.clj b/compute/src/test/clojure/org/jclouds/compute2_test.clj index 731cc61b1d..fea140faea 100644 --- a/compute/src/test/clojure/org/jclouds/compute2_test.clj +++ b/compute/src/test/clojure/org/jclouds/compute2_test.clj @@ -54,21 +54,25 @@ list, Alan Dipert and MeikelBrandmeyer." (is (compute-service? *compute*)) (is (compute-service? (compute-service (compute-context *compute*))))) +(defn in-group [group] #(= (.getGroup %) group)) + (deftest nodes-test (is (empty? (nodes *compute*))) (is (create-node *compute* "fred" (build-template *compute* {} ))) (is (= 1 (count (nodes *compute*)))) (is (= 1 (count (nodes-in-group *compute* "fred")))) - (suspend-nodes-in-group *compute* "fred") - (is (suspended? (first (nodes-in-group *compute* "fred")))) - (resume-nodes-in-group *compute* "fred") + (is (= 1 (count (nodes-with-details-matching *compute* (in-group "fred"))))) + (is (= 0 (count (nodes-with-details-matching *compute* (in-group "othergroup"))))) + (suspend-nodes-matching *compute* (in-group "fred")) + (is (suspended? (first (nodes-with-details-matching *compute* (in-group "fred"))))) + (resume-nodes-matching *compute* (in-group "fred")) (is (running? (first (nodes-in-group *compute* "fred")))) - (reboot-nodes-in-group *compute* "fred") + (reboot-nodes-matching *compute* (in-group "fred")) (is (running? (first (nodes-in-group *compute* "fred")))) (is (create-nodes *compute* "fred" 2 (build-template *compute* {} ))) (is (= 3 (count (nodes-in-group *compute* "fred")))) (is (= "fred" (group (first (nodes *compute*))))) - (destroy-nodes-in-group *compute* "fred") + (destroy-nodes-matching *compute* (in-group "fred")) (is (terminated? (first (nodes-in-group *compute* "fred"))))) (deftest build-template-test