-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsul_entry.py
More file actions
67 lines (54 loc) · 1.82 KB
/
Copy pathconsul_entry.py
File metadata and controls
67 lines (54 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import subprocess
import json
from base64 import b64decode
from configs import DEFAULT_KV_ENDPOINT
def PutKv(key, val):
try:
url = "curl -s -k --request PUT --data " + val + " " + DEFAULT_KV_ENDPOINT + key
returnval = subprocess.Popen(url, shell=True, stdout=subprocess.PIPE).stdout
except:
print("Error occured")
raise
print(key, ":" ,val, "successfully put")
def GetKv(key, recurse=False):
try:
url = "curl -s -k " + DEFAULT_KV_ENDPOINT + key
url = url + "?recurse=true" if recurse else url
returnval = subprocess.Popen(url , shell=True, stdout=subprocess.PIPE) #+ "/\?recurse=true" if recurse else ""
val = returnval.stdout.read()
if "?keys" == key:
return val
ret_val = json.loads(val.decode("utf-8"))
print ({x['Key']:b64decode(x['Value']).decode('utf-8') for x in ret_val})
except:
print(key, ": Key not found!, please retry")
def DeleteKv(key, recurse=False):
try:
url = "curl -s -k --request DELETE " + DEFAULT_KV_ENDPOINT + key
url = url + "?recurse=true" if recurse else url
returnval = subprocess.Popen(url, shell=True, stdout=subprocess.PIPE).stdout
print(returnval)
except:
print("invalid key!")
raise
print(key, "deleted!")
#print(returnval)
def printList():
print("******************************************\n")
print("press 1 to put key-val")
print("press 2 to get val")
print("press 3 to delete a key-val pair")
print("******************************************\n")
choice = int(input())
Switch(choice)
def Switch(argument):
switcher = {
1: PutKv,
2: GetKv,
3: DeleteKv,
}
#function call
switcher[argument]()
if __name__ == "__main__":
while True:
printList()