Skip to content

Commit

Permalink
Add optional keepWebhook flag when removing an entity
Browse files Browse the repository at this point in the history
The user can opt to not delete the webhook (if installed) when removing
the entity from garm. Garm will only ever try to remove a webhook that
exactly matches the URL that is composed of the base webhook URL configured
in the config.toml file and the unique controller ID that is generated
when the controller is first installed. It should be safe to remove the
webhook when the entity is removed.

Of course, this behavior can be disabled.

Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed Aug 16, 2023
1 parent ce88f62 commit 5d57d86
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 2 deletions.
11 changes: 10 additions & 1 deletion apiserver/controllers/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"log"
"net/http"
"strconv"

gErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm/apiserver/params"
Expand Down Expand Up @@ -139,6 +140,12 @@ func (a *APIController) GetOrgByIDHandler(w http.ResponseWriter, r *http.Request
// in: path
// required: true
//
// + name: keepWebhook
// description: If true and a webhook is installed for this organization, it will not be removed.
// type: boolean
// in: query
// required: false
//
// Responses:
// default: APIErrorResponse
func (a *APIController) DeleteOrgHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -157,7 +164,9 @@ func (a *APIController) DeleteOrgHandler(w http.ResponseWriter, r *http.Request)
return
}

if err := a.r.DeleteOrganization(ctx, orgID, false); err != nil {
keepWebhook, _ := strconv.ParseBool(r.URL.Query().Get("keepWebhook"))

if err := a.r.DeleteOrganization(ctx, orgID, keepWebhook); err != nil {
log.Printf("removing org: %+v", err)
handleError(w, err)
return
Expand Down
10 changes: 9 additions & 1 deletion apiserver/controllers/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"log"
"net/http"
"strconv"

gErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm/apiserver/params"
Expand Down Expand Up @@ -139,6 +140,12 @@ func (a *APIController) GetRepoByIDHandler(w http.ResponseWriter, r *http.Reques
// in: path
// required: true
//
// + name: keepWebhook
// description: If true and a webhook is installed for this repo, it will not be removed.
// type: boolean
// in: query
// required: false
//
// Responses:
// default: APIErrorResponse
func (a *APIController) DeleteRepoHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -157,7 +164,8 @@ func (a *APIController) DeleteRepoHandler(w http.ResponseWriter, r *http.Request
return
}

if err := a.r.DeleteRepository(ctx, repoID, false); err != nil {
keepWebhook, _ := strconv.ParseBool(r.URL.Query().Get("keepWebhook"))
if err := a.r.DeleteRepository(ctx, repoID, keepWebhook); err != nil {
log.Printf("fetching repo: %s", err)
handleError(w, err)
return
Expand Down
8 changes: 8 additions & 0 deletions apiserver/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,10 @@ paths:
name: orgID
required: true
type: string
- description: If true and a webhook is installed for this organization, it will not be removed.
in: query
name: keepWebhook
type: boolean
responses:
default:
description: APIErrorResponse
Expand Down Expand Up @@ -1147,6 +1151,10 @@ paths:
name: repoID
required: true
type: string
- description: If true and a webhook is installed for this repo, it will not be removed.
in: query
name: keepWebhook
type: boolean
responses:
default:
description: APIErrorResponse
Expand Down
35 changes: 35 additions & 0 deletions client/organizations/delete_org_parameters.go

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

35 changes: 35 additions & 0 deletions client/repositories/delete_repo_parameters.go

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

5 changes: 5 additions & 0 deletions cmd/garm-cli/cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
orgCreds string
orgRandomWebhookSecret bool
insecureOrgWebhook bool
keepOrgWebhook bool
)

// organizationCmd represents the organization command
Expand Down Expand Up @@ -271,6 +272,7 @@ var orgDeleteCmd = &cobra.Command{
}
deleteOrgReq := apiClientOrgs.NewDeleteOrgParams()
deleteOrgReq.OrgID = args[0]
deleteOrgReq.KeepWebhook = &keepOrgWebhook
if err := apiCli.Organizations.DeleteOrg(deleteOrgReq, authToken); err != nil {
return err
}
Expand All @@ -289,6 +291,9 @@ func init() {

orgAddCmd.MarkFlagRequired("credentials") //nolint
orgAddCmd.MarkFlagRequired("name") //nolint

orgDeleteCmd.Flags().BoolVar(&keepOrgWebhook, "keep-webhook", false, "Do not delete any existing webhook when removing the organization from GARM.")

orgUpdateCmd.Flags().StringVar(&orgWebhookSecret, "webhook-secret", "", "The webhook secret for this organization")
orgUpdateCmd.Flags().StringVar(&orgCreds, "credentials", "", "Credentials name. See credentials list.")

Expand Down
6 changes: 6 additions & 0 deletions cmd/garm-cli/cmd/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
repoCreds string
randomWebhookSecret bool
insecureRepoWebhook bool
keepRepoWebhook bool
)

// repositoryCmd represents the repository command
Expand Down Expand Up @@ -274,6 +275,7 @@ var repoDeleteCmd = &cobra.Command{
}
deleteRepoReq := apiClientRepos.NewDeleteRepoParams()
deleteRepoReq.RepoID = args[0]
deleteRepoReq.KeepWebhook = &keepRepoWebhook
if err := apiCli.Repositories.DeleteRepo(deleteRepoReq, authToken); err != nil {
return err
}
Expand All @@ -294,10 +296,14 @@ func init() {
repoAddCmd.MarkFlagRequired("credentials") //nolint
repoAddCmd.MarkFlagRequired("owner") //nolint
repoAddCmd.MarkFlagRequired("name") //nolint

repoDeleteCmd.Flags().BoolVar(&keepRepoWebhook, "keep-webhook", false, "Do not delete any existing webhook when removing the repo from GARM.")

repoUpdateCmd.Flags().StringVar(&repoWebhookSecret, "webhook-secret", "", "The webhook secret for this repository. If you update this secret, you will have to manually update the secret in GitHub as well.")
repoUpdateCmd.Flags().StringVar(&repoCreds, "credentials", "", "Credentials name. See credentials list.")

repoWebhookInstallCmd.Flags().BoolVar(&insecureRepoWebhook, "insecure", false, "Ignore self signed certificate errors.")

repoWebhookCmd.AddCommand(
repoWebhookInstallCmd,
repoWebhookUninstallCmd,
Expand Down

0 comments on commit 5d57d86

Please sign in to comment.