-
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.
* master resize GA
- Loading branch information
Showing
14 changed files
with
1,078 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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/middleware" | ||
) | ||
|
||
func (f *frontend) postAdminOpenShiftClusterStartVM(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry) | ||
r.URL.Path = filepath.Dir(r.URL.Path) | ||
|
||
err := f._postAdminOpenShiftClusterStartVM(ctx, r, log) | ||
|
||
adminReply(log, w, nil, nil, err) | ||
} | ||
|
||
func (f *frontend) _postAdminOpenShiftClusterStartVM(ctx context.Context, r *http.Request, log *logrus.Entry) error { | ||
vars := mux.Vars(r) | ||
|
||
vmName := r.URL.Query().Get("vmName") | ||
err := validateAdminVMName(vmName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceID := strings.TrimPrefix(r.URL.Path, "/admin") | ||
|
||
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID) | ||
switch { | ||
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound): | ||
return 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 err | ||
} | ||
|
||
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return a.VMStartAndWait(ctx, vmName) | ||
} |
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,110 @@ | ||
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" | ||
testdatabase "github.com/Azure/ARO-RP/test/database" | ||
) | ||
|
||
func TestAdminStartVM(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 | ||
fixture func(*testdatabase.Fixture) | ||
vmName string | ||
mocks func(*test, *mock_adminactions.MockAzureActions) | ||
wantStatusCode int | ||
wantResponse []byte | ||
wantError string | ||
} | ||
|
||
for _, tt := range []*test{ | ||
{ | ||
name: "basic coverage", | ||
vmName: "aro-worker-australiasoutheast-7tcq7", | ||
resourceID: testdatabase.GetResourcePath(mockSubID, "resourceName"), | ||
fixture: func(f *testdatabase.Fixture) { | ||
f.AddOpenShiftClusterDocuments(&api.OpenShiftClusterDocument{ | ||
Key: strings.ToLower(testdatabase.GetResourcePath(mockSubID, "resourceName")), | ||
OpenShiftCluster: &api.OpenShiftCluster{ | ||
ID: testdatabase.GetResourcePath(mockSubID, "resourceName"), | ||
Properties: api.OpenShiftClusterProperties{ | ||
ClusterProfile: api.ClusterProfile{ | ||
ResourceGroupID: fmt.Sprintf("/subscriptions/%s/resourceGroups/test-cluster", mockSubID), | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
f.AddSubscriptionDocuments(&api.SubscriptionDocument{ | ||
ID: mockSubID, | ||
Subscription: &api.Subscription{ | ||
State: api.SubscriptionStateRegistered, | ||
Properties: &api.SubscriptionProperties{ | ||
TenantID: mockTenantID, | ||
}, | ||
}, | ||
}) | ||
}, | ||
mocks: func(tt *test, a *mock_adminactions.MockAzureActions) { | ||
a.EXPECT().VMStartAndWait(gomock.Any(), tt.vmName).Return(nil) | ||
}, | ||
wantStatusCode: http.StatusOK, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ti := newTestInfra(t).WithOpenShiftClusters().WithSubscriptions() | ||
defer ti.done() | ||
|
||
a := mock_adminactions.NewMockAzureActions(ti.controller) | ||
tt.mocks(tt, a) | ||
|
||
err := ti.buildFixtures(tt.fixture) | ||
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) | ||
|
||
resp, b, err := ti.request(http.MethodPost, | ||
fmt.Sprintf("https://server/admin%s/startvm?vmName=%s", tt.resourceID, tt.vmName), | ||
nil, nil) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
err = validateResponse(resp, b, tt.wantStatusCode, tt.wantError, tt.wantResponse) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
} | ||
} |
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,60 @@ | ||
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/middleware" | ||
) | ||
|
||
func (f *frontend) postAdminOpenShiftClusterStopVM(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry) | ||
r.URL.Path = filepath.Dir(r.URL.Path) | ||
|
||
err := f._postAdminOpenShiftClusterStopVM(ctx, r, log) | ||
|
||
adminReply(log, w, nil, nil, err) | ||
} | ||
|
||
func (f *frontend) _postAdminOpenShiftClusterStopVM(ctx context.Context, r *http.Request, log *logrus.Entry) error { | ||
vars := mux.Vars(r) | ||
|
||
vmName := r.URL.Query().Get("vmName") | ||
err := validateAdminVMName(vmName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceID := strings.TrimPrefix(r.URL.Path, "/admin") | ||
|
||
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID) | ||
switch { | ||
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound): | ||
return 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 err | ||
} | ||
|
||
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return a.VMStopAndWait(ctx, vmName) | ||
} |
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,110 @@ | ||
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" | ||
testdatabase "github.com/Azure/ARO-RP/test/database" | ||
) | ||
|
||
func TestAdminStopVM(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 | ||
fixture func(*testdatabase.Fixture) | ||
vmName string | ||
mocks func(*test, *mock_adminactions.MockAzureActions) | ||
wantStatusCode int | ||
wantResponse []byte | ||
wantError string | ||
} | ||
|
||
for _, tt := range []*test{ | ||
{ | ||
name: "basic coverage", | ||
vmName: "aro-worker-australiasoutheast-7tcq7", | ||
resourceID: testdatabase.GetResourcePath(mockSubID, "resourceName"), | ||
fixture: func(f *testdatabase.Fixture) { | ||
f.AddOpenShiftClusterDocuments(&api.OpenShiftClusterDocument{ | ||
Key: strings.ToLower(testdatabase.GetResourcePath(mockSubID, "resourceName")), | ||
OpenShiftCluster: &api.OpenShiftCluster{ | ||
ID: testdatabase.GetResourcePath(mockSubID, "resourceName"), | ||
Properties: api.OpenShiftClusterProperties{ | ||
ClusterProfile: api.ClusterProfile{ | ||
ResourceGroupID: fmt.Sprintf("/subscriptions/%s/resourceGroups/test-cluster", mockSubID), | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
f.AddSubscriptionDocuments(&api.SubscriptionDocument{ | ||
ID: mockSubID, | ||
Subscription: &api.Subscription{ | ||
State: api.SubscriptionStateRegistered, | ||
Properties: &api.SubscriptionProperties{ | ||
TenantID: mockTenantID, | ||
}, | ||
}, | ||
}) | ||
}, | ||
mocks: func(tt *test, a *mock_adminactions.MockAzureActions) { | ||
a.EXPECT().VMStopAndWait(gomock.Any(), tt.vmName).Return(nil) | ||
}, | ||
wantStatusCode: http.StatusOK, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ti := newTestInfra(t).WithOpenShiftClusters().WithSubscriptions() | ||
defer ti.done() | ||
|
||
a := mock_adminactions.NewMockAzureActions(ti.controller) | ||
tt.mocks(tt, a) | ||
|
||
err := ti.buildFixtures(tt.fixture) | ||
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) | ||
|
||
resp, b, err := ti.request(http.MethodPost, | ||
fmt.Sprintf("https://server/admin%s/stopvm?vmName=%s", tt.resourceID, tt.vmName), | ||
nil, nil) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
err = validateResponse(resp, b, tt.wantStatusCode, tt.wantError, tt.wantResponse) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.