Skip to content

Commit 6b05a74

Browse files
authored
ROX-28461: Add change cloud_account and subscription_Id admin endpoint (#2330)
* Add change cloud account admin endpoint * Revert rds moq changes * Add subscription_id change * Rollback rds moq change * Add subscription change
1 parent 2898997 commit 6b05a74

File tree

10 files changed

+374
-2
lines changed

10 files changed

+374
-2
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@
268268
"filename": "internal/central/pkg/services/centralservice_moq.go",
269269
"hashed_secret": "44e17306b837162269a410204daaa5ecee4ec22c",
270270
"is_verified": false,
271-
"line_number": 1112
271+
"line_number": 1180
272272
}
273273
],
274274
"pkg/client/fleetmanager/impl/testdata/token": [
@@ -391,5 +391,5 @@
391391
}
392392
]
393393
},
394-
"generated_at": "2025-05-12T10:17:39Z"
394+
"generated_at": "2025-06-10T08:35:01Z"
395395
}

internal/central/pkg/api/admin/private/api/openapi.yaml

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/central/pkg/api/admin/private/api_default.go

Lines changed: 108 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/central/pkg/api/admin/private/model_central_subscription_change_request.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/central/pkg/handlers/admin_central.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type AdminCentralHandler interface {
6868

6969
// PatchBillingParameters changes the billing model of a central
7070
PatchBillingParameters(w http.ResponseWriter, r *http.Request)
71+
72+
// PatchSubscriptionParameters changes the subscription and cloud account of a central
73+
PatchSubscriptionParameters(w http.ResponseWriter, r *http.Request)
7174
}
7275

7376
type adminCentralHandler struct {
@@ -424,3 +427,15 @@ func (h adminCentralHandler) PatchBillingParameters(w http.ResponseWriter, r *ht
424427
}
425428
handlers.Handle(w, r, cfg, http.StatusOK)
426429
}
430+
431+
func (h adminCentralHandler) PatchSubscriptionParameters(w http.ResponseWriter, r *http.Request) {
432+
var request *private.CentralSubscriptionChangeRequest
433+
cfg := &handlers.HandlerConfig{
434+
MarshalInto: &request,
435+
Action: func() (i any, serviceError *errors.ServiceError) {
436+
return nil, h.service.ChangeSubscription(r.Context(), mux.Vars(r)["id"],
437+
request.CloudAccountId, request.CloudProvider, request.SubscriptionId)
438+
},
439+
}
440+
handlers.Handle(w, r, cfg, http.StatusOK)
441+
}

internal/central/pkg/routes/route_loader.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ func (s *options) buildAPIBaseRouter(mainRouter *mux.Router, basePath string) er
233233
adminCentralsRouter.HandleFunc("/{id}/billing", adminCentralHandler.PatchBillingParameters).
234234
Name(logger.NewLogEvent("admin-billing", "[admin] change central billing parameters").ToString()).
235235
Methods(http.MethodPatch)
236+
adminCentralsRouter.HandleFunc("/{id}/subscription", adminCentralHandler.PatchSubscriptionParameters).
237+
Name(logger.NewLogEvent("admin-subscription", "[admin] change central subscription parameters").ToString()).
238+
Methods(http.MethodPatch)
236239

237240
if features.ClusterMigration.Enabled() {
238241
adminCentralsRouter.HandleFunc("/{id}/assign-cluster", adminCentralHandler.AssignCluster).

internal/central/pkg/services/central.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ type CentralService interface {
118118
ResetCentralSecretBackup(ctx context.Context, centralRequest *dbapi.CentralRequest) *errors.ServiceError
119119
ChangeBillingParameters(ctx context.Context, centralID string, billingModel string, cloudAccountID string, cloudProvider string, product string) *errors.ServiceError
120120
AssignCluster(ctx context.Context, centralID string, clusterID string) *errors.ServiceError
121+
ChangeSubscription(ctx context.Context, centralID string, cloudAccountID string, cloudProvider string, subscriptionID string) *errors.ServiceError
121122
}
122123

123124
var _ CentralService = &centralService{}
@@ -1143,3 +1144,23 @@ func (k *centralService) ChangeBillingParameters(ctx context.Context, centralID
11431144
}
11441145
return nil
11451146
}
1147+
1148+
// ChangeSubscription implements CentralService.
1149+
func (k *centralService) ChangeSubscription(ctx context.Context, centralID string, cloudAccountID string, cloudProvider string, subscriptionID string) *errors.ServiceError {
1150+
centralRequest, svcErr := k.GetByID(centralID)
1151+
if svcErr != nil {
1152+
return svcErr
1153+
}
1154+
1155+
centralRequest.CloudProvider = cloudProvider
1156+
centralRequest.CloudAccountID = cloudAccountID
1157+
centralRequest.SubscriptionID = subscriptionID
1158+
1159+
if svcErr = k.UpdateIgnoreNils(centralRequest); svcErr != nil {
1160+
glog.Errorf("Failed to update central %q record with subscription_id %q and updated cloud account %q: %v", centralID, subscriptionID, cloudAccountID, svcErr)
1161+
return svcErr
1162+
}
1163+
1164+
glog.Infof("Central %q cloud account parameters have been changed to %q with id %q", centralID, cloudProvider, cloudAccountID)
1165+
return nil
1166+
}

internal/central/pkg/services/central_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,33 @@ func Test_centralService_ChangeBillingParameters(t *testing.T) {
302302
deleteQuotaCalls := quotaService.DeleteQuotaCalls()
303303
require.Len(t, deleteQuotaCalls, 0)
304304
}
305+
306+
func Test_centralService_ChangeSubscription(t *testing.T) {
307+
service := &centralService{
308+
connectionFactory: db.NewMockConnectionFactory(nil),
309+
}
310+
central := buildCentralRequest(func(centralRequest *dbapi.CentralRequest) {
311+
centralRequest.CloudProvider = ""
312+
centralRequest.CloudAccountID = ""
313+
centralRequest.SubscriptionID = "original_subscription_id"
314+
})
315+
316+
catcher := mocket.Catcher.Reset()
317+
q0 := catcher.NewMock().WithQuery(`SELECT * FROM "central_requests" `+
318+
`WHERE id = $1 AND "central_requests"."deleted_at" IS NULL `+
319+
`ORDER BY "central_requests"."id" LIMIT $2`).
320+
OneTime().WithArgs(testID, int64(1)).
321+
WithReply(converters.ConvertCentralRequest(central))
322+
q1 := catcher.NewMock().WithQuery(`UPDATE "central_requests" ` +
323+
`SET "updated_at"=$1,"deleted_at"=$2,"region"=$3,"cluster_id"=$4,` +
324+
`"cloud_provider"=$5,"cloud_account_id"=$6,"name"=$7,"subscription_id"=$8,"owner"=$9 ` +
325+
`WHERE status not IN ($10,$11) AND "central_requests"."deleted_at" IS NULL AND "id" = $12`).
326+
OneTime()
327+
328+
svcErr := service.ChangeSubscription(context.Background(), central.ID, "aws_account_id", "aws", "new_subscription_id")
329+
assert.Nil(t, svcErr)
330+
331+
assert.True(t, q0.Triggered)
332+
assert.True(t, q1.Triggered)
333+
334+
}

0 commit comments

Comments
 (0)