added better id handling

This commit is contained in:
ston1th 2021-12-10 20:33:20 +01:00
commit 78b40626e6
4 changed files with 77 additions and 33 deletions

27
pool.go
View file

@ -132,7 +132,8 @@ func (c *PoolClient) ListMembers(ctx context.Context, name string) (sl ServerRef
return
}
func (c *PoolClient) FindServer(ctx context.Context, poolname, servername string, id int) (s *ServerRef, err error) {
func (c *PoolClient) FindServer(ctx context.Context, poolname, servername string, id int) (ref *ServerRef, err error) {
valid := ValidateID(id) == nil
var pools []string
if poolname != "" {
pools = append(pools, poolname)
@ -147,22 +148,28 @@ func (c *PoolClient) FindServer(ctx context.Context, poolname, servername string
if err != nil {
return nil, err
}
for _, srv := range sl {
if id != -1 && srv.ID == id {
srv.Pool = pool
return srv, nil
for _, s := range sl {
if valid && s.ID == id {
s.Pool = pool
return s, nil
}
if servername != "" && srv.Name == servername {
srv.Pool = pool
return srv, nil
if servername != "" && s.Name == servername {
if ref != nil {
return nil, ErrTooManyServersFound
}
s.Pool = pool
ref = s
}
}
}
return nil, ErrServerNotFound
if ref == nil {
err = ErrServerNotFound
}
return
}
func (c *PoolClient) FindServerByName(ctx context.Context, poolname, servername string) (s *ServerRef, err error) {
return c.FindServer(ctx, poolname, servername, -1)
return c.FindServer(ctx, poolname, servername, InvalidID)
}
func (c *PoolClient) FindServerByID(ctx context.Context, poolname string, id int) (s *ServerRef, err error) {