emissary/pkg/client/update_tenant.go

62 lines
1.2 KiB
Go
Raw Permalink Normal View History

package client
import (
"context"
"fmt"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"github.com/pkg/errors"
)
type UpdateTenantOptions struct {
Label *string
Options []OptionFunc
}
type UpdateTenantOptionFunc func(*UpdateTenantOptions)
func WithTenantLabel(label string) UpdateTenantOptionFunc {
return func(opts *UpdateTenantOptions) {
opts.Label = &label
}
}
func WithUpdateTenantOptions(funcs ...OptionFunc) UpdateTenantOptionFunc {
return func(opts *UpdateTenantOptions) {
opts.Options = funcs
}
}
func (c *Client) UpdateTenant(ctx context.Context, tenantID TenantID, funcs ...UpdateTenantOptionFunc) (*Tenant, error) {
opts := &UpdateTenantOptions{}
for _, fn := range funcs {
fn(opts)
}
payload := map[string]any{}
if opts.Label != nil {
payload["label"] = *opts.Label
}
response := withResponse[struct {
Tenant *datastore.Tenant `json:"tenant"`
}]()
path := fmt.Sprintf("/api/v1/tenants/%s", tenantID)
if opts.Options == nil {
opts.Options = make([]OptionFunc, 0)
}
if err := c.apiPut(ctx, path, payload, &response, opts.Options...); err != nil {
return nil, errors.WithStack(err)
}
if response.Error != nil {
return nil, errors.WithStack(response.Error)
}
return response.Data.Tenant, nil
}