Skip to content

Commit

Permalink
[FRAGENT-2908] Added the hostname_transform config option (#18652)
Browse files Browse the repository at this point in the history
* adding hostname_to_upper and hostname_to_lower config options

* added a changelog

* addressing comments

* modified changelog

* improved testing coverage

* changed 'none' keyword to 'default' and assert_external_tags

* addressing comments
  • Loading branch information
rahulkaukuntla authored Sep 25, 2024
1 parent 1ce2081 commit 68805b3
Show file tree
Hide file tree
Showing 14 changed files with 1,080 additions and 672 deletions.
10 changes: 10 additions & 0 deletions vsphere/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,16 @@ files:
value:
type: boolean
example: true
- name: hostname_transform
description: |
Should be either 'upper' or 'lower' or 'default'
If 'upper', the check will convert the vSphere hostname to uppercase.
If 'lower', the check will convert the vSphere hostname to lowercase.
If 'default', the check will not transform the vSphere hostname (the default option).
Use this if you install the agent in VMs so that the hosts are not duplicated in the web application UI.
value:
type: string
example: default
- name: rest_api_options
description: |
REST API options
Expand Down
1 change: 1 addition & 0 deletions vsphere/changelog.d/18652.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the hostname_transform config option
7 changes: 7 additions & 0 deletions vsphere/datadog_checks/vsphere/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
EXCLUDE_FILTERS,
EXTRA_FILTER_PROPERTIES_FOR_VMS,
HISTORICAL,
HOSTNAME_CASE_OPTIONS,
MOR_TYPE_AS_STRING,
OBJECT_PROPERTIES_BY_RESOURCE_TYPE,
PROPERTY_METRICS_BY_RESOURCE_TYPE,
Expand Down Expand Up @@ -141,6 +142,12 @@ def __init__(self, instance, init_config, log):
)
self.include_datastore_cluster_folder_tag = instance.get("include_datastore_cluster_folder_tag", True)
self.custom_tags = instance.get('tags', [])
self.hostname_transform = instance.get('hostname_transform', 'default')
if self.hostname_transform not in HOSTNAME_CASE_OPTIONS:
raise ConfigurationError(
"Invalid value for `hostname_transform` in the configuration file: "
+ "use one of: `default`, `lower`, or `upper`"
)
self.validate_config()

def is_historical(self):
Expand Down
4 changes: 4 additions & 0 deletions vsphere/datadog_checks/vsphere/config_models/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def instance_excluded_host_tags():
return []


def instance_hostname_transform():
return 'default'


def instance_include_datastore_cluster_folder_tag():
return True

Expand Down
1 change: 1 addition & 0 deletions vsphere/datadog_checks/vsphere/config_models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class InstanceConfig(BaseModel):
event_resource_filters: Optional[tuple[str, ...]] = None
excluded_host_tags: Optional[tuple[str, ...]] = None
host: str
hostname_transform: Optional[str] = None
include_datastore_cluster_folder_tag: Optional[bool] = None
include_events: Optional[tuple[IncludeEvent, ...]] = None
max_historical_metrics: Optional[int] = None
Expand Down
2 changes: 2 additions & 0 deletions vsphere/datadog_checks/vsphere/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,5 @@
'PermissionEvent',
'ScheduledTaskEvent',
]

HOSTNAME_CASE_OPTIONS = ['default', 'lower', 'upper']
9 changes: 9 additions & 0 deletions vsphere/datadog_checks/vsphere/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,15 @@ instances:
#
# include_datastore_cluster_folder_tag: true

## @param hostname_transform - string - optional - default: default
## Should be either 'upper' or 'lower' or 'default'
## If 'upper', the check will convert the vSphere hostname to uppercase.
## If 'lower', the check will convert the vSphere hostname to lowercase.
## If 'default', the check will not transform the vSphere hostname (the default option).
## Use this if you install the agent in VMs so that the hosts are not duplicated in the web application UI.
#
# hostname_transform: default

## REST API options
## The options are used when making vSphere REST API calls to retrieve vSphere tags
## when `collect_tags` instance config is enabled.
Expand Down
4 changes: 4 additions & 0 deletions vsphere/datadog_checks/vsphere/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ def refresh_infrastructure_cache(self):
mor_payload["tags"] = tags # type: Dict[str, Any]

if hostname:
if self._config.hostname_transform == 'upper':
hostname = hostname.upper()
elif self._config.hostname_transform == 'lower':
hostname = hostname.lower()
mor_payload['hostname'] = hostname

self.infrastructure_cache.set_mor_props(mor, mor_payload)
Expand Down
6 changes: 3 additions & 3 deletions vsphere/tests/fixtures/host_tags_values.json
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
}
],
[
"VM4-6",
"vm4-6",
{
"vsphere": [
"vsphere_host:10.0.0.104",
Expand All @@ -371,7 +371,7 @@
}
],
[
"VM4-7",
"vm4-7",
{
"vsphere": [
"vsphere_host:10.0.0.104",
Expand All @@ -386,7 +386,7 @@
}
],
[
"VM4-8",
"vm4-8",
{
"vsphere": [
"vsphere_host:10.0.0.104",
Expand Down
Loading

0 comments on commit 68805b3

Please sign in to comment.