From cd54e1f2a7cb5c2d0445eaf4c6baf685a8526884 Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Thu, 15 May 2025 13:50:12 +0200 Subject: [PATCH 1/4] Add tag and message to deployments in CI --- cmd/bundle.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd/bundle.go b/cmd/bundle.go index 169f110f..59029550 100644 --- a/cmd/bundle.go +++ b/cmd/bundle.go @@ -97,6 +97,16 @@ Examples: } } + if ci { + if ciMessage, _ := f.GetString("ci-message"); ciMessage != "" { + args = append(args, "--message", ciMessage) + } + if ciCommit, _ := f.GetString("ci-commit"); ciCommit != "" { + args = append(args, "--tag", ciCommit) + } + args = append(args, "--tag", "latest") + } + started = time.Now() projectContext.Logger.Trace("deploying to cloud with %s and args: %v", bin, args) cwd, err := os.Getwd() From 41e1a7070578bc9251900dd869a61caf043fb260 Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Thu, 15 May 2025 23:00:06 +0200 Subject: [PATCH 2/4] [AGENT-179] Call the agent from the corrent endpoint --- cmd/agent.go | 35 ++++++++++++++++++++++++++++++----- cmd/bundle.go | 1 - internal/agent/agent.go | 7 ++++--- internal/project/project.go | 7 ++++--- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/cmd/agent.go b/cmd/agent.go index e7ab7dee..90d4eede 100644 --- a/cmd/agent.go +++ b/cmd/agent.go @@ -717,7 +717,8 @@ var agentTestCmd = &cobra.Command{ payload, _ := cmd.Flags().GetString("payload") local, _ := cmd.Flags().GetBool("local") contentType, _ := cmd.Flags().GetString("content-type") - route, _ := cmd.Flags().GetString("route") + tag, _ := cmd.Flags().GetString("tag") + var selectedAgent *agent.Agent if agentID == "" { keys, state := reconcileAgentList(logger, cmd, theproject.APIURL, theproject.Token, theproject) if len(keys) == 0 { @@ -728,14 +729,34 @@ var agentTestCmd = &cobra.Command{ var options []tui.Option for _, v := range keys { options = append(options, tui.Option{ - ID: state[v].Agent.ID, + ID: v, Text: tui.PadRight(state[v].Agent.Name, 20, " ") + tui.Muted(state[v].Agent.ID), }) } - agentID = tui.Select(logger, "Select an agent", "Select the agent you want to test", options) + selected := tui.Select(logger, "Select an agent", "Select the agent you want to test", options) + selectedAgent = state[selected].Agent + agentID = selectedAgent.ID agentID = strings.Replace(agentID, "agent_", "", 1) } + if len(selectedAgent.IOTypes) == 0 { + // error out + logger.Fatal("Agent %s has no IO types", selectedAgent.Name) + } + var route string + if len(selectedAgent.IOTypes) > 1 { + options := []tui.Option{} + for _, route := range selectedAgent.IOTypes { + options = append(options, tui.Option{ + ID: route, + Text: route, + }) + } + route = tui.Select(logger, "Select an IO type", "Select the IO type you want to use", options) + } else { + route = selectedAgent.IOTypes[0] + } + if payload == "" { payload = tui.Input(logger, "Enter the payload to send to the agent", "{\"hello\": \"world\"}") } @@ -750,6 +771,10 @@ var agentTestCmd = &cobra.Command{ endpoint = fmt.Sprintf("http://localhost:%d/agent_%s", port, agentID) } + if tag != "" { + endpoint = fmt.Sprintf("%s/%s", endpoint, tag) + } + // use http package to send a POST request to the agent req, err := http.NewRequest("POST", endpoint, strings.NewReader(payload)) if err != nil { @@ -768,6 +793,7 @@ var agentTestCmd = &cobra.Command{ if apikey != "" { req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey)) } + resp, err := http.DefaultClient.Do(req) if err != nil { logger.Fatal("Failed to send request: %s", err) @@ -798,8 +824,7 @@ func init() { agentTestCmd.Flags().String("payload", "", "The payload to send to the agent") agentTestCmd.Flags().Bool("local", false, "Enable local testing") agentTestCmd.Flags().String("content-type", "", "The content type to use for the request, will try to detect if not provided") - agentTestCmd.Flags().String("route", "api", "The route to use for the request when --local is false, can be either 'api' or 'webhook'") - + agentTestCmd.Flags().String("tag", "", "The tag to use for the deployment") agentCmd.AddCommand(agentTestCmd) for _, cmd := range []*cobra.Command{agentListCmd, agentCreateCmd, agentDeleteCmd, agentGetApiKeyCmd, agentTestCmd} { diff --git a/cmd/bundle.go b/cmd/bundle.go index 59029550..bf9b70c5 100644 --- a/cmd/bundle.go +++ b/cmd/bundle.go @@ -104,7 +104,6 @@ Examples: if ciCommit, _ := f.GetString("ci-commit"); ciCommit != "" { args = append(args, "--tag", ciCommit) } - args = append(args, "--tag", "latest") } started = time.Now() diff --git a/internal/agent/agent.go b/internal/agent/agent.go index d7586486..bbe3231c 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -10,9 +10,10 @@ import ( ) type Agent struct { - ID string `json:"id" yaml:"id"` - Name string `json:"name" yaml:"name"` - Description string `json:"description,omitempty" yaml:"description,omitempty"` + ID string `json:"id" yaml:"id"` + Name string `json:"name" yaml:"name"` + Description string `json:"description,omitempty" yaml:"description,omitempty"` + IOTypes []string `json:"io_types,omitempty" yaml:"io_types,omitempty"` } type Response[T any] struct { diff --git a/internal/project/project.go b/internal/project/project.go index 2d285777..497b0836 100644 --- a/internal/project/project.go +++ b/internal/project/project.go @@ -135,9 +135,10 @@ type Development struct { } type AgentConfig struct { - ID string `json:"id" yaml:"id" hc:"The ID of the Agent which is automatically generated"` - Name string `json:"name" yaml:"name" hc:"The name of the Agent which is editable"` - Description string `json:"description,omitempty" yaml:"description,omitempty" hc:"The description of the Agent which is editable"` + ID string `json:"id" yaml:"id" hc:"The ID of the Agent which is automatically generated"` + Name string `json:"name" yaml:"name" hc:"The name of the Agent which is editable"` + Description string `json:"description,omitempty" yaml:"description,omitempty" hc:"The description of the Agent which is editable"` + IOTypes []string `json:"io_types,omitempty" yaml:"io_types,omitempty" hc:"The IO types of the Agent which is editable"` } type Project struct { From a7fbe5c6dce0b28c25ebc260621a0665eb79c358 Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Thu, 15 May 2025 23:40:47 +0200 Subject: [PATCH 3/4] fixed not-found issue --- cmd/agent.go | 19 +++++++++---------- cmd/cloud.go | 2 +- internal/agent/agent.go | 6 +++--- internal/project/project.go | 2 +- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/cmd/agent.go b/cmd/agent.go index 90d4eede..830c5517 100644 --- a/cmd/agent.go +++ b/cmd/agent.go @@ -680,7 +680,7 @@ Examples: tui.ShowWarning("Agent not found") return } - apikey, err := agent.GetApiKey(context.Background(), logger, apiUrl, project.Token, theagent.Agent.ID) + apikey, err := agent.GetApiKey(context.Background(), logger, apiUrl, project.Token, theagent.Agent.ID, theagent.Agent.Types[0]) if err != nil { errsystem.New(errsystem.ErrApiRequest, err, errsystem.WithContextMessage("Failed to get agent API key")).ShowErrorAndExit() } @@ -736,39 +736,38 @@ var agentTestCmd = &cobra.Command{ selected := tui.Select(logger, "Select an agent", "Select the agent you want to test", options) selectedAgent = state[selected].Agent agentID = selectedAgent.ID - agentID = strings.Replace(agentID, "agent_", "", 1) } - if len(selectedAgent.IOTypes) == 0 { + if len(selectedAgent.Types) == 0 { // error out - logger.Fatal("Agent %s has no IO types", selectedAgent.Name) + logger.Fatal("Agent %s has no running types (webhook or api)", selectedAgent.Name) } var route string - if len(selectedAgent.IOTypes) > 1 { + if len(selectedAgent.Types) > 1 { options := []tui.Option{} - for _, route := range selectedAgent.IOTypes { + for _, route := range selectedAgent.Types { options = append(options, tui.Option{ ID: route, Text: route, }) } - route = tui.Select(logger, "Select an IO type", "Select the IO type you want to use", options) + route = tui.Select(logger, "Select an running type", "Select the running type you want to use", options) } else { - route = selectedAgent.IOTypes[0] + route = selectedAgent.Types[0] } if payload == "" { payload = tui.Input(logger, "Enter the payload to send to the agent", "{\"hello\": \"world\"}") } - apikey, err := agent.GetApiKey(context.Background(), logger, theproject.APIURL, theproject.Token, agentID) + apikey, err := agent.GetApiKey(context.Background(), logger, theproject.APIURL, theproject.Token, agentID, route) if err != nil { errsystem.New(errsystem.ErrApiRequest, err, errsystem.WithContextMessage("Failed to get agent API key")).ShowErrorAndExit() } endpoint := fmt.Sprintf("%s/%s/%s", theproject.TransportURL, route, agentID) if local { port, _ := dev.FindAvailablePort(theproject) - endpoint = fmt.Sprintf("http://localhost:%d/agent_%s", port, agentID) + endpoint = fmt.Sprintf("http://localhost:%d/%s", port, agentID) } if tag != "" { diff --git a/cmd/cloud.go b/cmd/cloud.go index df83474d..128226ca 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -617,7 +617,7 @@ Examples: errsystem.WithContextMessage("Error updating deployment status to completed")).ShowErrorAndExit() } if len(theproject.Agents) == 1 { - webhookToken, err = agent.GetApiKey(ctx, logger, apiUrl, token, theproject.Agents[0].ID) + webhookToken, err = agent.GetApiKey(ctx, logger, apiUrl, token, theproject.Agents[0].ID, theproject.Agents[0].Types[0]) if err != nil { errsystem.New(errsystem.ErrApiRequest, err, errsystem.WithContextMessage("Error getting Agent API key")).ShowErrorAndExit() diff --git a/internal/agent/agent.go b/internal/agent/agent.go index bbe3231c..5cfda3a7 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -13,7 +13,7 @@ type Agent struct { ID string `json:"id" yaml:"id"` Name string `json:"name" yaml:"name"` Description string `json:"description,omitempty" yaml:"description,omitempty"` - IOTypes []string `json:"io_types,omitempty" yaml:"io_types,omitempty"` + Types []string `json:"io_types,omitempty" yaml:"io_types,omitempty"` } type Response[T any] struct { @@ -75,11 +75,11 @@ type AgentAPIKey struct { Config map[string]any `json:"config"` } -func GetApiKey(ctx context.Context, logger logger.Logger, baseUrl string, token string, agentId string) (string, error) { +func GetApiKey(ctx context.Context, logger logger.Logger, baseUrl string, token string, agentId string, route string) (string, error) { client := util.NewAPIClient(ctx, logger, baseUrl, token) var resp Response[*AgentAPIKey] - if err := client.Do("GET", fmt.Sprintf("/cli/agent/%s/io/source/webhook", url.PathEscape(agentId)), nil, &resp); err != nil { + if err := client.Do("GET", fmt.Sprintf("/cli/agent/%s/io/source/%s", url.PathEscape(agentId), route), nil, &resp); err != nil { return "", fmt.Errorf("error getting Agent API key: %s", err) } diff --git a/internal/project/project.go b/internal/project/project.go index 497b0836..2e981dc4 100644 --- a/internal/project/project.go +++ b/internal/project/project.go @@ -138,7 +138,7 @@ type AgentConfig struct { ID string `json:"id" yaml:"id" hc:"The ID of the Agent which is automatically generated"` Name string `json:"name" yaml:"name" hc:"The name of the Agent which is editable"` Description string `json:"description,omitempty" yaml:"description,omitempty" hc:"The description of the Agent which is editable"` - IOTypes []string `json:"io_types,omitempty" yaml:"io_types,omitempty" hc:"The IO types of the Agent which is editable"` + Types []string `json:"io_types,omitempty" yaml:"io_types,omitempty" hc:"The IO types of the Agent which is editable"` } type Project struct { From 096a76908cef854832ee7beee9099104e9677c89 Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Fri, 16 May 2025 09:57:52 +0200 Subject: [PATCH 4/4] check localhost to 127 and replaced error message with tui --- cmd/agent.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/agent.go b/cmd/agent.go index 830c5517..5edd9dcf 100644 --- a/cmd/agent.go +++ b/cmd/agent.go @@ -739,8 +739,9 @@ var agentTestCmd = &cobra.Command{ } if len(selectedAgent.Types) == 0 { - // error out - logger.Fatal("Agent %s has no running types (webhook or api)", selectedAgent.Name) + // this should never ever happen + tui.ShowError("Agent %s has no running types (webhook or api)", selectedAgent.Name) + os.Exit(1) } var route string if len(selectedAgent.Types) > 1 { @@ -767,7 +768,7 @@ var agentTestCmd = &cobra.Command{ endpoint := fmt.Sprintf("%s/%s/%s", theproject.TransportURL, route, agentID) if local { port, _ := dev.FindAvailablePort(theproject) - endpoint = fmt.Sprintf("http://localhost:%d/%s", port, agentID) + endpoint = fmt.Sprintf("http://127.0.0.1:%d/%s", port, agentID) } if tag != "" {