Simplify logic when creating a new installer; no longer requires an entire server object be passed.

This commit is contained in:
Dane Everitt
2021-08-29 14:08:01 -07:00
parent 7321c6aa45
commit 3b5e042ccc
5 changed files with 28 additions and 52 deletions

View File

@@ -5,26 +5,29 @@ import (
"emperror.dev/errors"
"github.com/asaskevich/govalidator"
"github.com/buger/jsonparser"
"github.com/pterodactyl/wings/remote"
"github.com/pterodactyl/wings/server"
)
type Installer struct {
server *server.Server
server *server.Server
StartOnCompletion bool
}
type ServerDetails struct {
UUID string `json:"uuid"`
StartOnCompletion bool `json:"start_on_completion"`
}
// New validates the received data to ensure that all the required fields
// have been passed along in the request. This should be manually run before
// calling Execute().
func New(ctx context.Context, manager *server.Manager, data []byte) (*Installer, error) {
uuid := getString(data, "uuid")
if !govalidator.IsUUIDv4(uuid) {
func New(ctx context.Context, manager *server.Manager, details ServerDetails) (*Installer, error) {
if !govalidator.IsUUIDv4(details.UUID) {
return nil, NewValidationError("uuid provided was not in a valid format")
}
c, err := manager.Client().GetServerConfiguration(ctx, uuid)
c, err := manager.Client().GetServerConfiguration(ctx, details.UUID)
if err != nil {
if !remote.IsRequestError(err) {
return nil, errors.WithStackIf(err)
@@ -38,35 +41,11 @@ func New(ctx context.Context, manager *server.Manager, data []byte) (*Installer,
if err != nil {
return nil, errors.WrapIf(err, "installer: could not init server instance")
}
return &Installer{server: s}, nil
}
// Uuid returns the UUID associated with this installer instance.
func (i *Installer) Uuid() string {
return i.server.ID()
i := Installer{server: s, StartOnCompletion: details.StartOnCompletion}
return &i, nil
}
// Server returns the server instance.
func (i *Installer) Server() *server.Server {
return i.server
}
// Returns a string value from the JSON data provided.
func getString(data []byte, key ...string) string {
value, _ := jsonparser.GetString(data, key...)
return value
}
// Returns an int value from the JSON data provided.
func getInt(data []byte, key ...string) int64 {
value, _ := jsonparser.GetInt(data, key...)
return value
}
func getBoolean(data []byte, key ...string) bool {
value, _ := jsonparser.GetBoolean(data, key...)
return value
}