gateway: api implement get secrets removeoverridden

Implement missing removeoverridden option for get secrets.
This commit is contained in:
Simone Gotti 2019-09-20 15:31:22 +02:00
parent c3ca892f00
commit f5c0e91f39
3 changed files with 55 additions and 4 deletions

View File

@ -0,0 +1,42 @@
// 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 common
import (
csapitypes "agola.io/agola/services/configstore/api/types"
)
func FilterOverriddenSecrets(secrets []*csapitypes.Secret) []*csapitypes.Secret {
secretsMap := map[string]*csapitypes.Secret{}
for _, s := range secrets {
if _, ok := secretsMap[s.Name]; !ok {
secretsMap[s.Name] = s
}
}
filteredSecrets := make([]*csapitypes.Secret, len(secretsMap))
i := 0
// keep the original order
for _, s := range secrets {
if _, ok := secretsMap[s.Name]; !ok {
continue
}
filteredSecrets[i] = s
delete(secretsMap, s.Name)
i++
}
return filteredSecrets
}

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"net/http" "net/http"
"agola.io/agola/internal/services/common"
"agola.io/agola/internal/util" "agola.io/agola/internal/util"
csapitypes "agola.io/agola/services/configstore/api/types" csapitypes "agola.io/agola/services/configstore/api/types"
cstypes "agola.io/agola/services/configstore/types" cstypes "agola.io/agola/services/configstore/types"
@ -29,7 +30,8 @@ type GetSecretsRequest struct {
ParentType cstypes.ConfigType ParentType cstypes.ConfigType
ParentRef string ParentRef string
Tree bool Tree bool
RemoveOverridden bool
} }
func (h *ActionHandler) GetSecrets(ctx context.Context, req *GetSecretsRequest) ([]*csapitypes.Secret, error) { func (h *ActionHandler) GetSecrets(ctx context.Context, req *GetSecretsRequest) ([]*csapitypes.Secret, error) {
@ -46,6 +48,11 @@ func (h *ActionHandler) GetSecrets(ctx context.Context, req *GetSecretsRequest)
return nil, ErrFromRemote(resp, err) return nil, ErrFromRemote(resp, err)
} }
if req.RemoveOverridden {
// remove overriden secrets
cssecrets = common.FilterOverriddenSecrets(cssecrets)
}
return cssecrets, nil return cssecrets, nil
} }

View File

@ -49,6 +49,7 @@ func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
query := r.URL.Query() query := r.URL.Query()
_, tree := query["tree"] _, tree := query["tree"]
_, removeoverridden := query["removeoverridden"]
parentType, parentRef, err := GetConfigTypeRef(r) parentType, parentRef, err := GetConfigTypeRef(r)
if httpError(w, err) { if httpError(w, err) {
@ -57,9 +58,10 @@ func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
areq := &action.GetSecretsRequest{ areq := &action.GetSecretsRequest{
ParentType: parentType, ParentType: parentType,
ParentRef: parentRef, ParentRef: parentRef,
Tree: tree, Tree: tree,
RemoveOverridden: removeoverridden,
} }
cssecrets, err := h.ah.GetSecrets(ctx, areq) cssecrets, err := h.ah.GetSecrets(ctx, areq)
if httpError(w, err) { if httpError(w, err) {