Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHINENG-5011 playbook not getting dispatched with POST /profiles #153

Merged
merged 2 commits into from
Apr 1, 2024
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
13 changes: 11 additions & 2 deletions infrastructure/persistence/inventory/inventory_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,17 @@ func (c *InventoryClient) GetInventoryClients(ctx context.Context, page int) (In
}
defer res.Body.Close()

err = json.NewDecoder(res.Body).Decode(&results)
if err != nil {
switch {
case res.StatusCode >= 400:
responseBody, err := io.ReadAll(res.Body)
if err != nil {
log.Error().Err(err).Msg("Error reading response body")
return results, fmt.Errorf("error reading response body: %w", err)
}
return results, fmt.Errorf("error response received: %v, response body: %v", res.StatusCode, string(responseBody))
}

if err := json.NewDecoder(res.Body).Decode(&results); err != nil {
body, _ := io.ReadAll(res.Body)
log.Error().Err(err).Msgf("error decoding inventory response: %v", string(body))
}
Expand Down
25 changes: 16 additions & 9 deletions internal/dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,38 @@ import (
"config-manager/internal/db"
"context"

"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/rs/zerolog/log"
)

var profiles chan db.Profile
type ProfileWithIdentity struct {
Profile db.Profile
Identity identity.XRHID
}

var profileIdentityChan chan ProfileWithIdentity

func init() {
profiles = make(chan db.Profile)
profileIdentityChan = make(chan ProfileWithIdentity)
go func() {
for p := range profiles {
hosts, err := inventory.NewInventoryClient().GetAllInventoryClients(context.Background())
for profileIdentity := range profileIdentityChan {
ctx := context.WithValue(context.Background(), identity.Key, profileIdentity.Identity)
hosts, err := inventory.NewInventoryClient().GetAllInventoryClients(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get hosts from inventory")
continue
}

internal.ApplyProfile(context.Background(), &p, hosts, func(resp []dispatcher.RunCreated) {
log.Info().Str("profile_id", p.ID.String()).Msg("applied profile")
internal.ApplyProfile(ctx, &profileIdentity.Profile, hosts, func(resp []dispatcher.RunCreated) {
log.Info().Str("profile_id", profileIdentity.Profile.ID.String()).Msg("applied profile")
})
}
}()
}

// Dispatch queues the profile on a channel to be dispatched to connected hosts.
func Dispatch(p db.Profile) {
// Dispatch queues the profile with identity on a channel to be dispatched to connected hosts.
func Dispatch(profile db.Profile, identity identity.XRHID) {
go func() {
profiles <- p
profileIdentityChan <- ProfileWithIdentity{Profile: profile, Identity: identity}
}()
}
3 changes: 1 addition & 2 deletions internal/http/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ func createProfile(w http.ResponseWriter, r *http.Request) {
render.RenderPlain(w, r, http.StatusInternalServerError, fmt.Sprintf("cannot insert new profile: %v", err), logger)
return
}

dispatch.Dispatch(newProfile)
dispatch.Dispatch(newProfile, identity.Get(r.Context()))

render.RenderJSON(w, r, http.StatusCreated, newProfile, logger)
}
Expand Down
Loading