Skip to content

ROX-28461: Add change cloud_account and subscription_Id admin endpoint #2330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
"filename": "internal/central/pkg/services/centralservice_moq.go",
"hashed_secret": "44e17306b837162269a410204daaa5ecee4ec22c",
"is_verified": false,
"line_number": 1112
"line_number": 1180
}
],
"pkg/client/fleetmanager/impl/testdata/token": [
Expand Down Expand Up @@ -391,5 +391,5 @@
}
]
},
"generated_at": "2025-05-12T10:17:39Z"
"generated_at": "2025-06-10T08:35:01Z"
}
59 changes: 59 additions & 0 deletions internal/central/pkg/api/admin/private/api/openapi.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions internal/central/pkg/api/admin/private/api_default.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/central/pkg/handlers/admin_central.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type AdminCentralHandler interface {

// PatchBillingParameters changes the billing model of a central
PatchBillingParameters(w http.ResponseWriter, r *http.Request)

// PatchSubscriptionParameters changes the subscription and cloud account of a central
PatchSubscriptionParameters(w http.ResponseWriter, r *http.Request)
}

type adminCentralHandler struct {
Expand Down Expand Up @@ -424,3 +427,15 @@ func (h adminCentralHandler) PatchBillingParameters(w http.ResponseWriter, r *ht
}
handlers.Handle(w, r, cfg, http.StatusOK)
}

func (h adminCentralHandler) PatchSubscriptionParameters(w http.ResponseWriter, r *http.Request) {
var request *private.CentralSubscriptionChangeRequest
cfg := &handlers.HandlerConfig{
MarshalInto: &request,
Action: func() (i any, serviceError *errors.ServiceError) {
return nil, h.service.ChangeSubscription(r.Context(), mux.Vars(r)["id"],
request.CloudAccountId, request.CloudProvider, request.SubscriptionId)
},
}
handlers.Handle(w, r, cfg, http.StatusOK)
}
3 changes: 3 additions & 0 deletions internal/central/pkg/routes/route_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ func (s *options) buildAPIBaseRouter(mainRouter *mux.Router, basePath string) er
adminCentralsRouter.HandleFunc("/{id}/billing", adminCentralHandler.PatchBillingParameters).
Name(logger.NewLogEvent("admin-billing", "[admin] change central billing parameters").ToString()).
Methods(http.MethodPatch)
adminCentralsRouter.HandleFunc("/{id}/subscription", adminCentralHandler.PatchSubscriptionParameters).
Name(logger.NewLogEvent("admin-subscription", "[admin] change central subscription parameters").ToString()).
Methods(http.MethodPatch)

if features.ClusterMigration.Enabled() {
adminCentralsRouter.HandleFunc("/{id}/assign-cluster", adminCentralHandler.AssignCluster).
Expand Down
21 changes: 21 additions & 0 deletions internal/central/pkg/services/central.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type CentralService interface {
ResetCentralSecretBackup(ctx context.Context, centralRequest *dbapi.CentralRequest) *errors.ServiceError
ChangeBillingParameters(ctx context.Context, centralID string, billingModel string, cloudAccountID string, cloudProvider string, product string) *errors.ServiceError
AssignCluster(ctx context.Context, centralID string, clusterID string) *errors.ServiceError
ChangeSubscription(ctx context.Context, centralID string, cloudAccountID string, cloudProvider string, subscriptionID string) *errors.ServiceError
}

var _ CentralService = &centralService{}
Expand Down Expand Up @@ -1143,3 +1144,23 @@ func (k *centralService) ChangeBillingParameters(ctx context.Context, centralID
}
return nil
}

// ChangeSubscription implements CentralService.
func (k *centralService) ChangeSubscription(ctx context.Context, centralID string, cloudAccountID string, cloudProvider string, subscriptionID string) *errors.ServiceError {
centralRequest, svcErr := k.GetByID(centralID)
if svcErr != nil {
return svcErr
}

centralRequest.CloudProvider = cloudProvider
centralRequest.CloudAccountID = cloudAccountID
centralRequest.SubscriptionID = subscriptionID

if svcErr = k.UpdateIgnoreNils(centralRequest); svcErr != nil {
glog.Errorf("Failed to update central %q record with subscription_id %q and updated cloud account %q: %v", centralID, subscriptionID, cloudAccountID, svcErr)
return svcErr
}

glog.Infof("Central %q cloud account parameters have been changed to %q with id %q", centralID, cloudProvider, cloudAccountID)
return nil
}
30 changes: 30 additions & 0 deletions internal/central/pkg/services/central_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,33 @@ func Test_centralService_ChangeBillingParameters(t *testing.T) {
deleteQuotaCalls := quotaService.DeleteQuotaCalls()
require.Len(t, deleteQuotaCalls, 0)
}

func Test_centralService_ChangeSubscription(t *testing.T) {
service := &centralService{
connectionFactory: db.NewMockConnectionFactory(nil),
}
central := buildCentralRequest(func(centralRequest *dbapi.CentralRequest) {
centralRequest.CloudProvider = ""
centralRequest.CloudAccountID = ""
centralRequest.SubscriptionID = "original_subscription_id"
})

catcher := mocket.Catcher.Reset()
q0 := catcher.NewMock().WithQuery(`SELECT * FROM "central_requests" `+
`WHERE id = $1 AND "central_requests"."deleted_at" IS NULL `+
`ORDER BY "central_requests"."id" LIMIT $2`).
OneTime().WithArgs(testID, int64(1)).
WithReply(converters.ConvertCentralRequest(central))
q1 := catcher.NewMock().WithQuery(`UPDATE "central_requests" ` +
`SET "updated_at"=$1,"deleted_at"=$2,"region"=$3,"cluster_id"=$4,` +
`"cloud_provider"=$5,"cloud_account_id"=$6,"name"=$7,"subscription_id"=$8,"owner"=$9 ` +
`WHERE status not IN ($10,$11) AND "central_requests"."deleted_at" IS NULL AND "id" = $12`).
OneTime()

svcErr := service.ChangeSubscription(context.Background(), central.ID, "aws_account_id", "aws", "new_subscription_id")
assert.Nil(t, svcErr)

assert.True(t, q0.Triggered)
assert.True(t, q1.Triggered)

}
Loading
Loading