Skip to content

Commit

Permalink
add simple client pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Seán C McCord <ulexus@gmail.com>
  • Loading branch information
Ulexus committed May 9, 2021
1 parent 5e0c1df commit eb0e8ba
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:alpine AS builder
ENV GO111MODULE on
RUN apk add --no-cache git
WORKDIR $GOPATH/src/github.com/talos-systems/wglan-manager
COPY . .
RUN go get -d -v
RUN go build -o /go/bin/web

FROM alpine
RUN apk add --no-cache ca-certificates
COPY --from=builder /go/bin/web /go/bin/web
ENTRYPOINT ["/go/bin/web"]
66 changes: 66 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package client

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/talos-systems/wglan-manager/types"
)

// Add adds a Node to the Cluster database, updating it if it already exists.
func Add(rootURL, clusterID string, n *types.Node) error {

body := new(bytes.Buffer)

if err := json.NewEncoder(body).Encode(n); err != nil {
return fmt.Errorf("failed to encode Node information: %w", err)
}

resp, err := http.Post(rootURL, "application/json", body); if err != nil {
return fmt.Errorf("failed to post Node information: %w", err)
}
defer resp.Body.Close() // nolint: errcheck

if resp.StatusCode > 299 {
return fmt.Errorf("server rejected Node information: %w", err)
}

return nil
}

// Get returns the Node defined by the given public key, if and only if it exists within the given Cluster ID.
func Get(rootURL, clusterID, publicKey string) (*types.Node, error) {

resp, err := http.Get(fmt.Sprintf("%s/%s/%s", rootURL, clusterID, publicKey))
if err != nil {
return nil, fmt.Errorf("failed to request node %q/%q from server %q: %w", clusterID, publicKey, rootURL, err)
}

node := new(types.Node)
if err = json.NewDecoder(resp.Body).Decode(node); err != nil {
return nil, fmt.Errorf("failed to decode response from server: %w", err)
}

return node, nil
}

// List returns the set of Nodes associated with the given Cluster ID.
func List(rootURL string, clusterID string) ([]*types.Node,error) {

resp, err := http.Get(fmt.Sprintf("%s/%s", rootURL, clusterID))
if err != nil {
return nil, fmt.Errorf("failed to request list from server %q: %w", rootURL, err)
}
defer resp.Body.Close() // nolint: errcheck

var list []*types.Node
if err = json.NewDecoder(resp.Body).Decode(&list); err != nil {
return nil, fmt.Errorf("failed to decode response from server: %w", err)
}

return list, nil

}

0 comments on commit eb0e8ba

Please sign in to comment.