Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions client/client.go

This file was deleted.

56 changes: 0 additions & 56 deletions client/cluster.go

This file was deleted.

12 changes: 0 additions & 12 deletions cluster/schema.go

This file was deleted.

36 changes: 16 additions & 20 deletions example/api/extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@ import (
"fmt"
"net/http"

"github.com/canonical/microcluster/v3/client"
extendedTypes "github.com/canonical/microcluster/v3/example/api/types"
extendedClient "github.com/canonical/microcluster/v3/example/client"
"github.com/canonical/microcluster/v3/rest"
"github.com/canonical/microcluster/v3/rest/response"
"github.com/canonical/microcluster/v3/rest/types"
"github.com/canonical/microcluster/v3/state"
"github.com/canonical/microcluster/v3/microcluster/types"
)

// This is an example extended endpoint reachable at /1.0/extended.
var extendedCmd = rest.Endpoint{
var extendedCmd = types.Endpoint{
Path: "extended",
Post: rest.EndpointAction{Handler: cmdPost, AllowUntrusted: true},
Post: types.EndpointAction{Handler: cmdPost, AllowUntrusted: true},
}

// This is the POST handler for the /1.0/extended endpoint.
// This example shows how to forward a request to other cluster members.
func cmdPost(state state.State, r *http.Request) response.Response {
func cmdPost(state types.State, r *http.Request) types.Response {
// Check the user agent header to check if we are the notifying cluster member.
if !client.IsNotification(r) {
if !types.IsNotification(r) {
// Get a collection of clients every other cluster member, with the notification user-agent set.
cluster, err := state.Cluster(true)
clients, err := state.Connect().Cluster(true)
if err != nil {
return response.SmartError(fmt.Errorf("Failed to get a client for every cluster member: %w", err))
return types.SmartError(fmt.Errorf("Failed to get a client for every cluster member: %w", err))
}

messages := make([]string, 0, len(cluster))
err = cluster.Query(r.Context(), true, func(ctx context.Context, c *client.Client) error {
addrPort, err := types.ParseAddrPort(state.Address().URL.Host)
messages := make([]string, 0, len(clients))
err = clients.Query(r.Context(), true, func(ctx context.Context, c types.Client) error {
addrPort, err := types.ParseAddrPort(state.Address().Host)
if err != nil {
return fmt.Errorf("Failed to parse addr:port of listen address %q: %w", state.Address().URL.Host, err)
return fmt.Errorf("Failed to parse addr:port of listen address %q: %w", state.Address().Host, err)
}

// Our payload in this case is defined by us as ExtendedType.
Expand All @@ -57,7 +53,7 @@ func cmdPost(state state.State, r *http.Request) response.Response {
return nil
})
if err != nil {
return response.SmartError(err)
return types.SmartError(err)
}

// Having received the result from all forwarded requests, compile them as a string and return.
Expand All @@ -66,18 +62,18 @@ func cmdPost(state state.State, r *http.Request) response.Response {
outMsg = outMsg + message + "\n"
}

return response.SyncResponse(true, outMsg)
return types.SyncResponse(true, outMsg)
}

// Decode the POST body using our defined ExtendedType.
var info extendedTypes.ExtendedType
err := json.NewDecoder(r.Body).Decode(&info)
if err != nil {
return response.SmartError(err)
return types.SmartError(err)
}

// Return some identifying information.
message := fmt.Sprintf("cluster member at address %q received message %q from cluster member at address %q", state.Address().URL.Host, info.Message, info.Sender.String())
message := fmt.Sprintf("cluster member at address %q received message %q from cluster member at address %q", state.Address().Host, info.Message, info.Sender.String())

return response.SyncResponse(true, message)
return types.SyncResponse(true, message)
}
12 changes: 6 additions & 6 deletions example/api/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
package api

import (
"github.com/canonical/microcluster/v3/example/api/types"
"github.com/canonical/microcluster/v3/rest"
apiTypes "github.com/canonical/microcluster/v3/example/api/types"
"github.com/canonical/microcluster/v3/microcluster/types"
)

// Servers represents the list of listeners that the daemon will start
// Each Server has pre-defined endpoints that will be added to the listener
// If the Server is marked as CoreAPI, its endpoints will be added to the core listener of Microcluster.
var Servers = map[string]rest.Server{
var Servers = map[string]types.Server{
"extended": {
CoreAPI: true,
ServeUnix: true,
Resources: []rest.Resources{
Resources: []types.Resources{
{
PathPrefix: types.ExtendedPathPrefix,
Endpoints: []rest.Endpoint{
PathPrefix: apiTypes.ExtendedPathPrefix,
Endpoints: []types.Endpoint{
extendedCmd,
},
},
Expand Down
2 changes: 1 addition & 1 deletion example/api/types/extended.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package types provides shared types and structs.
package types

import "github.com/canonical/microcluster/v3/rest/types"
import "github.com/canonical/microcluster/v3/microcluster/types"

// ExtendedType is an example of an API type usable by MicroCluster but defined by this example project.
type ExtendedType struct {
Expand Down
2 changes: 1 addition & 1 deletion example/api/types/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

import "github.com/canonical/microcluster/v3/rest/types"
import "github.com/canonical/microcluster/v3/microcluster/types"

const (
// ExtendedPathPrefix is the path prefix that will be used for the extended endpoints.
Expand Down
4 changes: 2 additions & 2 deletions example/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"net/url"
"time"

"github.com/canonical/microcluster/v3/client"
"github.com/canonical/microcluster/v3/example/api/types"
microTypes "github.com/canonical/microcluster/v3/microcluster/types"
)

// ExtendedPostCmd is a client function that sets a context timeout and sends a POST to /1.0/extended using the given
// client. This function is expected to be called from an api endpoint handler, which gives us access to the
// daemon state, from which we can create a client.
func ExtendedPostCmd(ctx context.Context, c *client.Client, data *types.ExtendedType) (string, error) {
func ExtendedPostCmd(ctx context.Context, c microTypes.Client, data *types.ExtendedType) (string, error) {
queryCtx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()

Expand Down
40 changes: 5 additions & 35 deletions example/cmd/microctl/cluster_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import (
"golang.org/x/sys/unix"
"gopkg.in/yaml.v3"

"github.com/canonical/microcluster/v3/client"
"github.com/canonical/microcluster/v3/cluster"
"github.com/canonical/microcluster/v3/microcluster"
"github.com/canonical/microcluster/v3/microcluster/types"
)

const recoveryConfirmation = `You should only run this command if:
Expand Down Expand Up @@ -78,7 +77,7 @@ type cmdClusterMembersList struct {

func (c *cmdClusterMembersList) command() *cobra.Command {
cmd := &cobra.Command{
Use: "list <address>",
Use: "list",
Short: "List cluster members locally, or remotely if an address is specified.",
RunE: c.run,
}
Expand All @@ -104,26 +103,7 @@ func (c *cmdClusterMembersList) run(cmd *cobra.Command, args []string) error {
return c.listLocalClusterMembers(m)
}

var client *client.Client

// Get a local client connected to the unix socket if no address is specified.
if len(args) == 1 {
client, err = m.RemoteClient(args[0])
if err != nil {
return err
}
} else {
client, err = m.LocalClient()
if err != nil {
return err
}
}

return c.listClusterMembers(cmd.Context(), client)
}

func (c *cmdClusterMembersList) listClusterMembers(ctx context.Context, client *client.Client) error {
clusterMembers, err := client.GetClusterMembers(ctx)
clusterMembers, err := m.GetClusterMembers(context.TODO())
if err != nil {
return err
}
Expand Down Expand Up @@ -184,17 +164,7 @@ func (c *cmdClusterMemberRemove) run(cmd *cobra.Command, args []string) error {
return err
}

client, err := m.LocalClient()
if err != nil {
return err
}

err = client.DeleteClusterMember(cmd.Context(), args[0], c.flagForce)
if err != nil {
return err
}

return nil
return m.RemoveClusterMember(cmd.Context(), args[0], c.flagForce)
}

type cmdClusterEdit struct {
Expand Down Expand Up @@ -254,7 +224,7 @@ func (c *cmdClusterEdit) run(cmd *cobra.Command, args []string) error {
}
}

newMembers := []cluster.DqliteMember{}
newMembers := []types.DqliteMember{}
err = yaml.Unmarshal(content, &newMembers)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions example/cmd/microctl/extended_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/spf13/cobra"

microClient "github.com/canonical/microcluster/v3/client"
"github.com/canonical/microcluster/v3/example/client"
"github.com/canonical/microcluster/v3/microcluster"
"github.com/canonical/microcluster/v3/microcluster/types"
)

type cmdExtended struct {
Expand Down Expand Up @@ -37,7 +37,7 @@ func (c *cmdExtended) run(cmd *cobra.Command, args []string) error {
return err
}

var cli *microClient.Client
var cli types.Client
if len(args) == 1 {
cli, err = m.RemoteClient(args[0])
} else {
Expand Down
7 changes: 1 addition & 6 deletions example/cmd/microctl/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ func (c *cmdShutdown) run(cmd *cobra.Command, args []string) error {
return err
}

client, err := m.LocalClient()
if err != nil {
return err
}

chResult := make(chan error, 1)
go func() {
defer close(chResult)

err := client.ShutdownDaemon(cmd.Context())
err := m.Shutdown(cmd.Context())
if err != nil {
chResult <- err
return
Expand Down
Loading