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

Validate TypeInstance input #629

Merged
merged 10 commits into from
Feb 18, 2022
Merged

Conversation

mkuziemko
Copy link

@mkuziemko mkuziemko commented Feb 5, 2022

Description

Changes proposed in this pull request:

  • Validate TypeInstance input during TypeInstance creation, edition and uploading/updating TypeInstance by Argo Action.

TODO:

Test Cases

Test Case 1

  1. Create a sample file:
typeInstances:
  - alias: aws-s3
    attributes:
      - path: cap.attribute.cloud.provider.aws
        revision: 0.1.0
    typeRef:
      path: cap.type.database.object.aws.s3.bucket-config
      revision: 0.1.0
    value:
      host: https://s3.amazonaws.com
  1. Build a binary:
make build-tool-cli
  1. Verify that the validation errors occur:
./bin/capact-linux-amd64 typeinstances create -f test.yml 
Error: - Validation TypeInstances "TypeInstance with alias aws-s3":
    * (root): name is required
    * (root): Additional property host is not allowed
  1. Change host to name and try one more time:
      name: https://s3.amazonaws.com
./bin/capact-linux-amd64 typeinstances create -f test.yml 
  ALIAS                ASSIGNED ID               
---------+---------------------------------------
  aws-s3   5605af48-c34f-4bdc-b2d8-53c679bdfa5a  
---------+---------------------------------------
  1. The same steps could be repeated by this time with the edit action for TypeInstance with generated id 5605af48-c34f-4bdc-b2d8-53c679bdfa5a

Test Case 2

  1. On this change try to install Matermost as described here.
  2. Installation should not succeed on the upload TypeInstance step due to validation errors.

Related issue(s)


Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: I didn't run it, I just make a code review. Before merging this PR, please make sure that at least our Mattermost, Rocketchat and Concourse manifests run well.

Comment on lines +24 to +25
LocalHubEndpoint string `envconfig:"default=http://capact-hub-local.capact-system/graphql"`
PublicHubEndpoint string `envconfig:"default=http://capact-hub-public.capact-system/graphql"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed previously, please make sure the Environmental variables are set properly during Engine Rendering both for download, update and create TypeInstances (see the pkg/sdk/renderer/argo/typeinstance_handler.go).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this comment? I don't see any render logic changed 🤔

cmd/argo-actions/main.go Outdated Show resolved Hide resolved
cmd/cli/cmd/typeinstance/create.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
@pkosiec
Copy link
Member

pkosiec commented Feb 8, 2022

BTW I forgot about two things:

  • please describe testing scenario in this PR (how to test the functionality that it works). This is helpful both for PR author and reviewer as well 🙂
  • consider an integration test case to test the uploader. To discuss whether it is really needed.

@mkuziemko mkuziemko added WIP Work in progress area/cli Relates to CLI area/hub Relates to Hub area/hub-manifests Relates to Hub manifests labels Feb 10, 2022
@mkuziemko mkuziemko removed the WIP Work in progress label Feb 10, 2022
@@ -1,3 +1,4 @@
//go:build integration
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to run integration tests, this change is needed: capactio/hub-manifests#56

pkg/argo-actions/upload_type_instances.go Outdated Show resolved Hide resolved
Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well, but still some improvements in code are needed 🙂 And there's one quite important comment not addressed since last review.

cmd/argo-actions/main.go Outdated Show resolved Hide resolved
Comment on lines +24 to +25
LocalHubEndpoint string `envconfig:"default=http://capact-hub-local.capact-system/graphql"`
PublicHubEndpoint string `envconfig:"default=http://capact-hub-public.capact-system/graphql"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this comment? I don't see any render logic changed 🤔

pkg/sdk/validation/typeinstance_test.go Show resolved Hide resolved
pkg/argo-actions/upload_type_instances.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Show resolved Hide resolved
Comment on lines 107 to 109
if _, ok := ti.value.(map[string]interface{}); !ok {
return Result{}, errors.New("could not create map from TypeInstance Value")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this if? The TypeInstance value not necessarily is object and json.Marshal marshalls virtually everything.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement should cover the case when the user tries to edit TypeInstance and delete the values from it. Then, it would not be converted to map(string)interface and validation would not pass.

Comment on lines 128 to 132
if ti.alias != nil {
msg = fmt.Sprintf("TypeInstance with alias %s", *ti.alias)
} else if ti.id != "" {
msg = fmt.Sprintf("TypeInstance with id %s", ti.id)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically, in this scope of the validation function, there could be a case that a given TypeInstance contains both Alias and ID, right? So please use StringBuilder to build proper message taking into account that 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, theoretically it could have both. I added a separate method and now the output is as:

Validation TypeInstances "TypeInstance(ID: some-id,Alias: aws-creds)":
    * (root): name is required
    * (root): Additional property host is not allowed

pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well 👍 Before merge, please apply suggestions, especially around (ti *TypeInstanceEssentialData) String() to have more elegant output for user. Thanks!

pkg/sdk/renderer/argo/renderer_test.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance_test.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
pkg/sdk/validation/typeinstance.go Outdated Show resolved Hide resolved
Alias: ptr.String("aws-creds-2"),
},
},
expError: fmt.Errorf("%s", "- Validation TypeInstances \"TypeInstance(Alias: aws-creds)\":\n * (root): key is required\n- Validation TypeInstances \"TypeInstance(Alias: aws-creds-2)\":\n * (root): key is required"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just do something like:

Suggested change
expError: fmt.Errorf("%s", "- Validation TypeInstances \"TypeInstance(Alias: aws-creds)\":\n * (root): key is required\n- Validation TypeInstances \"TypeInstance(Alias: aws-creds-2)\":\n * (root): key is required"),
expError: errors.New("- Validation TypeInstances \"TypeInstance(Alias: aws-creds)\":\n * (root): key is required\n- Validation TypeInstances \"TypeInstance(Alias: aws-creds-2)\":\n * (root): key is required"),

Please adjust that in all places in this file

}
typeRef, ok := typeInstancesTypeRef[ti.ID]
if !ok {
return nil, errors.Wrapf(err, "while finding TypeInstance Type reference for id %q", ti.ID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, errors.Wrapf(err, "while finding TypeInstance Type reference for id %q", ti.ID)
return nil, errors.Wrapf(err, "while finding TypeInstance Type reference for ID %q", ti.ID)

ID: ptr.String("5605af48-c34f-4bdc-b2d8-53c679bdfa5a"),
},
},
expError: fmt.Errorf("%s", "- Validation TypeInstances \"TypeInstance(ID: 5605af48-c34f-4bdc-b2d8-53c679bdfa5a)\":\n * replicas: Invalid type. Expected: string, given: integer"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW it would be good to trace the quotation around:

"TypeInstance(ID: 5605af48-c34f-4bdc-b2d8-53c679bdfa5a)\"

And remove it - as it shouldn't be there. In this case the quotation should be only for ID and Alias values.

Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@mkuziemko mkuziemko merged commit 041d69e into capactio:main Feb 18, 2022
@mkuziemko mkuziemko deleted the fix_typeinstance branch February 18, 2022 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cli Relates to CLI area/hub Relates to Hub area/hub-manifests Relates to Hub manifests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants