Add methods to get attributes more easily in Go API (#2299)
* Add methods to get attributes more easily in Go API * Add type check
This commit is contained in:
1 parent
ab3fe75537
commit
a67085f663
14 files changed
+480
-24
No files matched your search
@@ -281,14 +281,22 @@ var inputStructs = []*structInfo{
|
||||
},
|
||||
// Auth Methods related resources
|
||||
{
|
||||
inProto: &authmethods.PasswordAuthMethodAttributes{},
|
||||
outFile: "authmethods/password_auth_method_attributes.gen.go",
|
||||
subtypeName: "PasswordAuthMethod",
|
||||
inProto: &authmethods.PasswordAuthMethodAttributes{},
|
||||
outFile: "authmethods/password_auth_method_attributes.gen.go",
|
||||
subtypeName: "PasswordAuthMethod",
|
||||
parentTypeName: "AuthMethod",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &authmethods.OidcAuthMethodAttributes{},
|
||||
outFile: "authmethods/oidc_auth_method_attributes.gen.go",
|
||||
subtypeName: "OidcAuthMethod",
|
||||
inProto: &authmethods.OidcAuthMethodAttributes{},
|
||||
outFile: "authmethods/oidc_auth_method_attributes.gen.go",
|
||||
subtypeName: "OidcAuthMethod",
|
||||
parentTypeName: "AuthMethod",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &authmethods.OidcAuthMethodAuthenticateStartResponse{},
|
||||
@@ -320,14 +328,22 @@ var inputStructs = []*structInfo{
|
||||
},
|
||||
// Accounts
|
||||
{
|
||||
inProto: &accounts.PasswordAccountAttributes{},
|
||||
outFile: "accounts/password_account_attributes.gen.go",
|
||||
subtypeName: "PasswordAccount",
|
||||
inProto: &accounts.PasswordAccountAttributes{},
|
||||
outFile: "accounts/password_account_attributes.gen.go",
|
||||
subtypeName: "PasswordAccount",
|
||||
parentTypeName: "Account",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &accounts.OidcAccountAttributes{},
|
||||
outFile: "accounts/oidc_account_attributes.gen.go",
|
||||
subtypeName: "OidcAccount",
|
||||
inProto: &accounts.OidcAccountAttributes{},
|
||||
outFile: "accounts/oidc_account_attributes.gen.go",
|
||||
subtypeName: "OidcAccount",
|
||||
parentTypeName: "Account",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &accounts.Account{},
|
||||
@@ -356,6 +372,10 @@ var inputStructs = []*structInfo{
|
||||
SkipDefault: true,
|
||||
},
|
||||
},
|
||||
parentTypeName: "ManagedGroup",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &managedgroups.ManagedGroup{},
|
||||
@@ -389,9 +409,13 @@ var inputStructs = []*structInfo{
|
||||
},
|
||||
// Credentials
|
||||
{
|
||||
inProto: &credentialstores.VaultCredentialStoreAttributes{},
|
||||
outFile: "credentialstores/vault_credential_store_attributes.gen.go",
|
||||
subtypeName: "VaultCredentialStore",
|
||||
inProto: &credentialstores.VaultCredentialStoreAttributes{},
|
||||
outFile: "credentialstores/vault_credential_store_attributes.gen.go",
|
||||
subtypeName: "VaultCredentialStore",
|
||||
parentTypeName: "CredentialStore",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &credentialstores.CredentialStore{},
|
||||
@@ -437,6 +461,10 @@ var inputStructs = []*structInfo{
|
||||
SkipDefault: true,
|
||||
},
|
||||
},
|
||||
parentTypeName: "CredentialLibrary",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &credentiallibraries.CredentialLibrary{},
|
||||
@@ -468,6 +496,10 @@ var inputStructs = []*structInfo{
|
||||
SkipDefault: true,
|
||||
},
|
||||
},
|
||||
parentTypeName: "Credential",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &credentials.SshPrivateKeyAttributes{},
|
||||
@@ -483,6 +515,10 @@ var inputStructs = []*structInfo{
|
||||
SkipDefault: true,
|
||||
},
|
||||
},
|
||||
parentTypeName: "Credential",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &credentials.Credential{},
|
||||
@@ -564,9 +600,13 @@ var inputStructs = []*structInfo{
|
||||
createResponseTypes: true,
|
||||
},
|
||||
{
|
||||
inProto: &hosts.StaticHostAttributes{},
|
||||
outFile: "hosts/static_host_attributes.gen.go",
|
||||
subtypeName: "StaticHost",
|
||||
inProto: &hosts.StaticHostAttributes{},
|
||||
outFile: "hosts/static_host_attributes.gen.go",
|
||||
subtypeName: "StaticHost",
|
||||
parentTypeName: "Host",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &hostsets.HostSet{},
|
||||
@@ -627,14 +667,22 @@ var inputStructs = []*structInfo{
|
||||
subtypeName: "WorkerInfo",
|
||||
},
|
||||
{
|
||||
inProto: &targets.TcpTargetAttributes{},
|
||||
outFile: "targets/tcp_target_attributes.gen.go",
|
||||
subtypeName: "TcpTarget",
|
||||
inProto: &targets.TcpTargetAttributes{},
|
||||
outFile: "targets/tcp_target_attributes.gen.go",
|
||||
subtypeName: "TcpTarget",
|
||||
parentTypeName: "Target",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &targets.SshTargetAttributes{},
|
||||
outFile: "targets/ssh_target_attributes.gen.go",
|
||||
subtypeName: "SshTarget",
|
||||
inProto: &targets.SshTargetAttributes{},
|
||||
outFile: "targets/ssh_target_attributes.gen.go",
|
||||
subtypeName: "SshTarget",
|
||||
parentTypeName: "Target",
|
||||
templates: []*template.Template{
|
||||
mapstructureConversionTemplate,
|
||||
},
|
||||
},
|
||||
{
|
||||
inProto: &targets.Target{},
|
||||
|
||||
@@ -792,6 +792,38 @@ func Default{{ $subtypeName }}{{ $field.Name }}() Option {
|
||||
{{ end }}
|
||||
`))
|
||||
|
||||
var mapstructureConversionTemplate = template.Must(template.New("").Funcs(
|
||||
template.FuncMap{
|
||||
"typeFromSubtype": typeFromSubtype,
|
||||
"kebabCase": kebabCase,
|
||||
},
|
||||
).Parse(`
|
||||
func AttributesMapTo{{ .Name }}(in map[string]interface{}) (*{{ .Name }}, error) {
|
||||
if in == nil {
|
||||
return nil, fmt.Errorf("nil input map")
|
||||
}
|
||||
var out {{ .Name }}
|
||||
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
Result: &out,
|
||||
TagName: "json",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating mapstructure decoder: %w", err)
|
||||
}
|
||||
if err := dec.Decode(in); err != nil {
|
||||
return nil, fmt.Errorf("error decoding: %w", err)
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (pt *{{ .ParentTypeName }}) Get{{ .Name }}() (*{{ .Name }}, error) {
|
||||
if pt.Type != "{{ typeFromSubtype .Name .ParentTypeName "Attributes"}}" {
|
||||
return nil, fmt.Errorf("asked to fetch %s-type attributes but {{ kebabCase .ParentTypeName }} is of type %s", "{{ typeFromSubtype .Name .ParentTypeName "Attributes"}}", pt.Type)
|
||||
}
|
||||
return AttributesMapTo{{ .Name }}(pt.Attributes)
|
||||
}
|
||||
`))
|
||||
|
||||
func makeSlice(strs ...string) []string {
|
||||
return strs
|
||||
}
|
||||
@@ -829,3 +861,7 @@ func removeDups(in []string) []string {
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func typeFromSubtype(in, parent, extraSuffix string) string {
|
||||
return strings.ToLower(strings.TrimSuffix(strings.TrimSuffix(in, extraSuffix), parent))
|
||||
}
|
||||
Reference in new issue
Block a user