mirror of
https://github.com/ansible/awx.git
synced 2026-02-01 17:48:10 -03:30
Basic API object creation. Auth still stubbed out.
This commit is contained in:
@@ -6,17 +6,30 @@ import json
|
||||
|
||||
# this is temporary
|
||||
username = os.getenv("ACOM_USER","admin")
|
||||
password = os.getenv("ACOM_PASS""admin")
|
||||
password = os.getenv("ACOM_PASS","admin")
|
||||
server = os.getenv("ACOM_SERVER","127.0.0.1:8000")
|
||||
|
||||
handle = hammock.Hammock("http://%s/api/v1" % server, auth=(username,password))
|
||||
# TODO: error handling/output/etc
|
||||
# TODO: format into actual command line
|
||||
|
||||
PARAMS = {
|
||||
'format' : 'json',
|
||||
}
|
||||
HEADERS = {
|
||||
'Content-Type' : 'application/json'
|
||||
}
|
||||
AUTH = (username, password)
|
||||
|
||||
handle = hammock.Hammock("http://%s/api/v1" % server, auth=AUTH, append_slash=True, params=PARAMS, headers=HEADERS)
|
||||
|
||||
class Collection(object):
|
||||
|
||||
def __init__(self, handle):
|
||||
self.handle = handle
|
||||
self.response = self.accessor().GET()
|
||||
self.response = self.accessor().GET(auth=AUTH, headers=HEADERS)
|
||||
assert self.response.status_code == 200
|
||||
# TODO: error handling on non-200
|
||||
print "RESPONSE=%s" % self.response.text
|
||||
self.data = json.loads(self.response.text)
|
||||
self.meta = self.data['meta']
|
||||
self.objects = self.data['objects']
|
||||
@@ -26,6 +39,18 @@ class Collection(object):
|
||||
def accessor(self):
|
||||
return exceptions.NotImplementedError()
|
||||
|
||||
def add(self, data):
|
||||
# TODO: error handling
|
||||
json_data = json.dumps(data)
|
||||
response = self.accessor().POST(data=json_data)
|
||||
print response.status_code
|
||||
print response.text
|
||||
assert response.status_code == 201
|
||||
# FIXME: error handling
|
||||
data2 = response.text
|
||||
print data2
|
||||
return Entry(data)
|
||||
|
||||
def __iter__(self):
|
||||
for x in self.objects:
|
||||
yield Entry(x)
|
||||
@@ -34,8 +59,9 @@ class Entry(object):
|
||||
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.resource_uri = data['resource_uri']
|
||||
self.accessor = hammock.Hammock(self.resource_uri, auth=(username,password))
|
||||
self.resource_uri = data.get('resource_uri', None)
|
||||
print "LOADING"
|
||||
self.accessor = hammock.Hammock(self.resource_uri, auth=AUTH, append_slash=True, params=PARAMS, headers=HEADERS)
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.data)
|
||||
@@ -50,9 +76,20 @@ class Organizations(Collection):
|
||||
#
|
||||
|
||||
try:
|
||||
print "---"
|
||||
orgs = Organizations(handle)
|
||||
for x in orgs:
|
||||
print x
|
||||
print "---"
|
||||
orgs.add(dict(description="new org?", name="new org"))
|
||||
print "---"
|
||||
|
||||
print "---"
|
||||
orgs = Organizations(handle)
|
||||
for x in orgs:
|
||||
print x
|
||||
|
||||
|
||||
except requests.exceptions.ConnectionError:
|
||||
print "connect failed"
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user