Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
🪓
  • Loading branch information
aslilac committed May 16, 2024
commit 265a895c1c36c09049e5f300802c9940817145f7
2 changes: 2 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,8 @@ func New(options *Options) *API {
httpmw.ExtractOrganizationParam(options.Database),
)
r.Get("/", api.organization)
r.Patch("/", api.patchOrganization)
r.Delete("/", api.deleteOrganization)
r.Post("/templateversions", api.postTemplateVersionsByOrganization)
r.Route("/templates", func(r chi.Router) {
r.Post("/", api.postTemplateByOrganization)
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
return nil
}

func (q *FakeQuerier) DeleteOrganization(ctx context.Context, id uuid.UUID) error {
func (q *FakeQuerier) DeleteOrganization(_ context.Context, id uuid.UUID) error {
for i, org := range q.organizations {
if org.ID == id {
q.organizations = append(q.organizations[:i], q.organizations[i+1:]...)
Expand Down Expand Up @@ -7153,7 +7153,7 @@ func (q *FakeQuerier) UpdateOAuth2ProviderAppSecretByID(_ context.Context, arg d
return database.OAuth2ProviderAppSecret{}, sql.ErrNoRows
}

func (q *FakeQuerier) UpdateOrganization(ctx context.Context, arg database.UpdateOrganizationParams) (database.Organization, error) {
func (q *FakeQuerier) UpdateOrganization(_ context.Context, arg database.UpdateOrganizationParams) (database.Organization, error) {
err := validateDatabaseType(arg)
if err != nil {
return database.Organization{}, err
Expand Down
48 changes: 41 additions & 7 deletions coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,18 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
// @Produce json
// @Tags Organizations
// @Param request body codersdk.PatchOrganizationRequest true "Patch organization request"
// @Success 201 {object} codersdk.Organization
// @Success 200 {object} codersdk.Organization
// @Router /organizations/{organization} [patch]
func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
organization := httpmw.OrganizationParam(r)

var req codersdk.PatchOrganizationRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}

if req.Name == codersdk.DefaultOrganization {
if req.Name == codersdk.DefaultOrganization && !organization.IsDefault {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: fmt.Sprintf("Organization name %q is reserved.", codersdk.DefaultOrganization),
})
Expand All @@ -157,22 +158,55 @@ func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
return
}

organization, err := api.Database.UpdateOrganization(ctx, database.UpdateOrganizationParams{
ID: uuid.New(),
Name: req.Name,
organization, err = api.Database.UpdateOrganization(ctx, database.UpdateOrganizationParams{
ID: organization.ID,
UpdatedAt: dbtime.Now(),
Name: req.Name,
})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error inserting organization member.",
Detail: fmt.Sprintf("update organization: %w", err),
Message: "Internal error updating organization.",
Detail: fmt.Sprintf("update organization: %s", err.Error()),
})
return
}

httpapi.Write(ctx, rw, http.StatusOK, convertOrganization(organization))
}

// @Summary Delete organization
// @ID delete-organization
// @Security CoderSessionToken
// @Accept json
// @Produce json
// @Tags Organizations
// @Success 200
// @Router /organizations/{organization} [delete]
func (api *API) deleteOrganization(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
organization := httpmw.OrganizationParam(r)

if organization.IsDefault {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: fmt.Sprintf("Organization name %q is reserved.", codersdk.DefaultOrganization),
})
return
}

err := api.Database.DeleteOrganization(ctx, organization.ID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error deleting organization.",
Detail: fmt.Sprintf("delete organization: %s", err.Error()),
})
return
}

httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
Message: "Organization has been deleted.",
})
}

// convertOrganization consumes the database representation and outputs an API friendly representation.
func convertOrganization(organization database.Organization) codersdk.Organization {
return codersdk.Organization{
Expand Down
8 changes: 4 additions & 4 deletions scripts/rbacgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func generateRbacObjects(templateSource string) ([]byte, error) {
// Parse the policy.go file for the action enums
f, err := parser.ParseFile(token.NewFileSet(), "./coderd/rbac/policy/policy.go", nil, parser.ParseComments)
if err != nil {
return nil, fmt.Errorf("parsing policy.go: %w", err)
return nil, xerrors.Errorf("parsing policy.go: %w", err)
}
actionMap := fileActions(f)
actionList := make([]ActionDetails, 0)
Expand Down Expand Up @@ -176,14 +176,14 @@ func generateRbacObjects(templateSource string) ([]byte, error) {
x++
v, ok := actionMap[string(action)]
if !ok {
errorList = append(errorList, fmt.Errorf("action value %q does not have a constant a matching enum constant", action))
errorList = append(errorList, xerrors.Errorf("action value %q does not have a constant a matching enum constant", action))
}
return v
},
"concat": func(strs ...string) string { return strings.Join(strs, "") },
}).Parse(templateSource)
if err != nil {
return nil, fmt.Errorf("parse template: %w", err)
return nil, xerrors.Errorf("parse template: %w", err)
}

// Convert to sorted list for autogen consistency.
Expand All @@ -203,7 +203,7 @@ func generateRbacObjects(templateSource string) ([]byte, error) {

err = tpl.Execute(&out, list)
if err != nil {
return nil, fmt.Errorf("execute template: %w", err)
return nil, xerrors.Errorf("execute template: %w", err)
}

if len(errorList) > 0 {
Expand Down