Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(consumption-opensearch): opensearch api as separate construct #694

Merged
merged 18 commits into from
Nov 19, 2024
Prev Previous commit
Next Next commit
minor doc fix
lmouhib committed Nov 18, 2024
commit 8edd1d6a004492784299646a011f34683fa0699e
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ class ExampleOpenSearchApiStack extends cdk.Stack {
openSearchClusterType:dsf.consumption.OpenSearchClusterType.PROVISIONED,
removalPolicy:cdk.RemovalPolicy.DESTROY
});
/// !hide
//Add another admin
osApi.addRoleMapping('AnotherAdmin', 'all_access','sometestId');

@@ -64,7 +65,7 @@ class ExampleOpenSearchApiStack extends cdk.Stack {
add2Cr.node.addDependency(indexTemplateCr);
const add3Cr = osApi.callOpenSearchApi('AddData4', 'movies-01/_doc',{"title": "The Little Mermaid", "year": 2015}, 'POST');
add3Cr.node.addDependency(indexTemplateCr);
/// !hide


}
}
Original file line number Diff line number Diff line change
@@ -14,59 +14,14 @@ The construct leverages the [CDK Provider Framework](https://docs.aws.amazon.com
<TabItem value="typescript" label="TypeScript" default>

```typescript
const domainEndpoint='search-XXXXXX.XXXXXX.es.amazonaws.com';
const apiRole = Role.fromRoleName(this, 'ApiRole', '<IAMRoleWithOpenSearchPermissions>');
const osApi = new dsf.consumption.OpenSearchApi(this, 'MyOpenSearchApi',{
iamHandlerRole:apiRole,
openSearchEndpoint:domainEndpoint,
openSearchClusterType:dsf.consumption.OpenSearchClusterType.PROVISIONED,
removalPolicy:cdk.RemovalPolicy.DESTROY
});
//Add another admin
osApi.addRoleMapping('AnotherAdmin', 'all_access','sometestId');

//create index template
const indexTemplateCr = osApi.callOpenSearchApi('CreateIndexTemplate','_index_template/movies',
{
"index_patterns": [
"movies-*"
],
"template": {
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"title": {
"type": "text"
},
"year": {
"type": "integer"
}
}
}
}
});
const metadata='{ "index" : { "_index" : "movies-02"}}';
const bulk=`${metadata}
{"title": "Barbie", "year": 2023}
${metadata}
{"title": "Openheimer", "year": 2023}`;

// bulk ingestion using POST
const bulkCr = osApi.callOpenSearchApi('AddBulk','_bulk',bulk+'\n\n','POST');
//dependency to enforce sequential API calls
bulkCr.node.addDependency(indexTemplateCr);

const add1Cr = osApi.callOpenSearchApi('AddData1', 'movies-01/_doc/1111',{"title": "Rush", "year": 2013}, 'PUT');
add1Cr.node.addDependency(indexTemplateCr);
const add2Cr = osApi.callOpenSearchApi('AddData3', 'movies-01/_doc/2222',{"title": "Toy Story", "year": 2014}, 'PUT');
add2Cr.node.addDependency(indexTemplateCr);
const add3Cr = osApi.callOpenSearchApi('AddData4', 'movies-01/_doc',{"title": "The Little Mermaid", "year": 2015}, 'POST');
add3Cr.node.addDependency(indexTemplateCr);
const domainEndpoint='search-XXXXXX.XXXXXX.es.amazonaws.com';
const apiRole = Role.fromRoleName(this, 'ApiRole', '<IAMRoleWithOpenSearchPermissions>');
const osApi = new dsf.consumption.OpenSearchApi(this, 'MyOpenSearchApi',{
iamHandlerRole:apiRole,
openSearchEndpoint:domainEndpoint,
openSearchClusterType:dsf.consumption.OpenSearchClusterType.PROVISIONED,
removalPolicy:cdk.RemovalPolicy.DESTROY
});
```

</TabItem>
@@ -81,49 +36,6 @@ os_api = dsf.consumption.OpenSearchApi(self, "MyOpenSearchApi",
open_search_cluster_type=dsf.consumption.OpenSearchClusterType.PROVISIONED,
removal_policy=cdk.RemovalPolicy.DESTROY
)
# Add another admin
os_api.add_role_mapping("AnotherAdmin", "all_access", "sometestId")

# create index template
index_template_cr = os_api.call_open_search_api("CreateIndexTemplate", "_index_template/movies", {
"index_patterns": ["movies-*"
],
"template": {
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"title": {
"type": "text"
},
"year": {
"type": "integer"
}
}
}
}
})
metadata = "{ \"index\" : { \"_index\" : \"movies-02\"}}"
bulk = f"""{metadata}
{\"title\": \"Barbie\", \"year\": 2023}
{metadata}
{\"title\": \"Openheimer\", \"year\": 2023}"""

# bulk ingestion using POST
bulk_cr = os_api.call_open_search_api("AddBulk", "_bulk", bulk + "\n\n", "POST")
# dependency to enforce sequential API calls
bulk_cr.node.add_dependency(index_template_cr)

add1_cr = os_api.call_open_search_api("AddData1", "movies-01/_doc/1111", {"title": "Rush", "year": 2013}, "PUT")
add1_cr.node.add_dependency(index_template_cr)
add2_cr = os_api.call_open_search_api("AddData3", "movies-01/_doc/2222", {"title": "Toy Story", "year": 2014}, "PUT")
add2_cr.node.add_dependency(index_template_cr)
add3_cr = os_api.call_open_search_api("AddData4", "movies-01/_doc", {"title": "The Little Mermaid", "year": 2015}, "POST")
add3_cr.node.add_dependency(index_template_cr)
```

</TabItem>