Skip to content

Commit

Permalink
Review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
toszr committed Feb 28, 2022
1 parent 0a39ab5 commit 99313f8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 74 deletions.
11 changes: 8 additions & 3 deletions src/api/v1beta1/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"

"github.com/Dynatrace/dynatrace-operator/src/dtclient"
"github.com/Dynatrace/dynatrace-operator/src/util"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -256,19 +255,25 @@ func (dk *DynaKube) TenantUUID() (string, error) {
return tenantUUID(dk.Spec.APIURL)
}

func runeIs(wanted rune) func(rune) bool {
return func(actual rune) bool {
return actual == wanted
}
}

func tenantUUID(apiUrl string) (string, error) {
parsedUrl, err := url.Parse(apiUrl)
if err != nil {
return "", errors.WithMessagef(err, "problem parsing tenant id from url %s", apiUrl)
}

// Path = "/e/<token>/api" -> ["e", "<tenant>", "api"]
subPaths := util.Tokenize(parsedUrl.Path, '/')
subPaths := strings.FieldsFunc(parsedUrl.Path, runeIs('/'))
if len(subPaths) >= 3 && subPaths[0] == "e" && subPaths[2] == "api" {
return subPaths[1], nil
}

hostnameWithDomains := util.Tokenize(parsedUrl.Hostname(), '.')
hostnameWithDomains := strings.FieldsFunc(parsedUrl.Hostname(), runeIs('.'))
if len(hostnameWithDomains) >= 1 {
return hostnameWithDomains[0], nil

Expand Down
20 changes: 10 additions & 10 deletions src/api/v1beta1/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func TestTokens(t *testing.T) {

func TestTenantUUID(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
const apiUrl = "https://demo.dev.dynatracelabs.com/api"
const expectedTenantId = "demo"
apiUrl := "https://demo.dev.dynatracelabs.com/api"
expectedTenantId := "demo"

actualTenantId, err := tenantUUID(apiUrl)

Expand All @@ -129,8 +129,8 @@ func TestTenantUUID(t *testing.T) {
})

t.Run("happy path (alternative)", func(t *testing.T) {
const apiUrl = "https://dynakube-activegate.dynatrace/e/tenant/api/v2/metrics/ingest"
const expectedTenantId = "tenant"
apiUrl := "https://dynakube-activegate.dynatrace/e/tenant/api/v2/metrics/ingest"
expectedTenantId := "tenant"

actualTenantId, err := tenantUUID(apiUrl)

Expand All @@ -141,9 +141,9 @@ func TestTenantUUID(t *testing.T) {
})

t.Run("missing API URL protocol", func(t *testing.T) {
const apiUrl = "demo.dev.dynatracelabs.com/api"
const expectedTenantId = ""
const expectedError = "problem getting tenant id from API URL 'demo.dev.dynatracelabs.com/api'"
apiUrl := "demo.dev.dynatracelabs.com/api"
expectedTenantId := ""
expectedError := "problem getting tenant id from API URL 'demo.dev.dynatracelabs.com/api'"

actualTenantId, err := tenantUUID(apiUrl)

Expand All @@ -156,9 +156,9 @@ func TestTenantUUID(t *testing.T) {
})

t.Run("suffix-only, relative API URL", func(t *testing.T) {
const apiUrl = "/api"
const expectedTenantId = ""
const expectedError = "problem getting tenant id from API URL '/api'"
apiUrl := "/api"
expectedTenantId := ""
expectedError := "problem getting tenant id from API URL '/api'"

actualTenantId, err := tenantUUID(apiUrl)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
"github.com/Dynatrace/dynatrace-operator/src/controllers/activegate/capability"
"github.com/Dynatrace/dynatrace-operator/src/kubeobjects"
"github.com/Dynatrace/dynatrace-operator/src/util/address_of"
"github.com/Dynatrace/dynatrace-operator/src/kubeobjects/address_of"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
"github.com/Dynatrace/dynatrace-operator/src/controllers/activegate/capability"
"github.com/Dynatrace/dynatrace-operator/src/kubeobjects"
"github.com/Dynatrace/dynatrace-operator/src/util/address_of"
"github.com/Dynatrace/dynatrace-operator/src/kubeobjects/address_of"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 0 additions & 13 deletions src/util/strings.go

This file was deleted.

46 changes: 0 additions & 46 deletions src/util/strings_test.go

This file was deleted.

0 comments on commit 99313f8

Please sign in to comment.