From 1c4fa8b1b464005eee34060bba3841bbb67ed0f4 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Thu, 2 Nov 2023 17:16:09 +0300 Subject: [PATCH] Update constant packages with the new names and unify all http headers in one place Signed-off-by: Burak Sekili --- data/config.go | 4 +--- data/constants.go | 6 +++++- helpers/organisation.go | 12 ++++++------ helpers/portal.go | 11 +++++------ helpers/user.go | 12 ++++++------ predelete/predelete.go | 7 +++---- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/data/config.go b/data/config.go index 398487f..a02823d 100644 --- a/data/config.go +++ b/data/config.go @@ -3,8 +3,6 @@ package data import ( "context" "fmt" - "tyk/tyk/bootstrap/constants" - "github.com/kelseyhightower/envconfig" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -126,7 +124,7 @@ func discoverDashboardSvc() (string, error) { } ls := metav1.LabelSelector{MatchLabels: map[string]string{ - constants.TykBootstrapLabel: constants.TykBootstrapDashboardSvcLabel, + TykBootstrapLabel: TykBootstrapDashboardSvcLabel, }} l := labels.Set(ls.MatchLabels).String() diff --git a/data/constants.go b/data/constants.go index 14997ee..251d83e 100644 --- a/data/constants.go +++ b/data/constants.go @@ -1,8 +1,12 @@ -package constants +package data const ( TykBootstrapLabel = "tyk.tyk.io/k8s-bootstrap" TykBootstrapPreDeleteLabel = "tyk-k8s-bootstrap-pre-delete" TykBootstrapDashboardDeployLabel = "tyk-dashboard" TykBootstrapDashboardSvcLabel = "tyk-dashboard" + + AuthorizationHeader = "Authorization" + ContentTypeHeader = "Content-Type" + AdminAuthHeader = "admin-auth" ) diff --git a/helpers/organisation.go b/helpers/organisation.go index 3e9f832..54e29df 100644 --- a/helpers/organisation.go +++ b/helpers/organisation.go @@ -31,13 +31,13 @@ func CheckForExistingOrganisation(client http.Client) error { orgsApiEndpoint := data.BootstrapConf.K8s.DashboardSvcUrl + AdminOrganisationsEndpoint - req, err := http.NewRequest("GET", orgsApiEndpoint, nil) + req, err := http.NewRequest(http.MethodGet, orgsApiEndpoint, nil) if err != nil { return err } - req.Header.Set("admin-auth", data.BootstrapConf.Tyk.Admin.Secret) - req.Header.Set("Content-Type", "application/json") + req.Header.Set(data.AdminAuthHeader, data.BootstrapConf.Tyk.Admin.Secret) + req.Header.Set(data.ContentTypeHeader, "application/json") res, err := client.Do(req) if err != nil { @@ -91,13 +91,13 @@ func CreateOrganisation(client http.Client, dashBoardUrl string) (string, error) return "", err } - req, err := http.NewRequest("POST", dashBoardUrl+AdminOrganisationsEndpoint, bytes.NewReader(reqBodyBytes)) + req, err := http.NewRequest(http.MethodPost, dashBoardUrl+AdminOrganisationsEndpoint, bytes.NewReader(reqBodyBytes)) if err != nil { return "", err } - req.Header.Set("admin-auth", data.BootstrapConf.Tyk.Admin.Secret) - req.Header.Set("Content-Type", "application/json") + req.Header.Set(data.AdminAuthHeader, data.BootstrapConf.Tyk.Admin.Secret) + req.Header.Set(data.ContentTypeHeader, "application/json") res, err := client.Do(req) if err != nil { diff --git a/helpers/portal.go b/helpers/portal.go index 37cd0c7..ab498f8 100644 --- a/helpers/portal.go +++ b/helpers/portal.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "time" - "tyk/tyk/bootstrap/constants" "tyk/tyk/bootstrap/data" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -70,7 +69,7 @@ func SetPortalCname(client http.Client) error { return err } - req.Header.Set("Authorization", data.BootstrapConf.Tyk.UserAuth) + req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth) res, err := client.Do(req) if err != nil { @@ -104,7 +103,7 @@ func InitialiseCatalogue(client http.Client) error { return err } - req.Header.Set("Authorization", data.BootstrapConf.Tyk.UserAuth) + req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth) res, err := client.Do(req) if err != nil || res.StatusCode != http.StatusOK { @@ -143,7 +142,7 @@ func CreatePortalHomepage(client http.Client) error { return err } - req.Header.Set("Authorization", data.BootstrapConf.Tyk.UserAuth) + req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth) res, err := client.Do(req) if err != nil || res.StatusCode != http.StatusOK { @@ -232,7 +231,7 @@ func CreatePortalDefaultSettings(client http.Client) error { data.BootstrapConf.K8s.DashboardSvcUrl+ApiPortalConfigurationEndpoint, nil, ) - req.Header.Set("Authorization", data.BootstrapConf.Tyk.UserAuth) + req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth) if err != nil { return err @@ -259,7 +258,7 @@ func RestartDashboard() error { if data.BootstrapConf.K8s.DashboardDeploymentName == "" { ls := metav1.LabelSelector{MatchLabels: map[string]string{ - constants.TykBootstrapLabel: constants.TykBootstrapDashboardDeployLabel, + data.TykBootstrapLabel: data.TykBootstrapDashboardDeployLabel, }} deployments, err := clientset. diff --git a/helpers/user.go b/helpers/user.go index 1717405..bc97bb7 100644 --- a/helpers/user.go +++ b/helpers/user.go @@ -43,15 +43,15 @@ func SetUserPassword(client http.Client, userId, authCode, dashboardUrl string) } req, err := http.NewRequest( - "POST", + http.MethodPost, fmt.Sprintf(ApiUsersActionsResetEndpoint, dashboardUrl, userId), bytes.NewReader(reqBody)) if err != nil { return err } - req.Header.Set("authorization", authCode) - req.Header.Set("Content-Type", "application/json") + req.Header.Set(data.AuthorizationHeader, authCode) + req.Header.Set(data.ContentTypeHeader, "application/json") res, err := client.Do(req) if err != nil { @@ -135,13 +135,13 @@ func GetUserData(client http.Client, dashboardUrl, orgId string) (NeededUserData return NeededUserData{}, err } - req, err := http.NewRequest("POST", dashboardUrl+"/admin/users", bytes.NewReader(reqBytes)) + req, err := http.NewRequest(http.MethodPost, dashboardUrl+"/admin/users", bytes.NewReader(reqBytes)) if err != nil { return NeededUserData{}, err } - req.Header.Set("admin-auth", data.BootstrapConf.Tyk.Admin.Secret) - req.Header.Set("Content-Type", "application/json") + req.Header.Set(data.AdminAuthHeader, data.BootstrapConf.Tyk.Admin.Secret) + req.Header.Set(data.ContentTypeHeader, "application/json") res, err := client.Do(req) if err != nil { diff --git a/predelete/predelete.go b/predelete/predelete.go index ef1d5a6..2ac7e47 100644 --- a/predelete/predelete.go +++ b/predelete/predelete.go @@ -3,7 +3,6 @@ package predelete import ( "context" "fmt" - "tyk/tyk/bootstrap/constants" "tyk/tyk/bootstrap/data" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -126,7 +125,7 @@ func PreDeleteBootstrappingJobs(clientset *kubernetes.Clientset) error { List( context.TODO(), metav1.ListOptions{ - LabelSelector: constants.TykBootstrapLabel, + LabelSelector: data.TykBootstrapLabel, }, ) if err != nil { @@ -139,8 +138,8 @@ func PreDeleteBootstrappingJobs(clientset *kubernetes.Clientset) error { job := jobs.Items[i] // Do not need to delete pre-delete job. It will be deleted by Helm. - jobLabel := job.ObjectMeta.Labels[constants.TykBootstrapLabel] - if jobLabel != constants.TykBootstrapPreDeleteLabel { + jobLabel := job.ObjectMeta.Labels[data.TykBootstrapLabel] + if jobLabel != data.TykBootstrapPreDeleteLabel { deletePropagationType := metav1.DeletePropagationBackground err2 := clientset.