feat(client): tenant management commands
This commit is contained in:
@ -15,6 +15,8 @@ type (
|
||||
type (
|
||||
AgentID = datastore.AgentID
|
||||
Agent = datastore.Agent
|
||||
TenantID = datastore.TenantID
|
||||
Tenant = datastore.Tenant
|
||||
AgentStatus = datastore.AgentStatus
|
||||
)
|
||||
|
||||
|
28
pkg/client/create_tenant.go
Normal file
28
pkg/client/create_tenant.go
Normal file
@ -0,0 +1,28 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/server/api"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *Client) CreateTenant(ctx context.Context, label string, funcs ...OptionFunc) (*Tenant, error) {
|
||||
response := withResponse[struct {
|
||||
Tenant *Tenant `json:"tenant"`
|
||||
}]()
|
||||
|
||||
payload := api.CreateTenantRequest{
|
||||
Label: label,
|
||||
}
|
||||
|
||||
if err := c.apiPost(ctx, "/api/v1/tenants", payload, &response, funcs...); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return nil, errors.WithStack(response.Error)
|
||||
}
|
||||
|
||||
return response.Data.Tenant, nil
|
||||
}
|
26
pkg/client/get_tenant.go
Normal file
26
pkg/client/get_tenant.go
Normal file
@ -0,0 +1,26 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *Client) GetTenant(ctx context.Context, tenantID TenantID, funcs ...OptionFunc) (*Tenant, error) {
|
||||
response := withResponse[struct {
|
||||
Tenant *Tenant `json:"tenant"`
|
||||
}]()
|
||||
|
||||
path := fmt.Sprintf("/api/v1/tenants/%s", tenantID)
|
||||
|
||||
if err := c.apiGet(ctx, path, &response, funcs...); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return nil, errors.WithStack(response.Error)
|
||||
}
|
||||
|
||||
return response.Data.Tenant, nil
|
||||
}
|
@ -34,7 +34,7 @@ func WithUpdateAgentsOptions(funcs ...OptionFunc) UpdateAgentOptionFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) UpdateAgent(ctx context.Context, agentID datastore.AgentID, funcs ...UpdateAgentOptionFunc) (*datastore.Agent, error) {
|
||||
func (c *Client) UpdateAgent(ctx context.Context, agentID AgentID, funcs ...UpdateAgentOptionFunc) (*datastore.Agent, error) {
|
||||
opts := &UpdateAgentOptions{}
|
||||
for _, fn := range funcs {
|
||||
fn(opts)
|
||||
|
61
pkg/client/update_tenant.go
Normal file
61
pkg/client/update_tenant.go
Normal file
@ -0,0 +1,61 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user