python-consul icon indicating copy to clipboard operation
python-consul copied to clipboard

Support for `txn` in HTTP API

Open iandyh opened this issue 7 years ago • 15 comments

https://www.consul.io/api/txn.html

Any play for implementing this? Thanks.

iandyh avatar May 02 '17 05:05 iandyh

ping @cablehead

iandyh avatar May 10 '17 06:05 iandyh

Pull requests gratefully accepted.

cablehead avatar Jun 19 '17 01:06 cablehead

@cablehead are you planning to work on this anytime in the near future? If not I may take a look at implementing it.

beardedeagle avatar Aug 08 '17 16:08 beardedeagle

@beardedeagle Hi. I have a branch currently supports txn but does not support tornado and other async client yets. I am planning to add the support next week.

iandyh avatar Aug 09 '17 01:08 iandyh

I've deleted my previous comment, as I misread the conversation above.

Hi there. I won't have time to work on this in the near future (I've just recently become a parent, and I'm slowly adjusting!). @iandyh is it possible to create a PR for your branch? If it looks OK we can look to get it merge. @beardedeagle it'd be awesome if you had time to work on it to, particularly in order to support async clients as well.

cablehead avatar Aug 09 '17 04:08 cablehead

@iandyh That would be immediately be helpful to me since I am not using any async clients, but if you get it pr'd in I can look at helping to get those implemented.

beardedeagle avatar Aug 09 '17 20:08 beardedeagle

@beardedeagle @cablehead Let me work on it on Friday with async support if it's straightforward.

iandyh avatar Aug 10 '17 00:08 iandyh

@iandyh were you able to make any headway on that? I pulled your changes locally and have started to tinker.

beardedeagle avatar Aug 14 '17 14:08 beardedeagle

Hi

Sorry I was busy last Friday and then it's the Japan holiday season. If it's urgent, feel free to use iandyh/python-marathon txn branch. I've been used it for development for a while. I'll prepare the PR when I am back from holiday(Thursday this week)

Sent from my iPhone

On Aug 14, 2017, at 23:39, Randy Thompson [email protected] wrote:

@iandyh were you able to make any headway on that?

― You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

iandyh avatar Aug 15 '17 07:08 iandyh

I have merged initial work into master via #166. However, note that there is further work to be done here so leaving this issue open for now, might create more atomic issues later.

abn avatar Aug 22 '17 05:08 abn

@abn Just out of curiosity, what atomic issues you are referring to?

iandyh avatar Aug 22 '17 07:08 iandyh

@iandyh I just mean I might create more github issues with smaller chunks of further work required to expand on the feature.

abn avatar Aug 22 '17 09:08 abn

Hi, I tried to use txn like this:

curl --insecure --header "X-Consul-Token: abc123" \
--request PUT \
-H "Content-Type: application/json" \
-d '[{ "KV": {"Verb": "get-tree","Key": "service"} }]' \
https://consulhost:8501/v1/txn

This works fine.

My code does not:

import platform
print("python ", platform.python_version())
import consul
print("consul ", consul.__version__)
import urllib3
urllib3.disable_warnings()

payload = [{ "KV": { "Verb": "get-tree", "Key": "service" } }]

c = consul.Consul(host="consulhost",port="8501",token="abc123",scheme='https',verify=False)

result = c.txn.put(payload)
print(result)

However "Results" is always an empty list:

python  3.6.2
consul  0.7.2-dev
{'Results': [], 'Errors': None, 'Index': 0, 'LastContact': 0, 'KnownLeader': True}

What am I doing wrong?

paltryeffort avatar Aug 25 '17 14:08 paltryeffort

@paltryeffort I have tried to reproduce this but failed, havent tried with a token yet though. Here is what I did.

  1. Start consul instance via docker.
docker run --rm -it -p 8500:8500 consul
  1. Execute the following script.
import json

from consul.std import Consul


if __name__ == '__main__':
    c = Consul()
    c.txn.put(payload=[
        {
            "KV": {
                "Verb": "check-not-exists",
                "Key": "foo"
            }
        },
        {
            "KV": {
                "Verb": "set",
                "Key": "foo/bar",
                "Value": ""
            }
        },
        {
            "KV": {
                "Verb": "set",
                "Key": "foo/baz",
                "Value": ""
            }
        }
    ])
    r = c.txn.put(payload=[
        {
            "KV": {
                "Verb": "get-tree",
                "Key": "foo"
            }
        }
    ])
    print(json.dumps(r, indent=2))
    c.txn.put(payload=[
        {
            "KV": {
                "Verb": "delete-tree",
                "Key": "foo"
            }
        }
    ])

This produce the following output.

{
  "Index": 0, 
  "Errors": null, 
  "Results": [
    {
      "KV": {
        "LockIndex": 0, 
        "ModifyIndex": 99, 
        "Value": null, 
        "Flags": 0, 
        "Key": "foo/bar", 
        "CreateIndex": 99
      }
    }, 
    {
      "KV": {
        "LockIndex": 0, 
        "ModifyIndex": 99, 
        "Value": null, 
        "Flags": 0, 
        "Key": "foo/baz", 
        "CreateIndex": 99
      }
    }
  ], 
  "KnownLeader": true, 
  "LastContact": 0
}

abn avatar Aug 26 '17 05:08 abn

@abn you have no token required with docker image. That is the reason why your test has passed. If able, backport this pr to 0.7.2 (0.7.3?) as this version is included into EPEL repo for CentOS 7.

amuhametov avatar Aug 02 '18 13:08 amuhametov