first overflow service commit
This commit is contained in:
160
proxy/noauthagent/noauth_agent_service.go
Normal file
160
proxy/noauthagent/noauth_agent_service.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package noauthagent
|
||||
|
||||
import (
|
||||
|
||||
"git.loafle.net/overflow/commons_go/model/timestamp"
|
||||
"git.loafle.net/overflow/overflow_proxy_service/proxy"
|
||||
"encoding/json"
|
||||
"git.loafle.net/overflow/overflow_proxy_service/proxy/member"
|
||||
"git.loafle.net/overflow/overflow_proxy_service/proxy/agent"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type NoAuthAgentService struct {
|
||||
}
|
||||
|
||||
func NewNoAuthAgentService() *NoAuthAgentService {
|
||||
return &NoAuthAgentService{}
|
||||
}
|
||||
|
||||
|
||||
type NoAuthAgent struct {
|
||||
Id json.Number `json:"id,Number,omitempty"`
|
||||
TempKey string `json:"tempKey,omitempty"`
|
||||
Date timestamp.Timestamp`json:"date,omitempty"`
|
||||
ApiKey string `json:"apiKey,omitempty"`
|
||||
AuthStatus string `json:"authStatus,omitempty"`
|
||||
LocalIP int64 `json:"localIP,omitempty"`
|
||||
HostName string `json:"hostName,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func NewNoAuthAgent(apikey string, localIp int64, hostName string) *NoAuthAgent {
|
||||
|
||||
na := &NoAuthAgent{
|
||||
Date:timestamp.Now(),
|
||||
ApiKey:apikey,
|
||||
LocalIP:localIp,
|
||||
HostName:hostName,
|
||||
}
|
||||
return na
|
||||
}
|
||||
|
||||
|
||||
func(as *NoAuthAgentService)SaveNoAuthAgent(na *NoAuthAgent) (string, error) {
|
||||
|
||||
|
||||
bytes, err := json.Marshal(na)
|
||||
if err != nil {
|
||||
return "",err
|
||||
}
|
||||
|
||||
memMap := make(map[string]string)
|
||||
memMap["com.loafle.overflow.noauthagent.model.NoAuthAgent"] = string(bytes);
|
||||
|
||||
out, err := proxy.InvokeDB("noauthAgent", "create", memMap);
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return out, nil;
|
||||
}
|
||||
|
||||
func(as *NoAuthAgentService)CheckAuth(tempKey string) (string,error) {
|
||||
|
||||
memMap := make(map[string]string)
|
||||
|
||||
na := NewNoAuthAgent("", 0, "")
|
||||
na.TempKey = tempKey
|
||||
|
||||
bytes, err := json.Marshal(na)
|
||||
if err != nil {
|
||||
return "", err;
|
||||
}
|
||||
|
||||
memMap["com.loafle.overflow.noauthagent.model.NoAuthAgent"] = string(bytes);
|
||||
|
||||
out, err := proxy.InvokeDB("noauthAgent", "findByTempKey", memMap);
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
nn := NoAuthAgent{}
|
||||
err = json.Unmarshal([]byte(out), &nn)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return out,nil;
|
||||
}
|
||||
|
||||
|
||||
func(as *NoAuthAgentService)GetNoAuthList(excludeStatus string) (string,error) {
|
||||
|
||||
memMap := make(map[string]string)
|
||||
|
||||
na := NewNoAuthAgent("", 0, "")
|
||||
na.AuthStatus = excludeStatus
|
||||
|
||||
bytes, err := json.Marshal(na)
|
||||
if err != nil {
|
||||
return "", err;
|
||||
}
|
||||
|
||||
memMap["com.loafle.overflow.noauthagent.model.NoAuthAgent"] = string(bytes);
|
||||
|
||||
out, err := proxy.InvokeDB("noauthAgent", "findAllByNoAuth", memMap);
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
return out,nil;
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (as *NoAuthAgentService)RequestAuth(noauthAgt NoAuthAgent, memberId, desc string) (string, error) {
|
||||
paramMap := make(map[string]string)
|
||||
noauthAgt.AuthStatus = "ACCEPT"
|
||||
|
||||
bytes, err := json.Marshal(noauthAgt)
|
||||
if err != nil {
|
||||
return "", err;
|
||||
}
|
||||
paramMap["com.loafle.overflow.noauthagent.model.NoAuthAgent"] = string(bytes)
|
||||
out, err := proxy.InvokeDB("noauthAgent", "update", paramMap)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(out) == 0 {
|
||||
return "", errors.New("Cannot update Agent. ")
|
||||
}
|
||||
m := member.Member{}
|
||||
m.Id = json.Number(memberId)
|
||||
newAgent := agent.NewAgent(desc, m)
|
||||
newone, err := agent.NewAgentService().SaveAgent(newAgent)
|
||||
if err!= nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return newone, nil
|
||||
}
|
||||
|
||||
func (as *NoAuthAgentService)ReadNoAuthAgent(id string) (string, error){
|
||||
mm := make(map[string]string)
|
||||
mm["id"] = id
|
||||
out, err := proxy.InvokeDB("noauthAgent", "find", mm)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
105
proxy/noauthagent/noauth_agent_service_test.go
Normal file
105
proxy/noauthagent/noauth_agent_service_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package noauthagent
|
||||
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"testing"
|
||||
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
||||
func TestCreateUUid(t *testing.T) {
|
||||
|
||||
uu, err := uuid.NewUUID()
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log(uu)
|
||||
|
||||
}
|
||||
|
||||
func TestCreateNoAuthAgent(t *testing.T) {
|
||||
|
||||
na := NewNoAuthAgent("233421390283", 111, "Snoop")
|
||||
|
||||
na.TempKey = "1111111"
|
||||
na.AuthStatus = "WAIT"
|
||||
|
||||
nas := NewNoAuthAgentService()
|
||||
|
||||
out, err := nas.SaveNoAuthAgent(na)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log(out)
|
||||
|
||||
}
|
||||
|
||||
func TestCheckAuthNoAuthAgent(t *testing.T) {
|
||||
|
||||
na := NewNoAuthAgent("2334278390283", 111, "Snoop")
|
||||
|
||||
na.TempKey = "alsdkjf;alkdjsf;la"
|
||||
na.AuthStatus = "WAIT"
|
||||
|
||||
nas := NewNoAuthAgentService()
|
||||
|
||||
out, err := nas.CheckAuth(na.TempKey)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log(out)
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
func TestNoAuthList(t *testing.T) {
|
||||
|
||||
|
||||
nas := NewNoAuthAgentService()
|
||||
|
||||
out, err := nas.GetNoAuthList("ACCEPT")
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log(out)
|
||||
|
||||
}
|
||||
|
||||
func TestRequestAuth(t *testing.T) {
|
||||
nas := NewNoAuthAgentService()
|
||||
res, err := nas.ReadNoAuthAgent("1")
|
||||
|
||||
na := NoAuthAgent{}
|
||||
json.Unmarshal([]byte(res), na)
|
||||
|
||||
newone, err := nas.RequestAuth(na, "1", "test")
|
||||
if err!= nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(newone)
|
||||
}
|
||||
|
||||
|
||||
func TestCheckAuth(t *testing.T) {
|
||||
|
||||
|
||||
ns := NewNoAuthAgentService()
|
||||
|
||||
str, err := ns.CheckAuth("3398473-90847903874")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log(str)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user