-
Notifications
You must be signed in to change notification settings - Fork 34
/
ontaps3service.go
128 lines (110 loc) · 3.48 KB
/
ontaps3service.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright NetApp Inc, 2023 All rights reserved
*/
package ontaps3service
import (
"github.com/netapp/harvest/v2/cmd/collectors"
"github.com/netapp/harvest/v2/cmd/poller/plugin"
"github.com/netapp/harvest/v2/cmd/tools/rest"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/tidwall/gjson"
"strings"
"time"
)
type OntapS3Service struct {
*plugin.AbstractPlugin
client *rest.Client
query string
}
func New(p *plugin.AbstractPlugin) plugin.Plugin {
return &OntapS3Service{AbstractPlugin: p}
}
func (o *OntapS3Service) Init() error {
var err error
if err := o.InitAbc(); err != nil {
return err
}
clientTimeout := o.ParentParams.GetChildContentS("client_timeout")
timeout, _ := time.ParseDuration(rest.DefaultTimeout)
duration, err := time.ParseDuration(clientTimeout)
if err == nil {
timeout = duration
} else {
o.Logger.Debug().Str("timeout", timeout.String()).Msg("Using default timeout")
}
if o.client, err = rest.New(conf.ZapiPoller(o.ParentParams), timeout, o.Auth); err != nil {
o.Logger.Error().Stack().Err(err).Msg("connecting")
return err
}
if err := o.client.Init(5); err != nil {
return err
}
o.query = "api/protocols/s3/services"
return nil
}
func (o *OntapS3Service) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.Metadata, error) {
var (
result []gjson.Result
err error
svmToURLMap map[string][]string
urlValue []string
)
// reset svmToS3serverMap map
svmToURLMap = make(map[string][]string)
data := dataMap[o.Object]
o.client.Metadata.Reset()
fields := []string{"svm.name", "name", "is_http_enabled", "is_https_enabled", "secure_port", "port"}
href := rest.NewHrefBuilder().
APIPath(o.query).
Fields(fields).
Build()
if result, err = collectors.InvokeRestCall(o.client, href, o.Logger); err != nil {
return nil, nil, err
}
// Iterate over services API response
for _, ontaps3Service := range result {
if !ontaps3Service.IsObject() {
o.Logger.Error().Str("type", ontaps3Service.Type.String()).Msg("Ontap S3 Service is not an object, skipping")
return nil, nil, errs.New(errs.ErrNoInstance, "Ontap S3 Service is not an object")
}
s3ServerName := ontaps3Service.Get("name").String()
svm := ontaps3Service.Get("svm.name").String()
isHTTPEnabled := ontaps3Service.Get("is_http_enabled").Bool()
isHTTPSEnabled := ontaps3Service.Get("is_https_enabled").Bool()
securePort := ontaps3Service.Get("secure_port").String()
port := ontaps3Service.Get("port").String()
httpsURL := ""
httpURL := ""
if isHTTPSEnabled {
httpsURL = "https://" + s3ServerName
if securePort != "443" {
httpsURL += ":" + securePort
}
httpsURL += "/"
}
if isHTTPEnabled {
httpURL = "http://" + s3ServerName
if port != "80" {
httpURL += ":" + port
}
httpURL += "/"
}
// cache url and svm detail
svmToURLMap[svm] = []string{httpsURL, httpURL}
}
for _, ontapS3 := range data.GetInstances() {
urlValue = make([]string, 0)
for _, url := range svmToURLMap[ontapS3.GetLabel("svm")] {
if url != "" {
urlValue = append(urlValue, url+ontapS3.GetLabel("bucket"))
}
}
// Update url label in ontaps3_labels, Example: http://s3server/bucket1
// If http and https both are enabled, then url label in ontaps3_labels, https://s3server/bucket1, http://s3server/bucket1
ontapS3.SetLabel("url", strings.Join(urlValue, ","))
}
return nil, o.client.Metadata, nil
}