Skip to content

Commit

Permalink
refactor: introduce api.Get function to replace api.NewAPIs()[]
Browse files Browse the repository at this point in the history
The `api.NewAPIs` function shouldn't be used so often as it is. The most obvious problem is the lack of filtration of unreleased endpoints, which leads to inconsistency.
  • Loading branch information
jskelin committed May 15, 2024
1 parent bbefb1c commit 77145f5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/monaco/integrationtest/v1/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func assertConfigAvailable(t *testing.T, ctx context.Context, client client.Conf
typ, ok := c.Type.(config.ClassicApiType)
assert.True(t, ok, "Config %s should be a ClassicApiType, but is a %q", c.Coordinate, c.Type.ID())

a, found := api.NewAPIs()[typ.Api]
a, found := api.Get(typ.Api)
assert.True(t, found, "Config %s should have a known api, but does not. Api %s does not exist", c.Coordinate, typ.Api)

if c.Skip {
Expand Down
12 changes: 0 additions & 12 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ import (

const StandardApiPropertyNameOfGetAllResponse string = "values"

type Config struct {
configType string
configId string
}

func (p Config) Type() string {
return p.configType
}
func (p Config) Id() string {
return p.configId
}

// API structure present definition of config endpoints
type API struct {
ID string
Expand Down
22 changes: 22 additions & 0 deletions pkg/api/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* @license
* Copyright 2024 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package api

func Get(name string) (API, bool) {
a, ok := NewAPIs().Filter(RemoveDisabled)[name]
return a, ok
}
40 changes: 40 additions & 0 deletions pkg/api/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @license
* Copyright 2024 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package api_test

import (
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/api"
"github.com/stretchr/testify/require"
)

func Test_Get(t *testing.T) {
t.Run("positive case", func(t *testing.T) {
a, ok := api.Get(api.ApplicationWeb)

require.True(t, ok)
require.Equal(t, api.ApplicationWeb, a.ID)
})

t.Run("negative case", func(t *testing.T) {
a, ok := api.Get("doesn't exists")

require.False(t, ok)
require.Equal(t, "", a.ID)
})
}
4 changes: 2 additions & 2 deletions pkg/delete/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func convert(entry interface{}) (pointer.DeletePointer, error) {
if parsed.Type == "" {
return pointer.DeletePointer{}, errors.New("'type' is not supported for this API")
}
if a, known := api.NewAPIs()[parsed.Type]; known {
if a, known := api.Get(parsed.Type); known {
if err := verifyAPIEntry(parsed, a); err != nil {
return pointer.DeletePointer{}, fmt.Errorf("failed to parse entry for API '%s': %w", a.ID, err)
}
Expand All @@ -154,7 +154,7 @@ func convert(entry interface{}) (pointer.DeletePointer, error) {
Domain: parsed.CustomValues["domain"],
OriginObjectId: parsed.ObjectId,
}
if _, known := api.NewAPIs()[parsed.Type]; known {
if _, known := api.Get(parsed.Type); known {
dp.Identifier = parsed.ConfigName
} else {
dp.Identifier = parsed.ConfigId
Expand Down

0 comments on commit 77145f5

Please sign in to comment.