Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions pkg/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const NAME = "aws"

type baseProvider struct {
clientFactory ClientFactory
trimTiers int
}

type EC2Client interface {
Expand Down Expand Up @@ -71,9 +72,14 @@ func NamedLoader() (string, providers.Loader) {
}

func Loader(ctx context.Context, cfg providers.Config) (providers.Provider, *httperr.Error) {
creds, err := getCredentials(ctx, cfg.Creds)
creds, httpErr := getCredentials(ctx, cfg.Creds)
if httpErr != nil {
return nil, httpErr
}

trimTiers, err := providers.GetTrimTiers(cfg.Params)
if err != nil {
return nil, err
return nil, httperr.NewError(http.StatusBadRequest, "parameters error: "+err.Error())
}

clientFactory := func(region string, pageSize *int) (*Client, error) {
Expand All @@ -96,7 +102,7 @@ func Loader(ctx context.Context, cfg providers.Config) (providers.Provider, *htt
}, nil
}

return New(clientFactory), nil
return New(clientFactory, trimTiers), nil
}

func getCredentials(ctx context.Context, creds map[string]any) (*Credentials, *httperr.Error) {
Expand Down Expand Up @@ -167,16 +173,19 @@ func (p *baseProvider) GenerateTopologyConfig(ctx context.Context, pageSize *int

klog.Infof("Extracted topology for %d instances", topo.Len())

return topo.ToThreeTierGraph(NAME, instances, false), nil
return topo.ToThreeTierGraph(NAME, instances, p.trimTiers, false), nil
}

type Provider struct {
baseProvider
}

func New(clientFactory ClientFactory) *Provider {
func New(clientFactory ClientFactory, trimTiers int) *Provider {
return &Provider{
baseProvider: baseProvider{clientFactory: clientFactory},
baseProvider: baseProvider{
clientFactory: clientFactory,
trimTiers: trimTiers,
},
}
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/providers/aws/provider_sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,19 @@ func LoaderSim(ctx context.Context, cfg providers.Config) (providers.Provider, *
}, nil
}

return NewSim(clientFactory), nil
return NewSim(clientFactory, p.TrimTiers), nil
}

type simProvider struct {
baseProvider
}

func NewSim(clientFactory ClientFactory) *simProvider {
func NewSim(clientFactory ClientFactory, trimTiers int) *simProvider {
return &simProvider{
baseProvider: baseProvider{clientFactory: clientFactory},
baseProvider: baseProvider{
clientFactory: clientFactory,
trimTiers: trimTiers,
},
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func TestGetCredentials(t *testing.T) {
{
name: "Case 3: invalid secretAccessKey",
creds: map[string]any{"accessKeyId": "id", "secretAccessKey": false},
err: "credentials error: 'secretAccessKey' must be a string",
err: "credentials error: 'secretAccessKey' must be of type string",
},
{
name: "Case 4: invalid token",
creds: map[string]any{"accessKeyId": "id", "secretAccessKey": "secret", "token": false},
err: "credentials error: 'token' must be a string",
err: "credentials error: 'token' must be of type string",
},
{
name: "Case 5: valid provided creds",
Expand Down
16 changes: 12 additions & 4 deletions pkg/providers/gcp/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const NAME = "gcp"

type baseProvider struct {
clientFactory ClientFactory
trimTiers int
}

type ClientFactory func(pageSize *int) (Client, error)
Expand Down Expand Up @@ -83,6 +84,10 @@ func Loader(ctx context.Context, config providers.Config) (providers.Provider, *
if err != nil {
return nil, httperr.NewError(http.StatusBadRequest, err.Error())
}
trimTiers, err := providers.GetTrimTiers(config.Params)
if err != nil {
return nil, httperr.NewError(http.StatusBadRequest, "parameters error: "+err.Error())
}
clientFactory := func(pageSize *int) (Client, error) {
instanceClient, err := compute.NewInstancesRESTClient(ctx)
if err != nil {
Expand All @@ -96,7 +101,7 @@ func Loader(ctx context.Context, config providers.Config) (providers.Provider, *
}, nil
}

return New(clientFactory), nil
return New(clientFactory, trimTiers), nil
}

func getProjectID(ctx context.Context, params map[string]any) (string, error) {
Expand Down Expand Up @@ -142,16 +147,19 @@ func (p *baseProvider) GenerateTopologyConfig(ctx context.Context, pageSize *int
return nil, err
}

return topo.ToThreeTierGraph(NAME, instances, false), nil
return topo.ToThreeTierGraph(NAME, instances, p.trimTiers, false), nil
}

type Provider struct {
baseProvider
}

func New(clientFactory ClientFactory) *Provider {
func New(clientFactory ClientFactory, trimTiers int) *Provider {
return &Provider{
baseProvider: baseProvider{clientFactory: clientFactory},
baseProvider: baseProvider{
clientFactory: clientFactory,
trimTiers: trimTiers,
},
}
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/providers/gcp/provider_sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,19 @@ func LoaderSim(_ context.Context, cfg providers.Config) (providers.Provider, *ht
}, nil
}

return NewSim(clientFactory), nil
return NewSim(clientFactory, p.TrimTiers), nil
}

type simProvider struct {
baseProvider
}

func NewSim(clientFactory ClientFactory) *simProvider {
func NewSim(clientFactory ClientFactory, trimTiers int) *simProvider {
return &simProvider{
baseProvider: baseProvider{clientFactory: clientFactory},
baseProvider: baseProvider{
clientFactory: clientFactory,
trimTiers: trimTiers,
},
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/gcp/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func TestGetProjectID(t *testing.T) {
name: "Case 3: invalid project_id in params",
params: map[string]any{"project_id": false},
content: []byte(`{"project_id": "test-project"}`),
err: "error in topology request parameters: 'project_id' must be a string",
err: "error in topology request parameters: 'project_id' must be of type string",
},
{
name: "Case 4: invalid project_id in cert keys",
content: []byte(`{"project_id": false}`),
err: "error in GOOGLE_APPLICATION_CREDENTIALS: 'project_id' must be a string",
err: "error in GOOGLE_APPLICATION_CREDENTIALS: 'project_id' must be of type string",
},
{
name: "Case 5: invalid credentials file path",
Expand Down
16 changes: 12 additions & 4 deletions pkg/providers/lambdai/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ClientFactory func(pageSize *int) (Client, error)

type baseProvider struct {
clientFactory ClientFactory
trimTiers int
}

// lambdaiClient is a Topology API client.
Expand Down Expand Up @@ -123,6 +124,10 @@ func Loader(ctx context.Context, config providers.Config) (providers.Provider, *
if err != nil {
return nil, httperr.NewError(http.StatusBadRequest, "parameters error: "+err.Error())
}
trimTiers, err := providers.GetTrimTiers(config.Params)
if err != nil {
return nil, httperr.NewError(http.StatusBadRequest, "parameters error: "+err.Error())
}

clientFactory := func(pageSize *int) (Client, error) {
return &lambdaiClient{
Expand All @@ -133,7 +138,7 @@ func Loader(ctx context.Context, config providers.Config) (providers.Provider, *
}, nil
}

return New(clientFactory), nil
return New(clientFactory, trimTiers), nil
}

func getPageSize(sz *int) int {
Expand All @@ -149,15 +154,18 @@ func (p *baseProvider) GenerateTopologyConfig(ctx context.Context, pageSize *int
return nil, err
}

return topo.ToThreeTierGraph(NAME, instances, false), nil
return topo.ToThreeTierGraph(NAME, instances, p.trimTiers, false), nil
}

type Provider struct {
baseProvider
}

func New(clientFactory ClientFactory) *Provider {
func New(clientFactory ClientFactory, trimTiers int) *Provider {
return &Provider{
baseProvider: baseProvider{clientFactory: clientFactory},
baseProvider: baseProvider{
clientFactory: clientFactory,
trimTiers: trimTiers,
},
}
}
9 changes: 6 additions & 3 deletions pkg/providers/lambdai/provider_sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,19 @@ func LoaderSim(_ context.Context, cfg providers.Config) (providers.Provider, *ht
}, nil
}

return NewSim(clientFactory), nil
return NewSim(clientFactory, p.TrimTiers), nil
}

type simProvider struct {
baseProvider
}

func NewSim(clientFactory ClientFactory) *simProvider {
func NewSim(clientFactory ClientFactory, trimTiers int) *simProvider {
return &simProvider{
baseProvider: baseProvider{clientFactory: clientFactory},
baseProvider: baseProvider{
clientFactory: clientFactory,
trimTiers: trimTiers,
},
}
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/providers/lambdai/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/NVIDIA/topograph/pkg/providers"
"github.com/NVIDIA/topograph/pkg/topology"
)

func TestLoader(t *testing.T) {
Expand Down Expand Up @@ -67,6 +68,20 @@ func TestLoader(t *testing.T) {
},
err: "parameters error: missing 'url'",
},
{
name: "Case 5: invalid trimTiers",
config: providers.Config{
Creds: map[string]any{
authWorkspaceID: "workspace-123",
authToken: "token-abc",
},
Params: map[string]any{
apiBaseURL: "https://api.example.com",
topology.KeyTrimTiers: false,
},
},
err: "parameters error: invalid 'trimTiers' value 'false': unsupported type bool",
},
}

for _, tt := range tests {
Expand Down
17 changes: 13 additions & 4 deletions pkg/providers/nebius/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ClientFactory func(pageSize *int) (Client, error)

type baseProvider struct {
clientFactory ClientFactory
trimTiers int
}

type nebiusClient struct {
Expand Down Expand Up @@ -78,6 +79,11 @@ func Loader(ctx context.Context, config providers.Config) (providers.Provider, *
return nil, httpErr
}

trimTiers, err := providers.GetTrimTiers(config.Params)
if err != nil {
return nil, httperr.NewError(http.StatusBadRequest, "parameters error: "+err.Error())
}

// if project ID is not passed in credentials, get it from file
projectID, err := providers.StringFromMap(authProjectID, config.Creds, false)
if err != nil {
Expand All @@ -101,7 +107,7 @@ func Loader(ctx context.Context, config providers.Config) (providers.Provider, *
}, nil
}

return New(clientFactory), nil
return New(clientFactory, trimTiers), nil
}

func getAuthOption(creds map[string]any) (gosdk.Option, *httperr.Error) {
Expand Down Expand Up @@ -177,16 +183,19 @@ func (p *baseProvider) GenerateTopologyConfig(ctx context.Context, pageSize *int
return nil, err
}

return topo.ToThreeTierGraph(NAME, instances, false), nil
return topo.ToThreeTierGraph(NAME, instances, p.trimTiers, false), nil
}

type Provider struct {
baseProvider
}

func New(clientFactory ClientFactory) *Provider {
func New(clientFactory ClientFactory, trimTiers int) *Provider {
return &Provider{
baseProvider: baseProvider{clientFactory: clientFactory},
baseProvider: baseProvider{
clientFactory: clientFactory,
trimTiers: trimTiers,
},
}
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/providers/nebius/provider_sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,19 @@ func LoaderSim(ctx context.Context, cfg providers.Config) (providers.Provider, *
}, nil
}

return NewSim(clientFactory), nil
return NewSim(clientFactory, p.TrimTiers), nil
}

type simProvider struct {
baseProvider
}

func NewSim(factory ClientFactory) *simProvider {
func NewSim(factory ClientFactory, trimTiers int) *simProvider {
return &simProvider{
baseProvider: baseProvider{clientFactory: factory},
baseProvider: baseProvider{
clientFactory: factory,
trimTiers: trimTiers,
},
}
}

Expand Down
Loading
Loading