from st2actions.runners.pythonrunner import Action
from st2client.client import Client
from st2client.models.datastore import KeyValuePair


class KVPAction(Action):

    def run(self, key, action, st2host='localhost', value=""):
        st2_endpoints = {
            'action': "http://%s:9101" % st2host,
            'reactor': "http://%s:9102" % st2host,
            'datastore': "http://%s:9103" % st2host
        }

        try:
            client = Client(st2_endpoints)
        except Exception as e:
            return e

        if action == 'get':
            kvp = client.keys.get_by_name(key)

            if not kvp:
                raise Exception('Key error with %s.' % key)

            return kvp.value
        else:
            instance = KeyValuePair()
            instance.id = client.keys.get_by_name(key).name
            instance.name = key
            instance.value = value

            try:
                kvstore = getattr(client.keys, action)
                kvp = kvstore(instance)
            except Exception as e:
                raise

            if action == 'delete':
                return kvp
            else:
                return kvp.serialize()

@@ -748,16 +748,48 @@
 stance =
+ client.keys.get_by_name(key) or
  KeyValu
@@ -826,41 +826,11 @@
 d = 
-client.keys.get_by_name(key).name
+key
 %0A   
@@ -910,163 +910,85 @@
     
-try:%0A                kvstore = getattr(client.keys, action)%0A                kvp = kvstore(instance)%0A            except Exception as e:%0A                rais
+kvp = client.keys.update(instance) if action in %5B'create', 'update'%5D else Non
 e%0A%0A 

Fix create action for key value pair