From f0e7ce4a96687b8c02e7a41435eed33863a17de9 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Tue, 2 Apr 2019 11:07:39 +0200 Subject: [PATCH] cmd: implement project/projectgroup variable create --- cmd/agola/cmd/projectcreate.go | 2 +- cmd/agola/cmd/projectgroupcreate.go | 10 +-- cmd/agola/cmd/projectgroupsecret.go | 28 ++++++ cmd/agola/cmd/projectgroupsecretcreate.go | 41 +++++++++ cmd/agola/cmd/projectgroupvariable.go | 28 ++++++ cmd/agola/cmd/projectgroupvariablecreate.go | 43 +++++++++ cmd/agola/cmd/projectsecretcreate.go | 39 ++++---- cmd/agola/cmd/projectvariable.go | 2 +- cmd/agola/cmd/projectvariablecreate.go | 89 +++++++++++++++++++ internal/services/configstore/api/client.go | 20 ++--- internal/services/configstore/api/variable.go | 3 + .../services/configstore/command/command.go | 3 + internal/services/gateway/api/client.go | 67 ++++++-------- internal/services/gateway/api/variable.go | 12 ++- 14 files changed, 313 insertions(+), 74 deletions(-) create mode 100644 cmd/agola/cmd/projectgroupsecret.go create mode 100644 cmd/agola/cmd/projectgroupsecretcreate.go create mode 100644 cmd/agola/cmd/projectgroupvariable.go create mode 100644 cmd/agola/cmd/projectgroupvariablecreate.go create mode 100644 cmd/agola/cmd/projectvariablecreate.go diff --git a/cmd/agola/cmd/projectcreate.go b/cmd/agola/cmd/projectcreate.go index bc96e0d..68ef1b3 100644 --- a/cmd/agola/cmd/projectcreate.go +++ b/cmd/agola/cmd/projectcreate.go @@ -50,7 +50,7 @@ func init() { flags.StringVar(&projectCreateOpts.repoURL, "repo-url", "", "repository url") flags.StringVar(&projectCreateOpts.remoteSourceName, "remote-source", "", "remote source name") flags.BoolVarP(&projectCreateOpts.skipSSHHostKeyCheck, "skip-ssh-host-key-check", "s", false, "skip ssh host key check") - flags.StringVar(&projectCreateOpts.parentPath, "parent", "", `parent project group path (i.e "org/org01" for root project group in org01, "/user/user01/group01/subgroub01") or project group id where the project should be created`) + flags.StringVar(&projectCreateOpts.parentPath, "parent", "", `parent project group path (i.e "org/org01" for root project group in org01, "user/user01/group01/subgroub01") or project group id where the project should be created`) cmdProjectCreate.MarkFlagRequired("name") cmdProjectCreate.MarkFlagRequired("parent") diff --git a/cmd/agola/cmd/projectgroupcreate.go b/cmd/agola/cmd/projectgroupcreate.go index 0816986..27ad939 100644 --- a/cmd/agola/cmd/projectgroupcreate.go +++ b/cmd/agola/cmd/projectgroupcreate.go @@ -25,7 +25,7 @@ import ( var cmdProjectGroupCreate = &cobra.Command{ Use: "create", - Short: "create a project", + Short: "create a project group", Run: func(cmd *cobra.Command, args []string) { if err := projectGroupCreate(cmd, args); err != nil { log.Fatalf("err: %v", err) @@ -43,8 +43,8 @@ var projectGroupCreateOpts projectGroupCreateOptions func init() { flags := cmdProjectGroupCreate.Flags() - flags.StringVarP(&projectGroupCreateOpts.name, "name", "n", "", "project name") - flags.StringVar(&projectGroupCreateOpts.parentPath, "parent", "", `parent project group path (i.e "org/org01" for root project group in org01, "/user/user01/group01/subgroub01") or project group id where the project group should be created`) + flags.StringVarP(&projectGroupCreateOpts.name, "name", "n", "", "project group name") + flags.StringVar(&projectGroupCreateOpts.parentPath, "parent", "", `parent project group path (i.e "org/org01" for root project group in org01, "user/user01/group01/subgroub01") or project group id where the project group should be created`) cmdProjectGroupCreate.MarkFlagRequired("name") cmdProjectGroupCreate.MarkFlagRequired("parent") @@ -64,9 +64,9 @@ func projectGroupCreate(cmd *cobra.Command, args []string) error { project, _, err := gwclient.CreateProjectGroup(context.TODO(), req) if err != nil { - return errors.Wrapf(err, "failed to create project") + return errors.Wrapf(err, "failed to create project group") } - log.Infof("project %s created, ID: %s", project.Name, project.ID) + log.Infof("project group %s created, ID: %s", project.Name, project.ID) return nil } diff --git a/cmd/agola/cmd/projectgroupsecret.go b/cmd/agola/cmd/projectgroupsecret.go new file mode 100644 index 0000000..91c259b --- /dev/null +++ b/cmd/agola/cmd/projectgroupsecret.go @@ -0,0 +1,28 @@ +// 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 cmdProjectGroupSecret = &cobra.Command{ + Use: "secret", + Short: "secret", +} + +func init() { + cmdProjectGroup.AddCommand(cmdProjectGroupSecret) +} diff --git a/cmd/agola/cmd/projectgroupsecretcreate.go b/cmd/agola/cmd/projectgroupsecretcreate.go new file mode 100644 index 0000000..f3b3804 --- /dev/null +++ b/cmd/agola/cmd/projectgroupsecretcreate.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 cmdProjectGroupSecretCreate = &cobra.Command{ + Use: "create", + Short: "create a project group secret", + Run: func(cmd *cobra.Command, args []string) { + if err := secretCreate(cmd, "projectgroup", args); err != nil { + log.Fatalf("err: %v", err) + } + }, +} + +func init() { + flags := cmdProjectGroupSecretCreate.Flags() + + flags.StringVar(&secretCreateOpts.projectRef, "project", "", "project id or full path") + flags.StringVarP(&secretCreateOpts.name, "name", "n", "", "secret name") + + cmdProjectGroupSecretCreate.MarkFlagRequired("project") + cmdProjectGroupSecretCreate.MarkFlagRequired("name") + + cmdProjectGroupSecret.AddCommand(cmdProjectGroupSecretCreate) +} diff --git a/cmd/agola/cmd/projectgroupvariable.go b/cmd/agola/cmd/projectgroupvariable.go new file mode 100644 index 0000000..31ed265 --- /dev/null +++ b/cmd/agola/cmd/projectgroupvariable.go @@ -0,0 +1,28 @@ +// 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 cmdProjectGroupVariable = &cobra.Command{ + Use: "variable", + Short: "variable", +} + +func init() { + cmdProjectGroup.AddCommand(cmdProjectGroupVariable) +} diff --git a/cmd/agola/cmd/projectgroupvariablecreate.go b/cmd/agola/cmd/projectgroupvariablecreate.go new file mode 100644 index 0000000..7558760 --- /dev/null +++ b/cmd/agola/cmd/projectgroupvariablecreate.go @@ -0,0 +1,43 @@ +// 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 cmdProjectGroupVariableCreate = &cobra.Command{ + Use: "create", + Short: "create a project variable", + Run: func(cmd *cobra.Command, args []string) { + if err := variableCreate(cmd, "projectgroup", args); err != nil { + log.Fatalf("err: %v", err) + } + }, +} + +func init() { + flags := cmdProjectGroupVariableCreate.Flags() + + flags.StringVar(&variableCreateOpts.parentRef, "projectgroup", "", "project group id or full path") + flags.StringVarP(&variableCreateOpts.name, "name", "n", "", "variable name") + flags.StringVar(&variableCreateOpts.values, "values", "", "json list of values and conditions") + + cmdProjectGroupVariableCreate.MarkFlagRequired("project") + cmdProjectGroupVariableCreate.MarkFlagRequired("name") + cmdProjectGroupVariableCreate.MarkFlagRequired("values") + + cmdProjectGroupVariable.AddCommand(cmdProjectGroupVariableCreate) +} diff --git a/cmd/agola/cmd/projectsecretcreate.go b/cmd/agola/cmd/projectsecretcreate.go index 77867bc..d57a6ff 100644 --- a/cmd/agola/cmd/projectsecretcreate.go +++ b/cmd/agola/cmd/projectsecretcreate.go @@ -16,7 +16,6 @@ package cmd import ( "context" - "net/url" "github.com/pkg/errors" "github.com/sorintlab/agola/internal/services/gateway/api" @@ -27,24 +26,24 @@ var cmdProjectSecretCreate = &cobra.Command{ Use: "create", Short: "create a project secret", Run: func(cmd *cobra.Command, args []string) { - if err := projectSecretCreate(cmd, args); err != nil { + if err := secretCreate(cmd, "project", args); err != nil { log.Fatalf("err: %v", err) } }, } -type projectSecretCreateOptions struct { - projectID string - name string +type secretCreateOptions struct { + projectRef string + name string } -var projectSecretCreateOpts projectSecretCreateOptions +var secretCreateOpts secretCreateOptions func init() { flags := cmdProjectSecretCreate.Flags() - flags.StringVar(&projectSecretCreateOpts.projectID, "project", "", "project id or full path)") - flags.StringVarP(&projectSecretCreateOpts.name, "name", "n", "", "secret name") + flags.StringVar(&secretCreateOpts.projectRef, "project", "", "project id or full path)") + flags.StringVarP(&secretCreateOpts.name, "name", "n", "", "secret name") cmdProjectSecretCreate.MarkFlagRequired("project") cmdProjectSecretCreate.MarkFlagRequired("name") @@ -52,19 +51,29 @@ func init() { cmdProjectSecret.AddCommand(cmdProjectSecretCreate) } -func projectSecretCreate(cmd *cobra.Command, args []string) error { +func secretCreate(cmd *cobra.Command, ownertype string, args []string) error { gwclient := api.NewClient(gatewayURL, token) req := &api.CreateSecretRequest{ - Name: projectSecretCreateOpts.name, + Name: secretCreateOpts.name, } - log.Infof("creating project secret") - secret, _, err := gwclient.CreateProjectSecret(context.TODO(), url.PathEscape(projectSecretCreateOpts.projectID), req) - if err != nil { - return errors.Wrapf(err, "failed to create project secret") + switch ownertype { + case "project": + log.Infof("creating project secret") + secret, _, err := gwclient.CreateProjectSecret(context.TODO(), secretCreateOpts.projectRef, 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) + if err != nil { + return errors.Wrapf(err, "failed to create project group secret") + } + log.Infof("project group secret %q created, ID: %q", secret.Name, secret.ID) } - log.Infof("project secret %q created, ID: %q", secret.Name, secret.ID) return nil } diff --git a/cmd/agola/cmd/projectvariable.go b/cmd/agola/cmd/projectvariable.go index 06fd07b..4c4bdf6 100644 --- a/cmd/agola/cmd/projectvariable.go +++ b/cmd/agola/cmd/projectvariable.go @@ -24,5 +24,5 @@ var cmdProjectVariable = &cobra.Command{ } func init() { - cmdUser.AddCommand(cmdProjectVariable) + cmdProject.AddCommand(cmdProjectVariable) } diff --git a/cmd/agola/cmd/projectvariablecreate.go b/cmd/agola/cmd/projectvariablecreate.go new file mode 100644 index 0000000..5291f14 --- /dev/null +++ b/cmd/agola/cmd/projectvariablecreate.go @@ -0,0 +1,89 @@ +// 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" + "encoding/json" + + "github.com/pkg/errors" + "github.com/sorintlab/agola/internal/services/gateway/api" + "github.com/sorintlab/agola/internal/services/types" + "github.com/spf13/cobra" +) + +var cmdProjectVariableCreate = &cobra.Command{ + Use: "create", + Short: "create a project variable", + Run: func(cmd *cobra.Command, args []string) { + if err := variableCreate(cmd, "project", args); err != nil { + log.Fatalf("err: %v", err) + } + }, +} + +type variableCreateOptions struct { + parentRef string + name string + values string +} + +var variableCreateOpts variableCreateOptions + +func init() { + flags := cmdProjectVariableCreate.Flags() + + flags.StringVar(&variableCreateOpts.parentRef, "project", "", "project id or full path") + flags.StringVarP(&variableCreateOpts.name, "name", "n", "", "variable name") + flags.StringVar(&variableCreateOpts.values, "values", "", "json list of values and conditions") + + cmdProjectVariableCreate.MarkFlagRequired("project") + cmdProjectVariableCreate.MarkFlagRequired("name") + cmdProjectVariableCreate.MarkFlagRequired("values") + + cmdProjectVariable.AddCommand(cmdProjectVariableCreate) +} + +func variableCreate(cmd *cobra.Command, ownertype string, args []string) error { + gwclient := api.NewClient(gatewayURL, token) + + var values []types.VariableValue + if err := json.Unmarshal([]byte(variableCreateOpts.values), &values); err != nil { + log.Fatalf("failed to unmarshall values: %v", err) + } + req := &api.CreateVariableRequest{ + Name: variableCreateOpts.name, + Values: values, + } + + switch ownertype { + case "project": + log.Infof("creating project variable") + variable, _, err := gwclient.CreateProjectVariable(context.TODO(), variableCreateOpts.parentRef, req) + if err != nil { + return errors.Wrapf(err, "failed to create project variable") + } + log.Infof("project variable %q created, ID: %q", variable.Name, variable.ID) + case "projectgroup": + log.Infof("creating project group variable") + variable, _, err := gwclient.CreateProjectGroupVariable(context.TODO(), variableCreateOpts.parentRef, req) + if err != nil { + return errors.Wrapf(err, "failed to create project group variable") + } + log.Infof("project group variable %q created, ID: %q", variable.Name, variable.ID) + } + + return nil +} diff --git a/internal/services/configstore/api/client.go b/internal/services/configstore/api/client.go index 99748ae..78f7c19 100644 --- a/internal/services/configstore/api/client.go +++ b/internal/services/configstore/api/client.go @@ -108,21 +108,21 @@ func (c *Client) getParsedResponse(ctx context.Context, method, path string, que return resp, d.Decode(obj) } -func (c *Client) GetProjectGroup(ctx context.Context, projectGroupID string) (*types.ProjectGroup, *http.Response, error) { +func (c *Client) GetProjectGroup(ctx context.Context, projectGroupRef string) (*types.ProjectGroup, *http.Response, error) { projectGroup := new(types.ProjectGroup) - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s", url.PathEscape(projectGroupID)), nil, jsonContent, nil, projectGroup) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s", url.PathEscape(projectGroupRef)), nil, jsonContent, nil, projectGroup) return projectGroup, resp, err } -func (c *Client) GetProjectGroupSubgroups(ctx context.Context, projectGroupID string) ([]*types.ProjectGroup, *http.Response, error) { +func (c *Client) GetProjectGroupSubgroups(ctx context.Context, projectGroupRef string) ([]*types.ProjectGroup, *http.Response, error) { projectGroups := []*types.ProjectGroup{} - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/subgroups", url.PathEscape(projectGroupID)), nil, jsonContent, nil, &projectGroups) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/subgroups", url.PathEscape(projectGroupRef)), nil, jsonContent, nil, &projectGroups) return projectGroups, resp, err } -func (c *Client) GetProjectGroupProjects(ctx context.Context, projectGroupID string) ([]*types.Project, *http.Response, error) { +func (c *Client) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*types.Project, *http.Response, error) { projects := []*types.Project{} - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/projects", url.PathEscape(projectGroupID)), nil, jsonContent, nil, &projects) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/projects", url.PathEscape(projectGroupRef)), nil, jsonContent, nil, &projects) return projects, resp, err } @@ -137,9 +137,9 @@ func (c *Client) CreateProjectGroup(ctx context.Context, projectGroup *types.Pro return projectGroup, resp, err } -func (c *Client) GetProject(ctx context.Context, projectID string) (*types.Project, *http.Response, error) { +func (c *Client) GetProject(ctx context.Context, projectRef string) (*types.Project, *http.Response, error) { project := new(types.Project) - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projects/%s", url.PathEscape(projectID)), nil, jsonContent, nil, project) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projects/%s", url.PathEscape(projectRef)), nil, jsonContent, nil, project) return project, resp, err } @@ -154,8 +154,8 @@ func (c *Client) CreateProject(ctx context.Context, project *types.Project) (*ty return project, resp, err } -func (c *Client) DeleteProject(ctx context.Context, projectID string) (*http.Response, error) { - return c.getResponse(ctx, "DELETE", fmt.Sprintf("/projects/%s", url.PathEscape(projectID)), nil, jsonContent, nil) +func (c *Client) DeleteProject(ctx context.Context, projectRef string) (*http.Response, error) { + return c.getResponse(ctx, "DELETE", fmt.Sprintf("/projects/%s", url.PathEscape(projectRef)), nil, jsonContent, nil) } func (c *Client) GetProjectGroupSecrets(ctx context.Context, projectGroupRef string, tree bool) ([]*types.Secret, *http.Response, error) { diff --git a/internal/services/configstore/api/variable.go b/internal/services/configstore/api/variable.go index dd17c7a..ab45c4c 100644 --- a/internal/services/configstore/api/variable.go +++ b/internal/services/configstore/api/variable.go @@ -22,6 +22,7 @@ import ( "github.com/sorintlab/agola/internal/services/configstore/command" "github.com/sorintlab/agola/internal/services/configstore/readdb" "github.com/sorintlab/agola/internal/services/types" + "github.com/sorintlab/agola/internal/util" "github.com/gorilla/mux" "go.uber.org/zap" @@ -107,6 +108,8 @@ func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request variable.Parent.Type = parentType variable.Parent.ID = parentRef + h.log.Infof("variable: %s", util.Dump(variable)) + variable, err = h.ch.CreateVariable(ctx, variable) if httpError(w, err) { h.log.Errorf("err: %+v", err) diff --git a/internal/services/configstore/command/command.go b/internal/services/configstore/command/command.go index cd9f3eb..0ac783f 100644 --- a/internal/services/configstore/command/command.go +++ b/internal/services/configstore/command/command.go @@ -1007,6 +1007,9 @@ func (s *CommandHandler) CreateVariable(ctx context.Context, variable *types.Var if variable.Name == "" { return nil, util.NewErrBadRequest(errors.Errorf("variable name required")) } + if len(variable.Values) == 0 { + return nil, util.NewErrBadRequest(errors.Errorf("variable values required")) + } if variable.Parent.Type == "" { return nil, util.NewErrBadRequest(errors.Errorf("variable parent type required")) } diff --git a/internal/services/gateway/api/client.go b/internal/services/gateway/api/client.go index fc30d93..ca35f9d 100644 --- a/internal/services/gateway/api/client.go +++ b/internal/services/gateway/api/client.go @@ -113,27 +113,27 @@ func (c *Client) getParsedResponse(ctx context.Context, method, path string, que return resp, d.Decode(obj) } -func (c *Client) GetProjectGroup(ctx context.Context, projectGroupID string) (*ProjectGroupResponse, *http.Response, error) { +func (c *Client) GetProjectGroup(ctx context.Context, projectGroupRef string) (*ProjectGroupResponse, *http.Response, error) { projectGroup := new(ProjectGroupResponse) - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s", url.PathEscape(projectGroupID)), nil, jsonContent, nil, projectGroup) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s", url.PathEscape(projectGroupRef)), nil, jsonContent, nil, projectGroup) return projectGroup, resp, err } -func (c *Client) GetProjectGroupSubgroups(ctx context.Context, projectGroupID string) ([]*ProjectGroupResponse, *http.Response, error) { +func (c *Client) GetProjectGroupSubgroups(ctx context.Context, projectGroupRef string) ([]*ProjectGroupResponse, *http.Response, error) { projectGroups := []*ProjectGroupResponse{} - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/subgroups", url.PathEscape(projectGroupID)), nil, jsonContent, nil, &projectGroups) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/subgroups", url.PathEscape(projectGroupRef)), nil, jsonContent, nil, &projectGroups) return projectGroups, resp, err } -func (c *Client) GetProjectGroupProjects(ctx context.Context, projectGroupID string) ([]*ProjectResponse, *http.Response, error) { +func (c *Client) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*ProjectResponse, *http.Response, error) { projects := []*ProjectResponse{} - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/projects", url.PathEscape(projectGroupID)), nil, jsonContent, nil, &projects) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projectgroups/%s/projects", url.PathEscape(projectGroupRef)), nil, jsonContent, nil, &projects) return projects, resp, err } -func (c *Client) GetProject(ctx context.Context, projectID string) (*types.Project, *http.Response, error) { +func (c *Client) GetProject(ctx context.Context, projectRef string) (*types.Project, *http.Response, error) { project := new(types.Project) - resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projects/%s", url.PathEscape(projectID)), nil, jsonContent, nil, project) + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projects/%s", url.PathEscape(projectRef)), nil, jsonContent, nil, project) return project, resp, err } @@ -159,65 +159,56 @@ func (c *Client) CreateProject(ctx context.Context, req *CreateProjectRequest) ( return project, resp, err } -func (c *Client) CreateProjectGroupSecret(ctx context.Context, projectGroupID string, req *CreateSecretRequest) (*SecretResponse, *http.Response, error) { +func (c *Client) CreateProjectGroupSecret(ctx context.Context, projectGroupRef string, req *CreateSecretRequest) (*SecretResponse, *http.Response, error) { reqj, err := json.Marshal(req) if err != nil { return nil, nil, err } secret := new(SecretResponse) - resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projectgroups", projectGroupID, "secrets"), nil, jsonContent, bytes.NewReader(reqj), secret) + resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projectgroups", url.PathEscape(projectGroupRef), "secrets"), nil, jsonContent, bytes.NewReader(reqj), secret) return secret, resp, err } -func (c *Client) CreateProjectSecret(ctx context.Context, projectID string, req *CreateSecretRequest) (*SecretResponse, *http.Response, error) { +func (c *Client) CreateProjectSecret(ctx context.Context, projectRef string, req *CreateSecretRequest) (*SecretResponse, *http.Response, error) { reqj, err := json.Marshal(req) if err != nil { return nil, nil, err } secret := new(SecretResponse) - resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projects", projectID, "secrets"), nil, jsonContent, bytes.NewReader(reqj), secret) + resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projects", url.PathEscape(projectRef), "secrets"), nil, jsonContent, bytes.NewReader(reqj), secret) return secret, resp, err } -func (c *Client) createSecret(ctx context.Context, containertype, containerid string, req *CreateSecretRequest) (*SecretResponse, *http.Response, error) { +func (c *Client) CreateProjectGroupVariable(ctx context.Context, projectGroupRef string, req *CreateVariableRequest) (*VariableResponse, *http.Response, error) { reqj, err := json.Marshal(req) if err != nil { return nil, nil, err } - var basepath string - switch containertype { - case "project": - basepath = "projects" - default: - return nil, nil, fmt.Errorf("invalid container type") + variable := new(VariableResponse) + resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projectgroups", url.PathEscape(projectGroupRef), "variables"), nil, jsonContent, bytes.NewReader(reqj), variable) + return variable, resp, err +} + +func (c *Client) CreateProjectVariable(ctx context.Context, projectRef string, req *CreateVariableRequest) (*VariableResponse, *http.Response, error) { + reqj, err := json.Marshal(req) + if err != nil { + return nil, nil, err } - secret := new(SecretResponse) - resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/", basepath, containerid, "secrets"), nil, jsonContent, bytes.NewReader(reqj), secret) - return secret, resp, err + variable := new(VariableResponse) + resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projects", url.PathEscape(projectRef), "variables"), nil, jsonContent, bytes.NewReader(reqj), variable) + return variable, resp, err } -func (c *Client) DeleteCurrentUserProject(ctx context.Context, projectName string) (*http.Response, error) { - return c.deleteProject(ctx, "user", "", projectName) +func (c *Client) DeleteProject(ctx context.Context, projectRef string) (*http.Response, error) { + return c.getResponse(ctx, "DELETE", fmt.Sprintf("/projects/%s", url.PathEscape(projectRef)), nil, jsonContent, nil) } -func (c *Client) DeleteUserProject(ctx context.Context, username, projectName string) (*http.Response, error) { - return c.deleteProject(ctx, "user", username, projectName) -} - -func (c *Client) DeleteOrgProject(ctx context.Context, orgname, projectName string) (*http.Response, error) { - return c.deleteProject(ctx, "org", orgname, projectName) -} - -func (c *Client) deleteProject(ctx context.Context, ownertype, ownername, projectName string) (*http.Response, error) { - return c.getResponse(ctx, "DELETE", path.Join("/projects", ownertype, ownername, projectName), nil, jsonContent, nil) -} - -func (c *Client) ReconfigProject(ctx context.Context, projectName string) (*http.Response, error) { - return c.getResponse(ctx, "POST", fmt.Sprintf("/projects/%s/reconfig", projectName), nil, jsonContent, nil) +func (c *Client) ReconfigProject(ctx context.Context, projectRef string) (*http.Response, error) { + return c.getResponse(ctx, "POST", fmt.Sprintf("/projects/%s/reconfig", url.PathEscape(projectRef)), nil, jsonContent, nil) } func (c *Client) GetUser(ctx context.Context, userID string) (*types.User, *http.Response, error) { diff --git a/internal/services/gateway/api/variable.go b/internal/services/gateway/api/variable.go index 300e148..6c8d488 100644 --- a/internal/services/gateway/api/variable.go +++ b/internal/services/gateway/api/variable.go @@ -141,10 +141,7 @@ func (h *VariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { type CreateVariableRequest struct { Name string `json:"name,omitempty"` - SecretName string `json:"secret_name,omitempty"` - SecretVar string `json:"secret_var,omitempty"` - - When *types.When `json:"when,omitempty"` + Values []types.VariableValue `json:"values,omitempty"` } type CreateVariableHandler struct { @@ -177,12 +174,19 @@ func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request http.Error(w, err.Error(), http.StatusBadRequest) } + if len(req.Values) == 0 { + err := errors.Errorf("empty variable values") + h.log.Errorf("err: %+v", err) + http.Error(w, err.Error(), http.StatusBadRequest) + } + v := &types.Variable{ Name: req.Name, Parent: types.Parent{ Type: parentType, ID: parentRef, }, + Values: req.Values, } var cssecrets []*types.Secret