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

Added trigger sync and save #230

Merged
merged 6 commits into from
Nov 20, 2023
Merged

Conversation

eddbbt
Copy link
Contributor

@eddbbt eddbbt commented Oct 16, 2023

Add command to sync local trigger to remote astarte cluster, after validation
Add command to save remote astarte trigger to local json files

Depends on astarte-platform/astarte-go#59

@eddbbt eddbbt force-pushed the master branch 5 times, most recently from df907e7 to 72a6616 Compare October 16, 2023 14:57
Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
Signed-off-by: Eddy Babetto <eddy.babetto@gmail.com>
@eddbbt eddbbt force-pushed the master branch 2 times, most recently from ddb1c41 to 8a4814a Compare October 18, 2023 13:51
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
cmd/realm/triggers.go Outdated Show resolved Hide resolved
Refactor of functions with pre-existing ones in order to save lines of code

Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
Add command triggers validate for validate json payload

Signed-off-by: Eddy Babetto <eddy.babetto@gmail.com>
@@ -69,111 +71,372 @@ var triggersDeleteCmd = &cobra.Command{
Aliases: []string{"del"},
}

var triggersSaveCmd = &cobra.Command{
Use: "save [destination-path]",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's keep consistency with other commands

Suggested change
Use: "save [destination-path]",
Use: "save <destination-path>",

Use: "save [destination-path]",
Short: "Save triggers to a local folder",
Long: `Save each trigger in a realm to a local folder. Each trigger will
be saved in a dedicated file whose name will be in the form '<trigger_name>_v<version>.json'.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Triggers are not versioned

Suggested change
be saved in a dedicated file whose name will be in the form '<trigger_name>_v<version>.json'.
be saved in a dedicated file whose name will be in the form '<trigger_name>.json'.

}

var triggersSyncCmd = &cobra.Command{
Use: "sync <interface_files> [...]",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Use: "sync <interface_files> [...]",
Use: "sync <trigger_files> [...]",

}

func triggersShowF(command *cobra.Command, args []string) error {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove newline

cmd/realm/triggers.go Show resolved Hide resolved

fmt.Printf("\n")
fmt.Printf("The following new triggers will be installed: %+q \n", list)
fmt.Printf("\n")
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a \n at the end of the above print, no need for another

Copy link
Collaborator

Choose a reason for hiding this comment

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

up

return err
}

var astarteTrigger map[string]interface{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note: once astarte-go offers utilities for triggers, this will be something like map[string]triggers.Trigger{}

triggerDefinition, _ := getTriggerRes.Parse()
respJSON, _ := json.MarshalIndent(triggerDefinition, "", " ")
fmt.Println(string(respJSON))
triggerToInstall := []map[string]interface{}{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
triggerToInstall := []map[string]interface{}{}
triggersToInstall := []map[string]interface{}{}

respJSON, _ := json.MarshalIndent(triggerDefinition, "", " ")
fmt.Println(string(respJSON))
triggerToInstall := []map[string]interface{}{}
triggerToUpdate := []map[string]interface{}{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
triggerToUpdate := []map[string]interface{}{}
triggersToUpdate := []map[string]interface{}{}

fmt.Println(string(respJSON))
triggerToInstall := []map[string]interface{}{}
triggerToUpdate := []map[string]interface{}{}
invalidtrigger := []string{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
invalidtrigger := []string{}
invalidTriggers := []string{}

@eddbbt eddbbt force-pushed the master branch 3 times, most recently from 2f9c28b to 9197109 Compare November 16, 2023 13:42
@eddbbt eddbbt requested a review from Annopaolo November 16, 2023 13:58
@eddbbt eddbbt force-pushed the master branch 2 times, most recently from fec6e1a to da15c68 Compare November 17, 2023 09:36

RealmManagementCmd.AddCommand(triggersCmd)
triggersSyncCmd.Flags().Lookup("force").NoOptDefVal = "false"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that the flag should have NoOptDefVal set to true, so that one could write astartectl realm-management triggers sync ... --force (note the missing =true) and force the trigger synchronization

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, if you set the NoOptDefVal here rather than in the sync function, I think that the lookup is redundant: something like this could work, too:

	tiggersSyncCmd.PersistentFlags().BoolVar(...)
	tiggersSyncCmd.Flag("force").NoOptDefVal = "true"

}

// var astarteTrigger map[string]triggers.Trigger{}
var astarteTrigger map[string]interface{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should be a triggers.AstarteTrigger{} as your above comment suggests

return err
}

if _, err := getTriggerDefinition(realm, astarteTrigger["name"].(string)); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if _, err := getTriggerDefinition(realm, astarteTrigger["name"].(string)); err != nil {
if _, err := getTriggerDefinition(realm, astarteTrigger.name); err != nil {


fmt.Printf("\n")
fmt.Printf("The following new triggers will be installed: %+q \n", list)
fmt.Printf("\n")
Copy link
Collaborator

Choose a reason for hiding this comment

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

up

return nil
}

for _, v := range triggersToInstall {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since one of the fileds is used in the for body, give a meaningful name to v (e.g. trigger)

return nil
}

func updateTrigger(realm string, triggername string, newtrig map[string]interface{}) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func updateTrigger(realm string, triggername string, newtrig map[string]interface{}) error {
func updateTrigger(realm string, triggerName string, newTrigger triggers.AstarteTrigger) error {


_, _ = deleteTriggerRes.Parse()
func getTriggerDefinition(realm, triggerName string) (map[string]interface{}, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func getTriggerDefinition(realm, triggerName string) (map[string]interface{}, error) {
func getTriggerDefinition(realm, triggerName string) (triggers.AstarteTrigger, error) {

Comment on lines 264 to 266
for _, v := range triggersToInstall {
list = append(list, v["name"].(string))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
for _, v := range triggersToInstall {
list = append(list, v["name"].(string))
}
for _, trigger := range triggersToInstall {
list = append(list, trigger.name)
}

Comment on lines 227 to 230
triggersToInstall := []map[string]interface{}{}
triggersToUpdate := []map[string]interface{}{}
invalidTriggers := []string{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
triggersToInstall := []map[string]interface{}{}
triggersToUpdate := []map[string]interface{}{}
invalidTriggers := []string{}
triggersToInstall := []triggers.AstarteTrigger{}
triggersToUpdate := []triggers.AstarteTrigger{}
invalidTriggers := []string{}

Comment on lines 270 to 272
for _, v := range triggersToUpdate {
list_existing = append(list_existing, v["name"].(string))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comment

@eddbbt eddbbt force-pushed the master branch 5 times, most recently from f1170a9 to b8d5f59 Compare November 17, 2023 17:14
@eddbbt eddbbt force-pushed the master branch 4 times, most recently from 1e359d0 to 9114d2a Compare November 20, 2023 12:00
Copy link
Collaborator

@Annopaolo Annopaolo left a comment

Choose a reason for hiding this comment

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

Some nitpicking, but the PR is good!

@@ -5,6 +5,7 @@ astartectl_*
.gobuild
dist/
/result
.idea/
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this line is used, put this change in a separate commit

github.com/Azure/go-autorest/autorest/adal v0.9.13/go.mod h1:W/MM4U6nLxnIskrw4UwWzlHfGjwUS50aOsc/I3yuU8M=
github.com/Azure/go-autorest/autorest/adal v0.9.23 h1:Yepx8CvFxwNKpH6ja7RZ+sKX+DWYNldbLiALMC3BTz8=
github.com/Azure/go-autorest/autorest/adal v0.9.23/go.mod h1:5pcMqFkdPhviJdlEy3kC/v1ZLnQl0MH6XA5YCcMhy4c=
Copy link
Collaborator

Choose a reason for hiding this comment

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

Put dependency update in a different commit, too

list = append(list, trigger.Name)
}

list_existing := []string{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's keep naming consistency

Suggested change
list_existing := []string{}
listExisting := []string{}

Add trigger validation on sync, removing invalid payload from process queue

Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
Edit .gitignore file to remove ide temp folder

Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
Update project dependencies to latest

Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
@Annopaolo Annopaolo merged commit 000f486 into astarte-platform:master Nov 20, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants