Skip to content

Commit

Permalink
chore: Fix linter findings for revive:exported in plugins/inputs/o*
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel committed Nov 23, 2024
1 parent eb2afad commit 5000d89
Show file tree
Hide file tree
Showing 23 changed files with 407 additions and 424 deletions.
12 changes: 5 additions & 7 deletions plugins/inputs/opcua/opcua.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@ import (
var sampleConfig string

type OpcUA struct {
ReadClientConfig
readClientConfig
Log telegraf.Logger `toml:"-"`

client *ReadClient
client *readClient
}

func (*OpcUA) SampleConfig() string {
return sampleConfig
}

// Init Initialise all required objects
func (o *OpcUA) Init() (err error) {
o.client, err = o.ReadClientConfig.CreateReadClient(o.Log)
o.client, err = o.readClientConfig.createReadClient(o.Log)
return err
}

// Gather defines what data the plugin will gather.
func (o *OpcUA) Gather(acc telegraf.Accumulator) error {
// Will (re)connect if the client is disconnected
metrics, err := o.client.CurrentValues()
metrics, err := o.client.currentValues()
if err != nil {
return err
}
Expand All @@ -51,7 +49,7 @@ func (o *OpcUA) Gather(acc telegraf.Accumulator) error {
func init() {
inputs.Add("opcua", func() telegraf.Input {
return &OpcUA{
ReadClientConfig: ReadClientConfig{
readClientConfig: readClientConfig{
InputClientConfig: input.InputClientConfig{
OpcUAClientConfig: opcua.OpcUAClientConfig{
Endpoint: "opc.tcp://localhost:4840",
Expand Down
106 changes: 53 additions & 53 deletions plugins/inputs/opcua/opcua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ import (

const servicePort = "4840"

type OPCTags struct {
Name string
Namespace string
IdentifierType string
Identifier string
Want interface{}
type opcTags struct {
name string
namespace string
identifierType string
identifier string
want interface{}
}

func MapOPCTag(tags OPCTags) (out input.NodeSettings) {
out.FieldName = tags.Name
out.Namespace = tags.Namespace
out.IdentifierType = tags.IdentifierType
out.Identifier = tags.Identifier
func mapOPCTag(tags opcTags) (out input.NodeSettings) {
out.FieldName = tags.name
out.Namespace = tags.namespace
out.IdentifierType = tags.identifierType
out.Identifier = tags.identifier
return out
}

Expand All @@ -52,13 +52,13 @@ func TestGetDataBadNodeContainerIntegration(t *testing.T) {
require.NoError(t, err, "failed to start container")
defer container.Terminate()

testopctags := []OPCTags{
testopctags := []opcTags{
{"ProductName", "1", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
{"ManufacturerName", "0", "i", "2263", "open62541"},
}

readConfig := ReadClientConfig{
readConfig := readClientConfig{
InputClientConfig: input.InputClientConfig{
OpcUAClientConfig: opcua.OpcUAClientConfig{
Endpoint: fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Ports[servicePort]),
Expand All @@ -83,14 +83,14 @@ func TestGetDataBadNodeContainerIntegration(t *testing.T) {
}

for _, tags := range testopctags {
g.Nodes = append(g.Nodes, MapOPCTag(tags))
g.Nodes = append(g.Nodes, mapOPCTag(tags))
}
readConfig.Groups = append(readConfig.Groups, g)

logger := &testutil.CaptureLogger{}
readClient, err := readConfig.CreateReadClient(logger)
readClient, err := readConfig.createReadClient(logger)
require.NoError(t, err)
err = readClient.Connect()
err = readClient.connect()
require.NoError(t, err)
}

Expand All @@ -111,7 +111,7 @@ func TestReadClientIntegration(t *testing.T) {
require.NoError(t, err, "failed to start container")
defer container.Terminate()

testopctags := []OPCTags{
testopctags := []opcTags{
{"ProductName", "0", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
{"ManufacturerName", "0", "i", "2263", "open62541"},
Expand All @@ -120,7 +120,7 @@ func TestReadClientIntegration(t *testing.T) {
{"DateTime", "1", "i", "51037", "0001-01-01T00:00:00Z"},
}

readConfig := ReadClientConfig{
readConfig := readClientConfig{
InputClientConfig: input.InputClientConfig{
OpcUAClientConfig: opcua.OpcUAClientConfig{
Endpoint: fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Ports[servicePort]),
Expand All @@ -138,17 +138,17 @@ func TestReadClientIntegration(t *testing.T) {
}

for _, tags := range testopctags {
readConfig.RootNodes = append(readConfig.RootNodes, MapOPCTag(tags))
readConfig.RootNodes = append(readConfig.RootNodes, mapOPCTag(tags))
}

client, err := readConfig.CreateReadClient(testutil.Logger{})
client, err := readConfig.createReadClient(testutil.Logger{})
require.NoError(t, err)

err = client.Connect()
require.NoError(t, err, "Connect")
err = client.connect()
require.NoError(t, err)

for i, v := range client.LastReceivedData {
require.Equal(t, testopctags[i].Want, v.Value)
require.Equal(t, testopctags[i].want, v.Value)
}
}

Expand All @@ -168,7 +168,7 @@ func TestReadClientIntegrationAdditionalFields(t *testing.T) {
require.NoError(t, container.Start(), "failed to start container")
defer container.Terminate()

testopctags := []OPCTags{
testopctags := []opcTags{
{"ProductName", "0", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
{"ManufacturerName", "0", "i", "2263", "open62541"},
Expand Down Expand Up @@ -196,17 +196,17 @@ func TestReadClientIntegrationAdditionalFields(t *testing.T) {
for i, x := range testopctags {
now := time.Now()
tags := map[string]string{
"id": fmt.Sprintf("ns=%s;%s=%s", x.Namespace, x.IdentifierType, x.Identifier),
"id": fmt.Sprintf("ns=%s;%s=%s", x.namespace, x.identifierType, x.identifier),
}
fields := map[string]interface{}{
x.Name: x.Want,
x.name: x.want,
"Quality": testopcquality[i],
"DataType": testopctypes[i],
}
expectedopcmetrics = append(expectedopcmetrics, metric.New("testing", tags, fields, now))
}

readConfig := ReadClientConfig{
readConfig := readClientConfig{
InputClientConfig: input.InputClientConfig{
OpcUAClientConfig: opcua.OpcUAClientConfig{
Endpoint: fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Ports[servicePort]),
Expand All @@ -225,13 +225,13 @@ func TestReadClientIntegrationAdditionalFields(t *testing.T) {
}

for _, tags := range testopctags {
readConfig.RootNodes = append(readConfig.RootNodes, MapOPCTag(tags))
readConfig.RootNodes = append(readConfig.RootNodes, mapOPCTag(tags))
}

client, err := readConfig.CreateReadClient(testutil.Logger{})
client, err := readConfig.createReadClient(testutil.Logger{})
require.NoError(t, err)

require.NoError(t, client.Connect())
require.NoError(t, client.connect())

actualopcmetrics := make([]telegraf.Metric, 0, len(client.LastReceivedData))
for i := range client.LastReceivedData {
Expand All @@ -258,13 +258,13 @@ func TestReadClientIntegrationWithPasswordAuth(t *testing.T) {
require.NoError(t, err, "failed to start container")
defer container.Terminate()

testopctags := []OPCTags{
testopctags := []opcTags{
{"ProductName", "0", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
{"ManufacturerName", "0", "i", "2263", "open62541"},
}

readConfig := ReadClientConfig{
readConfig := readClientConfig{
InputClientConfig: input.InputClientConfig{
OpcUAClientConfig: opcua.OpcUAClientConfig{
Endpoint: fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Ports[servicePort]),
Expand All @@ -284,17 +284,17 @@ func TestReadClientIntegrationWithPasswordAuth(t *testing.T) {
}

for _, tags := range testopctags {
readConfig.RootNodes = append(readConfig.RootNodes, MapOPCTag(tags))
readConfig.RootNodes = append(readConfig.RootNodes, mapOPCTag(tags))
}

client, err := readConfig.CreateReadClient(testutil.Logger{})
client, err := readConfig.createReadClient(testutil.Logger{})
require.NoError(t, err)

err = client.Connect()
require.NoError(t, err, "Connect")
err = client.connect()
require.NoError(t, err)

for i, v := range client.LastReceivedData {
require.Equal(t, testopctags[i].Want, v.Value)
require.Equal(t, testopctags[i].want, v.Value)
}
}

Expand Down Expand Up @@ -369,17 +369,17 @@ use_unregistered_reads = true
o, ok := c.Inputs[0].Input.(*OpcUA)
require.True(t, ok)

require.Equal(t, "localhost", o.ReadClientConfig.MetricName)
require.Equal(t, "opc.tcp://localhost:4840", o.ReadClientConfig.Endpoint)
require.Equal(t, config.Duration(10*time.Second), o.ReadClientConfig.ConnectTimeout)
require.Equal(t, config.Duration(5*time.Second), o.ReadClientConfig.RequestTimeout)
require.Equal(t, "auto", o.ReadClientConfig.SecurityPolicy)
require.Equal(t, "auto", o.ReadClientConfig.SecurityMode)
require.Equal(t, "/etc/telegraf/cert.pem", o.ReadClientConfig.Certificate)
require.Equal(t, "/etc/telegraf/key.pem", o.ReadClientConfig.PrivateKey)
require.Equal(t, "Anonymous", o.ReadClientConfig.AuthMethod)
require.True(t, o.ReadClientConfig.Username.Empty())
require.True(t, o.ReadClientConfig.Password.Empty())
require.Equal(t, "localhost", o.readClientConfig.MetricName)
require.Equal(t, "opc.tcp://localhost:4840", o.readClientConfig.Endpoint)
require.Equal(t, config.Duration(10*time.Second), o.readClientConfig.ConnectTimeout)
require.Equal(t, config.Duration(5*time.Second), o.readClientConfig.RequestTimeout)
require.Equal(t, "auto", o.readClientConfig.SecurityPolicy)
require.Equal(t, "auto", o.readClientConfig.SecurityMode)
require.Equal(t, "/etc/telegraf/cert.pem", o.readClientConfig.Certificate)
require.Equal(t, "/etc/telegraf/key.pem", o.readClientConfig.PrivateKey)
require.Equal(t, "Anonymous", o.readClientConfig.AuthMethod)
require.True(t, o.readClientConfig.Username.Empty())
require.True(t, o.readClientConfig.Password.Empty())
require.Equal(t, []input.NodeSettings{
{
FieldName: "name",
Expand All @@ -396,7 +396,7 @@ use_unregistered_reads = true
TagsSlice: [][]string{{"tag0", "val0"}, {"tag00", "val00"}},
DefaultTags: map[string]string{"tag6": "val6"},
},
}, o.ReadClientConfig.RootNodes)
}, o.readClientConfig.RootNodes)
require.Equal(t, []input.NodeGroupSettings{
{
MetricName: "foo",
Expand Down Expand Up @@ -424,10 +424,10 @@ use_unregistered_reads = true
Identifier: "4001",
}},
},
}, o.ReadClientConfig.Groups)
require.Equal(t, opcua.OpcUAWorkarounds{AdditionalValidStatusCodes: []string{"0xC0"}}, o.ReadClientConfig.Workarounds)
require.Equal(t, ReadClientWorkarounds{UseUnregisteredReads: true}, o.ReadClientConfig.ReadClientWorkarounds)
require.Equal(t, []string{"DataType"}, o.ReadClientConfig.OptionalFields)
}, o.readClientConfig.Groups)
require.Equal(t, opcua.OpcUAWorkarounds{AdditionalValidStatusCodes: []string{"0xC0"}}, o.readClientConfig.Workarounds)
require.Equal(t, readClientWorkarounds{UseUnregisteredReads: true}, o.readClientConfig.ReadClientWorkarounds)
require.Equal(t, []string{"DataType"}, o.readClientConfig.OptionalFields)
err = o.Init()
require.NoError(t, err)
require.Len(t, o.client.NodeMetricMapping, 5, "incorrect number of nodes")
Expand Down
26 changes: 13 additions & 13 deletions plugins/inputs/opcua/read_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ import (
"github.com/influxdata/telegraf/selfstat"
)

type ReadClientWorkarounds struct {
type readClientWorkarounds struct {
UseUnregisteredReads bool `toml:"use_unregistered_reads"`
}

type ReadClientConfig struct {
type readClientConfig struct {
ReadRetryTimeout config.Duration `toml:"read_retry_timeout"`
ReadRetries uint64 `toml:"read_retry_count"`
ReadClientWorkarounds ReadClientWorkarounds `toml:"request_workarounds"`
ReadClientWorkarounds readClientWorkarounds `toml:"request_workarounds"`
input.InputClientConfig
}

// ReadClient Requests the current values from the required nodes when gather is called.
type ReadClient struct {
// readClient Requests the current values from the required nodes when gather is called.
type readClient struct {
*input.OpcUAInputClient

ReadRetryTimeout time.Duration
ReadRetries uint64
ReadSuccess selfstat.Stat
ReadError selfstat.Stat
Workarounds ReadClientWorkarounds
Workarounds readClientWorkarounds

// internal values
reqIDs []*ua.ReadValueID
ctx context.Context
}

func (rc *ReadClientConfig) CreateReadClient(log telegraf.Logger) (*ReadClient, error) {
func (rc *readClientConfig) createReadClient(log telegraf.Logger) (*readClient, error) {
inputClient, err := rc.InputClientConfig.CreateInputClient(log)
if err != nil {
return nil, err
Expand All @@ -55,7 +55,7 @@ func (rc *ReadClientConfig) CreateReadClient(log telegraf.Logger) (*ReadClient,
rc.ReadRetryTimeout = config.Duration(100 * time.Millisecond)
}

return &ReadClient{
return &readClient{
OpcUAInputClient: inputClient,
ReadRetryTimeout: time.Duration(rc.ReadRetryTimeout),
ReadRetries: rc.ReadRetries,
Expand All @@ -65,7 +65,7 @@ func (rc *ReadClientConfig) CreateReadClient(log telegraf.Logger) (*ReadClient,
}, nil
}

func (o *ReadClient) Connect() error {
func (o *readClient) connect() error {
o.ctx = context.Background()

if err := o.OpcUAClient.Connect(o.ctx); err != nil {
Expand Down Expand Up @@ -103,14 +103,14 @@ func (o *ReadClient) Connect() error {
return nil
}

func (o *ReadClient) ensureConnected() error {
func (o *readClient) ensureConnected() error {
if o.State() == opcua.Disconnected || o.State() == opcua.Closed {
return o.Connect()
return o.connect()
}
return nil
}

func (o *ReadClient) CurrentValues() ([]telegraf.Metric, error) {
func (o *readClient) currentValues() ([]telegraf.Metric, error) {
if err := o.ensureConnected(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (o *ReadClient) CurrentValues() ([]telegraf.Metric, error) {
return metrics, nil
}

func (o *ReadClient) read() error {
func (o *readClient) read() error {
req := &ua.ReadRequest{
MaxAge: 2000,
TimestampsToReturn: ua.TimestampsToReturnBoth,
Expand Down
Loading

0 comments on commit 5000d89

Please sign in to comment.