Package: lxd
Async python client forLXD REST API(currently under heavy development).ContentsUsageInstallationInitialize clientExample usagesChange instance stateGet event streamAvailable EndpointsServerserver.getserver.update_configurationserver.update_configuration_subsetserver.get_resourcesserver.get_eventsCertificatescertificates.listcertificates.getcertificates.addcertificates.update_configurationcertificates.update_configuration_subsetcertificates.removeInstancesOperationsTODOUsageInstallationpipinstalllxdInitialize clientimportasynciofrompathlibimportPathfromyarlimportURLfromlxd.clientimportlxd_clientasyncdefmain():client=lxd_client(URL('https://mylxd:8443/'),cert_path=Path('~/.config/lxc/client.crt'),key_path=Path('~/.config/lxc/client.key'),endpoint_cert_path=Path('~/.config/lxc/servercerts/mylxd.crt'),)awaitclient.authenticate(cert_path=Path('~/.config/lxc/client.crt'),password='your-trust-password')asyncio.run(main())Example usages# Recursion 0 returns only links to objects,# you can resolve them by awaitinginstance_links=awaitclient.instances.list(recursion=0)instance=awaitinstance_links[0]# Recursion 1 returns only some fieldsinstances=awaitclient.instances.list(recursion=1)# Recursion 2 returns all possible informationinstances=awaitclient.instances.list(recursion=2)Change instance statefromlxd.entities.instancesimportInstanceActioninstances=awaitclient.instances.list()operation=awaitclient.instances.update_state(instances[0].name,action=InstanceAction.STOP)awaitclient.operations.wait(operation.id)# wait as long as possibleawaitclient.operations.wait(operation.id,timeout=30)# 30sGet event streamasyncforeventinclient.server.get_events():# See Event object for more propertiesprint(event.type)print(event.metadata)Available EndpointsServerserver.getGet server environment and configuration.# See lxd.entities.server.Serverinfo=awaitclient.server.get()print(info.config)print(info.environment)server.update_configurationUpdate the entireserver configuration.awaitclient.server.update_configuration({'core.https_address':'0.0.0.0:8443''core.trust_password':'very-strong-password'})server.update_configuration_subsetUpdate a subset of theserver configuration.awaitclient.server.update_configuration_subset({'images.remote_cache_expiry':2})server.get_resourcesGets the hardware information profile of the LXD server.# See lxd.entities.server.ServerResourcesserver_resources=awaitclient.server.get_resources()print(server_resources.cpu)server.get_eventsConnect toevent APIusing websocket.# Listen all eventsasyncforeventinclient.server.get_events():print(event.type)print(event.metadata)# Listen to specific eventsasyncforeventinclient.server.get_events(type='operation'):print(event.metadata.id)print(event.metadata.status)Certificatescertificates.listReturns a list of trusted certificates.# See lxd.entities.certificates.Certificatecerts=awaitclient.certificates.list()print(certs[0].fingerprint)If you passrecursion=0parameter, lxd would return just references,
which are represented in current module aslxd.entities.certificates.CertificateLinkobjects.If youawaitsuch link object - you would get object itself (separate http
request is performed for every await call).cert_links=awaitclient.certificates.list(recursion=0)certs=awaitasyncio.gather(*cert_links)certificates.getGets a specific certificate entry from the trust store by fingerprint.fromcryptography.x509importload_pem_x509_certificatefromcryptography.hazmat.primitivesimporthashesfprint='97f267c0fe20fd013b6b4ba3f5440ea3e9361ce8568d41c633f28c620ab37ea0'cert=awaitclient.certificates.get(fprint)cert_obj=load_pem_x509_certificate(cert.certificate.encode())assertcert_obj.fingerprint(hashes.SHA256()).hex()==fprintcertificates.addAdds a certificate to the trust store as trusted user (client certificate
should be trusted).fromcryptographyimportx509fromcryptography.x509.oidimportNameOIDfromcryptography.hazmat.backendsimportdefault_backendfromcryptography.hazmat.primitivesimporthashes,serializationfromcryptography.hazmat.primitives.asymmetricimportrsaprivate_key=rsa.generate_private_key(public_exponent=65537,key_size=2048,backend=default_backend())subj=x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,"alvassin@osx")])cert=x509.CertificateBuilder().subject_name(subj).issuer_name(subj).public_key(private_key.public_key()).serial_number(x509.random_serial_number()).not_valid_before(datetime.utcnow()).not_valid_after(datetime.utcnow()+timedelta(days=365)).sign(private_key=private_key,algorithm=hashes.SHA256(),backend=default_backend())awaitclient.certificates.add(cert.public_bytes(serialization.Encoding.PEM))Ifpasswordargument is specified, adds a certificate to the trust store
as an untrusted user.awaitclient.certificates.add(cert.public_bytes(serialization.Encoding.PEM),password='your-trust-password')certificates.update_configurationUpdate the entire certificate configuration.awaitclient.certificates.update_configuration('97f267c0fe20fd013b6b4ba3f5440ea3e9361ce8568d41c633f28c620ab37ea0',certificate='-----BEGIN CERTIFICATE-----\n...',name='new-name',projects=[],restricted=False,type='client')certificates.update_configuration_subsetUpdate a subset of the certificate configuration.awaitclient.certificates.update_configuration_subset('97f267c0fe20fd013b6b4ba3f5440ea3e9361ce8568d41c633f28c620ab37ea0',name='another-name')certificates.removeRemoves the certificate from the trust store.awaitclient.certificates.remove('97f267c0fe20fd013b6b4ba3f5440ea3e9361ce8568d41c633f28c620ab37ea0')Instancesclient.instances.listclient.instances.getclient.instances.createclient.instances.deleteclient.instances.get_stateclient.instances.update_stateOperationsclient.operations.listclient.operations.getclient.operations.waitclient.operations.cancelTODOAddfiltering support.