mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Use interfaces where possible (#26392)
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/client"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
|
||||
@@ -99,7 +100,7 @@ func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string,
|
||||
return result.Location, nil
|
||||
}
|
||||
|
||||
func webIdentityProvider(sess *session.Session) credentials.Provider {
|
||||
func webIdentityProvider(sess client.ConfigProvider) credentials.Provider {
|
||||
svc := sts.New(sess)
|
||||
|
||||
roleARN := os.Getenv("AWS_ROLE_ARN")
|
||||
@@ -128,6 +129,6 @@ func ecsCredProvider(sess *session.Session, uri string) credentials.Provider {
|
||||
func(p *endpointcreds.Provider) { p.ExpiryWindow = 5 * time.Minute })
|
||||
}
|
||||
|
||||
func ec2RoleProvider(sess *session.Session) credentials.Provider {
|
||||
func ec2RoleProvider(sess client.ConfigProvider) credentials.Provider {
|
||||
return &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess), ExpiryWindow: 5 * time.Minute}
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ func writeMetric(buf *bufio.Writer, m model.Metric, mf *dto.MetricFamily) error
|
||||
return addExtensionConventionForRollups(buf, mf, m)
|
||||
}
|
||||
|
||||
func addExtensionConventionForRollups(buf *bufio.Writer, mf *dto.MetricFamily, m model.Metric) error {
|
||||
func addExtensionConventionForRollups(buf io.Writer, mf *dto.MetricFamily, m model.Metric) error {
|
||||
// Adding `.count` `.sum` suffix makes it possible to configure
|
||||
// different rollup strategies based on metric type
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package alerting
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
@@ -75,7 +74,7 @@ func (ae *AlertEngine) mapRulesToUsageStats(rules []*models.Alert) (DatasourceAl
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (ae *AlertEngine) parseAlertRuleModel(settings *simplejson.Json) ([]int64, error) {
|
||||
func (ae *AlertEngine) parseAlertRuleModel(settings json.Marshaler) ([]int64, error) {
|
||||
datasourceIDs := []int64{}
|
||||
model := alertJSONModel{}
|
||||
|
||||
@@ -85,7 +84,7 @@ func (ae *AlertEngine) parseAlertRuleModel(settings *simplejson.Json) ([]int64,
|
||||
|
||||
bytes, err := settings.MarshalJSON()
|
||||
if err != nil {
|
||||
return datasourceIDs, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bytes, &model)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
@@ -95,8 +96,7 @@ func TestParsingAlertRuleSettings(t *testing.T) {
|
||||
shouldErr: require.NoError,
|
||||
},
|
||||
{
|
||||
name: "can parse blank content",
|
||||
file: "testdata/settings/invalid_format.json",
|
||||
name: "can handle nil content",
|
||||
expected: []int64{},
|
||||
shouldErr: require.NoError,
|
||||
},
|
||||
@@ -108,12 +108,16 @@ func TestParsingAlertRuleSettings(t *testing.T) {
|
||||
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
content, err := ioutil.ReadFile(tc.file)
|
||||
require.NoError(t, err, "expected to be able to read file")
|
||||
var settings json.Marshaler
|
||||
if tc.file != "" {
|
||||
content, err := ioutil.ReadFile(tc.file)
|
||||
require.NoError(t, err, "expected to be able to read file")
|
||||
|
||||
j, _ := simplejson.NewJson(content)
|
||||
settings, err = simplejson.NewJson(content)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
result, err := ae.parseAlertRuleModel(j)
|
||||
result, err := ae.parseAlertRuleModel(settings)
|
||||
|
||||
tc.shouldErr(t, err)
|
||||
diff := cmp.Diff(tc.expected, result)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -64,7 +65,7 @@ func findPanelQueryByRefID(panel *simplejson.Json, refID string) *simplejson.Jso
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyJSON(in *simplejson.Json) (*simplejson.Json, error) {
|
||||
func copyJSON(in json.Marshaler) (*simplejson.Json, error) {
|
||||
rawJSON, err := in.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/aws/aws-sdk-go/aws/request"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
|
||||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
||||
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
@@ -115,7 +116,8 @@ func (e *cloudWatchExecutor) getCWLogsClient(region string) (*cloudwatchlogs.Clo
|
||||
return newLogsClient, nil
|
||||
}
|
||||
|
||||
func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient *cloudwatchlogs.CloudWatchLogs, queryContext *tsdb.TsdbQuery) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||
func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient cloudwatchlogsiface.CloudWatchLogsAPI,
|
||||
queryContext *tsdb.TsdbQuery) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||
const maxAttempts = 8
|
||||
const pollPeriod = 1000 * time.Millisecond
|
||||
|
||||
|
||||
@@ -142,7 +142,7 @@ func getCredentials(dsInfo *datasourceInfo) (*credentials.Credentials, error) {
|
||||
return creds, nil
|
||||
}
|
||||
|
||||
func webIdentityProvider(sess *session.Session) credentials.Provider {
|
||||
func webIdentityProvider(sess client.ConfigProvider) credentials.Provider {
|
||||
svc := newSTSService(sess)
|
||||
|
||||
roleARN := os.Getenv("AWS_ROLE_ARN")
|
||||
@@ -171,7 +171,7 @@ func ecsCredProvider(sess *session.Session, uri string) credentials.Provider {
|
||||
func(p *endpointcreds.Provider) { p.ExpiryWindow = 5 * time.Minute })
|
||||
}
|
||||
|
||||
func ec2RoleProvider(sess *session.Session) credentials.Provider {
|
||||
func ec2RoleProvider(sess client.ConfigProvider) credentials.Provider {
|
||||
return &ec2rolecreds.EC2RoleProvider{Client: newEC2Metadata(sess), ExpiryWindow: 5 * time.Minute}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ enable = [
|
||||
"gosimple",
|
||||
"govet",
|
||||
"ineffassign",
|
||||
# "interfacer",
|
||||
"misspell",
|
||||
"nakedret",
|
||||
"rowserrcheck",
|
||||
|
||||
Reference in New Issue
Block a user