Skip to content

Commit 781904e

Browse files
Merge pull request #217699 from PallabPaul/pallabpaul/confidential-ledger-python-quickstart-fix
[Confidential Ledger] Update Python Quickstart Document
2 parents effb627 + 29702d6 commit 781904e

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

articles/confidential-ledger/quickstart-python.md

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ We'll finish setup by setting some variables for use in your application: the re
9797
> Each ledger must have a globally unique name. Replace \<your-unique-ledger-name\> with the name of your ledger in the following example.
9898
9999
```python
100-
resource_group = "myResourceGroup"
100+
resource_group = "<azure-resource-group>"
101101
ledger_name = "<your-unique-ledger-name>"
102102
subscription_id = "<azure-subscription-id>"
103103

@@ -166,7 +166,7 @@ network_identity = identity_client.get_ledger_identity(
166166

167167
ledger_tls_cert_file_name = "networkcert.pem"
168168
with open(ledger_tls_cert_file_name, "w") as cert_file:
169-
cert_file.write(network_identity.ledger_tls_certificate)
169+
cert_file.write(network_identity['ledgerTlsCertificate'])
170170
```
171171

172172
Now we can use the network certificate, along with the ledger URL and our credentials, to create a confidential ledger client.
@@ -179,21 +179,29 @@ ledger_client = ConfidentialLedgerClient(
179179
)
180180
```
181181

182-
We are prepared to write to the ledger. We will do so using the `create_ledger_entry` function.
182+
We are prepared to write to the ledger. We will do so using the `create_ledger_entry` function.
183183

184184
```python
185-
append_result = ledger_client.create_ledger_entry(entry_contents="Hello world!")
186-
print(append_result.transaction_id)
185+
sample_entry = {"contents": "Hello world!"}
186+
append_result = ledger_client.create_ledger_entry(entry=sample_entry)
187+
print(append_result['transactionId'])
187188
```
188189

189190
The print function will return the transaction ID of your write to the ledger, which can be used to retrieve the message you wrote to the ledger.
190191

191192
```python
192-
latest_entry = ledger_client.get_current_ledger_entry(transaction_id=append_result.transaction_id)
193+
entry = ledger_client.get_ledger_entry(transaction_id=append_result['transactionId'])['entry']
194+
print(f"Entry (transaction id = {entry['transactionId']}) in collection {entry['collectionId']}: {entry['contents']}")
195+
```
196+
197+
If you would simply like the latest transaction that was committed to the ledger, you can use the `get_current_ledger_entry` function.
198+
199+
```python
200+
latest_entry = ledger_client.get_current_ledger_entry()
193201
print(f"Current entry (transaction id = {latest_entry['transactionId']}) in collection {latest_entry['collectionId']}: {latest_entry['contents']}")
194202
```
195203

196-
The print function will return "Hello world!", as that is the message in the ledger that that corresponds to the transaction ID.
204+
The print function will return "Hello world!", as that is the message in the ledger that that corresponds to the transaction ID and is the latest transaction.
197205

198206
## Full sample code
199207

@@ -210,12 +218,11 @@ from azure.mgmt.confidentialledger.models import ConfidentialLedger
210218

211219
from azure.confidentialledger import ConfidentialLedgerClient
212220
from azure.confidentialledger.certificate import ConfidentialLedgerCertificateClient
213-
from azure.confidentialledger import TransactionState
214221

215222
# Set variables
216223

217-
rg = "myResourceGroup"
218-
ledger_name = "<unique-ledger-name>"
224+
resource_group = "<azure-resource-group>"
225+
ledger_name = "<your-unique-ledger-name>"
219226
subscription_id = "<azure-subscription-id>"
220227

221228
identity_url = "https://identity.confidential-ledger.core.azure.com"
@@ -250,14 +257,14 @@ ledger_properties = ConfidentialLedger(**properties)
250257

251258
# Create a ledger
252259

253-
confidential_ledger_mgmt.ledger.begin_create(rg, ledger_name, ledger_properties)
260+
confidential_ledger_mgmt.ledger.begin_create(resource_group, ledger_name, ledger_properties)
254261

255262
# Get the details of the ledger you just created
256263

257-
print(f"{rg} / {ledger_name}")
264+
print(f"{resource_group} / {ledger_name}")
258265

259266
print("Here are the details of your newly created ledger:")
260-
myledger = confidential_ledger_mgmt.ledger.get(rg, ledger_name)
267+
myledger = confidential_ledger_mgmt.ledger.get(resource_group, ledger_name)
261268

262269
print (f"- Name: {myledger.name}")
263270
print (f"- Location: {myledger.location}")
@@ -274,7 +281,7 @@ network_identity = identity_client.get_ledger_identity(
274281

275282
ledger_tls_cert_file_name = "networkcert.pem"
276283
with open(ledger_tls_cert_file_name, "w") as cert_file:
277-
cert_file.write(network_identity.ledger_tls_certificate)
284+
cert_file.write(network_identity['ledgerTlsCertificate'])
278285

279286

280287
ledger_client = ConfidentialLedgerClient(
@@ -284,14 +291,35 @@ ledger_client = ConfidentialLedgerClient(
284291
)
285292

286293
# Write to the ledger
287-
append_result = ledger_client.create_ledger_entry(entry_contents="Hello world!")
288-
print(append_result.transaction_id)
294+
sample_entry = {"contents": "Hello world!"}
295+
ledger_client.create_ledger_entry(entry=sample_entry)
289296

290297
# Read from the ledger
291-
entry = ledger_client.get_current_ledger_entry(transaction_id=append_result.transaction_id)
298+
latest_entry = ledger_client.get_current_ledger_entry()
292299
print(f"Current entry (transaction id = {latest_entry['transactionId']}) in collection {latest_entry['collectionId']}: {latest_entry['contents']}")
293300
```
294301

302+
## Pollers
303+
304+
If you would like to wait for your write transaction to be committed to your ledger you can use the `begin_create_ledger_entry` function. This will return a poller to wait until the entry is durably committed.
305+
306+
```python
307+
sample_entry = {"contents": "Hello world!"}
308+
ledger_entry_poller = ledger_client.begin_create_ledger_entry(
309+
entry=sample_entry
310+
)
311+
ledger_entry_result = ledger_entry_poller.result()
312+
```
313+
314+
Querying an older ledger entry requires the ledger to read the entry from disk and validate it. You can use the `begin_get_ledger_entry` function to create a poller that will wait until the queried entry is in a ready state to view.
315+
316+
```python
317+
get_entry_poller = ledger_client.begin_get_ledger_entry(
318+
transaction_id=ledger_entry_result['transactionId']
319+
)
320+
entry = get_entry_poller.result()
321+
```
322+
295323
## Clean up resources
296324

297325
Other Azure confidential ledger articles can build upon this quickstart. If you plan to continue on to work with subsequent quickstarts and tutorials, you may wish to leave these resources in place.

0 commit comments

Comments
 (0)