Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Hawes committed Jun 12, 2024
1 parent 355ff0e commit 14a5b3f
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 1 deletion.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject timotheosh/cljaws "0.5.0-SNAPSHOT"
(defproject timotheosh/cljaws "0.5.1-SNAPSHOT"
:description "Convenience functions for interacting with AWS API's from closh"
:url "https://github.com/timotheosh/cljaws"
:license {:name "MIT"
Expand Down
2 changes: 2 additions & 0 deletions src/cljaws/dynamo_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns cljaws.dynamo-spec)

86 changes: 86 additions & 0 deletions src/cljaws/dynamodb.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@
:ExpressionAttributeNames expr-attr-nams}
expr-attr-vals (assoc :ExpressionAttributeValues expr-attr-vals))})))

(defn format-put
"Formats a put item for DynamoDB."
[item]
(let [keys (merge (:pk item) (:sk item))
formatted-item (into {}
(concat
(map (fn [[k v]] [(name k) (format-value v)]) keys)
(map (fn [[k v]] [(name k) (format-value v)]) (:attributes item))))]
{:PutRequest {:Item formatted-item}}))

(defn format-delete
"Formats a delete item for DynamoDB."
[item]
(let [key-map (into {}
(map (fn [[k v]] [(name k) (format-value v)])
(merge (:pk item) (:sk item))))]
(doseq [[k v] (merge (:pk item) (:sk item))]
(validate-key v))
{:DeleteRequest {:Key key-map}}))

(defn format-operations
"Formats the operations (put and delete) for DynamoDB."
[ops]
(let [put-requests (mapv format-put (get ops :put []))
delete-requests (mapv format-delete (get ops :delete []))]
(apply conj put-requests delete-requests)))

(defn format-batch-write
"Formats data for a batch write operation in DynamoDB, supporting both put and delete operations, grouped by table."
[requests]
{:op :BatchWriteItem
:request {:RequestItems (into {}
(mapv (fn [[table-name ops]]
[table-name (format-operations ops)])
requests))}})

(defn scan-table
"Returns a list of all items in a DynamoDB table"
([table-name] (scan-table table-name :default (get-region :default)))
Expand Down Expand Up @@ -121,3 +157,53 @@
([entity-type entity-id updates removals] (update-item *table-name* entity-type entity-id updates nil))
([table-name entity-type entity-id updates removals]
(format-update-item table-name entity-type entity-id updates removals)))

(defn batch-write
"Batch write operations can span multiple tables."
[requests]
)


(comment
(def r
{:jira-account-map
{:put [{:pk {:AccountId "key1"}
:sk {:Email "[email protected]"}
:description "Something"
:resources ["resource1" "resource2"]}]
:delete [{:pk {:AccountId "jfdjd"}
:sk {:Email "[email protected]"}}]}
:jira-resource-manager
{:put [{:pk {:entity-type "resource"} :sk {:entity-id "New Relic"}
:attributes {:okta-group-ids ["yetAnotherGroupId"]}}
{:pk {:entity-type "resource"} :sk {:entity-id "Odin"}
:attibutes {:okta-group-ids ["andAnotherGroupId"]}}]
:delete [{:pk {:entity-type "resource"} :sk {:entity-id "Databricks"}}]}}
)

(def q
{:op :BatchWriteItem,
:request
{:RequestItems
{"jira-account-map"
[{:PutRequest
{:Item
{"description" {:S "Something"},
"resources" {:L [{:S "resource1"} {:S "resource2"}]},
"AccountId" {:S "key1"},
"Email" {:S "[email protected]"}}}}
{:DeleteRequest {:Key {"AccountId" {:S "jfdjd"}, "Email" {:S "[email protected]"}}}}],
"jira-resource-manager"
[{:PutRequest
{:Item
{"entity-type" {:S "resource"},
"entity-id" {:S "New Relic"}
"okta-group-ids" {:L [{:S "yetAnotherGroupId"}]}}}}
{:PutRequest
{:Item
{"entity-type" {:S "resource"},
"entity-id" {:S "Odin"}
"okta-group-ids" {:L [{:S "andAnotherGroupId"}]}}}}
{:DeleteRequest {:Key {"entity-type" {:S "resource"} "entity-id" {:S "Databricks"}}}}]}}})

)
70 changes: 70 additions & 0 deletions test/cljaws/dynamodb_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,73 @@
{:L [{:S "AWS SSO Dev"} {:S "AWS SSO Test"} {:S "Databricks"}]}}}}
(sut/format-update-item "jira-resource-manager" {:entity-type "group"} {:entity-id "Intern"}
{:resources ["AWS SSO Dev" "AWS SSO Test" "Databricks"]} ["no-op"])))))

(deftest test-format-put
(testing "Test format-put"
(is (= {:PutRequest
{:Item
{"AccountId" {:S "key1"},
"Email" {:S "[email protected]"},
"description" {:S "Something"},
"resources" {:L [{:S "resource1"} {:S "resource2"}]}}}}
(sut/format-put {:pk {:AccountId "key1"}
:sk {:Email "[email protected]"}
:attributes {:description "Something"
:resources ["resource1" "resource2"]}})))))

(deftest test-format-delete
(testing "Test format-delete"
(is (= {:DeleteRequest {:Key {"AccountId" {:S "jfdjd"}, "Email" {:S "[email protected]"}}}}
(sut/format-delete {:pk {:AccountId "jfdjd"}
:sk {:Email "[email protected]"}})))))

(deftest test-format-operations
(testing "Test format-operations"
(is (= [{:PutRequest
{:Item
{"description" {:S "Something"},
"resources" {:L [{:S "resource1"} {:S "resource2"}]},
"AccountId" {:S "key1"},
"Email" {:S "[email protected]"}}}}
{:DeleteRequest {:Key {"AccountId" {:S "jfdjd"}, "Email" {:S "[email protected]"}}}}]
(sut/format-operations {:put [{:pk {:AccountId "key1"}
:sk {:Email "[email protected]"}
:attributes {:description "Something"
:resources ["resource1" "resource2"]}}]
:delete [{:pk {:AccountId "jfdjd"}
:sk {:Email "[email protected]"}}]})))))

(deftest test-format-batch-write
(testing "Multiple tables with put and delete items."
(is (= {:op :BatchWriteItem,
:request
{:RequestItems
{:jira-account-map
[{:PutRequest
{:Item
{"AccountId" {:S "key1"}, "Email" {:S "[email protected]"}}}}
{:DeleteRequest
{:Key {"AccountId" {:S "jfdjd"}, "Email" {:S "[email protected]"}}}}],
:jira-resource-manager
[{:PutRequest
{:Item
{"entity-type" {:S "resource"},
"entity-id" {:S "New Relic"},
"okta-group-ids" {:L [{:S "yetAnotherGroupId"}]}}}}
{:PutRequest
{:Item {"entity-type" {:S "resource"}, "entity-id" {:S "Odin"}}}}
{:DeleteRequest
{:Key {"entity-type" {:S "resource"}, "entity-id" {:S "Databricks"}}}}]}}}
(sut/format-batch-write {:jira-account-map
{:put [{:pk {:AccountId "key1"}
:sk {:Email "[email protected]"}
:description "Something"
:resources ["resource1" "resource2"]}]
:delete [{:pk {:AccountId "jfdjd"}
:sk {:Email "[email protected]"}}]}
:jira-resource-manager
{:put [{:pk {:entity-type "resource"} :sk {:entity-id "New Relic"}
:attributes {:okta-group-ids ["yetAnotherGroupId"]}}
{:pk {:entity-type "resource"} :sk {:entity-id "Odin"}
:attibutes {:okta-group-ids ["andAnotherGroupId"]}}]
:delete [{:pk {:entity-type "resource"} :sk {:entity-id "Databricks"}}]}})))))

0 comments on commit 14a5b3f

Please sign in to comment.