StackStorm-Exchange / stackstorm-st2

StackStorm pack management
https://exchange.stackstorm.org/
Apache License 2.0
5 stars 18 forks source link

Feature: Add KV "entry" actions #24

Open cognifloyd opened 3 years ago

cognifloyd commented 3 years ago

An "entry" is a JSON object with properties. Multiple entries are stored under a JSON serialized object (as created by st2.kv.set_object) in the datastore. When you retrieve an "entry", you can specify a fallback "entry" to use for defaults.

For example, consider this object:

{
  "myentry": {
    "prop_a": "overriden"
  },
  "default": {
    "prop_a": "A",
    "prop_b": "B",
    "prop_c": "C"
  }
}

If you retrieve myentry with the default fallback, you would get this object:

{
  "prop_a": "overriden",
  "prop_b": "B",
  "prop_c": "C"
}

These are the new actions:

Since more than one action could edit the same KV object at the same time, the edit actions (set_property, append_property, and delete_property) all use a distributed lock through the coordination backend (if configured) to serialize edits.

My use-case: I am using this as to provide environment specific config in the datastore. So, I have something like these entries: default (the fallback), prod, stage, qa, dev. Then various actions or workflows just need to get the entry for the environment they're working with, and not have to worry about selecting from a different default object if it a var is undefined for that environment (The YAQL for selecting from multiple objects like that gets really ugly really fast).

This pattern of config with defaults seems common, so am submitting this as part of the st2 pack to make the datastore an even more useful tool with less effort.

amanda11 commented 3 years ago

Just curious - why "upsert_property" instead of just "set_property" - I think a set would be normally considered an add or update if exists function.

cognifloyd commented 3 years ago

set_property would be more consistent with the others. My original reasons for selecting upsert instead of set don't apply now because I refactored before submitting the PR :P Before I had upsert_entry_property (instead of entry.upsert_property) - trying to indicate that it was being inserted or updated within the entry.

I think I'll just rename it to set for consistency :)

cognifloyd commented 3 years ago

Updated to use set instead of upsert.

CLAassistant commented 2 years ago

CLA assistant check
All committers have signed the CLA.