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

Issue #59: Display the connector variables #61

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import org.sentrysoftware.maven.metricshub.connector.producer.model.common.ConnectorDefaultVariable;
import org.sentrysoftware.maven.metricshub.connector.producer.model.common.TechnologyType;

/**
Expand Down Expand Up @@ -605,4 +608,56 @@ public List<String> getTags() {
}
return Collections.emptyList();
}

/**
* Retrieves all variable names from the connector template.
* Variables are expected to be in the format: ${var::variableName}.
*
* @return a set of unique variable names found within the connector template.
*/
public Set<String> getVariablesNames() {
final String stringConnector = connector.toString();
final String variableRegex = "\\$\\{var::(.*?)\\}";
final Set<String> variables = new HashSet<>();

final Pattern pattern = Pattern.compile(variableRegex);
CherfaElyes marked this conversation as resolved.
Show resolved Hide resolved
final Matcher matcher = pattern.matcher(stringConnector);

while (matcher.find()) {
variables.add(matcher.group(1));
}
return variables;
}

/**
* Retrieves the default connector variables declared in the connector.
* These variables include their descriptions and default values.
*
* @return a map of variable names to their corresponding {@link ConnectorDefaultVariable} objects,
* each containing a description and a default value. Returns an empty map if no variables are declared.
*/
public Map<String, ConnectorDefaultVariable> getDefaultVariables() {
final JsonNode variablesNode = getConnectorSection().map(node -> node.get("variables")).orElse(null);

final Map<String, ConnectorDefaultVariable> defaultVariables = new HashMap<>();
if (nonNull(variablesNode)) {
variablesNode
.fields()
.forEachRemaining(entry -> {
final String variableName = entry.getKey();
final JsonNode variableValue = entry.getValue();

final String description = variableValue.get("description").asText();
final String defaultValue = variableValue.get("defaultValue").asText();

// Create a ConnectorDefaultVariable object and put it into the map
final ConnectorDefaultVariable connectorDefaultVariable = new ConnectorDefaultVariable(
description,
defaultValue
);
defaultVariables.put(variableName, connectorDefaultVariable);
});
}
return defaultVariables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import lombok.Builder;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.plugin.logging.Log;
import org.sentrysoftware.maven.metricshub.connector.producer.model.common.ConnectorDefaultVariable;
import org.sentrysoftware.maven.metricshub.connector.producer.model.common.OpenTelemetryHardwareType;
import org.sentrysoftware.maven.metricshub.connector.producer.model.common.OsType;
import org.sentrysoftware.maven.metricshub.connector.producer.model.common.TechnologyType;
Expand Down Expand Up @@ -187,6 +188,38 @@ public void produce(
sink.bold_();
sink.paragraph_();

// Displaying connector variables list
final Set<String> connectorVariables = connectorJsonNodeReader.getVariablesNames();
final Map<String, ConnectorDefaultVariable> connectorDefaultVariables =
connectorJsonNodeReader.getDefaultVariables();
if (!connectorVariables.isEmpty()) {
sink.paragraph();
sink.text("Variables:");
sink.list();
for (final String variable : connectorVariables) {
sink.listItem();
sink.rawText(String.format("<code>%s</code>", variable));
sink.list();
sink.listItem();
final ConnectorDefaultVariable defaultVariable = connectorDefaultVariables.getOrDefault(
variable,
new ConnectorDefaultVariable("", "No default value.")
);
sink.text("description: ");
sink.text(defaultVariable.getDescription());
sink.listItem_();

sink.listItem();
sink.text("default value: ");
sink.text(defaultVariable.getDefaultValue());
sink.listItem_();
sink.list_();
sink.listItem_();
}
sink.list_();
sink.paragraph_();
}

// Sudo Commands?
final List<String> sudoCommands = connectorJsonNodeReader.getSudoCommands();
produceSudoCommandsContent(sink, sudoCommands);
Expand All @@ -212,7 +245,7 @@ public void produce(
sink.section2_();

// MetricsHub Example
produceMetricsHubExamplesContent(sink, appliesTo, technologies, sudoCommands);
produceMetricsHubExamplesContent(sink, appliesTo, technologies, sudoCommands, connectorVariables);

// Detection criteria
sink.section2();
Expand Down Expand Up @@ -388,7 +421,8 @@ private void produceMetricsHubExamplesContent(
final Sink sink,
final List<String> osTypes,
final Set<TechnologyType> technologies,
final List<String> sudoCommands
final List<String> sudoCommands,
final Set<String> connectorVariables
) {
sink.section2();
sink.sectionTitle2();
Expand Down Expand Up @@ -482,6 +516,12 @@ private void produceMetricsHubExamplesContent(
appendYamlUsernameAndPassword(yamlBuilder);
}

// Connector variable
if (connectorVariables != null && !connectorVariables.isEmpty()) {
yamlBuilder.append(" variables:\n");
yamlBuilder.append(String.format(" %s: %s", connectorVariables.iterator().next(), "<VALUE>"));
yamlBuilder.append(" # Replace with desired value.\n");
}
// CLI
sink.section3();
sink.sectionTitle3();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.sentrysoftware.maven.metricshub.connector.producer.model.common;

import lombok.AllArgsConstructor;
CherfaElyes marked this conversation as resolved.
Show resolved Hide resolved
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/*-
* ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
* MetricsHub Connector Maven Plugin
* ჻჻჻჻჻჻
* Copyright (C) 2023 Sentry Software
* ჻჻჻჻჻჻
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
*/

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class ConnectorDefaultVariable {

/**
* The default connector variable description.
*/
private String description;
/**
* The default connector variable default value.
*/
private String defaultValue;
}
Loading