Skip to content

Commit 38cfd58

Browse files
feat: add support for creating metric index (#134)
**Issue number:** ### PR Type **What kind of change does this PR introduce?** * [x] Feature * [ ] Bug Fix * [ ] Refactoring (no functional or API changes) * [ ] Documentation Update * [ ] Maintenance (dependency updates, CI, etc.) ## Summary These updates make it possible to create a metric index. Test run with these changes: https://github.com/splunk/splunk-add-on-for-microsoft-cloud-services/pull/1313/checks Test using metric index `test_metrics_index_virtual_machines_basic` is passing on noah and classic: https://cd.splunkdev.com/taautomation/ta-automation-compatibility-tests/-/jobs/154571168 https://cd.splunkdev.com/taautomation/ta-automation-compatibility-tests/-/jobs/154571170 ### Changes Introduced a `datatype` parameter to index creation functions. By default, it's `event` index ### User experience Previously, only event indexes could be created. Now, users can choose to create a metric index. ## Checklist If an item doesn't apply to your changes, leave it unchecked. * [x] I have performed a self-review of this change according to the [development guidelines](https://splunk.github.io/addonfactory-ucc-test/contributing/#development-guidelines) * [ ] Tests have been added/modified to cover the changes [(testing doc)](https://splunk.github.io/addonfactory-ucc-test/contributing/#build-and-test) * [ ] Changes are documented * [x] PR title and description follows the [contributing principles](https://splunk.github.io/addonfactory-ucc-test/contributing/#pull-requests)
2 parents c3c6e2e + 35ef4b5 commit 38cfd58

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

docs/framework_deepdive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Splunk client class is supposed to implement methods providing access to Splunk
8282

8383
- *splunk_client.search(query)* - executes SPL search query at Splunk instance and returns result in SearchState class object (*splunk_add_on_ucc_modinput_test.common.splunk_instance.SearchState*)
8484

85-
- *splunk_client.create_index(name)* - creates Splunk index with a given name and returns *splunklib.Index* object.
85+
- *splunk_client.create_index(name, datatype)* - creates Splunk index with a given name and data type (event or metric). Returns *splunklib.Index* object.
8686

8787
- *splunk_client.default_index()* - returns default Splunk index name if framework configured to create one.
8888

splunk_add_on_ucc_modinput_test/common/splunk_instance.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ def _get_index(
9191
acs_server: str | None = None,
9292
splunk_token: str | None = None,
9393
) -> Index | None:
94-
if any(i.name == index_name for i in client_service.indexes):
94+
if any(
95+
i.name == index_name
96+
for i in client_service.indexes.iter(datatype="all")
97+
):
9598
return client_service.indexes[index_name]
9699
else:
97100
if (
@@ -130,13 +133,18 @@ def _validate_index_name(index_name: str) -> None:
130133

131134
@staticmethod
132135
def _victoria_create_index(
133-
index_name: str, *, acs_stack: str, acs_server: str, splunk_token: str
136+
index_name: str,
137+
*,
138+
datatype: str,
139+
acs_stack: str,
140+
acs_server: str,
141+
splunk_token: str,
134142
) -> None:
135143
Configuration._validate_index_name(index_name)
136144
url = f"{acs_server}/{acs_stack}/adminconfig/v2/indexes"
137145
data = json.dumps(
138146
{
139-
"datatype": "event",
147+
"datatype": datatype,
140148
"maxDataSizeMB": 0,
141149
"name": index_name,
142150
"searchableDays": 365,
@@ -210,12 +218,14 @@ def _victoria_create_index(
210218

211219
@staticmethod
212220
def _enterprise_create_index(
213-
index_name: str, client_service: Service
221+
index_name: str, datatype: str, client_service: Service
214222
) -> Index:
215223
idx_not_created_msg = f"Index {index_name} was not created on \
216224
instance {client_service.host}"
217225
try:
218-
new_index = client_service.indexes.create(index_name)
226+
new_index = client_service.indexes.create(
227+
index_name, datatype=datatype
228+
)
219229
except Exception as e:
220230
reason = f"{idx_not_created_msg}\nException raised:\n{e}"
221231
logger.critical(reason)
@@ -231,6 +241,7 @@ def _create_index(
231241
index_name: str,
232242
client_service: SplunkServicePool,
233243
*,
244+
datatype: str = "event",
234245
is_cloud: bool = False,
235246
acs_stack: str | None = None,
236247
acs_server: str | None = None,
@@ -249,6 +260,7 @@ def _create_index(
249260
if is_cloud:
250261
Configuration._victoria_create_index(
251262
index_name,
263+
datatype=datatype,
252264
acs_stack=acs_stack,
253265
acs_server=acs_server,
254266
splunk_token=splunk_token,
@@ -263,6 +275,7 @@ def _create_index(
263275
else:
264276
created_index = Configuration._enterprise_create_index(
265277
index_name,
278+
datatype,
266279
client_service,
267280
)
268281
logger.debug(f"Index {index_name} has just been created")

splunk_add_on_ucc_modinput_test/functional/splunk/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ def search(self, searchquery: str) -> SearchState:
149149
def _is_cloud(self) -> bool:
150150
return "splunkcloud.com" in self.config.host.lower()
151151

152-
def create_index(self, index_name: str) -> Index:
152+
def create_index(self, index_name: str, datatype: str = "event") -> Index:
153153
return self.config._create_index(
154154
index_name,
155155
self.splunk,
156+
datatype=datatype,
156157
is_cloud=self._is_cloud,
157158
acs_stack=self.config.acs_stack if self._is_cloud else None,
158159
acs_server=self.config.acs_server if self._is_cloud else None,

0 commit comments

Comments
 (0)