diff --git a/pkg/services/serviceaccounts/api/api.go b/pkg/services/serviceaccounts/api/api.go index 7b8c7242876..06b190c1f12 100644 --- a/pkg/services/serviceaccounts/api/api.go +++ b/pkg/services/serviceaccounts/api/api.go @@ -71,8 +71,11 @@ func (api *ServiceAccountsAPI) CreateServiceAccount(c *models.ReqContext) respon case err != nil: return response.Error(http.StatusInternalServerError, "Failed to create service account", err) } - - return response.JSON(http.StatusCreated, user) + result := models.UserIdDTO{ + Message: "Service account created", + Id: user.Id, + } + return response.JSON(http.StatusCreated, result) } func (api *ServiceAccountsAPI) DeleteServiceAccount(ctx *models.ReqContext) response.Response { diff --git a/pkg/services/serviceaccounts/database/database.go b/pkg/services/serviceaccounts/database/database.go index 7de90976be2..212f761d0cf 100644 --- a/pkg/services/serviceaccounts/database/database.go +++ b/pkg/services/serviceaccounts/database/database.go @@ -27,9 +27,10 @@ func NewServiceAccountsStore(store *sqlstore.SQLStore) *ServiceAccountsStoreImpl func (s *ServiceAccountsStoreImpl) CreateServiceAccount(ctx context.Context, sa *serviceaccounts.CreateServiceaccountForm) (user *models.User, err error) { // create a new service account - "user" with empty permissions + generatedLogin := "Service-Account-" + uuid.New().String() cmd := models.CreateUserCommand{ - Login: "Service-Account-" + uuid.New().String(), - Name: sa.Name + "-Service-Account-" + uuid.New().String(), + Login: generatedLogin, + Name: sa.Name, OrgId: sa.OrgID, IsServiceAccount: true, } diff --git a/pkg/services/serviceaccounts/models.go b/pkg/services/serviceaccounts/models.go index ded792a2182..1108cc46d88 100644 --- a/pkg/services/serviceaccounts/models.go +++ b/pkg/services/serviceaccounts/models.go @@ -19,8 +19,6 @@ type ServiceAccount struct { } type CreateServiceaccountForm struct { - OrgID int64 `json:"-"` - Name string `json:"name" binding:"Required"` - DisplayName string `json:"displayName"` - Description string `json:"description"` + OrgID int64 `json:"-"` + Name string `json:"name" binding:"Required"` } diff --git a/public/app/features/serviceaccounts/ServiceAccountCreatePage.tsx b/public/app/features/serviceaccounts/ServiceAccountCreatePage.tsx new file mode 100644 index 00000000000..56a4d35b331 --- /dev/null +++ b/public/app/features/serviceaccounts/ServiceAccountCreatePage.tsx @@ -0,0 +1,61 @@ +import React, { useCallback } from 'react'; +import { connect } from 'react-redux'; +import { Form, Button, Input, Field } from '@grafana/ui'; +import { NavModel } from '@grafana/data'; +import { getBackendSrv } from '@grafana/runtime'; +import { StoreState } from '../../types'; +import { getNavModel } from '../../core/selectors/navModel'; +import Page from 'app/core/components/Page/Page'; +import { useHistory } from 'react-router-dom'; + +interface ServiceAccountCreatePageProps { + navModel: NavModel; +} +interface ServiceAccountDTO { + name: string; +} + +const createServiceAccount = async (sa: ServiceAccountDTO) => getBackendSrv().post('/api/serviceaccounts/', sa); + +const ServiceAccountCreatePage: React.FC = ({ navModel }) => { + const history = useHistory(); + + const onSubmit = useCallback( + async (data: ServiceAccountDTO) => { + await createServiceAccount(data); + history.push('/org/serviceaccounts/'); + }, + [history] + ); + + return ( + + +

Add new service account

+
+ {({ register, errors }) => { + return ( + <> + + + + + + ); + }} +
+
+
+ ); +}; + +const mapStateToProps = (state: StoreState) => ({ + navModel: getNavModel(state.navIndex, 'serviceaccounts'), +}); + +export default connect(mapStateToProps)(ServiceAccountCreatePage); diff --git a/public/app/features/serviceaccounts/ServiceAccountProfile.tsx b/public/app/features/serviceaccounts/ServiceAccountProfile.tsx index b7e10fbe345..81eafffbd3a 100644 --- a/public/app/features/serviceaccounts/ServiceAccountProfile.tsx +++ b/public/app/features/serviceaccounts/ServiceAccountProfile.tsx @@ -58,6 +58,9 @@ export function ServiceAccountProfile({ return ( <>

Service account information

+ +