45 lines
841 B
Go
45 lines
841 B
Go
|
package credential
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
|
||
|
"git.loafle.net/overflow/model/meta"
|
||
|
)
|
||
|
|
||
|
type Credential struct {
|
||
|
}
|
||
|
|
||
|
type CredentialMap map[string]*Credential
|
||
|
|
||
|
func (m *CredentialMap) UnmarshalJSON(data []byte) error {
|
||
|
credentials := make(map[string]json.RawMessage)
|
||
|
err := json.Unmarshal(data, &credentials)
|
||
|
if nil != err {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
result := make(CredentialMap)
|
||
|
for k, v := range credentials {
|
||
|
var i interface{}
|
||
|
|
||
|
switch meta.ToMetaCredentialTypeEnum(&meta.MetaCredentialType{Key: k}) {
|
||
|
case meta.MetaCredentialTypeEnumSNMP:
|
||
|
i = &SNMPCredential{}
|
||
|
case meta.MetaCredentialTypeEnumWindows:
|
||
|
i = &WindowsCredential{}
|
||
|
default:
|
||
|
return errors.New("Unrecognized Credential")
|
||
|
}
|
||
|
|
||
|
err := json.Unmarshal(v, i)
|
||
|
if nil != err {
|
||
|
return err
|
||
|
}
|
||
|
result[k] = i.(*Credential)
|
||
|
}
|
||
|
*m = result
|
||
|
|
||
|
return nil
|
||
|
}
|