Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom UnmarshalJSON for configurepackage plugin #297

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions agent/plugins/configurepackage/configurepackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package configurepackage

import (
"encoding/json"
"errors"
"fmt"
"regexp"
Expand Down Expand Up @@ -73,6 +74,30 @@ type ConfigurePackagePluginInput struct {
Repository string `json:"repository"`
}

//Unpacker helper for handling additionalParameters
type Unpacker struct {
Data map[string]interface{}
}

//UnmarshalJSON should do something
func (u *Unpacker) UnmarshalJSON(b []byte) error {

err := json.Unmarshal(b, &u.Data)
if err != nil {
return err
}

for _, v := range u.Data {
switch v.(type) {
case map[string]interface{}:
u.Data["additionalArguments"] = ""
default:
}
}

return nil
}

// NewPlugin returns a new instance of the plugin.
func NewPlugin() (*Plugin, error) {
var plugin Plugin
Expand Down Expand Up @@ -292,9 +317,20 @@ func getVersionToUninstall(

// parseAndValidateInput marshals raw JSON and returns the result of input validation or an error
func parseAndValidateInput(rawPluginInput interface{}) (*ConfigurePackagePluginInput, error) {
var input ConfigurePackagePluginInput
var err error
if err = jsonutil.Remarshal(rawPluginInput, &input); err != nil {
u := &Unpacker{}
b, err := json.Marshal(rawPluginInput)
if err != nil {
return nil, fmt.Errorf("could not marshal rawPluginInput")
}

err = json.Unmarshal(b, u)
if err != nil {
return nil, fmt.Errorf("could not Umarshal to Unpacker")
}

var input ConfigurePackagePluginInput
if err = jsonutil.Remarshal(u, &input); err != nil {
return nil, fmt.Errorf("invalid format in plugin properties %v; \nerror %v", rawPluginInput, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/aws/amazon-ssm-agent/agent/context"
"github.com/aws/amazon-ssm-agent/agent/plugins/configurepackage/localpackages"
"github.com/aws/amazon-ssm-agent/agent/plugins/inventory/model"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ func TestApplicationDataWithPackageRepositoryData(t *testing.T) {
data := CollectApplicationData(mockContext)
assert.Equal(t, len(sampleDataParsed), len(data))
assert.NotEqual(t, len(mockData), len(data))
}
}