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.
"
(: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."

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-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