Last active
          August 29, 2015 14:16 
        
      - 
      
- 
        Save d11wtq/702f272ff0234c83f88a to your computer and use it in GitHub Desktop. 
Revisions
- 
        d11wtq revised this gist Mar 12, 2015 . 1 changed file with 42 additions and 0 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ // example/factory/aws/cloudformation.js module.exports.applyStackFunc = function(createFunc, updateFunc){ return function(payload) { try { return createFunc(payload); } catch (e if e instanceof AlreadyExistsException) { return updateFunc(payload); } }; }; module.exports.deployStackFunc = function(applyFunc) { return function(stackName, template, params) { return applyFunc({ stackName: stackName, templateBody: JSON.stringify(template), parameters: expandParams(params) }); } }; // example/aws/cloudformation.js var factory = require('example/factory/aws/cloudformation') , AWS = require('aws-sdk') ; var cloudformation = new AWS.CloudFormation(); module.exports.applyStack = factory.applyStackFunc( cloudformation.createStack, cloudformation.updateStack ); module.exports.deployStack = factory.deployStackFunc( module.exports.applyStack ); 
- 
        d11wtq revised this gist Mar 12, 2015 . 1 changed file with 37 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,6 +29,7 @@ (ns example.aws.cloudformation (:require [example.factory.aws.cloudformation :as factory] [amazonica.aws.cloudformation :as cloudformation])) ; side-effecty AWS SDK @@ -38,4 +39,39 @@ cloudformation/update-stack)) (def deploy-stack (factory/deploy-stack-fn apply-stack)) (ns example.factory.aws.cloudformation-test (:require [clojure.test :refer :all] [example.factory.aws.cloudformation :as factory])) (deftest apply-stack-fn-test (testing "applies create-fn with payload" (let [payload {:stack-name "example"} create-fn (spy) update-fn (spy)] ((factory/apply-stack-fn create-fn update-fn) payload) (is (called? (create-fn payload))))) (testing "when stack already exists" (testing "applies update-fn with payload" (let [payload {:stack-name "example"} create-fn (fn [& args] (throw (AlreadyExistsException.))) update-fn (spy)] ((factory/apply-stack-fn create-fn update-fn) payload) (is (called? (update-fn payload))))))) (deftest deploy-stack-fn (testing "applies apply-fn with constructed payload" (let [apply-fn (spy)] ((factory/deploy-stack-fn apply-fn) "example" {:Description "Example"} {:port "8080"}) (is (called? (apply-fn {:stack-name "example" :template-body "{\"Description\":\"Example\"}" :parameters [{:parameter-key :port :parameter-value "8080"}]})))))) 
- 
        d11wtq revised this gist Mar 12, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,6 +26,9 @@ :template-body (json/write-str template-hash) :parameters (expand-params params-hash)))) (ns example.aws.cloudformation (:require [example.factory.aws.cloudformation :as factory] [amazonica.aws.cloudformation :as cloudformation])) ; side-effecty AWS SDK 
- 
        d11wtq revised this gist Mar 12, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ (ns example.factory.aws.cloudformation (:require [clojure.data.json :as json])) (defn apply-stack-fn [create-fn update-fn] 
- 
        d11wtq revised this gist Mar 12, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ (defn expand-params [params-hash] (map (fn [[key value]] (zipmap [:parameter-key :parameter-value] [key value])) params)) 
- 
        d11wtq revised this gist Mar 12, 2015 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,8 +11,11 @@ (defn expand-params [params-hash] (map (fn [key value] (zipmap [:parameter-key :parameter-value] [key value])) params)) (defn deploy-stack-fn [apply-fn] 
- 
        d11wtq revised this gist Mar 12, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,8 @@ :parameters (expand-params params-hash)))) (ns example.aws.cloudformation (:require [example.factory.aws.cloudformation :as factory] [amazonica.aws.cloudformation :as cloudformation])) ; side-effecty AWS SDK (def apply-stack (factory/apply-stack-fn cloudformation/create-stack 
- 
        d11wtq created this gist Mar 12, 2015 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ (ns example.factory.aws.cloudformation) (defn apply-stack-fn [create-fn update-fn] (fn apply-stack [payload] (try (create-fn payload) (catch AlreadyExistsException e (update-fn payload))))) (defn expand-params [params-hash] ; something pure ) (defn deploy-stack-fn [apply-fn] (fn deploy-stack [stack-name template-hash params-hash] (apply-fn :stack-name stack-name :template-body (json/write-str template-hash) :parameters (expand-params params-hash)))) (ns example.aws.cloudformation (:require [example.factory.aws.cloudformation :as factory])) (def apply-stack (factory/apply-stack-fn cloudformation/create-stack cloudformation/update-stack)) (def deploy-stack (factory/deploy-stack-fn apply-stack))