diff --git a/cmd/agola/cmd/projectgroupsecretdelete.go b/cmd/agola/cmd/projectgroupsecretdelete.go new file mode 100644 index 0000000..b8e118f --- /dev/null +++ b/cmd/agola/cmd/projectgroupsecretdelete.go @@ -0,0 +1,41 @@ +// Copyright 2019 Sorint.lab +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "github.com/spf13/cobra" +) + +var cmdProjectGroupSecretDelete = &cobra.Command{ + Use: "delete", + Short: "delete a secret", + Run: func(cmd *cobra.Command, args []string) { + if err := secretDelete(cmd, "projectgroup", args); err != nil { + log.Fatalf("err: %v", err) + } + }, +} + +func init() { + flags := cmdProjectGroupSecretDelete.Flags() + + flags.StringVar(&secretDeleteOpts.parentRef, "projectgroup", "", "project group id or full path") + flags.StringVarP(&secretDeleteOpts.name, "name", "n", "", "secret name") + + cmdProjectGroupSecretDelete.MarkFlagRequired("projectgroup") + cmdProjectGroupSecretDelete.MarkFlagRequired("name") + + cmdProjectGroupSecret.AddCommand(cmdProjectGroupSecretDelete) +} diff --git a/cmd/agola/cmd/projectsecretcreate.go b/cmd/agola/cmd/projectsecretcreate.go index a091ff2..74c39e5 100644 --- a/cmd/agola/cmd/projectsecretcreate.go +++ b/cmd/agola/cmd/projectsecretcreate.go @@ -36,9 +36,9 @@ var cmdProjectSecretCreate = &cobra.Command{ } type secretCreateOptions struct { - projectRef string - name string - data string + parentRef string + name string + data string } var secretCreateOpts secretCreateOptions @@ -46,7 +46,7 @@ var secretCreateOpts secretCreateOptions func init() { flags := cmdProjectSecretCreate.Flags() - flags.StringVar(&secretCreateOpts.projectRef, "project", "", "project id or full path)") + flags.StringVar(&secretCreateOpts.parentRef, "project", "", "project id or full path") flags.StringVarP(&secretCreateOpts.name, "name", "n", "", "secret name") flags.StringVar(&secretCreateOpts.data, "data", "", "json map of secret data") @@ -73,14 +73,14 @@ func secretCreate(cmd *cobra.Command, ownertype string, args []string) error { switch ownertype { case "project": log.Infof("creating project secret") - secret, _, err := gwclient.CreateProjectSecret(context.TODO(), secretCreateOpts.projectRef, req) + secret, _, err := gwclient.CreateProjectSecret(context.TODO(), secretCreateOpts.parentRef, req) if err != nil { return errors.Wrapf(err, "failed to create project secret") } log.Infof("project secret %q created, ID: %q", secret.Name, secret.ID) case "projectgroup": log.Infof("creating project group secret") - secret, _, err := gwclient.CreateProjectGroupSecret(context.TODO(), secretCreateOpts.projectRef, req) + secret, _, err := gwclient.CreateProjectGroupSecret(context.TODO(), secretCreateOpts.parentRef, req) if err != nil { return errors.Wrapf(err, "failed to create project group secret") } diff --git a/cmd/agola/cmd/projectsecretdelete.go b/cmd/agola/cmd/projectsecretdelete.go new file mode 100644 index 0000000..f85f4c9 --- /dev/null +++ b/cmd/agola/cmd/projectsecretdelete.go @@ -0,0 +1,76 @@ +// Copyright 2019 Sorint.lab +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + + "github.com/sorintlab/agola/internal/services/gateway/api" + + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var cmdProjectSecretDelete = &cobra.Command{ + Use: "delete", + Short: "delete a secret", + Run: func(cmd *cobra.Command, args []string) { + if err := secretDelete(cmd, "project", args); err != nil { + log.Fatalf("err: %v", err) + } + }, +} + +type secretDeleteOptions struct { + parentRef string + name string +} + +var secretDeleteOpts secretDeleteOptions + +func init() { + flags := cmdProjectSecretDelete.Flags() + + flags.StringVar(&secretDeleteOpts.parentRef, "project", "", "project id or full path") + flags.StringVarP(&secretDeleteOpts.name, "name", "n", "", "secret name") + + cmdProjectSecretDelete.MarkFlagRequired("projectgroup") + cmdProjectSecretDelete.MarkFlagRequired("name") + + cmdProjectSecret.AddCommand(cmdProjectSecretDelete) +} + +func secretDelete(cmd *cobra.Command, ownertype string, args []string) error { + gwclient := api.NewClient(gatewayURL, token) + + switch ownertype { + case "project": + log.Infof("deleting project secret") + _, err := gwclient.DeleteProjectSecret(context.TODO(), secretDeleteOpts.parentRef, secretDeleteOpts.name) + if err != nil { + return errors.Wrapf(err, "failed to delete project secret") + } + log.Infof("project secret deleted") + case "projectgroup": + log.Infof("deleting project group secret") + _, err := gwclient.DeleteProjectGroupSecret(context.TODO(), secretDeleteOpts.parentRef, secretDeleteOpts.name) + if err != nil { + return errors.Wrapf(err, "failed to delete project group secret") + } + log.Infof("project group secret deleted") + } + + return nil +} diff --git a/internal/services/gateway/api/client.go b/internal/services/gateway/api/client.go index f7c43f5..5689a81 100644 --- a/internal/services/gateway/api/client.go +++ b/internal/services/gateway/api/client.go @@ -168,6 +168,10 @@ func (c *Client) CreateProjectGroupSecret(ctx context.Context, projectGroupRef s return secret, resp, err } +func (c *Client) DeleteProjectGroupSecret(ctx context.Context, projectGroupRef, secretName string) (*http.Response, error) { + return c.getResponse(ctx, "DELETE", path.Join("/projectgroups", url.PathEscape(projectGroupRef), "secrets", secretName), nil, jsonContent, nil) +} + func (c *Client) CreateProjectSecret(ctx context.Context, projectRef string, req *CreateSecretRequest) (*SecretResponse, *http.Response, error) { reqj, err := json.Marshal(req) if err != nil { @@ -179,6 +183,10 @@ func (c *Client) CreateProjectSecret(ctx context.Context, projectRef string, req return secret, resp, err } +func (c *Client) DeleteProjectSecret(ctx context.Context, projectRef, secretName string) (*http.Response, error) { + return c.getResponse(ctx, "DELETE", path.Join("/projects", url.PathEscape(projectRef), "secrets", secretName), nil, jsonContent, nil) +} + func (c *Client) CreateProjectGroupVariable(ctx context.Context, projectGroupRef string, req *CreateVariableRequest) (*VariableResponse, *http.Response, error) { reqj, err := json.Marshal(req) if err != nil {