1
0
woodpecker-sonatype-nexus/nexus-status.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-05-28 14:41:14 +03:00
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"context"
"encoding/json"
"errors"
"github.com/rs/zerolog/log"
)
type ReadOnlyStatus struct {
Frozen bool `json:"frozen"`
SystemInitiated bool `json:"systemInitiated"`
Reason string `json:"summaryReason"`
}
func (p *Plugin) GetNexusStatus(ctx context.Context) error {
res, err := p.NexusRequest(ctx, "v1/status/writable")
if err == nil {
defer res.Body.Close()
err = GenericResponseHandler(res)
}
if err != nil {
log.Error().Msg("Nexus is not writable")
return err
}
res, err = p.NexusRequest(ctx, "v1/read-only")
if err == nil {
defer res.Body.Close()
err = GenericResponseHandler(res)
}
if err != nil {
log.Error().Msg("Nexus is unable to report it's \"read-only\" status")
return err
}
var roStatus ReadOnlyStatus
dec := json.NewDecoder(res.Body)
err = dec.Decode(&roStatus)
if err != nil {
log.Error().Msg("unable to decode information for \"read-only\" status")
return err
}
if roStatus.Frozen {
if roStatus.Reason == "" {
log.Error().Msgf("Nexus is read-only (system-initiated: %v)", roStatus.SystemInitiated)
} else {
log.Error().Msgf("Nexus is read-only (system-initiated: %v), reason: %q", roStatus.SystemInitiated, roStatus.Reason)
}
return errors.New("readonly")
}
//TODO: determine early whether supplied credentials allows one to proceed with Sonatype Nexus
return nil
}