create/delete organization handlers (#537)

This commit is contained in:
Michael Quigley 2024-12-09 13:30:45 -05:00
parent 38b32d15d0
commit c98aaa8e00
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
8 changed files with 200 additions and 0 deletions

View File

@ -54,7 +54,9 @@ func Run(inCfg *config.Config) error {
api.AdminCreateAccountHandler = newCreateAccountHandler()
api.AdminCreateFrontendHandler = newCreateFrontendHandler()
api.AdminCreateIdentityHandler = newCreateIdentityHandler()
api.AdminCreateOrganizationHandler = newCreateOrganizationHandler()
api.AdminDeleteFrontendHandler = newDeleteFrontendHandler()
api.AdminDeleteOrganizationHandler = newDeleteOrganizationHandler()
api.AdminGrantsHandler = newGrantsHandler()
api.AdminInviteTokenGenerateHandler = newInviteTokenGenerateHandler()
api.AdminListFrontendsHandler = newListFrontendsHandler()

View File

@ -0,0 +1,53 @@
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/admin"
"github.com/sirupsen/logrus"
)
type createOrganizationHandler struct{}
func newCreateOrganizationHandler() *createOrganizationHandler {
return &createOrganizationHandler{}
}
func (h *createOrganizationHandler) Handle(params admin.CreateOrganizationParams, principal *rest_model_zrok.Principal) middleware.Responder {
if !principal.Admin {
logrus.Errorf("invalid admin principal")
return admin.NewCreateOrganizationUnauthorized()
}
trx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return admin.NewCreateOrganizationInternalServerError()
}
defer func() { _ = trx.Rollback() }()
orgToken, err := CreateToken()
if err != nil {
logrus.Errorf("error creating organization token: %v", err)
return admin.NewCreateOrganizationInternalServerError()
}
org := &store.Organization{
Token: orgToken,
Description: params.Body.Description,
}
if _, err := str.CreateOrganization(org, trx); err != nil {
logrus.Errorf("error creating organization: %v", err)
return admin.NewCreateOrganizationInternalServerError()
}
if err := trx.Commit(); err != nil {
logrus.Errorf("error committing organization: %v", err)
return admin.NewCreateOrganizationInternalServerError()
}
logrus.Infof("added organzation '%v' with description '%v'", org.Token, org.Description)
return admin.NewCreateOrganizationCreated().WithPayload(&admin.CreateOrganizationCreatedBody{Token: org.Token})
}

View File

@ -0,0 +1,42 @@
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/admin"
"github.com/sirupsen/logrus"
)
type deleteOrganizationHandler struct{}
func newDeleteOrganizationHandler() *deleteOrganizationHandler {
return &deleteOrganizationHandler{}
}
func (h *deleteOrganizationHandler) Handle(params admin.DeleteOrganizationParams, principal *rest_model_zrok.Principal) middleware.Responder {
if !principal.Admin {
logrus.Errorf("invalid admin principal")
return admin.NewDeleteOrganizationUnauthorized()
}
trx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return admin.NewDeleteOrganizationInternalServerError()
}
defer func() { _ = trx.Rollback() }()
org, err := str.FindOrganizationByToken(params.Body.Token, trx)
if err != nil {
logrus.Errorf("error finding organization by token: %v", err)
return admin.NewDeleteOrganizationNotFound()
}
err = str.DeleteOrganization(org.Id, trx)
if err != nil {
logrus.Errorf("error deleting organization: %v", err)
return admin.NewDeleteOrganizationInternalServerError()
}
return admin.NewDeleteOrganizationOK()
}

View File

@ -23,6 +23,14 @@ func (str *Store) CreateOrganization(org *Organization, trx *sqlx.Tx) (int, erro
return id, nil
}
func (str *Store) FindOrganizationByToken(token string, trx *sqlx.Tx) (*Organization, error) {
org := &Organization{}
if err := trx.QueryRowx("select * from organizations where token = $1", token).StructScan(org); err != nil {
return nil, errors.Wrap(err, "error selecting frontend by token")
}
return org, nil
}
func (str *Store) DeleteOrganization(id int, trx *sqlx.Tx) error {
stmt, err := trx.Prepare("update organizations set updated_at = current_timestamp, deleted = true where id = $1")
if err != nil {

View File

@ -34,6 +34,12 @@ func (o *DeleteOrganizationReader) ReadResponse(response runtime.ClientResponse,
return nil, err
}
return nil, result
case 404:
result := NewDeleteOrganizationNotFound()
if err := result.readResponse(response, consumer, o.formats); err != nil {
return nil, err
}
return nil, result
case 500:
result := NewDeleteOrganizationInternalServerError()
if err := result.readResponse(response, consumer, o.formats); err != nil {
@ -157,6 +163,62 @@ func (o *DeleteOrganizationUnauthorized) readResponse(response runtime.ClientRes
return nil
}
// NewDeleteOrganizationNotFound creates a DeleteOrganizationNotFound with default headers values
func NewDeleteOrganizationNotFound() *DeleteOrganizationNotFound {
return &DeleteOrganizationNotFound{}
}
/*
DeleteOrganizationNotFound describes a response with status code 404, with default header values.
organization not found
*/
type DeleteOrganizationNotFound struct {
}
// IsSuccess returns true when this delete organization not found response has a 2xx status code
func (o *DeleteOrganizationNotFound) IsSuccess() bool {
return false
}
// IsRedirect returns true when this delete organization not found response has a 3xx status code
func (o *DeleteOrganizationNotFound) IsRedirect() bool {
return false
}
// IsClientError returns true when this delete organization not found response has a 4xx status code
func (o *DeleteOrganizationNotFound) IsClientError() bool {
return true
}
// IsServerError returns true when this delete organization not found response has a 5xx status code
func (o *DeleteOrganizationNotFound) IsServerError() bool {
return false
}
// IsCode returns true when this delete organization not found response a status code equal to that given
func (o *DeleteOrganizationNotFound) IsCode(code int) bool {
return code == 404
}
// Code gets the status code for the delete organization not found response
func (o *DeleteOrganizationNotFound) Code() int {
return 404
}
func (o *DeleteOrganizationNotFound) Error() string {
return fmt.Sprintf("[DELETE /organization][%d] deleteOrganizationNotFound ", 404)
}
func (o *DeleteOrganizationNotFound) String() string {
return fmt.Sprintf("[DELETE /organization][%d] deleteOrganizationNotFound ", 404)
}
func (o *DeleteOrganizationNotFound) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {
return nil
}
// NewDeleteOrganizationInternalServerError creates a DeleteOrganizationInternalServerError with default headers values
func NewDeleteOrganizationInternalServerError() *DeleteOrganizationInternalServerError {
return &DeleteOrganizationInternalServerError{}

View File

@ -904,6 +904,9 @@ func init() {
"401": {
"description": "unauthorized"
},
"404": {
"description": "organization not found"
},
"500": {
"description": "internal server error"
}
@ -2958,6 +2961,9 @@ func init() {
"401": {
"description": "unauthorized"
},
"404": {
"description": "organization not found"
},
"500": {
"description": "internal server error"
}

View File

@ -61,6 +61,31 @@ func (o *DeleteOrganizationUnauthorized) WriteResponse(rw http.ResponseWriter, p
rw.WriteHeader(401)
}
// DeleteOrganizationNotFoundCode is the HTTP code returned for type DeleteOrganizationNotFound
const DeleteOrganizationNotFoundCode int = 404
/*
DeleteOrganizationNotFound organization not found
swagger:response deleteOrganizationNotFound
*/
type DeleteOrganizationNotFound struct {
}
// NewDeleteOrganizationNotFound creates DeleteOrganizationNotFound with default headers values
func NewDeleteOrganizationNotFound() *DeleteOrganizationNotFound {
return &DeleteOrganizationNotFound{}
}
// WriteResponse to the client
func (o *DeleteOrganizationNotFound) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses
rw.WriteHeader(404)
}
// DeleteOrganizationInternalServerErrorCode is the HTTP code returned for type DeleteOrganizationInternalServerError
const DeleteOrganizationInternalServerErrorCode int = 500

View File

@ -425,6 +425,8 @@ paths:
description: organization deleted
401:
description: unauthorized
404:
description: organization not found
500:
description: internal server error