Skip to content

Commit

Permalink
Merge pull request #64 from kdeng3849/add-endpoints
Browse files Browse the repository at this point in the history
add endpoints for crud operations & update hardware data model
  • Loading branch information
kqdeng authored Jun 22, 2020
2 parents aac71dc + bc1a5a9 commit 5e66098
Show file tree
Hide file tree
Showing 35 changed files with 3,982 additions and 498 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ certs/
cmd/tink-cli/tink-cli
cmd/tink-server/tink-server
cmd/tink-worker/tink-worker
.idea

# Terraform
.terraform
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cli: ${cli}
worker : ${worker}

${server} ${cli} ${worker}:
CGO_ENABLED=0 go build -o $@ ./$@
CGO_ENABLED=0 GOOS=$$GOOS go build -o $@ ./$@

run: ${binaries}
docker-compose up -d --build db
Expand Down
9 changes: 7 additions & 2 deletions cmd/tink-cli/cmd/hardware/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -24,10 +25,14 @@ var allCmd = &cobra.Command{
var hw *hardware.Hardware
err = nil
for hw, err = alls.Recv(); err == nil && hw != nil; hw, err = alls.Recv() {
fmt.Println(hw.JSON)
b, err := json.Marshal(hw)
if err != nil {
log.Println(err)
}
fmt.Println(string(b))
}
if err != nil && err != io.EOF {
log.Fatal(err)
log.Println(err)
}
},
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/tink-cli/cmd/hardware/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"log"

Expand All @@ -22,11 +23,15 @@ var idCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
for _, id := range args {
hw, err := client.HardwareClient.ByID(context.Background(), &hardware.GetRequest{ID: id})
hw, err := client.HardwareClient.ByID(context.Background(), &hardware.GetRequest{Id: id})
if err != nil {
log.Fatal(err)
}
fmt.Println(hw.JSON)
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
},
}
Expand Down
32 changes: 0 additions & 32 deletions cmd/tink-cli/cmd/hardware/ingest.go

This file was deleted.

9 changes: 7 additions & 2 deletions cmd/tink-cli/cmd/hardware/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"log"
"net"
Expand All @@ -28,11 +29,15 @@ var ipCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
for _, ip := range args {
hw, err := client.HardwareClient.ByIP(context.Background(), &hardware.GetRequest{IP: ip})
hw, err := client.HardwareClient.ByIP(context.Background(), &hardware.GetRequest{Ip: ip})
if err != nil {
log.Fatal(err)
}
fmt.Println(hw.JSON)
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
},
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/tink-cli/cmd/hardware/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"log"
"net"
Expand All @@ -28,11 +29,15 @@ var macCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
for _, mac := range args {
hw, err := client.HardwareClient.ByMAC(context.Background(), &hardware.GetRequest{MAC: mac})
hw, err := client.HardwareClient.ByMAC(context.Background(), &hardware.GetRequest{Mac: mac})
if err != nil {
log.Fatal(err)
}
fmt.Println(hw.JSON)
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
},
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/tink-cli/cmd/hardware/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ tink hardware push --file /tmp/data.json`,
} else if s.ID == "" {
log.Fatalf("invalid json, ID is required: %s", data)
}
if _, err := client.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: data}); err != nil {

hw := hardware.Hardware{}
err := json.Unmarshal([]byte(data), &hw)
if err != nil {
log.Fatal(err)
}
if _, err := client.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: &hw}); err != nil {
log.Fatal(err)
}
log.Println("Hardware data pushed successfully")
Expand Down
9 changes: 7 additions & 2 deletions cmd/tink-cli/cmd/hardware/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -26,7 +27,7 @@ var watchCmd = &cobra.Command{
stdoutLock := sync.Mutex{}
for _, id := range args {
go func(id string) {
stream, err := client.HardwareClient.Watch(context.Background(), &hardware.GetRequest{ID: id})
stream, err := client.HardwareClient.Watch(context.Background(), &hardware.GetRequest{Id: id})
if err != nil {
log.Fatal(err)
}
Expand All @@ -35,7 +36,11 @@ var watchCmd = &cobra.Command{
err = nil
for hw, err = stream.Recv(); err == nil && hw != nil; hw, err = stream.Recv() {
stdoutLock.Lock()
fmt.Println(hw.JSON)
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
stdoutLock.Unlock()
}
if err != nil && err != io.EOF {
Expand Down
8 changes: 4 additions & 4 deletions cmd/tink-cli/cmd/template/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ cat /tmp/example.tmpl | tink template create -n example`,

data := readAll(reader)
if data != nil {
if err := tryParseTemplate(data); err != nil {
if err := tryParseTemplate(string(data)); err != nil {
log.Println(err)
return
}
Expand All @@ -74,16 +74,16 @@ func addFlags() {
createCmd.MarkPersistentFlagRequired(fName)
}

func tryParseTemplate(data []byte) error {
func tryParseTemplate(data string) error {
tmpl := *tt.New("")
if _, err := tmpl.Parse(string(data)); err != nil {
if _, err := tmpl.Parse(data); err != nil {
return err
}
return nil
}

func createTemplate(data []byte) {
req := template.WorkflowTemplate{Name: templateName, Data: data}
req := template.WorkflowTemplate{Name: templateName, Data: string(data)}
res, err := client.TemplateClient.CreateTemplate(context.Background(), &req)
if err != nil {
log.Println(err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/tink-cli/cmd/template/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func updateTemplate(id string) {
req.Name = templateName
} else if filePath != "" && templateName == "" {
data := readTemplateData()
if data != nil {
if data != "" {
if err := tryParseTemplate(data); err != nil {
log.Println(err)
return
Expand All @@ -69,7 +69,7 @@ func updateTemplate(id string) {
fmt.Println("Updated Template: ", id)
}

func readTemplateData() []byte {
func readTemplateData() string {
f, err := os.Open(filePath)
if err != nil {
log.Println(err)
Expand All @@ -80,7 +80,7 @@ func readTemplateData() []byte {
if err != nil {
log.Println(err)
}
return data
return string(data)
}

func init() {
Expand Down
38 changes: 23 additions & 15 deletions db/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ func InsertIntoDB(ctx context.Context, db *sql.DB, data string) error {
func GetByMAC(ctx context.Context, db *sql.DB, mac string) (string, error) {
arg := `
{
"network_ports": [
{
"data": {
"mac": "` + mac + `"
}
}
]
"network": {
"interfaces": [
{
"dhcp": {
"mac": "` + mac + `"
}
}
]
}
}
`
query := `
Expand All @@ -94,19 +96,25 @@ func GetByIP(ctx context.Context, db *sql.DB, ip string) (string, error) {
"instance": {
"ip_addresses": [
{
"address": "` + ip + `"
"address": "` + ip + `"
}
]
}
}
`
hardwareOrManagement := `
{
"ip_addresses": [
{
"address": "` + ip + `"
}
]
"network": {
"interfaces": [
{
"dhcp": {
"ip": {
"address": "` + ip + `"
}
}
}
]
}
}
`

Expand Down Expand Up @@ -141,7 +149,7 @@ func GetByID(ctx context.Context, db *sql.DB, id string) (string, error) {
}

// GetAll : get data for all machine
func GetAll(db *sql.DB, fn func(string) error) error {
func GetAll(db *sql.DB, fn func([]byte) error) error {
rows, err := db.Query(`
SELECT data
FROM hardware
Expand All @@ -163,7 +171,7 @@ func GetAll(db *sql.DB, fn func(string) error) error {
return err
}

err = fn(string(buf))
err = fn(buf)
if err != nil {
return err
}
Expand Down
21 changes: 11 additions & 10 deletions db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// CreateTemplate creates a new workflow template
func CreateTemplate(ctx context.Context, db *sql.DB, name string, data []byte, id uuid.UUID) error {
func CreateTemplate(ctx context.Context, db *sql.DB, name string, data string, id uuid.UUID) error {
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
if err != nil {
return errors.Wrap(err, "BEGIN transaction")
Expand Down Expand Up @@ -40,20 +40,21 @@ func CreateTemplate(ctx context.Context, db *sql.DB, name string, data []byte, i
}

// GetTemplate returns a workflow template
func GetTemplate(ctx context.Context, db *sql.DB, id string) ([]byte, error) {
func GetTemplate(ctx context.Context, db *sql.DB, id string) (string, string, error) {
query := `
SELECT data
SELECT name, data
FROM template
WHERE
id = $1
AND
deleted_at IS NULL
`
row := db.QueryRowContext(ctx, query, id)
buf := []byte{}
err := row.Scan(&buf)
name := []byte{}
data := []byte{}
err := row.Scan(&name, &data)
if err == nil {
return buf, nil
return string(name), string(data), nil
}

if err != sql.ErrNoRows {
Expand All @@ -63,7 +64,7 @@ func GetTemplate(ctx context.Context, db *sql.DB, id string) ([]byte, error) {
err = nil
}

return []byte{}, nil
return "", "", nil
}

// DeleteTemplate deletes a workflow template
Expand Down Expand Up @@ -136,20 +137,20 @@ func ListTemplates(db *sql.DB, fn func(id, n string, in, del *timestamp.Timestam
}

// UpdateTemplate update a given template
func UpdateTemplate(ctx context.Context, db *sql.DB, name string, data []byte, id uuid.UUID) error {
func UpdateTemplate(ctx context.Context, db *sql.DB, name string, data string, id uuid.UUID) error {
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
if err != nil {
return errors.Wrap(err, "BEGIN transaction")
}

if data == nil && name != "" {
if data == "" && name != "" {
_, err = tx.Exec(`
UPDATE template
SET
updated_at = NOW(), name = $2
WHERE
id = $1;`, id, name)
} else if data != nil && name == "" {
} else if data != "" && name == "" {
_, err = tx.Exec(`
UPDATE template
SET
Expand Down
Loading

0 comments on commit 5e66098

Please sign in to comment.