credential unmarshal added
This commit is contained in:
parent
7b804b2da2
commit
6288478c89
|
@ -3,6 +3,7 @@ package credential
|
|||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
"git.loafle.net/overflow/model/meta"
|
||||
)
|
||||
|
@ -10,35 +11,45 @@ import (
|
|||
type Credential struct {
|
||||
}
|
||||
|
||||
type CredentialMap map[string]*Credential
|
||||
// CredentialsMap is map of array of Credential
|
||||
type CredentialsMap map[string]interface{}
|
||||
|
||||
func (m *CredentialMap) UnmarshalJSON(data []byte) error {
|
||||
func (m *CredentialsMap) UnmarshalJSON(data []byte) error {
|
||||
credentials := make(map[string]json.RawMessage)
|
||||
err := json.Unmarshal(data, &credentials)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
result := make(CredentialMap)
|
||||
result := make(CredentialsMap)
|
||||
for k, v := range credentials {
|
||||
var i interface{}
|
||||
|
||||
switch meta.ToMetaCredentialTypeEnum(&meta.MetaCredentialType{Key: k}) {
|
||||
case meta.MetaCredentialTypeEnumSNMP:
|
||||
i = &SNMPCredential{}
|
||||
var _i []*SNMPCredential
|
||||
i = &_i
|
||||
case meta.MetaCredentialTypeEnumWindows:
|
||||
i = &WindowsCredential{}
|
||||
var _i []*WindowsCredential
|
||||
i = &_i
|
||||
default:
|
||||
return errors.New("Unrecognized Credential")
|
||||
}
|
||||
|
||||
err := json.Unmarshal(v, i)
|
||||
err := json.Unmarshal(v, &i)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
result[k] = i.(*Credential)
|
||||
result[k] = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
*m = result
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func unmarshalJSON(v json.RawMessage, i interface{}) error {
|
||||
err := json.Unmarshal(v, &i)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
69
config/credential/Credential_test.go
Normal file
69
config/credential/Credential_test.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package credential
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCredentialsMap_UnmarshalJSON(t *testing.T) {
|
||||
credentialsMap := CredentialsMap{
|
||||
"SNMP": []*SNMPCredential{
|
||||
&SNMPCredential{
|
||||
Version: "2c",
|
||||
Community: "public",
|
||||
},
|
||||
&SNMPCredential{
|
||||
Version: "2c",
|
||||
Community: "atgame",
|
||||
},
|
||||
},
|
||||
"WINDOWS": []*WindowsCredential{
|
||||
&WindowsCredential{
|
||||
DomainName: "atgame.net",
|
||||
User: "crusader",
|
||||
Password: "1234",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
b, err := json.Marshal(credentialsMap)
|
||||
if nil != err {
|
||||
t.Errorf("CredentialsMap.UnmarshalJSON() error = %v", err)
|
||||
}
|
||||
|
||||
_credentialsMap := CredentialsMap{}
|
||||
|
||||
type args struct {
|
||||
data []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
m CredentialsMap
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
m: _credentialsMap,
|
||||
args: args{
|
||||
data: b,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.m.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
|
||||
t.Errorf("CredentialsMap.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
cs := tt.m["SNMP"].([]*SNMPCredential)
|
||||
for _, c := range cs {
|
||||
t.Log(c)
|
||||
}
|
||||
cs1 := tt.m["WINDOWS"].([]*WindowsCredential)
|
||||
for _, c1 := range cs1 {
|
||||
t.Log(c1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import "encoding/json"
|
|||
|
||||
type SNMPCredential struct {
|
||||
Credential
|
||||
|
||||
Version string `json:"version,omitempty"`
|
||||
Community string `json:"community,omitempty"`
|
||||
AuthenticationType string `json:"authenticationType,omitempty"`
|
||||
|
|
|
@ -4,6 +4,7 @@ import "encoding/json"
|
|||
|
||||
type UnixCredential struct {
|
||||
Credential
|
||||
|
||||
User string `json:"user,omitempty"`
|
||||
LoginType string `json:"loginType,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
|
|
|
@ -2,6 +2,7 @@ package credential
|
|||
|
||||
type WindowsCredential struct {
|
||||
Credential
|
||||
|
||||
DomainName string `json:"domainName,omitempty"`
|
||||
User string `json:"user,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user