Reorganize documentation and add samples. (#447)

Signed-off-by: dblock <[email protected]>
This commit is contained in:
Daniel (dB.) Doubrovkine
2023-07-24 10:23:51 -07:00
committed by GitHub
parent 5866880c81
commit 58217d98ff
18 changed files with 1097 additions and 691 deletions
+149
View File
@@ -0,0 +1,149 @@
- [Alerting Plugin](#alerting-plugin)
- [Creating a Monitor](#creating-a-monitor)
- [Get a Monitor](#get-a-monitor)
- [Search for a Monitor](#search-for-a-monitor)
- [Create an Email Destination](#create-an-email-destination)
- [Get Alerts](#get-alerts)
- [Acknowledge Alerts](#acknowledge-alerts)
### Alerting Plugin
You can use the [Alerting Plugin API](https://opensearch.org/docs/latest/observing-your-data/alerting/api/) to programmatically create, update, and manage monitors and alerts.
#### Creating a Monitor
Create a bucket-level monitor.
```python
query = {
"type": "monitor",
"name": "Demo bucket-level monitor",
"monitor_type": "bucket_level_monitor",
"enabled": True,
"schedule": {
"period": {
"interval": 1,
"unit": "MINUTES"
}
},
"inputs": [
{
"search": {
"indices": [
"test-index"
],
"query": {
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"order_date": {
"from": "||-1h",
"to": "",
"include_lower": True,
"include_upper": True,
"format": "epoch_millis"
}
}
}
]
}
},
"aggregations": {
"composite_agg": {
"composite": {
"sources": [
{
"user": {
"terms": {
"field": "user"
}
}
}
]
},
"aggregations": {
"avg_products_base_price": {
"avg": {
"field": "products.base_price"
}
}
}
}
}
}
}
}
],
}
response = client.plugins.alerting.create_monitor(query)
print(response)
```
#### Get a Monitor
```python
response = client.plugins.alerting.get_monitor("monitorID")
print(response)
```
#### Search for a Monitor
```python
query = {
"query": {
"match" : {
"monitor.name": "test-monitor"
}
}
}
response = client.plugins.alerting.search_monitor(query)
print(response)
```
#### Create an Email Destination
```python
query = {
"type": "email",
"name": "my-email-destination",
"email": {
"email_account_id": "YjY7mXMBx015759_IcfW",
"recipients": [
{
"type": "email_group",
"email_group_id": "YzY-mXMBx015759_dscs"
},
{
"type": "email",
"email": "[email protected]"
}
]
}
}
response = client.plugins.alerting.create_destination(query)
print(response)
```
#### Get Alerts
```python
response = client.plugins.alerting.get_alerts()
print(response)
```
#### Acknowledge Alerts
```python
query = {
"alerts": ["eQURa3gBKo1jAh6qUo49"]
}
response = client.plugins.alerting.acknowledge_alert(query)
print(response)
```
+68
View File
@@ -0,0 +1,68 @@
- [Index Management Plugin](#index-management-plugin)
- [Create a Policy](#create-a-policy)
- [Get a Policy](#get-a-policy)
- [Delete a Policy](#delete-a-policy)
### Index Management Plugin
You can use the [Index Management Plugin (ISM) API](https://opensearch.org/docs/latest/im-plugin/ism/api) to programmatically automate periodic, administrative operations on indexes by triggering them based on changes in the index age, index size, or number of documents.
#### Create a Policy
```python
policy_name = "test-policy"
policy_content = {
"policy": {
"description": "hot warm delete workflow",
"default_state": "hot",
"schema_version": 1,
"states": [
{
"name": "hot",
"actions": [{"rollover": {"min_index_age": "1d"}}],
"transitions": [{"state_name": "warm"}],
},
{
"name": "warm",
"actions": [{"replica_count": {"number_of_replicas": 5}}],
"transitions": [{"state_name": "delete", "conditions": {"min_index_age": "30d"}}],
},
{
"name": "delete",
"actions": [
{
"notification": {
"destination": {"chime": {"url": "<URL>"}},
"message_template": {"source": "The index {{ctx.index}} is being deleted"},
}
},
{"delete": {}},
],
},
],
"ism_template": {"index_patterns": ["log*"], "priority": 100},
}
}
response = client.index_managment.put_policy(policy_name, body=policy_content)
print(response)
```
#### Get a Policy
```python
policy_name = "test-policy"
response = client.index_managment.get_policy(policy_name)
print(response)
```
#### Delete a Policy
```python
policy_name = "test-policy"
response = client.index_managment.delete_policy(policy_name)
print(response)
```
+59
View File
@@ -0,0 +1,59 @@
- [Security Plugin](#security-plugin)
- [Create a Role](#create-a-role)
- [Get a Role](#get-a-role)
- [Create a User](#create-a-user)
- [Get a User](#get-a-user)
### Security Plugin
The [Security Plugin API](https://opensearch.org/docs/latest/security/access-control/api/) lets you programmatically create and manage users, roles, role mappings, action groups, and tenants.
#### Create a Role
```python
role_name = "test-role"
role_content = {
"cluster_permissions": ["cluster_monitor"],
"index_permissions": [
{
"index_patterns": ["index", "test-*"],
"allowed_actions": [
"data_access",
"indices_monitor",
],
}
],
}
response = client.security.put_role(role_name, body=role_content)
print(response)
```
#### Get a Role
```python
role_name = "test-role"
response = client.security.get_role(role_name)
print(response)
```
#### Create a User
```python
user_name = "test-user"
user_content = {"password": "test_password", "opendistro_security_roles": []}
response = client.security.put_role(user_name, body=user_content)
print(response)
```
#### Get a User
```python
user_name = "test-user"
response = client.security.get_user(user_name)
print(response)
```