Skip to content

Commit

Permalink
fix(openstack/clients): configure OpenStack in worker.go, validate in…
Browse files Browse the repository at this point in the history
… configuration, not in worker.
  • Loading branch information
Bobi-Wan committed Feb 6, 2025
1 parent 7fcb1bd commit 2a32424
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
26 changes: 21 additions & 5 deletions cmd/inventory/utils_openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func validateOpenStackConfig(conf *config.Config) error {
if creds.User.PasswordFile == "" {
return fmt.Errorf("OpenStack: %w: %s", errNoPasswordFile, name)
}
break
case config.OpenStackAuthenticationMethodAppCredentials:
if creds.AppCredentials.AppCredentialsIDFile == "" {
return fmt.Errorf("OpenStack: %w: %s", errNoAppCredentialsIDFile, name)
Expand All @@ -101,7 +100,6 @@ func validateOpenStackConfig(conf *config.Config) error {
if creds.AppCredentials.AppCredentialsNameFile == "" {
return fmt.Errorf("OpenStack: %w: %s", errNoAppCredentialsNameFile, name)
}
break
default:
return fmt.Errorf("OpenStack: %w: %s uses %s", errUnknownAuthenticationMethod, name, creds.Authentication)
}
Expand All @@ -120,6 +118,12 @@ func configureOpenStackClients(ctx context.Context, conf *config.Config) error {
}

slog.Info("configuring OpenStack clients")

err := validateOpenStackConfig(conf)
if err != nil {
return fmt.Errorf("invalid OpenStack configuration: %w", err)
}

configFuncs := map[string]func(ctx context.Context, conf *config.Config) error{
"compute": configureOpenStackComputeClientsets,
"network": configureOpenStackNetworkClientsets,
Expand Down Expand Up @@ -204,7 +208,7 @@ func configureOpenStackComputeClientsets(ctx context.Context, conf *config.Confi
ApplicationCredentialName: appName,
}
default:
fmt.Errorf("unknown authentication method: %s", namedCreds.Authentication)
return fmt.Errorf("unknown authentication method: %s", namedCreds.Authentication)
}

providerClient, err := gophercloudconfig.NewProviderClient(ctx, authOpts)
Expand All @@ -216,6 +220,10 @@ func configureOpenStackComputeClientsets(ctx context.Context, conf *config.Confi
Region: region,
})

if err != nil {
return fmt.Errorf("unable to create client for compute service with credentials %s: %w", cred, err)
}

client := openstackclients.Client[*gophercloud.ServiceClient]{
NamedCredentials: cred,
ProjectID: projectID,
Expand Down Expand Up @@ -305,7 +313,7 @@ func configureOpenStackNetworkClientsets(ctx context.Context, conf *config.Confi
ApplicationCredentialName: appName,
}
default:
fmt.Errorf("unknown authentication method: %s", namedCreds.Authentication)
return fmt.Errorf("unknown authentication method: %s", namedCreds.Authentication)
}

providerClient, err := gophercloudconfig.NewProviderClient(ctx, authOpts)
Expand All @@ -317,6 +325,10 @@ func configureOpenStackNetworkClientsets(ctx context.Context, conf *config.Confi
Region: region,
})

if err != nil {
return fmt.Errorf("unable to create client for network service with credentials %s: %w", cred, err)
}

client := openstackclients.Client[*gophercloud.ServiceClient]{
NamedCredentials: cred,
ProjectID: projectID,
Expand Down Expand Up @@ -406,7 +418,7 @@ func configureOpenStackBlockStorageClientsets(ctx context.Context, conf *config.
ApplicationCredentialName: appName,
}
default:
fmt.Errorf("unknown authentication method: %s", namedCreds.Authentication)
return fmt.Errorf("unknown authentication method: %s", namedCreds.Authentication)
}

providerClient, err := gophercloudconfig.NewProviderClient(ctx, authOpts)
Expand All @@ -418,6 +430,10 @@ func configureOpenStackBlockStorageClientsets(ctx context.Context, conf *config.
Region: region,
})

if err != nil {
return fmt.Errorf("unable to create client for block storage service with credentials %s: %w", cred, err)
}

client := openstackclients.Client[*gophercloud.ServiceClient]{
NamedCredentials: cred,
ProjectID: projectID,
Expand Down
4 changes: 4 additions & 0 deletions cmd/inventory/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ func NewWorkerCommand() *cli.Command {
return err
}

if err := configureOpenStackClients(ctx.Context, conf); err != nil {
return err
}

// Configure logging and middlewares
slog.Info("configuring logging and middlewares")
logger, err := newLogger(os.Stdout, conf)
Expand Down

0 comments on commit 2a32424

Please sign in to comment.