-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Hawes
committed
Jun 12, 2024
1 parent
355ff0e
commit 14a5b3f
Showing
4 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
(ns cljaws.dynamo-spec) | ||
|
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))) | ||
|
@@ -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"}}}}]}}}) | ||
|
||
) |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"}}]}}))))) |