Review feedback

Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
Joe Adams 2025-07-31 21:22:43 -04:00
parent 56a3bbf5c5
commit cdfb67467f
No known key found for this signature in database
GPG key ID: B5D9F80A5CE7B14C
3 changed files with 17 additions and 37 deletions

View file

@ -27,7 +27,6 @@ import (
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
@ -125,9 +124,7 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
return err
}
if c.Region == "" {
// TODO(@sysadmind): Should we get a context from somewhere?
ctx := context.TODO()
cfg, err := awsConfig.LoadDefaultConfig(ctx)
cfg, err := awsConfig.LoadDefaultConfig(context.Background())
if err != nil {
return err
}
@ -136,17 +133,13 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// If the region is already set in the config, use it.
// This can happen if the user has set the region in the AWS config file or environment variables.
c.Region = cfg.Region
} else {
// Attempt to get the region from the instance metadata service.
metaClient := imds.NewFromConfig(cfg)
result, err := metaClient.GetRegion(ctx, &imds.GetRegionInput{})
if err != nil {
return errors.New("EC2 SD configuration requires a region")
}
c.Region = result.Region
}
}
if c.Region == "" {
return errors.New("EC2 SD configuration requires a region")
}
for _, f := range c.Filters {
if len(f.Values) == 0 {
return errors.New("EC2 SD configuration filter values cannot be empty")

View file

@ -38,10 +38,6 @@ func boolptr(b bool) *bool {
return &b
}
func int32ptr(i int32) *int32 {
return &i
}
// Struct for test data.
type ec2DataStore struct {
region string
@ -289,7 +285,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
// interface without primary IPv6, index 2
{
Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{
DeviceIndex: int32ptr(3),
DeviceIndex: aws.Int32(3),
},
Ipv6Addresses: []ec2Types.InstanceIpv6Address{
{
@ -302,7 +298,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
// interface with primary IPv6, index 1
{
Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{
DeviceIndex: int32ptr(1),
DeviceIndex: aws.Int32(1),
},
Ipv6Addresses: []ec2Types.InstanceIpv6Address{
{
@ -319,7 +315,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
// interface with primary IPv6, index 3
{
Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{
DeviceIndex: int32ptr(3),
DeviceIndex: aws.Int32(3),
},
Ipv6Addresses: []ec2Types.InstanceIpv6Address{
{
@ -332,7 +328,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
// interface without primary IPv6, index 0
{
Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{
DeviceIndex: int32ptr(0),
DeviceIndex: aws.Int32(0),
},
Ipv6Addresses: []ec2Types.InstanceIpv6Address{},
SubnetId: strptr("azid-3"),

View file

@ -27,7 +27,6 @@ import (
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/smithy-go"
@ -107,27 +106,19 @@ func (c *LightsailSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) err
return err
}
if c.Region == "" {
// TODO(@sysadmind): Should we get a context from somewhere?
ctx := context.TODO()
cfg, err := awsConfig.LoadDefaultConfig(ctx)
cfg, err := awsConfig.LoadDefaultConfig(context.Background())
if err != nil {
return err
}
if cfg.Region != "" {
// If the region is already set in the config, use it.
// This can happen if the user has set the region in the AWS config file or environment variables.
c.Region = cfg.Region
} else {
// Attempt to get the region from the instance metadata service.
metaClient := imds.NewFromConfig(cfg)
result, err := metaClient.GetRegion(ctx, &imds.GetRegionInput{})
if err != nil {
return errors.New("EC2 SD configuration requires a region")
}
c.Region = result.Region
}
// Use the region from the AWS config. It will load environment variables and shared config files.
c.Region = cfg.Region
}
if c.Region == "" {
return errors.New("lightsail SD configuration requires a region")
}
return c.HTTPClientConfig.Validate()
}