forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scada.go
58 lines (48 loc) · 1.48 KB
/
scada.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package scada
import (
"fmt"
"net"
"github.com/hernad/consul/agent/hcp/config"
"github.com/hashicorp/go-hclog"
libscada "github.com/hashicorp/hcp-scada-provider"
"github.com/hashicorp/hcp-scada-provider/capability"
"github.com/hashicorp/hcp-sdk-go/resource"
)
// Provider is the interface used in the rest of Consul core when using SCADA, it is aliased here to the same interface
// provided by the hcp-scada-provider library. If the interfaces needs to be extended in the future it can be done so
// with minimal impact on the rest of the codebase.
//
//go:generate mockery --name Provider --with-expecter --inpackage
type Provider interface {
libscada.SCADAProvider
}
const (
scadaConsulServiceKey = "consul"
)
func New(cfg config.CloudConfig, logger hclog.Logger) (Provider, error) {
resource, err := resource.FromString(cfg.ResourceID)
if err != nil {
return nil, fmt.Errorf("failed to parse cloud resource_id: %w", err)
}
hcpConfig, err := cfg.HCPConfig()
if err != nil {
return nil, fmt.Errorf("failed to build HCPConfig: %w", err)
}
pvd, err := libscada.New(&libscada.Config{
Service: scadaConsulServiceKey,
HCPConfig: hcpConfig,
Resource: *resource.Link(),
Logger: logger,
})
if err != nil {
return nil, err
}
return pvd, nil
}
// IsCapability takes a net.Addr and returns true if it is a SCADA capability.Addr
func IsCapability(a net.Addr) bool {
_, ok := a.(*capability.Addr)
return ok
}