Rename pcsdk module to commands
This commit is contained in:
		
							
								
								
									
										53
									
								
								commands/commands.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								commands/commands.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
package pcsdk
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/netip"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	"github.com/thefeli73/polemos/state"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ProxyCommandCreate struct {
 | 
			
		||||
	Command CommandCreate `json:"create"`
 | 
			
		||||
	// signature Signature
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type CommandCreate struct {
 | 
			
		||||
	IncomingPort    uint16     `json:"incoming_port"`
 | 
			
		||||
	DestinationPort uint16     `json:"destination_port"`
 | 
			
		||||
	DestinationIP   netip.Addr `json:"destination_ip"`
 | 
			
		||||
	Id              string     `json:"id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewCommandCreate(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandCreate {
 | 
			
		||||
	c := CommandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))}
 | 
			
		||||
	return ProxyCommandCreate{c}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ProxyCommandModify struct {
 | 
			
		||||
	Command CommandModify `json:"modify"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type CommandModify struct {
 | 
			
		||||
	DestinationPort uint16     `json:"destination_port"`
 | 
			
		||||
	DestinationIP   netip.Addr `json:"destination_ip"`
 | 
			
		||||
	Id              string     `json:"id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewCommandModify(oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandModify {
 | 
			
		||||
	c := CommandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))}
 | 
			
		||||
	return ProxyCommandModify{c}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ProxyCommandDelete struct {
 | 
			
		||||
	Command CommandDelete `json:"delete"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type CommandDelete struct {
 | 
			
		||||
	Id string `json:"id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewCommandDelete(id state.CustomUUID) ProxyCommandDelete {
 | 
			
		||||
	c := CommandDelete{uuid.UUID.String(uuid.UUID(id))}
 | 
			
		||||
	return ProxyCommandDelete{c}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								commands/commands_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								commands/commands_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
package pcsdk
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"net/netip"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	"github.com/thefeli73/polemos/state"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestCommandCreateJsonParse(t *testing.T) {
 | 
			
		||||
	ip, _ := netip.ParseAddr("127.0.0.99")
 | 
			
		||||
	uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
 | 
			
		||||
	c := NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid))
 | 
			
		||||
	m := ProxyCommandCreate{c}
 | 
			
		||||
	msg, err := json.Marshal(m)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf(`%q`, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	expected := "{\"create\":{\"incoming_port\":5555,\"destination_port\":6666,\"destination_ip\":\"127.0.0.99\",\"id\":\"87e79cbc-6df6-4462-8412-85d6c473e3b1\"}}"
 | 
			
		||||
	if string(msg) != expected {
 | 
			
		||||
		t.Fatalf(
 | 
			
		||||
			"\nExpected:\t %q\nGot:\t\t %q\n", expected, msg)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user