-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: addition of applens client and detector endpoints
- Loading branch information
Zach Jones
committed
Jul 12, 2022
1 parent
7439c4b
commit 5807b66
Showing
26 changed files
with
2,030 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package frontend | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
"github.com/Azure/ARO-RP/pkg/database/cosmosdb" | ||
"github.com/Azure/ARO-RP/pkg/frontend/adminactions" | ||
"github.com/Azure/ARO-RP/pkg/frontend/middleware" | ||
) | ||
|
||
func (f *frontend) listAppLensDetectors(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry) | ||
r.URL.Path = filepath.Dir(r.URL.Path) | ||
|
||
b, err := f._listAppLensDetectors(ctx, r, log) | ||
|
||
reply(log, w, nil, b, err) | ||
} | ||
|
||
func (f *frontend) _listAppLensDetectors(ctx context.Context, r *http.Request, log *logrus.Entry) ([]byte, error) { | ||
a, err := f._createAzureActionsFactory(ctx, r, log) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return a.AppLensListDetectors(ctx) | ||
} | ||
|
||
func (f *frontend) getAppLensDetector(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry) | ||
r.URL.Path = filepath.Dir(r.URL.Path) | ||
|
||
b, err := f._appLensDetectors(ctx, r, log) | ||
|
||
reply(log, w, nil, b, err) | ||
} | ||
|
||
func (f *frontend) _appLensDetectors(ctx context.Context, r *http.Request, log *logrus.Entry) ([]byte, error) { | ||
a, err := f._createAzureActionsFactory(ctx, r, log) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vars := mux.Vars(r) | ||
return a.AppLensGetDetector(ctx, vars["detectorId"]) | ||
} | ||
|
||
func (f *frontend) _createAzureActionsFactory(ctx context.Context, r *http.Request, log *logrus.Entry) (adminactions.AzureActions, error) { | ||
vars := mux.Vars(r) | ||
|
||
resourceID := strings.TrimSuffix(r.URL.Path, "/detectors") | ||
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID) | ||
switch { | ||
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound): | ||
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s' under resource group '%s' was not found.", vars["resourceType"], vars["resourceName"], vars["resourceGroupName"]) | ||
case err != nil: | ||
return nil, err | ||
} | ||
|
||
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return a, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package frontend | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
"github.com/Azure/ARO-RP/pkg/env" | ||
"github.com/Azure/ARO-RP/pkg/frontend/adminactions" | ||
"github.com/Azure/ARO-RP/pkg/metrics/noop" | ||
mock_adminactions "github.com/Azure/ARO-RP/pkg/util/mocks/adminactions" | ||
) | ||
|
||
func TestAppLensDetectors(t *testing.T) { | ||
mockSubID := "00000000-0000-0000-0000-000000000000" | ||
mockTenantID := "00000000-0000-0000-0000-000000000000" | ||
ctx := context.Background() | ||
|
||
type test struct { | ||
name string | ||
resourceID string | ||
detectorID string | ||
getDetector bool | ||
mocks func(*test, *mock_adminactions.MockAzureActions) | ||
method string | ||
wantStatusCode int | ||
wantResponse []byte | ||
wantError string | ||
} | ||
|
||
for _, tt := range []*test{ | ||
{ | ||
method: http.MethodGet, | ||
name: "list applens detectors", | ||
resourceID: fmt.Sprintf("/subscriptions/%s/resourcegroups/resourceGroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/resourceName", mockSubID), | ||
detectorID: "", | ||
getDetector: false, | ||
mocks: func(tt *test, a *mock_adminactions.MockAzureActions) { | ||
a.EXPECT(). | ||
AppLensListDetectors(gomock.Any()). | ||
Return([]byte(`{"Kind": "test"}`), nil) | ||
}, | ||
wantStatusCode: http.StatusOK, | ||
wantResponse: []byte(`{"Kind": "test"}` + "\n"), | ||
}, | ||
{ | ||
method: http.MethodGet, | ||
name: "get applens detector", | ||
resourceID: fmt.Sprintf("/subscriptions/%s/resourcegroups/resourceGroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/resourceName", mockSubID), | ||
detectorID: "testdetector", | ||
getDetector: true, | ||
mocks: func(tt *test, a *mock_adminactions.MockAzureActions) { | ||
a.EXPECT(). | ||
AppLensGetDetector(gomock.Any(), tt.detectorID). | ||
Return([]byte(`{"Kind": "test"}`), nil) | ||
}, | ||
wantStatusCode: http.StatusOK, | ||
wantResponse: []byte(`{"Kind": "test"}` + "\n"), | ||
}, | ||
{ | ||
method: http.MethodGet, | ||
name: "no applens detector specified", | ||
resourceID: fmt.Sprintf("/subscriptions/%s/resourcegroups/resourceGroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/resourceName", mockSubID), | ||
detectorID: "", | ||
getDetector: true, | ||
mocks: func(tt *test, a *mock_adminactions.MockAzureActions) { | ||
}, | ||
wantStatusCode: http.StatusNotFound, | ||
wantError: "404: NotFound: : The requested path could not be found.", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ti := newTestInfra(t).WithSubscriptions().WithOpenShiftClusters() | ||
defer ti.done() | ||
|
||
a := mock_adminactions.NewMockAzureActions(ti.controller) | ||
tt.mocks(tt, a) | ||
|
||
ti.fixture.AddOpenShiftClusterDocuments(&api.OpenShiftClusterDocument{ | ||
Key: strings.ToLower(tt.resourceID), | ||
OpenShiftCluster: &api.OpenShiftCluster{ | ||
ID: tt.resourceID, | ||
Name: "resourceName", | ||
Type: "Microsoft.RedHatOpenShift/openshiftClusters", | ||
}, | ||
}) | ||
ti.fixture.AddSubscriptionDocuments(&api.SubscriptionDocument{ | ||
ID: mockSubID, | ||
Subscription: &api.Subscription{ | ||
State: api.SubscriptionStateRegistered, | ||
Properties: &api.SubscriptionProperties{ | ||
TenantID: mockTenantID, | ||
}, | ||
}, | ||
}) | ||
|
||
err := ti.buildFixtures(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, api.APIs, &noop.Noop{}, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster, *api.SubscriptionDocument) (adminactions.AzureActions, error) { | ||
return a, nil | ||
}, nil) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
go f.Run(ctx, nil, nil) | ||
|
||
finalURL := fmt.Sprintf("https://server%s/detectors", tt.resourceID) | ||
if tt.getDetector { | ||
finalURL = fmt.Sprintf("%s/%s", finalURL, tt.detectorID) | ||
} | ||
|
||
resp, b, err := ti.request(http.MethodGet, finalURL, nil, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = validateResponse(resp, b, tt.wantStatusCode, tt.wantError, tt.wantResponse) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.