From 48a6a0fb0e32e688f18f5b40d9c07fe312680adb Mon Sep 17 00:00:00 2001 From: Nuvindu Date: Tue, 13 Aug 2024 12:18:40 +0530 Subject: [PATCH 1/7] Add BBE for add/remove entries using LDAP --- .../ldap_add_remove_entry.bal | 24 +++++++++++++++++++ .../ldap_add_remove_entry.md | 22 +++++++++++++++++ .../ldap_add_remove_entry.metatags | 2 ++ .../ldap_add_remove_entry.server.out | 3 +++ 4 files changed, 51 insertions(+) create mode 100644 examples/ldap-add-remove-entry/ldap_add_remove_entry.bal create mode 100644 examples/ldap-add-remove-entry/ldap_add_remove_entry.md create mode 100644 examples/ldap-add-remove-entry/ldap_add_remove_entry.metatags create mode 100644 examples/ldap-add-remove-entry/ldap_add_remove_entry.server.out diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal new file mode 100644 index 0000000000..aec27e9279 --- /dev/null +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal @@ -0,0 +1,24 @@ +import ballerina/ldap; +import ballerina/io; + +public function main() returns error? { + ldap:Client ldap = check new({ + hostName: "localhost", + port: 389, + domainName: "cn=admin,dc=example,dc=com", + password: "adminpassword" + }); + + ldap:LdapResponse addResponse = check ldap->add( + "cn=user,dc=example,dc=com", + { + "objectClass": ["top", "person"], + "sn": "user", + "cn": "user" + } + ); + io:println("Add Response: ", addResponse.resultCode); + + ldap:LdapResponse deleteResponse = check ldap->delete("cn=user,dc=example,dc=com"); + io:println("Delete Response: ", deleteResponse.resultCode); +} diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.md b/examples/ldap-add-remove-entry/ldap_add_remove_entry.md new file mode 100644 index 0000000000..97f297da42 --- /dev/null +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.md @@ -0,0 +1,22 @@ +# LDAP Client - Add/Remove entries + +The `ldap:Client` connects to a directory server and performs various operations on directories. Currently, it supports the generic LDAP operations; `add`, `modify`, `modifyDN`, `compare`, `search`, `searchWithType`, `delete`, and `close`. + +The `add` operation creates a new entry in a directory server. It requires a `DN` (Distinguished Name) for the entry and the attributes to be included. The `objectClass` attribute must be specified to define the object classes for the entry, and any attributes required by these object classes should also be included. + +The `delete` operation removes an entry from a directory server. It requires a `DN` (Distinguished Name) of the entry to be deleted. + +::: code ldap_add_remove_entry.bal ::: + +## Prerequisites + +- Make sure to keep an LDAP server up and running while running the example. + +- Run the example by executing the command below. + +::: out ldap_add_remove_entry.server.out ::: + +## Related links + +- [`ldap:Client` - API documentation](https://lib.ballerina.io/ballerina/ldap/latest#Client) +- [`ldap` module - API documentation](https://lib.ballerina.io/ballerina/ldap/latest/) diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.metatags b/examples/ldap-add-remove-entry/ldap_add_remove_entry.metatags new file mode 100644 index 0000000000..e46d830780 --- /dev/null +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.metatags @@ -0,0 +1,2 @@ +description: This example is on how to create/delete entries in a directory server using LDAP in Ballerina. +keywords: ballerina, ballerina by example, bbe, ldap, add diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.server.out b/examples/ldap-add-remove-entry/ldap_add_remove_entry.server.out new file mode 100644 index 0000000000..a833c02592 --- /dev/null +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.server.out @@ -0,0 +1,3 @@ +$ bal run ldap_add_remove_entry.bal +Add Response: SUCCESS +Delete Response: SUCCESS From 84d6d4183cb53dbf14224a1e453b6339903c3c66 Mon Sep 17 00:00:00 2001 From: Nuvindu Date: Tue, 13 Aug 2024 12:18:56 +0530 Subject: [PATCH 2/7] Add BBE for searching entries using LDAP --- .../ldap_search_entry.bal | 19 +++++++++++++++ .../ldap_search_entry.md | 24 +++++++++++++++++++ .../ldap_search_entry.metatags | 2 ++ .../ldap_search_entry.server.out | 2 ++ 4 files changed, 47 insertions(+) create mode 100644 examples/ldap-search-entry copy/ldap_search_entry.bal create mode 100644 examples/ldap-search-entry copy/ldap_search_entry.md create mode 100644 examples/ldap-search-entry copy/ldap_search_entry.metatags create mode 100644 examples/ldap-search-entry copy/ldap_search_entry.server.out diff --git a/examples/ldap-search-entry copy/ldap_search_entry.bal b/examples/ldap-search-entry copy/ldap_search_entry.bal new file mode 100644 index 0000000000..a37163a911 --- /dev/null +++ b/examples/ldap-search-entry copy/ldap_search_entry.bal @@ -0,0 +1,19 @@ +import ballerina/ldap; +import ballerina/io; + +public function main() returns error? { + ldap:Client ldap = check new({ + hostName: "localhost", + port: 389, + domainName: "cn=admin,dc=example,dc=com", + password: "adminpassword" + }); + + ldap:SearchResult searchResult = check ldap->search( + "cn=user,dc=example,dc=com", + "(sn=user)", + ldap:SUB + ); + + io:println("Search Response: ", searchResult.resultCode); +} diff --git a/examples/ldap-search-entry copy/ldap_search_entry.md b/examples/ldap-search-entry copy/ldap_search_entry.md new file mode 100644 index 0000000000..fcbda0379c --- /dev/null +++ b/examples/ldap-search-entry copy/ldap_search_entry.md @@ -0,0 +1,24 @@ +# LDAP Client - Seacrh for an entry + +The `ldap:Client` connects to a directory server and performs various operations on directories. Currently, it supports the main LDAP operations; `add`, `modify`, `modifyDN`, `compare`, `search`, `searchWithType`, `delete`, and `close`. + +The `search` operation is used to retrieve entries that match specified criteria. The components of an LDAP search request include: + +- `baseDN`: Defines the base of the subtree for the search. +- `filter`: Specifies the criteria that entries must meet. +- `scope`: Determines extent of the the search coverage within the subtree. + +When a directory server processes a valid search request, it finds entries within the defined scope that meet the filter criteria. The matching entries are then returned to the client in a `ldap:SearchResult` record. + +::: code ldap_search_entry.bal ::: + +## Prerequisites +- Make sure to keep an LDAP server up and running while running the example. + +- Run the example by executing the command below. + +::: out ldap_search_entry.server.out ::: + +## Related links +- [`ldap:Client` - API documentation](https://lib.ballerina.io/ballerina/ldap/latest#Client) +- [`ldap` module - API documentation](https://lib.ballerina.io/ballerina/ldap/latest/) diff --git a/examples/ldap-search-entry copy/ldap_search_entry.metatags b/examples/ldap-search-entry copy/ldap_search_entry.metatags new file mode 100644 index 0000000000..0dff8d6d60 --- /dev/null +++ b/examples/ldap-search-entry copy/ldap_search_entry.metatags @@ -0,0 +1,2 @@ +description: This example is on how to search for an entry in a directory server using LDAP in Ballerina. +keywords: ballerina, ballerina by example, bbe, ldap, search diff --git a/examples/ldap-search-entry copy/ldap_search_entry.server.out b/examples/ldap-search-entry copy/ldap_search_entry.server.out new file mode 100644 index 0000000000..761487a021 --- /dev/null +++ b/examples/ldap-search-entry copy/ldap_search_entry.server.out @@ -0,0 +1,2 @@ +$ bal run ldap_search_entry.bal +Search Response: SUCCESS From d3abec7ee82005a4952f9aaf782cdd3be9e29ef0 Mon Sep 17 00:00:00 2001 From: Nuvindu Date: Tue, 13 Aug 2024 12:19:08 +0530 Subject: [PATCH 3/7] Index new BBEs for LDAP client --- examples/index.json | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/examples/index.json b/examples/index.json index 4e03aa6245..b61588fda4 100644 --- a/examples/index.json +++ b/examples/index.json @@ -3821,6 +3821,29 @@ } ] }, + { + "title": "LDAP client", + "column": 3, + "category": "Network libraries", + "samples": [ + { + "name": "Add/Remove entries", + "url": "ldap-add-remove-entry", + "verifyBuild": true, + "verifyOutput": false, + "disablePlayground": true, + "isLearnByExample": false + }, + { + "name": "Search for an entry", + "url": "ldap-search-entry", + "verifyBuild": true, + "verifyOutput": false, + "disablePlayground": true, + "isLearnByExample": false + } + ] + }, { "title": "JMS service", "column": 3, From 3a43320fa5f404fa95e32c8a7453964dc3f7c9c8 Mon Sep 17 00:00:00 2001 From: Nuvindu Date: Tue, 13 Aug 2024 14:30:03 +0530 Subject: [PATCH 4/7] Update description in LDAP BBEs --- .../ldap-add-remove-entry/ldap_add_remove_entry.bal | 13 ++++++++----- .../ldap-add-remove-entry/ldap_add_remove_entry.md | 2 +- .../ldap-search-entry copy/ldap_search_entry.bal | 11 ++++++----- .../ldap-search-entry copy/ldap_search_entry.md | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal index aec27e9279..1d31fee65c 100644 --- a/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal @@ -1,16 +1,18 @@ -import ballerina/ldap; import ballerina/io; +import ballerina/ldap; public function main() returns error? { - ldap:Client ldap = check new({ + // Initializes a new LDAP client with credentials. + ldap:Client ldapClient = check new ({ hostName: "localhost", port: 389, domainName: "cn=admin,dc=example,dc=com", password: "adminpassword" }); - ldap:LdapResponse addResponse = check ldap->add( - "cn=user,dc=example,dc=com", + // Adds an entry to the directory server. + ldap:LdapResponse addResponse = check ldapClient->add( + "cn=user,dc=example,dc=com", { "objectClass": ["top", "person"], "sn": "user", @@ -19,6 +21,7 @@ public function main() returns error? { ); io:println("Add Response: ", addResponse.resultCode); - ldap:LdapResponse deleteResponse = check ldap->delete("cn=user,dc=example,dc=com"); + // Deletes an entry from the directory server. + ldap:LdapResponse deleteResponse = check ldapClient->delete("cn=user,dc=example,dc=com"); io:println("Delete Response: ", deleteResponse.resultCode); } diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.md b/examples/ldap-add-remove-entry/ldap_add_remove_entry.md index 97f297da42..ba92341023 100644 --- a/examples/ldap-add-remove-entry/ldap_add_remove_entry.md +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.md @@ -1,4 +1,4 @@ -# LDAP Client - Add/Remove entries +# LDAP client - Add/Remove entries The `ldap:Client` connects to a directory server and performs various operations on directories. Currently, it supports the generic LDAP operations; `add`, `modify`, `modifyDN`, `compare`, `search`, `searchWithType`, `delete`, and `close`. diff --git a/examples/ldap-search-entry copy/ldap_search_entry.bal b/examples/ldap-search-entry copy/ldap_search_entry.bal index a37163a911..c452c5d928 100644 --- a/examples/ldap-search-entry copy/ldap_search_entry.bal +++ b/examples/ldap-search-entry copy/ldap_search_entry.bal @@ -1,19 +1,20 @@ -import ballerina/ldap; import ballerina/io; +import ballerina/ldap; public function main() returns error? { - ldap:Client ldap = check new({ + // Initialized a new LDAP client. + ldap:Client ldapClient = check new ({ hostName: "localhost", port: 389, domainName: "cn=admin,dc=example,dc=com", password: "adminpassword" }); - ldap:SearchResult searchResult = check ldap->search( - "cn=user,dc=example,dc=com", + // Searches for an entry in the directory server. + ldap:SearchResult searchResult = check ldapClient->search( + "cn=user,dc=example,dc=com", "(sn=user)", ldap:SUB ); - io:println("Search Response: ", searchResult.resultCode); } diff --git a/examples/ldap-search-entry copy/ldap_search_entry.md b/examples/ldap-search-entry copy/ldap_search_entry.md index fcbda0379c..9dac6f852c 100644 --- a/examples/ldap-search-entry copy/ldap_search_entry.md +++ b/examples/ldap-search-entry copy/ldap_search_entry.md @@ -1,4 +1,4 @@ -# LDAP Client - Seacrh for an entry +# LDAP client - Seacrh for an entry The `ldap:Client` connects to a directory server and performs various operations on directories. Currently, it supports the main LDAP operations; `add`, `modify`, `modifyDN`, `compare`, `search`, `searchWithType`, `delete`, and `close`. From 1b0616bbed574a8933bb776084e903ce0a017d5b Mon Sep 17 00:00:00 2001 From: Nuvindu Date: Tue, 13 Aug 2024 14:46:45 +0530 Subject: [PATCH 5/7] Improve the code in LDAP add/remove BBE --- .../ldap_add_remove_entry.bal | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal index 1d31fee65c..c77a6e2efb 100644 --- a/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal @@ -10,15 +10,15 @@ public function main() returns error? { password: "adminpassword" }); + // Creates an `ldap:Entry` record for the new entry. + ldap:Entry addEntry = { + "objectClass": ["top", "person"], + "sn": "user", + "cn": "user" + }; + // Adds an entry to the directory server. - ldap:LdapResponse addResponse = check ldapClient->add( - "cn=user,dc=example,dc=com", - { - "objectClass": ["top", "person"], - "sn": "user", - "cn": "user" - } - ); + ldap:LdapResponse addResponse = check ldapClient->add("cn=user,dc=example,dc=com", addEntry); io:println("Add Response: ", addResponse.resultCode); // Deletes an entry from the directory server. From 921313d43b08ccff1ebf31cd9246dba076df1f0d Mon Sep 17 00:00:00 2001 From: Nuvindu Date: Wed, 14 Aug 2024 00:51:53 +0530 Subject: [PATCH 6/7] Update the directory path of a ldap BBE --- .../ldap_search_entry.bal | 2 +- .../ldap_search_entry.md | 0 .../ldap_search_entry.metatags | 0 .../ldap_search_entry.server.out | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename examples/{ldap-search-entry copy => ldap-search-entry}/ldap_search_entry.bal (93%) rename examples/{ldap-search-entry copy => ldap-search-entry}/ldap_search_entry.md (100%) rename examples/{ldap-search-entry copy => ldap-search-entry}/ldap_search_entry.metatags (100%) rename examples/{ldap-search-entry copy => ldap-search-entry}/ldap_search_entry.server.out (100%) diff --git a/examples/ldap-search-entry copy/ldap_search_entry.bal b/examples/ldap-search-entry/ldap_search_entry.bal similarity index 93% rename from examples/ldap-search-entry copy/ldap_search_entry.bal rename to examples/ldap-search-entry/ldap_search_entry.bal index c452c5d928..9674eaa188 100644 --- a/examples/ldap-search-entry copy/ldap_search_entry.bal +++ b/examples/ldap-search-entry/ldap_search_entry.bal @@ -2,7 +2,7 @@ import ballerina/io; import ballerina/ldap; public function main() returns error? { - // Initialized a new LDAP client. + // Initializes a new LDAP client. ldap:Client ldapClient = check new ({ hostName: "localhost", port: 389, diff --git a/examples/ldap-search-entry copy/ldap_search_entry.md b/examples/ldap-search-entry/ldap_search_entry.md similarity index 100% rename from examples/ldap-search-entry copy/ldap_search_entry.md rename to examples/ldap-search-entry/ldap_search_entry.md diff --git a/examples/ldap-search-entry copy/ldap_search_entry.metatags b/examples/ldap-search-entry/ldap_search_entry.metatags similarity index 100% rename from examples/ldap-search-entry copy/ldap_search_entry.metatags rename to examples/ldap-search-entry/ldap_search_entry.metatags diff --git a/examples/ldap-search-entry copy/ldap_search_entry.server.out b/examples/ldap-search-entry/ldap_search_entry.server.out similarity index 100% rename from examples/ldap-search-entry copy/ldap_search_entry.server.out rename to examples/ldap-search-entry/ldap_search_entry.server.out From 18d4af6309395bb6b608e3ebff40e3d7f1adbc1f Mon Sep 17 00:00:00 2001 From: Nuvindu Date: Wed, 21 Aug 2024 10:51:34 +0530 Subject: [PATCH 7/7] Apply review suggestions to LDAP BBEs --- .../ldap_add_remove_entry.bal | 12 ++++++------ .../ldap-add-remove-entry/ldap_add_remove_entry.md | 8 ++++---- examples/ldap-search-entry/ldap_search_entry.bal | 14 +++++++------- examples/ldap-search-entry/ldap_search_entry.md | 5 +++-- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal index c77a6e2efb..3f59aa41f1 100644 --- a/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.bal @@ -3,12 +3,12 @@ import ballerina/ldap; public function main() returns error? { // Initializes a new LDAP client with credentials. - ldap:Client ldapClient = check new ({ - hostName: "localhost", - port: 389, - domainName: "cn=admin,dc=example,dc=com", - password: "adminpassword" - }); + ldap:Client ldapClient = check new ( + hostName = "localhost", + port = 389, + domainName = "cn=admin,dc=example,dc=com", + password = "adminpassword" + ); // Creates an `ldap:Entry` record for the new entry. ldap:Entry addEntry = { diff --git a/examples/ldap-add-remove-entry/ldap_add_remove_entry.md b/examples/ldap-add-remove-entry/ldap_add_remove_entry.md index ba92341023..194b88f6e1 100644 --- a/examples/ldap-add-remove-entry/ldap_add_remove_entry.md +++ b/examples/ldap-add-remove-entry/ldap_add_remove_entry.md @@ -9,14 +9,14 @@ The `delete` operation removes an entry from a directory server. It requires a ` ::: code ldap_add_remove_entry.bal ::: ## Prerequisites - -- Make sure to keep an LDAP server up and running while running the example. +- Ensure that an LDAP server is up and running locally on port 389 while running the example. - Run the example by executing the command below. ::: out ldap_add_remove_entry.server.out ::: ## Related links - -- [`ldap:Client` - API documentation](https://lib.ballerina.io/ballerina/ldap/latest#Client) +- [`ldap:Client` `add` operation - API documentation](https://central.ballerina.io/ballerina/ldap/latest#Client-add) +- [`ldap:Client` `delete` operation - API documentation](https://central.ballerina.io/ballerina/ldap/latest#Client-delete) - [`ldap` module - API documentation](https://lib.ballerina.io/ballerina/ldap/latest/) +- [`ldap:Client` - Specification](/spec/ldap/#2-ldap-client) diff --git a/examples/ldap-search-entry/ldap_search_entry.bal b/examples/ldap-search-entry/ldap_search_entry.bal index 9674eaa188..92d9a97a56 100644 --- a/examples/ldap-search-entry/ldap_search_entry.bal +++ b/examples/ldap-search-entry/ldap_search_entry.bal @@ -2,13 +2,13 @@ import ballerina/io; import ballerina/ldap; public function main() returns error? { - // Initializes a new LDAP client. - ldap:Client ldapClient = check new ({ - hostName: "localhost", - port: 389, - domainName: "cn=admin,dc=example,dc=com", - password: "adminpassword" - }); + // Initializes a new LDAP client with credentials. + ldap:Client ldapClient = check new ( + hostName = "localhost", + port = 389, + domainName = "cn=admin,dc=example,dc=com", + password = "adminpassword" + ); // Searches for an entry in the directory server. ldap:SearchResult searchResult = check ldapClient->search( diff --git a/examples/ldap-search-entry/ldap_search_entry.md b/examples/ldap-search-entry/ldap_search_entry.md index 9dac6f852c..cb7617f382 100644 --- a/examples/ldap-search-entry/ldap_search_entry.md +++ b/examples/ldap-search-entry/ldap_search_entry.md @@ -13,12 +13,13 @@ When a directory server processes a valid search request, it finds entries withi ::: code ldap_search_entry.bal ::: ## Prerequisites -- Make sure to keep an LDAP server up and running while running the example. +- Ensure that an LDAP server is up and running locally on port 389 while running the example. - Run the example by executing the command below. ::: out ldap_search_entry.server.out ::: ## Related links -- [`ldap:Client` - API documentation](https://lib.ballerina.io/ballerina/ldap/latest#Client) +- [`ldap:Client` `search` operation - API documentation](https://central.ballerina.io/ballerina/ldap/latest#Client-search) - [`ldap` module - API documentation](https://lib.ballerina.io/ballerina/ldap/latest/) +- [`ldap:Client` - Specification](/spec/ldap/#2-ldap-client)