Skip to content

Commit

Permalink
Merge pull request #92 from grafana/jjo/make-provider-name-configurable
Browse files Browse the repository at this point in the history
make provider name configurable via cli flags
  • Loading branch information
jjo authored Apr 11, 2024
2 parents 7a15d5e + 69e0de4 commit b728b1a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion aws/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestDisk(t *testing.T) {
},
},
},
&Provider{},
nil,
nil,
}

Expand Down
4 changes: 3 additions & 1 deletion aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

var _ unused.Provider = &Provider{}

var ProviderName = "AWS"

// Provider implements [unused.Provider] for AWS.
type Provider struct {
client *ec2.Client
Expand All @@ -21,7 +23,7 @@ type Provider struct {
}

// Name returns AWS.
func (p *Provider) Name() string { return "AWS" }
func (p *Provider) Name() string { return ProviderName }

// Meta returns the provider metadata.
func (p *Provider) Meta() unused.Meta { return p.meta }
Expand Down
2 changes: 1 addition & 1 deletion azure/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestDisk(t *testing.T) {
TimeCreated: &date.Time{Time: createdAt},
},
},
&Provider{},
nil,
nil,
}

Expand Down
4 changes: 3 additions & 1 deletion azure/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/grafana/unused"
)

var ProviderName = "Azure"

var _ unused.Provider = &Provider{}

const ResourceGroupMetaKey = "resource-group"
Expand All @@ -20,7 +22,7 @@ type Provider struct {
}

// Name returns Azure.
func (p *Provider) Name() string { return "Azure" }
func (p *Provider) Name() string { return ProviderName }

// Meta returns the provider metadata.
func (p *Provider) Meta() unused.Meta { return p.meta }
Expand Down
11 changes: 7 additions & 4 deletions cmd/internal/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ func CreateProviders(ctx context.Context, logger *slog.Logger, gcpProjects, awsP

// ProviderFlags adds the provider configuration flags to the given
// flag set.
func ProviderFlags(fs *flag.FlagSet, gcp, aws, azure *StringSliceFlag) {
fs.Var(gcp, "gcp.project", "GCP project ID (can be specified multiple times)")
fs.Var(aws, "aws.profile", "AWS profile (can be specified multiple times)")
fs.Var(azure, "azure.sub", "Azure subscription (can be specified multiple times)")
func ProviderFlags(fs *flag.FlagSet, gcpProject, awsProfile, azureSub *StringSliceFlag) {
fs.Var(gcpProject, "gcp.project", "GCP project ID (can be specified multiple times)")
fs.Var(awsProfile, "aws.profile", "AWS profile (can be specified multiple times)")
fs.Var(azureSub, "azure.sub", "Azure subscription (can be specified multiple times)")
fs.StringVar(&gcp.ProviderName, "gcp.providername", gcp.ProviderName, `GCP provider name to use, default: "GCP" (e.g. "GKE")`)
fs.StringVar(&aws.ProviderName, "aws.providername", aws.ProviderName, `AWS provider name to use, default: "AWS" (e.g. "EKS")`)
fs.StringVar(&azure.ProviderName, "azure.providername", azure.ProviderName, `Azure provider name to use, default: "Azure" (e.g. "AKS")`)
}
27 changes: 20 additions & 7 deletions cmd/internal/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,46 @@ func TestCreateProviders(t *testing.T) {
}

func TestProviderFlags(t *testing.T) {
var gcp, aws, azure internal.StringSliceFlag
var gcpProject, awsProfile, azureSub internal.StringSliceFlag

fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.SetOutput(io.Discard)
fs.Usage = func() {}

internal.ProviderFlags(fs, &gcp, &aws, &azure)
internal.ProviderFlags(fs, &gcpProject, &awsProfile, &azureSub)

args := []string{
"-gcp.project=my-project",
"-azure.sub=my-subscription",
"-aws.profile=my-profile",
"-gcp.providername=GKE",
"-azure.providername=AKS",
"-aws.providername=EKS",
}

if err := fs.Parse(args); err != nil {
t.Fatalf("unexpected error: %v", err)
}

tests := map[*internal.StringSliceFlag]string{
&gcp: "my-project",
&aws: "my-profile",
&azure: "my-subscription",
testSlices := map[*internal.StringSliceFlag]string{
&gcpProject: "my-project",
&awsProfile: "my-profile",
&azureSub: "my-subscription",
}
testStrings := map[*string]string{
&gcp.ProviderName: "GKE",
&aws.ProviderName: "EKS",
&azure.ProviderName: "AKS",
}

for v, exp := range tests {
for v, exp := range testSlices {
if len(*v) != 1 || (*v)[0] != exp {
t.Errorf("expecting one value (%q), got %v", exp, v)
}
}
for v, exp := range testStrings {
if *v != exp {
t.Errorf("expecting %q, got %v", exp, v)
}
}
}
2 changes: 1 addition & 1 deletion gcp/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestDisk(t *testing.T) {
CreationTimestamp: createdAt.Format(time.RFC3339),
LastDetachTimestamp: detachedAt.Format(time.RFC3339),
},
&Provider{},
nil,
unused.Meta{"foo": "bar"},
}

Expand Down
4 changes: 3 additions & 1 deletion gcp/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
compute "google.golang.org/api/compute/v1"
)

var ProviderName = "GCP"

// ErrMissingProject is the error used when no project ID is provided
// when trying to create a provider.
var ErrMissingProject = errors.New("missing project id")
Expand All @@ -27,7 +29,7 @@ type Provider struct {
}

// Name returns GCP.
func (p *Provider) Name() string { return "GCP" }
func (p *Provider) Name() string { return ProviderName }

// Meta returns the provider metadata.
func (p *Provider) Meta() unused.Meta { return p.meta }
Expand Down

0 comments on commit b728b1a

Please sign in to comment.