From 1509a70521b7896a3468b7921650c0a38a83a1b1 Mon Sep 17 00:00:00 2001 From: Pierre G Date: Thu, 19 Dec 2024 12:28:15 +0100 Subject: [PATCH] feat: read admin url from envirnment variable to be able to target dev core (#401) --- cmd/admin_cluster_deploy.go | 2 +- cmd/admin_cluster_list.go | 2 +- cmd/admin_demo_get_logs.go | 2 +- cmd/admin_demo_list_logs.go | 2 +- cmd/admin_jw_qovery_usage_create.go | 4 +--- cmd/admin_jw_qovery_usage_delete.go | 4 +--- cmd/admin_jw_qovery_usage_list.go | 7 ++----- cmd/admin_jwt_create.go | 4 +--- cmd/admin_jwt_delete.go | 4 +--- cmd/admin_jwt_list.go | 4 +--- cmd/admin_k9s.go | 2 +- cmd/admin_notify_users_cluster_failure.go | 2 +- pkg/admin_cluster_services.go | 13 ++++++++----- pkg/admin_environment_deployment_rules.go | 4 ++-- pkg/admin_notify_users_cluster_failure.go | 2 +- pkg/delete_cluster.go | 9 +++------ pkg/delete_orga.go | 4 ++-- pkg/delete_project.go | 4 +--- pkg/deploy.go | 2 +- pkg/download_s3_archive.go | 4 +--- pkg/lock.go | 10 +++++----- pkg/update.go | 6 ++---- utils/qovery.go | 10 +++++----- 23 files changed, 44 insertions(+), 63 deletions(-) diff --git a/cmd/admin_cluster_deploy.go b/cmd/admin_cluster_deploy.go index ad7f1725..c65ee99c 100644 --- a/cmd/admin_cluster_deploy.go +++ b/cmd/admin_cluster_deploy.go @@ -98,7 +98,7 @@ func init() { } func deployClusters() { - utils.CheckAdminUrl() + utils.GetAdminUrl() // if no filter is set, enforce to select only RUNNING clusters to avoid mistakes (e.g deploying a stopped cluster) _, containsKey := filters["CurrentStatus"] diff --git a/cmd/admin_cluster_list.go b/cmd/admin_cluster_list.go index c1d895ff..5c3f17da 100644 --- a/cmd/admin_cluster_list.go +++ b/cmd/admin_cluster_list.go @@ -53,7 +53,7 @@ func init() { } func listClusters() { - utils.CheckAdminUrl() + utils.GetAdminUrl() listService, err := pkg.NewAdminClusterListServiceImpl(filters) if err != nil { diff --git a/cmd/admin_demo_get_logs.go b/cmd/admin_demo_get_logs.go index 4158637b..d7b21047 100644 --- a/cmd/admin_demo_get_logs.go +++ b/cmd/admin_demo_get_logs.go @@ -36,7 +36,7 @@ var ( os.Exit(1) } - url := fmt.Sprintf("%s/demoDebugLog", utils.AdminUrl) + url := fmt.Sprintf("%s/demoDebugLog", utils.GetAdminUrl()) req, _ := http.NewRequest(http.MethodGet, url, bytes.NewReader([]byte{})) query := req.URL.Query() query.Add("filename", args[0]) diff --git a/cmd/admin_demo_list_logs.go b/cmd/admin_demo_list_logs.go index fa064c6a..00750752 100644 --- a/cmd/admin_demo_list_logs.go +++ b/cmd/admin_demo_list_logs.go @@ -31,7 +31,7 @@ var ( os.Exit(1) } - url := fmt.Sprintf("%s/demoDebugLog", utils.AdminUrl) + url := fmt.Sprintf("%s/demoDebugLog", utils.GetAdminUrl()) req, _ := http.NewRequest(http.MethodGet, url, bytes.NewReader([]byte{})) query := req.URL.Query() orgaId, _ := cmd.Flags().GetString("organizationId") diff --git a/cmd/admin_jw_qovery_usage_create.go b/cmd/admin_jw_qovery_usage_create.go index 4dd28693..72fea09d 100644 --- a/cmd/admin_jw_qovery_usage_create.go +++ b/cmd/admin_jw_qovery_usage_create.go @@ -35,8 +35,6 @@ func init() { } func createJwtForQoveryUsage() { - utils.CheckAdminUrl() - tokenType, token, err := utils.GetAccessToken() if err != nil { utils.PrintlnError(err) @@ -66,7 +64,7 @@ func createJwtForQoveryUsage() { Description: description, }) - url := fmt.Sprintf("%s/jwts", utils.AdminUrl) + url := fmt.Sprintf("%s/jwts", utils.GetAdminUrl()) req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(payload)) if err != nil { log.Fatal(err) diff --git a/cmd/admin_jw_qovery_usage_delete.go b/cmd/admin_jw_qovery_usage_delete.go index 0b913e64..0313e5b7 100644 --- a/cmd/admin_jw_qovery_usage_delete.go +++ b/cmd/admin_jw_qovery_usage_delete.go @@ -28,15 +28,13 @@ func init() { } func deleteJwtForQoveryUsage() { - utils.CheckAdminUrl() - tokenType, token, err := utils.GetAccessToken() if err != nil { utils.PrintlnError(err) os.Exit(0) } - url := fmt.Sprintf("%s/jwts/%s", utils.AdminUrl, jwtKid) + url := fmt.Sprintf("%s/jwts/%s", utils.GetAdminUrl(), jwtKid) req, err := http.NewRequest(http.MethodDelete, url, nil) if err != nil { log.Fatal(err) diff --git a/cmd/admin_jw_qovery_usage_list.go b/cmd/admin_jw_qovery_usage_list.go index 880c8e35..ff6ee4f0 100644 --- a/cmd/admin_jw_qovery_usage_list.go +++ b/cmd/admin_jw_qovery_usage_list.go @@ -32,15 +32,13 @@ func init() { } func listJwtsForQoveryUsage() { - utils.CheckAdminUrl() - tokenType, token, err := utils.GetAccessToken() if err != nil { utils.PrintlnError(err) os.Exit(0) } - url := fmt.Sprintf("%s/jwts", utils.AdminUrl) + url := fmt.Sprintf("%s/jwts", utils.GetAdminUrl()) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Fatal(err) @@ -72,13 +70,12 @@ func listJwtsForQoveryUsage() { } w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) - format := "%s\t | %s\t | %s\t | %s\t | %s\n" - _, _ = fmt.Fprintf(w, format, "", "key_id", "descripton", "jwt payload", "created_at") for idx, jwtForQoveryUsage := range resp.Results { _, jwtPayload, err := DecodeJWT(jwtForQoveryUsage.Jwt) if err != nil { log.Fatal(err) } + _, _ = fmt.Fprintln(w, "\t") _, _ = fmt.Fprintln(w, "Field\t | Value") _, _ = fmt.Fprintln(w, "------\t | ------") diff --git a/cmd/admin_jwt_create.go b/cmd/admin_jwt_create.go index 8392d84e..478c7dbb 100644 --- a/cmd/admin_jwt_create.go +++ b/cmd/admin_jwt_create.go @@ -32,15 +32,13 @@ func init() { } func createJwt() { - utils.CheckAdminUrl() - tokenType, token, err := utils.GetAccessToken() if err != nil { utils.PrintlnError(err) os.Exit(0) } - url := fmt.Sprintf("%s/clusters/%s/jwts", utils.AdminUrl, clusterId) + url := fmt.Sprintf("%s/clusters/%s/jwts", utils.GetAdminUrl(), clusterId) req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte("{ }"))) if err != nil { log.Fatal(err) diff --git a/cmd/admin_jwt_delete.go b/cmd/admin_jwt_delete.go index 1f376e77..8325038d 100644 --- a/cmd/admin_jwt_delete.go +++ b/cmd/admin_jwt_delete.go @@ -28,15 +28,13 @@ func init() { } func deleteJwt() { - utils.CheckAdminUrl() - tokenType, token, err := utils.GetAccessToken() if err != nil { utils.PrintlnError(err) os.Exit(0) } - url := fmt.Sprintf("%s/clusters/jwts/%s", utils.AdminUrl, jwtKid) + url := fmt.Sprintf("%s/clusters/jwts/%s", utils.GetAdminUrl(), jwtKid) req, err := http.NewRequest(http.MethodDelete, url, nil) if err != nil { log.Fatal(err) diff --git a/cmd/admin_jwt_list.go b/cmd/admin_jwt_list.go index 0aec9b43..3fcf7df0 100644 --- a/cmd/admin_jwt_list.go +++ b/cmd/admin_jwt_list.go @@ -31,15 +31,13 @@ func init() { } func listJwts() { - utils.CheckAdminUrl() - tokenType, token, err := utils.GetAccessToken() if err != nil { utils.PrintlnError(err) os.Exit(0) } - url := fmt.Sprintf("%s/clusters/%s/jwts", utils.AdminUrl, clusterId) + url := fmt.Sprintf("%s/clusters/%s/jwts", utils.GetAdminUrl(), clusterId) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Fatal(err) diff --git a/cmd/admin_k9s.go b/cmd/admin_k9s.go index dd7b9a5b..7693cd1e 100644 --- a/cmd/admin_k9s.go +++ b/cmd/admin_k9s.go @@ -195,7 +195,7 @@ func getClusterCredentials(clusterId string) []utils.Var { os.Exit(0) } - url := fmt.Sprintf("%s/cluster/%s/credential", utils.AdminUrl, clusterId) + url := fmt.Sprintf("%s/cluster/%s/credential", utils.GetAdminUrl(), clusterId) req, err := http.NewRequest(http.MethodGet, url, bytes.NewBuffer([]byte("{}"))) if err != nil { log.Fatal(err) diff --git a/cmd/admin_notify_users_cluster_failure.go b/cmd/admin_notify_users_cluster_failure.go index 0eaed6e5..ed62395e 100644 --- a/cmd/admin_notify_users_cluster_failure.go +++ b/cmd/admin_notify_users_cluster_failure.go @@ -29,7 +29,7 @@ func init() { } func notifyUsersClusterFailure() { - utils.CheckAdminUrl() + utils.GetAdminUrl() err := pkg.NotifyUsersClusterFailure(&clusterId) if err != nil { diff --git a/pkg/admin_cluster_services.go b/pkg/admin_cluster_services.go index 90d4caa5..2185139b 100644 --- a/pkg/admin_cluster_services.go +++ b/pkg/admin_cluster_services.go @@ -144,7 +144,7 @@ func (service AdminClusterListServiceImpl) fetchClustersEligibleToUpdate() ([]Cl return nil, err } - req, err := http.NewRequest(http.MethodGet, utils.AdminUrl+"/listClustersEligibleToUpdate", nil) + req, err := http.NewRequest(http.MethodGet, utils.GetAdminUrl()+"/listClustersEligibleToUpdate", nil) if err != nil { return nil, err } @@ -452,10 +452,11 @@ func (service AdminClusterBatchDeployServiceImpl) Deploy(clusters []ClusterDetai } func (service AdminClusterBatchDeployServiceImpl) deployCluster(clusterId string, dryRunDisabled bool) error { - response := execAdminRequest(utils.AdminUrl+"/cluster/deploy/"+clusterId, http.MethodPost, dryRunDisabled, map[string]string{}) + adminUrl := utils.GetAdminUrl() + response := execAdminRequest(adminUrl+"/cluster/deploy/"+clusterId, http.MethodPost, dryRunDisabled, map[string]string{}) if response.StatusCode == 401 { DoRequestUserToAuthenticate(false) - response = execAdminRequest(utils.AdminUrl+"/cluster/deploy/"+clusterId, http.MethodPost, dryRunDisabled, map[string]string{}) + response = execAdminRequest(adminUrl+"/cluster/deploy/"+clusterId, http.MethodPost, dryRunDisabled, map[string]string{}) } if response.StatusCode != 200 { result, _ := io.ReadAll(response.Body) @@ -471,8 +472,10 @@ func (service AdminClusterBatchDeployServiceImpl) upgradeCluster(clusterId strin os.Exit(0) } + adminUrl := utils.GetAdminUrl() + body := bytes.NewBuffer([]byte(fmt.Sprintf("{ \"metadata\": { \"dry_run_deploy\": \"%s\", \"target_version\": \"%s\" } }", strconv.FormatBool(!dryRunDisabled), targetVersion))) - request, err := http.NewRequest(http.MethodPost, utils.AdminUrl+"/cluster/update/"+clusterId, body) + request, err := http.NewRequest(http.MethodPost, adminUrl+"/cluster/update/"+clusterId, body) if err != nil { return err } @@ -487,7 +490,7 @@ func (service AdminClusterBatchDeployServiceImpl) upgradeCluster(clusterId strin if response.StatusCode == 401 { DoRequestUserToAuthenticate(false) - request, err = http.NewRequest(http.MethodPost, utils.AdminUrl+"/cluster/update/"+clusterId, body) + request, err = http.NewRequest(http.MethodPost, adminUrl+"/cluster/update/"+clusterId, body) if err != nil { return err } diff --git a/pkg/admin_environment_deployment_rules.go b/pkg/admin_environment_deployment_rules.go index 1a914498..e477df0e 100644 --- a/pkg/admin_environment_deployment_rules.go +++ b/pkg/admin_environment_deployment_rules.go @@ -10,7 +10,7 @@ import ( ) func PublishEnvironmentDeploymentRules() error { - utils.CheckAdminUrl() + utils.GetAdminUrl() utils.Println("Publishing environment deployment rules to scheduler...") err := callPublishEnvironmentDeploymentRulesApi() @@ -28,7 +28,7 @@ func callPublishEnvironmentDeploymentRulesApi() error { os.Exit(0) } - url := fmt.Sprintf("%s/environmentDeploymentRules/pushToScheduler", utils.AdminUrl) + url := fmt.Sprintf("%s/environmentDeploymentRules/pushToScheduler", utils.GetAdminUrl()) req, err := http.NewRequest(http.MethodPost, url, nil) if err != nil { log.Fatal(err) diff --git a/pkg/admin_notify_users_cluster_failure.go b/pkg/admin_notify_users_cluster_failure.go index ecec7ad7..91555d58 100644 --- a/pkg/admin_notify_users_cluster_failure.go +++ b/pkg/admin_notify_users_cluster_failure.go @@ -19,7 +19,7 @@ func NotifyUsersClusterFailure(clusterId *string) error { body = `{"all_failing_clusters": true}` } - notifiedClustersResponse, err := postWithBody(utils.AdminUrl+"/cluster/notifyFailedClustersAdmins", body) + notifiedClustersResponse, err := postWithBody(utils.GetAdminUrl()+"/cluster/notifyFailedClustersAdmins", body) if err != nil { return err } diff --git a/pkg/delete_cluster.go b/pkg/delete_cluster.go index 03bfd6ca..2dd40577 100644 --- a/pkg/delete_cluster.go +++ b/pkg/delete_cluster.go @@ -14,11 +14,10 @@ import ( ) func DeleteClusterById(clusterId string, dryRunDisabled bool) { - utils.CheckAdminUrl() utils.DryRunPrint(dryRunDisabled) if utils.Validate("delete") { - res := httpDelete(utils.AdminUrl+"/cluster/"+clusterId, http.MethodDelete, dryRunDisabled) + res := httpDelete(utils.GetAdminUrl()+"/cluster/"+clusterId, http.MethodDelete, dryRunDisabled) if !dryRunDisabled { fmt.Println("Cluster with id " + clusterId + " deletable.") @@ -31,10 +30,9 @@ func DeleteClusterById(clusterId string, dryRunDisabled bool) { } } func DeleteClusterUnDeployedInError() { - utils.CheckAdminUrl() if utils.Validate("delete") { - res := httpDelete(utils.AdminUrl+"/cluster/deleteNotDeployedInErrorClusters", http.MethodPost, true) + res := httpDelete(utils.GetAdminUrl()+"/cluster/deleteNotDeployedInErrorClusters", http.MethodPost, true) if !strings.Contains(res.Status, "200") { result, _ := io.ReadAll(res.Body) @@ -47,7 +45,6 @@ func DeleteClusterUnDeployedInError() { } func DeleteOldClustersWithInvalidCredentials(ageInDay int, dryRunDisabled bool) { - utils.CheckAdminUrl() if utils.Validate("delete") { @@ -62,7 +59,7 @@ func DeleteOldClustersWithInvalidCredentials(ageInDay int, dryRunDisabled bool) return } - res := deleteWithBody(utils.AdminUrl+"/cluster/deleteOldClustersWithInvalidCredentials", http.MethodPost, true, bytes.NewBuffer(requestBody)) + res := deleteWithBody(utils.GetAdminUrl()+"/cluster/deleteOldClustersWithInvalidCredentials", http.MethodPost, true, bytes.NewBuffer(requestBody)) if !strings.Contains(res.Status, "200") { result, _ := io.ReadAll(res.Body) diff --git a/pkg/delete_orga.go b/pkg/delete_orga.go index 4cd84d21..361459b5 100644 --- a/pkg/delete_orga.go +++ b/pkg/delete_orga.go @@ -13,11 +13,11 @@ import ( ) func DeleteOrganizationByClusterId(clusterId string, dryRunDisabled bool) { - utils.CheckAdminUrl() + utils.GetAdminUrl() utils.DryRunPrint(dryRunDisabled) if utils.Validate("delete") { - res := httpDelete(utils.AdminUrl+"/organization?clusterId="+clusterId, http.MethodDelete, dryRunDisabled) + res := httpDelete(utils.GetAdminUrl()+"/organization?clusterId="+clusterId, http.MethodDelete, dryRunDisabled) if !dryRunDisabled { fmt.Println("Organization owning cluster" + clusterId + " deletable.") diff --git a/pkg/delete_project.go b/pkg/delete_project.go index 474cd0c8..e36341e6 100644 --- a/pkg/delete_project.go +++ b/pkg/delete_project.go @@ -12,11 +12,9 @@ import ( ) func DeleteProjectById(projectId string, dryRunDisabled bool) { - utils.CheckAdminUrl() - utils.DryRunPrint(dryRunDisabled) if utils.Validate("delete") { - res := httpDelete(utils.AdminUrl+"/project/"+projectId, http.MethodDelete, dryRunDisabled) + res := httpDelete(utils.GetAdminUrl()+"/project/"+projectId, http.MethodDelete, dryRunDisabled) if !dryRunDisabled { fmt.Println("Project with id " + projectId + " deletable.") diff --git a/pkg/deploy.go b/pkg/deploy.go index 6d2cb0d0..dcc4a646 100644 --- a/pkg/deploy.go +++ b/pkg/deploy.go @@ -57,7 +57,7 @@ func ForceFailedDeploymentsToInternalErrorStatus(safeguardDuration time.Duration durationIso8601 := fmt.Sprintf("PT%dM", nbMinutes) queryParams := map[string]string{"safeguardDuration": durationIso8601} - res := execAdminRequest(utils.AdminUrl+"/deployment/forceFailedDeploymentsToInternalErrorStatus", http.MethodPost, true, queryParams) + res := execAdminRequest(utils.GetAdminUrl()+"/deployment/forceFailedDeploymentsToInternalErrorStatus", http.MethodPost, true, queryParams) if !strings.Contains(res.Status, "200") { result, _ := io.ReadAll(res.Body) log.Errorf("Could not force the deployments status : %s. %s", res.Status, string(result)) diff --git a/pkg/download_s3_archive.go b/pkg/download_s3_archive.go index 7eabe8a5..eb02abab 100644 --- a/pkg/download_s3_archive.go +++ b/pkg/download_s3_archive.go @@ -25,10 +25,8 @@ type ArchiveResponse struct { } func DownloadS3Archive(executionId string, directory string) { - utils.CheckAdminUrl() - fileName := executionId + ".tgz" - res := download(utils.AdminUrl+"/getS3ArchiveObject", fileName) + res := download(utils.GetAdminUrl()+"/getS3ArchiveObject", fileName) if !strings.Contains(res.Status, "200") { result, _ := io.ReadAll(res.Body) diff --git a/pkg/lock.go b/pkg/lock.go index e92500bb..778eff59 100644 --- a/pkg/lock.go +++ b/pkg/lock.go @@ -15,7 +15,7 @@ import ( ) func LockedClusters() { - utils.CheckAdminUrl() + utils.GetAdminUrl() res := listLockedClusters() @@ -53,7 +53,7 @@ func LockedClusters() { } func LockById(clusterId string, reason string) { - utils.CheckAdminUrl() + utils.GetAdminUrl() if reason == "" { log.Errorf("Lock reason is required") @@ -73,7 +73,7 @@ func LockById(clusterId string, reason string) { } func UnockById(clusterId string) { - utils.CheckAdminUrl() + utils.GetAdminUrl() if utils.Validate("unlock") { res := updateLockById(clusterId, "", http.MethodDelete) @@ -94,7 +94,7 @@ func listLockedClusters() *http.Response { os.Exit(0) } - url := fmt.Sprintf("%s/cluster/lock", utils.AdminUrl) + url := fmt.Sprintf("%s/cluster/lock", utils.GetAdminUrl()) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Fatal(err) @@ -125,7 +125,7 @@ func updateLockById(clusterId string, reason string, method string) *http.Respon log.Fatal(err) } - url := fmt.Sprintf("%s/cluster/lock/%s", utils.AdminUrl, clusterId) + url := fmt.Sprintf("%s/cluster/lock/%s", utils.GetAdminUrl(), clusterId) req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) if err != nil { log.Fatal(err) diff --git a/pkg/update.go b/pkg/update.go index 6b4bd76b..51adcd76 100644 --- a/pkg/update.go +++ b/pkg/update.go @@ -12,11 +12,10 @@ import ( ) func UpdateById(clusterId string, dryRunDisabled bool, version string) { - utils.CheckAdminUrl() utils.DryRunPrint(dryRunDisabled) if utils.Validate("update") { - res := update(utils.AdminUrl+"/cluster/update/"+clusterId, http.MethodPost, dryRunDisabled, version, "", 0) + res := update(utils.GetAdminUrl()+"/cluster/update/"+clusterId, http.MethodPost, dryRunDisabled, version, "", 0) if !strings.Contains(res.Status, "200") { result, _ := io.ReadAll(res.Body) @@ -30,11 +29,10 @@ func UpdateById(clusterId string, dryRunDisabled bool, version string) { } func UpdateAll(dryRunDisabled bool, version string, providerKind string, parallelRun int) { - utils.CheckAdminUrl() utils.DryRunPrint(dryRunDisabled) if utils.Validate("update") { - res := update(utils.AdminUrl+"/cluster/update", http.MethodPost, dryRunDisabled, version, providerKind, parallelRun) + res := update(utils.GetAdminUrl()+"/cluster/update", http.MethodPost, dryRunDisabled, version, providerKind, parallelRun) result, _ := io.ReadAll(res.Body) if strings.Contains(res.Status, "40") || strings.Contains(res.Status, "50") { log.Errorf("Could not update clusters : %s. %s", res.Status, string(result)) diff --git a/utils/qovery.go b/utils/qovery.go index fda23d13..e42e7406 100644 --- a/utils/qovery.go +++ b/utils/qovery.go @@ -42,8 +42,6 @@ type Role struct { Name Name } -const AdminUrl = "https://api-admin.qovery.com" - func WebsocketUrl() string { if url := os.Getenv("QOVERY_WS_URL"); url != "" { return url @@ -781,12 +779,14 @@ func GetJobById(id string) (*Job, error) { return nil, errors.New("Invalid job response") } -func CheckAdminUrl() { - if _, ok := os.LookupEnv("ADMIN_URL"); !ok { - log.Error("You must set the Qovery admin root url (ADMIN_URL).") +func GetAdminUrl() string { + url, ok := os.LookupEnv("ADMIN_URL") + if !ok { + log.Fatal("You must set the Qovery admin root url (ADMIN_URL).") os.Exit(1) panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + return url } func DeleteEnvironmentVariable(application Id, key string) error {