package aws
import (
"errors"
"time"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/grafana/agent/component"
"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/pkg/river/rivertypes"
promcfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
promaws "github.com/prometheus/prometheus/discovery/aws"
)
func init() {
component.Register(component.Registration{
Name: "discovery.lightsail",
Args: LightsailArguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return NewLightsail(opts, args.(LightsailArguments))
},
})
}
type LightsailArguments struct {
Endpoint string `river:"endpoint,attr,optional"`
Region string `river:"region,attr,optional"`
AccessKey string `river:"access_key,attr,optional"`
SecretKey rivertypes.Secret `river:"secret_key,attr,optional"`
Profile string `river:"profile,attr,optional"`
RoleARN string `river:"role_arn,attr,optional"`
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
Port int `river:"port,attr,optional"`
}
func (args LightsailArguments) Convert() *promaws.LightsailSDConfig {
cfg := &promaws.LightsailSDConfig{
Endpoint: args.Endpoint,
Region: args.Region,
AccessKey: args.AccessKey,
SecretKey: promcfg.Secret(args.SecretKey),
Profile: args.Profile,
RoleARN: args.RoleARN,
RefreshInterval: model.Duration(args.RefreshInterval),
Port: args.Port,
}
return cfg
}
var DefaultLightsailSDConfig = LightsailArguments{
Port: 80,
RefreshInterval: 60 * time.Second,
}
func (args *LightsailArguments) UnmarshalRiver(f func(interface{}) error) error {
*args = DefaultLightsailSDConfig
type arguments LightsailArguments
if err := f((*arguments)(args)); err != nil {
return err
}
if args.Region == "" {
sess, err := session.NewSession()
if err != nil {
return err
}
metadata := ec2metadata.New(sess)
region, err := metadata.Region()
if err != nil {
return errors.New("Lightsail SD configuration requires a region")
}
args.Region = region
}
return nil
}
func NewLightsail(opts component.Options, args LightsailArguments) (component.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
conf := args.(LightsailArguments).Convert()
return promaws.NewLightsailDiscovery(conf, opts.Logger), nil
})
}