本文整理汇总了Python中cloudhands.common.connectors.Registry.query方法的典型用法代码示例。如果您正苦于以下问题:Python Registry.query方法的具体用法?Python Registry.query怎么用?Python Registry.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cloudhands.common.connectors.Registry
的用法示例。
在下文中一共展示了Registry.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def setUp(self):
""" Populate test database"""
session = Registry().connect(sqlite3, ":memory:").session
initialise(session)
session.add_all((
Organisation(
uuid=uuid.uuid4().hex,
name="TestOrg"),
Provider(
uuid=uuid.uuid4().hex,
name="testcloud.io"),
)
)
session.commit()
org = session.query(Organisation).one()
provider = session.query(Provider).one()
actor = session.query(Component).filter(
Component.handle == "burst.controller").one()
active = session.query(SubscriptionState).filter(
SubscriptionState.name == "active").one()
subs = Subscription(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org, provider=provider)
session.add(subs)
session.commit()
now = datetime.datetime.utcnow()
act = Touch(artifact=subs, actor=actor, state=active, at=now)
net = ipaddress.ip_network("172.16.144.0/29")
session.add_all(
(IPAddress(value=str(ip), provider=provider, touch=act)
for ip in net.hosts()))
session.commit()
self.assertEqual(6, session.query(IPAddress).count())
session.add_all((
CatalogueItem(
uuid=uuid.uuid4().hex,
name="Web Server",
description="Apache server VM",
note=None,
logo=None,
natrouted=True,
organisation=org,
),
CatalogueItem(
uuid=uuid.uuid4().hex,
name="File Server",
description="OpenSSH server VM",
note=None,
logo=None,
natrouted=False,
organisation=org,
)
))
session.commit()
示例2: publish_user_membership
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def publish_user_membership(self):
log = logging.getLogger(__name__ + ".publish_user_membership")
session = Registry().connect(sqlite3, self.args.db).session
initialise(session)
while True:
try:
# Unwieldy, but left outer joins seem not to work with table
# inheritance.
unpublished = [
mship for mship in session.query(Membership).join(
Touch).join(State, State.name == "accepted").all()
if not any(isinstance(r, LDAPAttribute)
for c in mship.changes for r in c.resources)]
for mship in unpublished:
user = mship.changes[1].actor
reg = next((
r for r in session.query(Registration).all()
if (r.changes[0].actor is user)), None)
if reg is None:
raise StopIteration("Failed finding registration.")
try:
uid = next(r for c in reversed(reg.changes)
for r in c.resources if isinstance(r, PosixUId))
except StopIteration:
continue
record = LDAPRecord(
dn={
("cn={},ou=Groups,ou=jasmin2,"
"ou=People,o=hpc,dc=rl,dc=ac,dc=uk").format(
mship.organisation.name.lower() + "_vcloud-admins")
},
memberUId={uid.value},
)
msg = LDAPProxy.WriteLDAPAttribute(record, mship.uuid)
yield from self.ldapQ.put(msg)
record = LDAPRecord(
dn={("cn={},ou=jasmin2,"
"ou=People,o=hpc,dc=rl,dc=ac,dc=uk").format(uid.value)},
description={
"jvo:{}".format(mship.organisation.name.lower())
},
)
msg = LDAPProxy.WriteLDAPAttribute(record, mship.uuid)
yield from self.ldapQ.put(msg)
except Exception as e:
log.error(e)
finally:
session.close()
log.debug("Waiting for {}s".format(self.args.interval))
yield from asyncio.sleep(self.args.interval)
示例3: test_using_touches
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_using_touches(self):
then = datetime.datetime.utcnow() - datetime.timedelta(seconds=1)
session = Registry().connect(sqlite3, ":memory:").session
user = User(handle=None, uuid=uuid.uuid4().hex)
reg = Registration(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__)
preconfirm = session.query(RegistrationState).filter(
RegistrationState.name == "pre_registration_inetorgperson").one()
reg.changes.append(
Touch(artifact=reg, actor=user, state=preconfirm, at=then))
session.add(reg)
session.commit()
self.assertIs(reg.changes[0].state, preconfirm)
self.assertIs(session.query(Touch).first().state, preconfirm)
self.assertEqual(session.query(Touch).count(), 1)
now = datetime.datetime.utcnow()
self.assertTrue(now > then)
valid = session.query(RegistrationState).filter(
RegistrationState.name == "valid").one()
act = Touch(artifact=reg, actor=user, state=valid, at=now)
reg.changes.append(act)
hash = BcryptedPassword(value="a" * 60, touch=act, provider=None)
session.add(hash)
session.commit()
self.assertIs(reg.changes[1].state, valid)
self.assertIs(
session.query(Touch).order_by(Touch.at)[-1].state, valid)
self.assertEqual(session.query(Touch).count(), 2)
self.assertEqual(
session.query(Touch).filter(Touch.at < now).first(),
reg.changes[0])
self.assertIs(
session.query(Touch).filter(
Touch.at > then).first(),
reg.changes[1])
reg.changes.sort(key=operator.attrgetter("at"), reverse=True)
self.assertEqual(
session.query(Touch).filter(
Touch.at < now).first(),
reg.changes[1])
self.assertIs(
session.query(Touch).filter(
Touch.at > then).first(),
reg.changes[0])
示例4: test_organisation_field
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_organisation_field(self):
session = Registry().connect(sqlite3, ":memory:").session
org = session.query(Organisation).one()
mship = Membership(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org,
role="user")
session.add(mship)
session.commit()
self.assertIs(mship, session.query(Membership).first())
self.assertIs(org, mship.organisation)
示例5: test_directory_attaches_to_membership
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_directory_attaches_to_membership(self):
session = Registry().connect(sqlite3, ":memory:").session
session.flush()
archive = session.query(Archive).filter(
Archive.name == "NITS").first()
org = session.query(Organisation).one()
subs = Subscription(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org, provider=archive)
user = User(handle=None, uuid=uuid.uuid4().hex)
session.add_all((subs, user))
session.commit()
mship = Membership(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org,
role="user")
now = datetime.datetime.utcnow()
invite = session.query(MembershipState).filter(
MembershipState.name == "created").one()
mship.changes.append(
Touch(artifact=mship, actor=user, state=invite, at=now))
session.add(mship)
session.commit()
# illustrates user onboarding - membership gets decorated with
# directory resources
now = datetime.datetime.utcnow()
for subs in org.subscriptions:
if isinstance(subs.provider, Archive):
d = Directory(
description="CEDA data archive",
mount_path="/{mount}/panfs/ceda") # anticipates templating
latest = mship.changes[-1]
act = Touch(
artifact=mship, actor=user, state=latest.state, at=now)
d.touch = act
mship.changes.append(act)
session.add(d)
session.commit()
# Check we can get at the resources from the membership
self.assertIs(
d,
session.query(Resource).join(Touch).join(Membership).filter(
Membership.id == mship.id).one())
示例6: test_name_unique_across_organisations
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_name_unique_across_organisations(self):
session = Registry().connect(sqlite3, ":memory:").session
session.add(Organisation(
name="BRANSTON", uuid=uuid.uuid4().hex))
session.commit()
orgs = session.query(Organisation).all()
session.add_all((
CatalogueItem(
name="Blog Server",
description="WordPress server VM",
note=None,
logo=None,
organisation=orgs[0]
),
CatalogueItem(
name="Blog Server",
description="Tumblr server VM",
note=None,
logo=None,
organisation=orgs[1]
)
))
self.assertRaises(
sqlalchemy.exc.IntegrityError, session.commit)
示例7: test_reallocate_ip
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_reallocate_ip(self):
session = Registry().connect(sqlite3, ":memory:").session
session.autoflush = False # http://stackoverflow.com/a/4202016
oName = "TestOrg"
providerName = "testcloud.io"
handle = "Test User"
hName = "mynode.test.org"
ipAddr = "192.168.1.1"
user = User(handle=handle, uuid=uuid.uuid4().hex)
org = session.query(Organisation).one()
provider = Provider(
name=providerName, uuid=uuid.uuid4().hex)
session.add_all((user, org, provider))
session.commit()
scheduling = session.query(HostState).filter(
HostState.name == "scheduling").one()
up = session.query(HostState).filter(
HostState.name == "up").one()
hosts = [
Host(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org,
name=hName),
Host(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org,
name=hName),
]
now = datetime.datetime.utcnow()
hosts[0].changes.append(
Touch(artifact=hosts[0], actor=user, state=up, at=now))
hosts[1].changes.append(
Touch(artifact=hosts[1], actor=user, state=scheduling, at=now))
session.add_all(hosts)
session.commit()
ip = allocate_ip(session, hosts[0], provider, ipAddr)
self.assertIn(ip, [r for c in hosts[0].changes for r in c.resources])
ip = allocate_ip(session, hosts[1], provider, ipAddr)
self.assertNotIn(
ip, [r for c in hosts[0].changes for r in c.resources])
self.assertIn(ip, [r for c in hosts[1].changes for r in c.resources])
示例8: publish_uidnumber
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def publish_uidnumber(self):
log = logging.getLogger(__name__ + ".uidnumber")
session = Registry().connect(sqlite3, self.args.db).session
initialise(session)
actor = session.query(Component).filter(
Component.handle=="identity.controller").one()
while True:
try:
unpublished = [
r for r in session.query(Registration).all() if (
r.changes[-1].state.name ==
"user_posixaccount")]
for reg in unpublished:
resources = [r for c in reversed(reg.changes)
for r in c.resources]
emailAddr = next(i for i in resources
if isinstance(i, EmailAddress))
uid = next(i for i in resources if isinstance(i, PosixUId))
uidNumber = next(i for i in resources
if isinstance(i, PosixUIdNumber))
record = LDAPRecord(
dn={("cn={},ou=jasmin2,"
"ou=People,o=hpc,dc=rl,dc=ac,dc=uk").format(uid.value)},
objectclass={"posixAccount"},
uid={uid.value},
uidNumber={uidNumber.value},
gidNumber={uidNumber.value},
gecos={"{} <{}>".format(uid.value, emailAddr.value)},
homeDirectory={
self.config.get(
"mount", "home", fallback="/home/{}"
).format(uid.value)
},
loginShell={"/bin/bash"},
)
log.debug(record)
msg = LDAPProxy.WriteUIdNumber(record, reg.uuid)
yield from self.ldapQ.put(msg)
session.expire(reg)
except Exception as e:
log.error(e)
finally:
log.debug("Waiting for {}s".format(self.args.interval))
yield from asyncio.sleep(self.args.interval)
示例9: test_descriptions_may_be_omitted
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_descriptions_may_be_omitted(self):
session = Registry().connect(sqlite3, ":memory:").session
session.add_all((
Label(name="One"),
Label(name="Two"),
))
session.commit()
self.assertEqual(2, session.query(Label).count())
示例10: test_names_are_not_unique
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_names_are_not_unique(self):
session = Registry().connect(sqlite3, ":memory:").session
session.add_all((
Label(name="Test", description="Test description"),
Label(name="Test", description="Test description"),
))
session.commit()
self.assertEqual(2, session.query(Label).count())
示例11: publish_uuid
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def publish_uuid(self):
log = logging.getLogger(__name__ + ".uuid")
session = Registry().connect(sqlite3, self.args.db).session
initialise(session)
actor = session.query(Component).filter(
Component.handle=="identity.controller").one()
while True:
try:
unpublished = [
r for r in session.query(Registration).all() if (
r.changes[-1].state.name ==
"pre_user_inetorgperson_dn")]
for reg in unpublished:
# TODO: Get latest PosixUId resource
try:
uid = next(r for c in reversed(reg.changes)
for r in c.resources if isinstance(r, PosixUId))
except StopIteration:
continue
log.debug(uid)
user = reg.changes[0].actor
surname = user.surname or "UNKNOWN"
record = LDAPRecord(
dn={("cn={},ou=jasmin2,"
"ou=People,o=hpc,dc=rl,dc=ac,dc=uk").format(reg.uuid)},
objectclass={"top", "person", "organizationalPerson",
"inetOrgPerson"},
description={"cluster:jasmin-login"},
cn={reg.uuid},
sn={surname},
)
resources = [
r for i in reg.changes for r in i.resources
if isinstance(r, EmailAddress)]
if resources:
record["mail"].add(resources[0].value)
msg = LDAPProxy.WriteCommonName(record, reg.uuid)
yield from self.ldapQ.put(msg)
session.expire(reg)
except Exception as e:
log.error(e)
finally:
log.debug("Waiting for {}s".format(self.args.interval))
yield from asyncio.sleep(self.args.interval)
示例12: test_organisation_subscribes_to_archive
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_organisation_subscribes_to_archive(self):
session = Registry().connect(sqlite3, ":memory:").session
archive = session.query(Archive).filter(
Archive.name == "NITS").first()
self.assertTrue(archive)
org = session.query(Organisation).one()
subs = Subscription(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org, provider=archive)
session.add(subs)
session.commit()
self.assertEqual(
1, session.query(Subscription, Organisation, Archive).filter(
Organisation.id == org.id).filter(
Archive.id == archive.id).count())
self.assertEqual(1, len(org.subscriptions))
示例13: AgentTesting
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
class AgentTesting(unittest.TestCase):
def setUp(self):
""" Populate test database"""
self.session = Registry().connect(sqlite3, ":memory:").session
initialise(self.session)
self.session.add_all((
Organisation(
uuid=uuid.uuid4().hex,
name="TestOrg"),
Provider(
uuid=uuid.uuid4().hex,
name="cloudhands.jasmin.vcloud.phase04.cfg"),
User(handle="testuser", uuid=uuid.uuid4().hex),
))
self.session.commit()
org, user = (
self.session.query(Organisation).one(),
self.session.query(User).one(),
)
self.session.add(
Membership(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org,
role="user")
)
self.session.commit()
self.mship = self.session.query(Membership).one()
accepted = self.session.query(
MembershipState).filter(
MembershipState.name == "accepted").one()
now = datetime.datetime.utcnow()
act = Touch(artifact=self.mship, actor=user, state=accepted, at=now)
self.session.add(act)
self.session.commit()
def tearDown(self):
""" Every test gets its own in-memory database """
r = Registry()
r.disconnect(sqlite3, ":memory:")
示例14: test_required_field
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_required_field(self):
session = Registry().connect(sqlite3, ":memory:").session
org = session.query(Organisation).one()
mship = Membership(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org)
session.add(mship)
self.assertRaises(
sqlalchemy.exc.IntegrityError, session.commit)
示例15: test_ldapattribute_attaches_to_membership
# 需要导入模块: from cloudhands.common.connectors import Registry [as 别名]
# 或者: from cloudhands.common.connectors.Registry import query [as 别名]
def test_ldapattribute_attaches_to_membership(self):
def find_mships_without_attributes(session):
return [
mship for mship in session.query(Membership).join(Touch).join(
State, State.name == "accepted").all()
if not any(isinstance(r, LDAPAttribute)
for c in mship.changes for r in c.resources)]
session = Registry().connect(sqlite3, ":memory:").session
session.flush()
user = session.query(User).one()
org = session.query(Organisation).one()
mship = Membership(
uuid=uuid.uuid4().hex,
model=cloudhands.common.__version__,
organisation=org,
role="user")
accepted = session.query(MembershipState).filter(
MembershipState.name == "accepted").one()
now = datetime.datetime.utcnow()
session.add(Touch(artifact=mship, actor=user, state=accepted, at=now))
session.commit()
remaining = find_mships_without_attributes(session)
self.assertEqual(1, len(remaining))
now = datetime.datetime.utcnow()
act = Touch(artifact=mship, actor=user, state=accepted, at=now)
resource = LDAPAttribute(
dn="cn={},ou=jasmin,ou=Groups,o=hpc,dc=rl,dc=ac,dc=uk".format(org.name),
key="memberUid", value=user.uuid, verb="add", touch=act)
session.add(resource)
session.commit()
# Check we can get at the resources from the membership
self.assertEqual(
(mship, resource),
session.query(Membership, LDAPAttribute).join(Touch).join(LDAPAttribute).filter(
Membership.id == mship.id).one())
remaining = find_mships_without_attributes(session)
self.assertFalse(remaining, remaining)