Skip to content

Commit d910592

Browse files
Merge pull request #488 from Saglodha/master
Addition of schema to schema groups in event hub - PS/ Bash sample
2 parents 0b193ff + ab93acf commit d910592

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#Step 1- first step is to get the bearer token using client credential flow. We should set the resource to be URL encoded string for eventhubs.azure.net
2+
response=$(curl -X POST -d 'grant_type=client_credentials&client_id=<Application_ID>&client_secret=<Application_Secret>&resource=https%3A%2F%2Feventhubs.azure.net' https://login.microsoftonline.com/<Tenant_Id>/oauth2/token)
3+
4+
#formatting token to drop "" quotes and suffixing bearer for final use
5+
token="Bearer `echo $response | jq ."access_token" | tr -d '"'`"
6+
7+
#Step 2-Making a REST call to dataplane endpoint to add schema to schema groups
8+
curl -X PUT -d '{"namespace": "com.azure.schemaregistry.samples","type": "record","name": "Order","fields": [{"name": "id","type": "string"},{"name": "amount","type": "double"}]}' -H "Content-Type:application/json" -H "Authorization:$token" -H "Serialization-Type:Avro" \
9+
'https://<Namespace_Name>.servicebus.windows.net/$schemagroups/<SchemaGroup_Name>/schemas/<Schema_Name>?api-version=2020-09-01-preview'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Addition of Schema under Schema groups
2+
3+
With release of [Schema registry](https://docs.microsoft.com/en-us/azure/event-hubs/schema-registry-overview#what-is-azure-schema-registry) feature in Event Hubs, it becomes easier to store different kinds of schemas under schema groups and have seamless interaction between producer and consumer application. Since addition of schema under schema groups is a data plane operation ( not intended to hit management endpoint) hence, there is no publicly available PS Cmdlet to abstract this functionality.
4+
5+
This sample is meant to serve as reference code for adding schemas under schema groups using Bash.
6+
7+
# Prerequisites
8+
9+
Please ensure that following steps are followed before following the sample code:
10+
11+
1. Local machine with linux/mac OS or you could use Azure CloudShell via https://shell.azure.com
12+
13+
2. Create AD application for authentication,here are additional details: https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal
14+
3. Please keep a note of Application ID, secret that you had created in last step ( you would be using this in PS sample)
15+
4. Assign the service principal Schema registry contributor role (To know more about how to assign a role: https://docs.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal?tabs=current ), so that it could make changes to schema groups. Here is quick read about Schema registry contributor role and permissions it posseses : https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#schema-registry-contributor-preview
16+
5. Finally, update the following placeholders in sample code with information associated to your environment:
17+
- Application_ID: Application ID of AD Application
18+
- Application_Secret: Secret value associated to Application
19+
- Tenant_Id: Tenant ID of Associated AD tenant
20+
- Namespace_Name: Name of Event Hubs namespace
21+
- SchemaGroup_Name: Name of schema groups under event hub namespace
22+
- Schema_Name: Name of schema to be added under Schema group
23+
24+
# Script Overview
25+
26+
This PS sample can be spliced into two REST calls that are being made.
27+
28+
1. Once we have the AD application details as talked above, we would make first API call to fetch the authorization token needed to successfully make schema addition call.
29+
2. After fetching and formatting the bearer token, we trigger another API call to add schema under schema groups. Once this code runs successfully, you should be able to find the newly added schema under schema groups on Azure Portal.
30+
31+
# Additional Reference:
32+
33+
Here are some additional reference links that could be helpful:
34+
35+
- Schema Registry in Event Hubs - https://docs.microsoft.com/en-us/azure/event-hubs/schema-registry-overview
36+
- Event hubs Overview- https://docs.microsoft.com/en-us/azure/event-hubs/
37+
38+
# Conclusion
39+
40+
We would love to hear about your feedback about this sample. We would be happy to see any pull requests if in case you are interested to contribute to our community.
41+
42+
Stay tuned for future updates from Azure Messaging team. Happy scripting!
43+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
#Step 1 - Pass in AD Application details below to be able to fetch authorization token against legitimate Service Principal.
3+
$Fields = @{
4+
grant_type = "client_credentials"
5+
client_id = "<Application_ID>"
6+
resource = "https://eventhubs.azure.net"
7+
client_secret = "<Application_Secret>"
8+
};
9+
10+
#update tenant ID to the correct tenant where you would like to get authorization token from
11+
$response = Invoke-RestMethod –Uri "https://login.microsoftonline.com/<Tenant_ID>/oauth2/token" –ContentType "application/x-www-form-urlencoded" –Method POST –Body $Fields
12+
13+
#response would have bearer token in the properties that we would use to trigger next call.
14+
$token = $response.access_token
15+
16+
#Step 2- You can declare schema JSON associated to schema in $Body variable below
17+
$Body = @"
18+
{
19+
"namespace": "com.azure.schemaregistry.samples",
20+
"type": "record",
21+
"name": "Order",
22+
"fields": [
23+
{
24+
"name": "id",
25+
"type": "string"
26+
},
27+
{
28+
"name": "amount",
29+
"type": "double"
30+
}
31+
]
32+
}
33+
"@
34+
35+
#Step 3- This Uri needs to have details added- namespace name, schema group name and schema name respectively.
36+
37+
$NamespaceName = '<namespacename>'
38+
$SchemaGroupName = '<schemagroupname>'
39+
$SchemaName = '<SchemaName>'
40+
41+
42+
$uri = "https://$NamespaceName.servicebus.windows.net/$schemagroups/$SchemaGroupName/schemas/$SchemaName`?api-version=2020-09-01-preview"
43+
44+
#Step 4- These headers would be sent along with the API Call
45+
$headers = @{
46+
"Content-Type" = "application/atom+xml;type=entry;charset=utf-8"
47+
"Serialization-Type" = "Avro"
48+
"Authorization" = "Bearer " + $token
49+
}
50+
51+
#Step 5- This step shows the final call that we make to add schema under the schema groups,
52+
Invoke-RestMethod -Method "PUT" -Uri $uri -Headers $headers -Body $Body -ContentType "application/json"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Addition of Schema under Schema groups
2+
3+
With release of [Schema registry](https://docs.microsoft.com/en-us/azure/event-hubs/schema-registry-overview#what-is-azure-schema-registry) feature in Event Hubs, it becomes easier to store different kinds of schemas under schema groups and have seamless interaction between producer and consumer application. Since addition of schema under schema groups is a data plane operation ( not intended to hit management endpoint) hence, there is no publicly available PS Cmdlet to abstract this functionality.
4+
5+
This sample is meant to serve as reference code for adding schemas under schema groups using PowerShell.
6+
7+
# Prerequisites
8+
9+
Please ensure that following steps are followed before following the sample code:
10+
11+
1. Updated PS module in Windows machine or you could use Azure CloudShell via https://shell.azure.com
12+
13+
2. Create AD application for authentication,here are additional details: https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal
14+
3. Please keep a note of Application ID, secret that you had created in last step ( you would be using this in PS sample)
15+
4. Assign the service principal Schema registry contributor role (To know more about how to assign a role: https://docs.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal?tabs=current ), so that it could make changes to schema groups. Here is quick read about Schema registry contributor role and permissions it posseses : https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#schema-registry-contributor-preview
16+
5. Finally, update the following placeholders in sample code with information associated to your environment:
17+
- Application_ID: Application ID of AD Application
18+
- Application_Secret: Secret value associated to Application
19+
- resource: needs to be set as "https://eventhubs.azure.net", since we are authenticating against data endpoint
20+
- Tenant_Id: Tenant ID of Associated AD tenant
21+
- Namespace_Name: Name of Event Hubs namespace
22+
- SchemaGroup_Name: Name of schema groups under event hub namespace
23+
- Schema_Name: Name of schema to be added under Schema group
24+
25+
# Script Overview
26+
27+
This PS sample can be spliced into two REST calls that are being made.
28+
29+
1. Once we have the AD application details as talked above, we would make first API call to fetch the authorization token needed to successfully make schema addition call.
30+
2. After fetching and formatting the bearer token, we trigger another API call to add schema under schema groups. Once this code runs successfully, you should be able to find the newly added schema under schema groups on Azure Portal.
31+
32+
# Additional Reference:
33+
34+
Here are some additional reference links that could be helpful:
35+
36+
- Schema Registry in Event Hubs - https://docs.microsoft.com/en-us/azure/event-hubs/schema-registry-overview
37+
- Event hubs Overview- https://docs.microsoft.com/en-us/azure/event-hubs/
38+
39+
# Conclusion
40+
41+
We would love to hear about your feedback about this sample. We would be happy to see any pull requests if in case you are interested to contribute to our community.
42+
43+
Stay tuned for future updates from Azure Messaging team. Happy scripting!
44+

0 commit comments

Comments
 (0)