From a21712acc860fac08131ac224cd45637ced0ea20 Mon Sep 17 00:00:00 2001 From: dblock Date: Mon, 5 Aug 2024 14:45:02 -0400 Subject: [PATCH] Added samples. Signed-off-by: dblock --- CHANGELOG.md | 1 + DEVELOPER_GUIDE.md | 8 +++ README.md | 25 ++++----- samples/README.md | 15 ++++++ samples/composer.json | 18 +++++++ samples/index.php | 23 +++++++++ .../Endpoints/MlNamespaceIntegrationTest.php | 6 +-- tests/Utility.php | 51 +++---------------- 8 files changed, 88 insertions(+), 59 deletions(-) create mode 100644 samples/README.md create mode 100644 samples/composer.json create mode 100644 samples/index.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c2902445..393be22af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Added - Generate endpoints from OpenSearch API Specification ([#194](https://github.com/opensearch-project/opensearch-php/pull/194)) - Added workflow for automated API update using OpenSearch API specification ([#209](https://github.com/opensearch-project/opensearch-php/pull/209)) +- Added samples ([#218](https://github.com/opensearch-project/opensearch-php/pull/218)) ### Changed ### Deprecated ### Removed diff --git a/DEVELOPER_GUIDE.md b/DEVELOPER_GUIDE.md index 6f615d3e6..11f396c40 100644 --- a/DEVELOPER_GUIDE.md +++ b/DEVELOPER_GUIDE.md @@ -36,6 +36,14 @@ The integration tests are using by default following address `https://admin:admi To run the integration tests, you can use `composer run integration` +```bash +export OPENSEARCH_PASSWORD=myStrongPassword123! +export OPENSEARCH_URL=https://admin:$OPENSEARCH_PASSWORD@localhost:9200 + +composer run integration +``` + + ### Static analyse and code style checker The project uses PhpStan for static analyse and php-cs-fixer for code style checker. You can use both tools with following codes diff --git a/README.md b/README.md index 62c2c163c..5c151062d 100644 --- a/README.md +++ b/README.md @@ -10,30 +10,31 @@ ## Welcome! -**opensearch-php** is [a community-driven, open source fork](https://aws.amazon.com/blogs/opensource/introducing-opensearch/) of elasticsearch-php licensed under the [Apache v2.0 License](https://github.com/opensearch-project/opensearch-php/blob/main/LICENSE). For more information, see [opensearch.org](https://opensearch.org/). +**opensearch-php** is [a community-driven, open source fork](https://aws.amazon.com/blogs/opensource/introducing-opensearch/) of elasticsearch-php licensed under the [Apache v2.0 License](LICENSE). For more information, see [opensearch.org](https://opensearch.org/). ## Project Resources * [Project Website](https://opensearch.org/) -* [User Guide And Sample Code](https://github.com/opensearch-project/opensearch-php/blob/main/USER_GUIDE.md) -* [Developer Guide](https://github.com/opensearch-project/opensearch-php/blob/main/DEVELOPER_GUIDE.md) +* [User Guide](USER_GUIDE.md) +* [Samples](samples) +* [Developer Guide](DEVELOPER_GUIDE.md) * [Downloads](https://opensearch.org/downloads.html). * [Documentation](https://opensearch.org/docs/latest/) * Need help? Try [Forums](https://discuss.opendistrocommunity.dev/) * [Project Principles](https://opensearch.org/#principles) -* [Contributing to OpenSearch](https://github.com/opensearch-project/opensearch-php/blob/main/CONTRIBUTING.md) -* [Maintainer Responsibilities](https://github.com/opensearch-project/opensearch-php/blob/main/MAINTAINERS.md) -* [Release Management](https://github.com/opensearch-project/opensearch-php/blob/main/RELEASING.md) -* [Admin Responsibilities](https://github.com/opensearch-project/opensearch-php/blob/main/ADMINS.md) -* [Security](https://github.com/opensearch-project/opensearch-php/blob/main/SECURITY.md) +* [Contributing to OpenSearch](CONTRIBUTING.md) +* [Maintainer Responsibilities](MAINTAINERS.md) +* [Release Management](RELEASING.md) +* [Admin Responsibilities](ADMINS.md) +* [Security](SECURITY.md) ## Code of Conduct -This project has adopted the [Amazon Open Source Code of Conduct](https://github.com/opensearch-project/opensearch-php/blob/main/CODE_OF_CONDUCT.md). For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq), or contact [opensource-codeofconduct@amazon.com](mailto:opensource-codeofconduct@amazon.com) with any additional questions or comments. +This project has adopted the [Amazon Open Source Code of Conduct](CODE_OF_CONDUCT.md). For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq), or contact [opensource-codeofconduct@amazon.com](mailto:opensource-codeofconduct@amazon.com) with any additional questions or comments. ## Sample code -See [Sample Code](https://github.com/opensearch-project/opensearch-php/blob/main/USER_GUIDE.md). +See [Sample Code](USER_GUIDE.md). ## Compatibility with OpenSearch @@ -41,8 +42,8 @@ See [Compatibility](COMPATIBILITY.md). ## License -This project is licensed under the [Apache v2.0 License](https://github.com/opensearch-project/opensearch-php/blob/main/LICENSE). +This project is licensed under the [Apache v2.0 License](LICENSE). ## Copyright -Copyright OpenSearch Contributors. See [NOTICE](https://github.com/opensearch-project/opensearch-php/blob/main/NOTICE) for details. \ No newline at end of file +Copyright OpenSearch Contributors. See [NOTICE](NOTICE) for details. \ No newline at end of file diff --git a/samples/README.md b/samples/README.md new file mode 100644 index 000000000..5c2fd6c9e --- /dev/null +++ b/samples/README.md @@ -0,0 +1,15 @@ +### OpenSearch PHP Client Samples + +Start an OpenSearch instance. + +``` +docker run -d -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" -e "OPENSEARCH_INITIAL_ADMIN_PASSWORD=myStrongPassword123!" opensearchproject/opensearch:latest +``` + +Run sample. + +``` +export OPENSEARCH_PASSWORD=myStrongPassword123! +composer install +composer run index +``` diff --git a/samples/composer.json b/samples/composer.json new file mode 100644 index 000000000..90aa91375 --- /dev/null +++ b/samples/composer.json @@ -0,0 +1,18 @@ +{ + "name": "opensearch-php/samples", + "description": "OpenSearch PHP client samples.", + "type": "project", + "authors": [], + "scripts": { + "index": ["php index.php"] + }, + "repositories": [ + { + "type": "path", + "url": "../" + } + ], + "require": { + "opensearch-project/opensearch-php": "dev-main" + } + } \ No newline at end of file diff --git a/samples/index.php b/samples/index.php new file mode 100644 index 000000000..570acaeb3 --- /dev/null +++ b/samples/index.php @@ -0,0 +1,23 @@ + [ + 'https://localhost:9200' + ], + 'BasicAuthentication' => ['admin', getenv('OPENSEARCH_PASSWORD')], + 'Retries' => 2, + 'SSLVerification' => false +]); + +$info = $client->info(); + +echo "{$info['version']['distribution']}: {$info['version']['number']}\n"; + +?> diff --git a/tests/Endpoints/MlNamespaceIntegrationTest.php b/tests/Endpoints/MlNamespaceIntegrationTest.php index 61962d88c..6c10335aa 100644 --- a/tests/Endpoints/MlNamespaceIntegrationTest.php +++ b/tests/Endpoints/MlNamespaceIntegrationTest.php @@ -30,7 +30,7 @@ public function testRegisterModelGroup() { $client = Utility::getClient(); - if (!Utility::isOpenSearchVersionAtLeast($client, '2.8.0') || !Utility::isOpenSearchVersionAtmost($client, '2.x')) { + if (!Utility::isOpenSearchVersionAtLeast($client, '2.8.0')) { $this->markTestSkipped('Ml plugin tests require OpenSearch >= 2.8.0'); } @@ -47,7 +47,7 @@ public function testgetModels() { $client = Utility::getClient(); - if (!Utility::isOpenSearchVersionAtLeast($client, '2.12.0') || !Utility::isOpenSearchVersionAtmost($client, '2.x')) { + if (!Utility::isOpenSearchVersionAtLeast($client, '2.12.0')) { $this->markTestSkipped('Ml plugin tests require OpenSearch >= 2.12.0'); } @@ -59,7 +59,7 @@ public function testsearchModels() { $client = Utility::getClient(); - if (!Utility::isOpenSearchVersionAtLeast($client, '2.12.0') || !Utility::isOpenSearchVersionAtmost($client, '2.x')) { + if (!Utility::isOpenSearchVersionAtLeast($client, '2.12.0')) { $this->markTestSkipped('Ml plugin tests require OpenSearch >= 2.12.0'); } diff --git a/tests/Utility.php b/tests/Utility.php index 8775829a8..7ca46e2dd 100644 --- a/tests/Utility.php +++ b/tests/Utility.php @@ -79,35 +79,7 @@ public static function isOpenSearchVersionAtLeast(Client $client, string $versio return false; } $versionNumber = $versionInfo['number']; - return version_compare($versionNumber, $version) >= 0; - } - - /** - * Check if cluster is OpenSearch and version is less than the specified version. - */ - public static function isOpenSearchVersionAtMost(Client $client, string $version): bool - { - $versionInfo = self::getVersion($client); - $distribution = $versionInfo['distribution'] ?? null; - if ($distribution !== 'opensearch') { - return false; - } - $versionNumber = $versionInfo['number']; - return version_compare($versionNumber, $version, '<'); - } - - /** - * Check if cluster is Elasticsearch and version is greater than or equal to specified version. - */ - public static function isElasticSearchVersionAtLeast(Client $client, string $version): bool - { - $versionInfo = self::getVersion($client); - $distribution = $versionInfo['distribution'] ?? null; - if ($distribution === 'opensearch') { - return false; - } - $versionNumber = $versionInfo['number']; - return version_compare($versionNumber, $version) >= 0; + return version_compare($versionNumber, $version, '>='); } private static function getVersion(Client $client): array @@ -137,10 +109,7 @@ public static function cleanUpCluster(Client $client): void */ private static function wipeCluster(Client $client): void { - if (self::isElasticSearchVersionAtLeast($client, '7.4.0')) { - self::deleteAllSLMPolicies($client); - } - + self::deleteAllSLMPolicies($client); self::wipeSnapshots($client); self::wipeDataStreams($client); self::wipeAllIndices($client); @@ -229,12 +198,10 @@ private static function deleteAllSLMPolicies(Client $client): void private static function wipeDataStreams(Client $client): void { try { - if (self::isElasticSearchVersionAtLeast($client, '7.9.0')) { - $client->indices()->deleteDataStream([ - 'name' => '*', - 'expand_wildcards' => 'all' - ]); - } + $client->indices()->deleteDataStream([ + 'name' => '*', + 'expand_wildcards' => 'all' + ]); } catch (OpenSearchException $e) { // We hit a version of ES that doesn't understand expand_wildcards, try again without it try { @@ -255,14 +222,10 @@ private static function wipeDataStreams(Client $client): void */ private static function wipeAllIndices(Client $client): void { - $expand = 'open,closed'; - if (self::isElasticSearchVersionAtLeast($client, '7.7.0')) { - $expand .= ',hidden'; - } try { $client->indices()->delete([ 'index' => '*,-.ds-ilm-history-*', - 'expand_wildcards' => $expand + 'expand_wildcards' => 'open,closed,hidden' ]); } catch (Exception $e) { if ($e->getCode() != '404') {