Support for regular Clojure fns as predicates to ..-matching fns in compute2.clj

This commit is contained in:
Mattias Holmqvist 2011-05-02 21:44:54 +02:00
parent a542e85dad
commit 6708106626
3 changed files with 70 additions and 22 deletions

View File

@ -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. See http://code.google.com/p/jclouds for details.
" "
(:use org.jclouds.core (:use org.jclouds.core
(clojure.contrib logging core)) (clojure.contrib logging core)
(org.jclouds predicate))
(:require (:require
[clojure.contrib.condition :as condition]) [clojure.contrib.condition :as condition])
(:import java.io.File (: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] ([#^ComputeService compute]
(seq (.listNodesDetailsMatching compute (NodePredicates/all))))) (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 (defn nodes-in-group
"list details of all the nodes in the given group." "list details of all the nodes in the given group."
([#^ComputeService compute #^String 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] ([#^ComputeService compute id]
(.getNodeMetadata compute id))) (.getNodeMetadata compute id)))
(defn suspend-nodes-in-group (defn suspend-nodes-matching
"Reboot all the nodes in the given group." "Suspend all nodes matching the fn pred.
([#^ComputeService compute #^String group] pred should be a fn of one argument that takes a ComputeMetadata and returns true or false."
(.suspendNodesMatching compute (NodePredicates/inGroup group)))) ([#^ComputeService compute pred]
(.suspendNodesMatching compute (to-predicate pred))))
(defn suspend-node (defn suspend-node
"Suspend a node, given its id." "Suspend a node, given its id."
([id #^ComputeService compute] ([id #^ComputeService compute]
(.suspendNode compute id))) (.suspendNode compute id)))
(defn resume-nodes-in-group (defn resume-nodes-matching
"Suspend all the nodes in the given group." "Suspend all the nodes in the fn pred.
([#^ComputeService compute #^String group] pred should be a fn of one argument that takes a ComputeMetadata and returns true or false."
(.resumeNodesMatching compute (NodePredicates/inGroup group)))) ([#^ComputeService compute pred]
(.resumeNodesMatching compute (to-predicate pred))))
(defn resume-node (defn resume-node
"Resume a node, given its id." "Resume a node, given its id."
([id #^ComputeService compute] ([id #^ComputeService compute]
(.resumeNode compute id))) (.resumeNode compute id)))
(defn reboot-nodes-in-group (defn reboot-nodes-matching
"Reboot all the nodes in the given group." "Reboot all the nodes in the fn pred.
([#^ComputeService compute #^String group] pred should be a fn of one argument that takes a ComputeMetadata and returns true or false."
(.rebootNodesMatching compute (NodePredicates/inGroup group)))) ([#^ComputeService compute pred]
(.rebootNodesMatching compute (to-predicate pred))))
(defn reboot-node (defn reboot-node
"Reboot a node, given its id." "Reboot a node, given its id."
([id #^ComputeService compute] ([id #^ComputeService compute]
(.rebootNode compute id))) (.rebootNode compute id)))
(defn destroy-nodes-in-group (defn destroy-nodes-matching
"Destroy all the nodes in the given group." "Destroy all the nodes in the fn pred.
([#^ComputeService compute #^String group] pred should be a fn of one argument that takes a ComputeMetadata and returns true or false."
(.destroyNodesMatching compute (NodePredicates/inGroup group)))) ([#^ComputeService compute pred]
(.destroyNodesMatching compute (to-predicate pred))))
(defn destroy-node (defn destroy-node
"Destroy a node, given its id." "Destroy a node, given its id."

View File

@ -0,0 +1,32 @@
;
;
; Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
;
; ====================================================================
; 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))

View File

@ -54,21 +54,25 @@ list, Alan Dipert and MeikelBrandmeyer."
(is (compute-service? *compute*)) (is (compute-service? *compute*))
(is (compute-service? (compute-service (compute-context *compute*))))) (is (compute-service? (compute-service (compute-context *compute*)))))
(defn in-group [group] #(= (.getGroup %) group))
(deftest nodes-test (deftest nodes-test
(is (empty? (nodes *compute*))) (is (empty? (nodes *compute*)))
(is (create-node *compute* "fred" (build-template *compute* {} ))) (is (create-node *compute* "fred" (build-template *compute* {} )))
(is (= 1 (count (nodes *compute*)))) (is (= 1 (count (nodes *compute*))))
(is (= 1 (count (nodes-in-group *compute* "fred")))) (is (= 1 (count (nodes-in-group *compute* "fred"))))
(suspend-nodes-in-group *compute* "fred") (is (= 1 (count (nodes-with-details-matching *compute* (in-group "fred")))))
(is (suspended? (first (nodes-in-group *compute* "fred")))) (is (= 0 (count (nodes-with-details-matching *compute* (in-group "othergroup")))))
(resume-nodes-in-group *compute* "fred") (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")))) (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 (running? (first (nodes-in-group *compute* "fred"))))
(is (create-nodes *compute* "fred" 2 (build-template *compute* {} ))) (is (create-nodes *compute* "fred" 2 (build-template *compute* {} )))
(is (= 3 (count (nodes-in-group *compute* "fred")))) (is (= 3 (count (nodes-in-group *compute* "fred"))))
(is (= "fred" (group (first (nodes *compute*))))) (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"))))) (is (terminated? (first (nodes-in-group *compute* "fred")))))
(deftest build-template-test (deftest build-template-test