Kaynağa Gözat

Choose accounts api host from environment

gabrielseibel1 2 yıl önce
ebeveyn
işleme
859319b775
3 değiştirilmiş dosya ile 99 ekleme ve 3 silme
  1. 12 1
      ee/license.go
  2. 74 0
      ee/license_test.go
  3. 13 2
      ee/types.go

+ 12 - 1
ee/license.go

@@ -186,7 +186,7 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
 		return nil, err
 	}
 
-	req, err := http.NewRequest(http.MethodPost, api_endpoint, bytes.NewReader(requestBody))
+	req, err := http.NewRequest(http.MethodPost, getAccountsHost()+"/api/v1/license/validate", bytes.NewReader(requestBody))
 	if err != nil {
 		return nil, err
 	}
@@ -217,6 +217,17 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
 	return body, err
 }
 
+func getAccountsHost() string {
+	switch servercfg.GetEnvironment() {
+	case "dev":
+		return accountsHostDevelopment
+	case "staging":
+		return accountsHostStaging
+	default:
+		return accountsHostProduction
+	}
+}
+
 func cacheResponse(response []byte) error {
 	var lrc = licenseResponseCache{
 		Body: response,

+ 74 - 0
ee/license_test.go

@@ -0,0 +1,74 @@
+package ee
+
+import (
+	"github.com/gravitl/netmaker/config"
+	"testing"
+)
+
+func Test_getAccountsHost(t *testing.T) {
+	tests := []struct {
+		name string
+		envK string
+		envV string
+		conf string
+		want string
+	}{
+		{
+			name: "no env var and no conf",
+			envK: "NOT_THE_CORRECT_ENV_VAR",
+			envV: "dev",
+			want: "https://api.accounts.netmaker.io",
+		},
+		{
+			name: "dev env var",
+			envK: "ENVIRONMENT",
+			envV: "dev",
+			want: "https://api.dev.accounts.netmaker.io",
+		},
+		{
+			name: "staging env var",
+			envK: "ENVIRONMENT",
+			envV: "staging",
+			want: "https://api.staging.accounts.netmaker.io",
+		},
+		{
+			name: "prod env var",
+			envK: "ENVIRONMENT",
+			envV: "prod",
+			want: "https://api.accounts.netmaker.io",
+		},
+		{
+			name: "dev conf",
+			conf: "dev",
+			want: "https://api.dev.accounts.netmaker.io",
+		},
+		{
+			name: "staging conf",
+			conf: "staging",
+			want: "https://api.staging.accounts.netmaker.io",
+		},
+		{
+			name: "prod conf",
+			conf: "prod",
+			want: "https://api.accounts.netmaker.io",
+		},
+		{
+			name: "env var vs conf precedence",
+			envK: "ENVIRONMENT",
+			envV: "prod",
+			conf: "staging",
+			want: "https://api.accounts.netmaker.io",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			config.Config.Server.Environment = tt.conf
+			if tt.envK != "" {
+				t.Setenv(tt.envK, tt.envV)
+			}
+			if got := getAccountsHost(); got != tt.want {
+				t.Errorf("getAccountsHost() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}

+ 13 - 2
ee/types.go

@@ -1,9 +1,20 @@
 package ee
 
-import "fmt"
+import (
+	"fmt"
+)
+
+// constants for accounts api hosts
+const (
+	// accountsHostDevelopment is the accounts api host for development environment
+	accountsHostDevelopment = "https://api.dev.accounts.netmaker.io"
+	// accountsHostStaging is the accounts api host for staging environment
+	accountsHostStaging = "https://api.staging.accounts.netmaker.io"
+	// accountsHostProduction is the accounts api host for production environment
+	accountsHostProduction = "https://api.accounts.netmaker.io"
+)
 
 const (
-	api_endpoint               = "https://api.accounts.netmaker.io/api/v1/license/validate"
 	license_cache_key          = "license_response_cache"
 	license_validation_err_msg = "invalid license"
 	server_id_key              = "nm-server-id"