diff options
author | Martin Polden <mpolden@mpolden.no> | 2023-08-22 13:07:56 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2023-08-23 09:05:53 +0200 |
commit | da92d923ce927d7b1b4587c649dbbf01f048fd51 (patch) | |
tree | 8079d952485c5b1b0bb71d902d9d1fe11895c120 | |
parent | 14f78d0376bbfc84b1ca13bb57558cbc9eadba48 (diff) |
Build URLs in one place
-rw-r--r-- | client/go/internal/cli/cmd/deploy.go | 6 | ||||
-rw-r--r-- | client/go/internal/cli/cmd/status.go | 6 | ||||
-rw-r--r-- | client/go/internal/vespa/deploy.go | 62 | ||||
-rw-r--r-- | client/go/internal/vespa/system.go | 59 | ||||
-rw-r--r-- | client/go/internal/vespa/target_cloud.go | 10 | ||||
-rw-r--r-- | client/go/internal/vespa/target_test.go | 1 |
6 files changed, 94 insertions, 50 deletions
diff --git a/client/go/internal/cli/cmd/deploy.go b/client/go/internal/cli/cmd/deploy.go index cf2e435fea5..fbd0529101f 100644 --- a/client/go/internal/cli/cmd/deploy.go +++ b/client/go/internal/cli/cmd/deploy.go @@ -93,11 +93,7 @@ $ vespa deploy -t cloud -z perf.aws-us-east-1c`, } if opts.Target.IsCloud() { log.Printf("\nUse %s for deployment status, or follow this deployment at", color.CyanString("vespa status deployment")) - log.Print(color.CyanString(fmt.Sprintf("%s/tenant/%s/application/%s/%s/instance/%s/job/%s-%s/run/%d", - opts.Target.Deployment().System.ConsoleURL, - opts.Target.Deployment().Application.Tenant, opts.Target.Deployment().Application.Application, opts.Target.Deployment().Zone.Environment, - opts.Target.Deployment().Application.Instance, opts.Target.Deployment().Zone.Environment, opts.Target.Deployment().Zone.Region, - result.ID))) + log.Print(color.CyanString(opts.Target.Deployment().System.ConsoleRunURL(opts.Target.Deployment(), result.ID))) } return waitForDeploymentReady(cli, target, result.ID, timeout) }, diff --git a/client/go/internal/cli/cmd/status.go b/client/go/internal/cli/cmd/status.go index f185fde6ca1..b88db6e0d0b 100644 --- a/client/go/internal/cli/cmd/status.go +++ b/client/go/internal/cli/cmd/status.go @@ -123,11 +123,7 @@ $ vespa status deployment -t local [session-id] } if t.IsCloud() { log.Printf("Deployment run %s has completed", color.CyanString(strconv.FormatInt(id, 10))) - log.Printf("See %s for more details", color.CyanString(fmt.Sprintf("%s/tenant/%s/application/%s/%s/instance/%s/job/%s-%s/run/%d", - t.Deployment().System.ConsoleURL, - t.Deployment().Application.Tenant, t.Deployment().Application.Application, t.Deployment().Zone.Environment, - t.Deployment().Application.Instance, t.Deployment().Zone.Environment, t.Deployment().Zone.Region, - id))) + log.Printf("See %s for more details", color.CyanString(t.Deployment().System.ConsoleRunURL(t.Deployment(), id))) } else { log.Printf("Deployment is %s on config generation %s", color.GreenString("ready"), color.CyanString(strconv.FormatInt(id, 10))) } diff --git a/client/go/internal/vespa/deploy.go b/client/go/internal/vespa/deploy.go index 3a3af0d66a0..1bfaf641243 100644 --- a/client/go/internal/vespa/deploy.go +++ b/client/go/internal/vespa/deploy.go @@ -189,25 +189,25 @@ func Activate(sessionID int64, deployment DeploymentOptions) error { } // Deactivate given deployment -func Deactivate(opts DeploymentOptions) error { - path := "/application/v2/tenant/default/application/default" - if opts.Target.IsCloud() { - if opts.Target.Deployment().Zone.Environment == "" || opts.Target.Deployment().Zone.Region == "" { - return fmt.Errorf("%s: missing zone", opts) +func Deactivate(deployment DeploymentOptions) error { + var ( + u *url.URL + err error + ) + if deployment.Target.IsCloud() { + if deployment.Target.Deployment().Zone.Environment == "" || deployment.Target.Deployment().Zone.Region == "" { + return fmt.Errorf("%s: missing zone", deployment) } - path = fmt.Sprintf("/application/v4/tenant/%s/application/%s/instance/%s/environment/%s/region/%s", - opts.Target.Deployment().Application.Tenant, - opts.Target.Deployment().Application.Application, - opts.Target.Deployment().Application.Instance, - opts.Target.Deployment().Zone.Environment, - opts.Target.Deployment().Zone.Region) - } - u, err := opts.url(path) + deploymentURL := deployment.Target.Deployment().System.DeploymentURL(deployment.Target.Deployment()) + u, err = url.Parse(deploymentURL) + } else { + u, err = deployment.url("/application/v2/tenant/default/application/default") + } if err != nil { return err } req := &http.Request{URL: u, Method: "DELETE"} - resp, err := deployServiceDo(req, 30*time.Second, opts) + resp, err := deployServiceDo(req, 30*time.Second, deployment) if err != nil { return err } @@ -215,27 +215,27 @@ func Deactivate(opts DeploymentOptions) error { return checkResponse(req, resp) } -func Deploy(opts DeploymentOptions) (PrepareResult, error) { - path := "/application/v2/tenant/default/prepareandactivate" - if opts.Target.IsCloud() { - if err := checkDeploymentOpts(opts); err != nil { +// Deploy deploys an application. +func Deploy(deployment DeploymentOptions) (PrepareResult, error) { + var ( + u *url.URL + err error + ) + if deployment.Target.IsCloud() { + if err := checkDeploymentOpts(deployment); err != nil { return PrepareResult{}, err } - if opts.Target.Deployment().Zone.Environment == "" || opts.Target.Deployment().Zone.Region == "" { - return PrepareResult{}, fmt.Errorf("%s: missing zone", opts) + if deployment.Target.Deployment().Zone.Environment == "" || deployment.Target.Deployment().Zone.Region == "" { + return PrepareResult{}, fmt.Errorf("%s: missing zone", deployment) } - path = fmt.Sprintf("/application/v4/tenant/%s/application/%s/instance/%s/deploy/%s-%s", - opts.Target.Deployment().Application.Tenant, - opts.Target.Deployment().Application.Application, - opts.Target.Deployment().Application.Instance, - opts.Target.Deployment().Zone.Environment, - opts.Target.Deployment().Zone.Region) - } - u, err := opts.url(path) + u, err = url.Parse(deployment.Target.Deployment().System.DeployURL(deployment.Target.Deployment())) + } else { + u, err = deployment.url("/application/v2/tenant/default/prepareandactivate") + } if err != nil { return PrepareResult{}, err } - return uploadApplicationPackage(u, opts) + return uploadApplicationPackage(u, deployment) } func copyToPart(dst *multipart.Writer, src io.Reader, fieldname, filename string) error { @@ -262,8 +262,8 @@ func Submit(opts DeploymentOptions, submission Submission) error { if err := checkDeploymentOpts(opts); err != nil { return err } - path := fmt.Sprintf("/application/v4/tenant/%s/application/%s/submit", opts.Target.Deployment().Application.Tenant, opts.Target.Deployment().Application.Application) - u, err := opts.url(path) + submitURL := opts.Target.Deployment().System.SubmitURL(opts.Target.Deployment()) + u, err := opts.url(submitURL) if err != nil { return err } diff --git a/client/go/internal/vespa/system.go b/client/go/internal/vespa/system.go index 96795cc0ef8..cdf401bf43c 100644 --- a/client/go/internal/vespa/system.go +++ b/client/go/internal/vespa/system.go @@ -56,7 +56,64 @@ type System struct { } // IsPublic returns whether system s is a public (Vespa Cloud) system. -func (s *System) IsPublic() bool { return s.Name == PublicSystem.Name || s.Name == PublicCDSystem.Name } +func (s System) IsPublic() bool { return s.Name == PublicSystem.Name || s.Name == PublicCDSystem.Name } + +// DeployURL returns the API URL to use for deploying to this system. +func (s System) DeployURL(deployment Deployment) string { + return fmt.Sprintf("%s/application/v4/tenant/%s/application/%s/instance/%s/deploy/%s", + s.URL, + deployment.Application.Tenant, + deployment.Application.Application, + deployment.Application.Instance, + jobName(deployment.Zone)) +} + +// SubmitURL returns the API URL for submitting an application package for production deployment. +func (s System) SubmitURL(deployment Deployment) string { + return fmt.Sprintf("%s/application/v4/tenant/%s/application/%s/submit", s.URL, deployment.Application.Tenant, deployment.Application.Application) +} + +// DeploymentURL returns the API URL of given deployment. +func (s System) DeploymentURL(deployment Deployment) string { + return fmt.Sprintf("%s/application/v4/tenant/%s/application/%s/instance/%s/environment/%s/region/%s", + s.URL, + deployment.Application.Tenant, + deployment.Application.Application, + deployment.Application.Instance, + deployment.Zone.Environment, + deployment.Zone.Region) +} + +// RunURL returns the API URL for a given deployment job run. +func (s System) RunURL(deployment Deployment, id int64) string { + return fmt.Sprintf("%s/application/v4/tenant/%s/application/%s/instance/%s/job/%s/run/%d", + s.URL, + deployment.Application.Tenant, deployment.Application.Application, deployment.Application.Instance, + jobName(deployment.Zone), id) +} + +// RunsURL returns the API URL listing all runs for given deployment. +func (s System) RunsURL(deployment Deployment) string { + return fmt.Sprintf("%s/application/v4/tenant/%s/application/%s/instance/%s/job/%s", + s.URL, + deployment.Application.Tenant, deployment.Application.Application, deployment.Application.Instance, + jobName(deployment.Zone)) +} + +// ConsoleRunURL returns the console URL for a deployment job run in this system. +func (s System) ConsoleRunURL(deployment Deployment, run int64) string { + return fmt.Sprintf("%s/tenant/%s/application/%s/%s/instance/%s/job/%s/run/%d", + s.ConsoleURL, deployment.Application.Tenant, deployment.Application.Application, deployment.Zone.Environment, + deployment.Application.Instance, jobName(deployment.Zone), run) +} + +func jobName(zone ZoneID) string { + env := zone.Environment + if env == "prod" { + env = "production" + } + return env + "-" + zone.Region +} // GetSystem returns the system of given name. func GetSystem(name string) (System, error) { diff --git a/client/go/internal/vespa/target_cloud.go b/client/go/internal/vespa/target_cloud.go index 24133ba5fc3..bc4934ea6fd 100644 --- a/client/go/internal/vespa/target_cloud.go +++ b/client/go/internal/vespa/target_cloud.go @@ -246,10 +246,7 @@ func (t *cloudTarget) deployServiceWait(fn responseFunc, reqFn requestFunc, time } func (t *cloudTarget) discoverLatestRun(timeout time.Duration) (int64, error) { - runsURL := fmt.Sprintf("%s/application/v4/tenant/%s/application/%s/instance/%s/job/%s-%s?limit=1", - t.apiOptions.System.URL, - t.deploymentOptions.Deployment.Application.Tenant, t.deploymentOptions.Deployment.Application.Application, t.deploymentOptions.Deployment.Application.Instance, - t.deploymentOptions.Deployment.Zone.Environment, t.deploymentOptions.Deployment.Zone.Region) + runsURL := t.apiOptions.System.RunsURL(t.deploymentOptions.Deployment) + "?limit=1" req, err := http.NewRequest("GET", runsURL, nil) if err != nil { return 0, err @@ -282,10 +279,7 @@ func (t *cloudTarget) AwaitDeployment(runID int64, timeout time.Duration) (int64 } runID = lastRunID } - runURL := fmt.Sprintf("%s/application/v4/tenant/%s/application/%s/instance/%s/job/%s-%s/run/%d", - t.apiOptions.System.URL, - t.deploymentOptions.Deployment.Application.Tenant, t.deploymentOptions.Deployment.Application.Application, t.deploymentOptions.Deployment.Application.Instance, - t.deploymentOptions.Deployment.Zone.Environment, t.deploymentOptions.Deployment.Zone.Region, runID) + runURL := t.apiOptions.System.RunURL(t.deploymentOptions.Deployment, runID) req, err := http.NewRequest("GET", runURL, nil) if err != nil { return 0, err diff --git a/client/go/internal/vespa/target_test.go b/client/go/internal/vespa/target_test.go index b208489ddce..68b60774c94 100644 --- a/client/go/internal/vespa/target_test.go +++ b/client/go/internal/vespa/target_test.go @@ -253,6 +253,7 @@ func createCloudTarget(t *testing.T, logWriter io.Writer) (Target, *mock.HTTPCli Deployment: Deployment{ Application: ApplicationID{Tenant: "t1", Application: "a1", Instance: "i1"}, Zone: ZoneID{Environment: "dev", Region: "us-north-1"}, + System: PublicSystem, }, }, LogOptions{Writer: logWriter}, |