diff --git a/config/credential/Credential.go b/config/credential/Credential.go index b09c7c4..23ab028 100644 --- a/config/credential/Credential.go +++ b/config/credential/Credential.go @@ -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 +} diff --git a/config/credential/Credential_test.go b/config/credential/Credential_test.go new file mode 100644 index 0000000..86449cf --- /dev/null +++ b/config/credential/Credential_test.go @@ -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) + } + }) + } +} diff --git a/config/credential/SNMPCredential.go b/config/credential/SNMPCredential.go index ce42ae5..e34d5bd 100644 --- a/config/credential/SNMPCredential.go +++ b/config/credential/SNMPCredential.go @@ -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"` diff --git a/config/credential/UnixCredential.go b/config/credential/UnixCredential.go index 85e6a77..68e9121 100644 --- a/config/credential/UnixCredential.go +++ b/config/credential/UnixCredential.go @@ -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"` diff --git a/config/credential/WindowsCredential.go b/config/credential/WindowsCredential.go index 81bac02..50b1e34 100644 --- a/config/credential/WindowsCredential.go +++ b/config/credential/WindowsCredential.go @@ -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"`