Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed to be able to read user-scope datastore value via '{{ st2kv.user }}' notation. #4463

Merged

Conversation

userlocalhost
Copy link
Member

@userlocalhost userlocalhost commented Dec 3, 2018

Background

The change of (#3199) and document of Datastore say StackStorm supports the I/F to get datastore value via {{ st2kv.system }} and {{ st2kv.user }}.

Related Issue

Problem

StackStorm didn't have an implementation to accept {{ st2kv.user }} expression.

Change

This implements the processing to accept {{ st2kv.user }} notation and render it to user-scope value in datastore which is associated with executed user.

@userlocalhost userlocalhost force-pushed the feature/st2common_st2kv_notation branch from efcde1f to 942ae59 Compare December 3, 2018 06:34
user = action_context['user'] if 'user' in action_context else None
user = action_context['api_user'] if 'api_user' in action_context else user
if user:
system_keyvalue_context[USER_SCOPE] = UserKeyValueLookup(scope=FULL_USER_SCOPE, user=user)
Copy link
Member

@Kami Kami Dec 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this approach, but we should document this behavior (something along the lines that st2kv.user.foo will only work for actions which have user context attached to and and are executed via the API or similar).

Also, what will happen if we are unable to read user from the context and action author references st2kv.user in a default value? I assume it will throw a template rendering error.

If that's the case, we should consider doing the following:

  1. Add a test case for this scenario
  2. Instead of generic template rendering error being thrown, we should see if we can throw a more user-friendly error

Another option is to default it to the system user (stanley), we do this in other places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides that, I'm fine with this change.

Nice work 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your review. I got it. I'll do that soon!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@userlocalhost Can you please also open st2docs PR which documents this behavior?

@Kami Kami added this to the 3.0.0 milestone Dec 3, 2018
@userlocalhost userlocalhost force-pushed the feature/st2common_st2kv_notation branch from 4730eab to 6ab5372 Compare December 7, 2018 03:58
@userlocalhost userlocalhost changed the title Changed to be able to read user-scope datastore value via '{{ st2kv.user }}' notation. Changed to be able to read user-scope datastore value via '{{ st2kv.user }}' notation. (WIP) Dec 7, 2018
…no user and api_user are specified for rendering parameter.
@userlocalhost userlocalhost force-pushed the feature/st2common_st2kv_notation branch from 6ab5372 to 832045c Compare December 7, 2018 06:12
@userlocalhost userlocalhost changed the title Changed to be able to read user-scope datastore value via '{{ st2kv.user }}' notation. (WIP) Changed to be able to read user-scope datastore value via '{{ st2kv.user }}' notation. Dec 7, 2018
@userlocalhost
Copy link
Member Author

Thank you again for your review and telling me advices to brush up.

Also, what will happen if we are unable to read user from the context and action author references st2kv.user in a default value?

I've taken care of this problem by the idea that system user fills in for the one.

@userlocalhost
Copy link
Member Author

I fixed the points that you mentioned, would you please review this patch, @Kami?

@Kami
Copy link
Member

Kami commented Jan 7, 2019

Thanks, I will have a look later today.

@Kami Kami modified the milestones: 2.10.0, 3.0.0 Jan 7, 2019
@userlocalhost
Copy link
Member Author

Thank you very much!

@Kami Kami self-assigned this Jan 7, 2019
@Kami Kami merged commit 10c27ce into StackStorm:master Jan 7, 2019
@Kami
Copy link
Member

Kami commented Jan 7, 2019

Merged, thanks.

Please also open a corresponding st2docs PR when you get a chance :)

@userlocalhost
Copy link
Member Author

I think we don't have to amend st2docs because the explanation of this feature has already been described at the Datastore page in st2docs.

This is an excerpt from it.

Also, you can refer to user variables in actions or workflows. The Jinja syntax to do so is {{st2kv.user.date_cmd}}.

Note that the notion of st2kv.user is available only when actions or workflows are run manually. The notion of st2kv.user is non-existent when actions or workflows are kicked off via rules. So the use of user scoped variables is limited to manual execution of actions or workflows.

I think this description is good enough to explain how to use this and what limitations are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants