Skip to content

Commit

Permalink
CPDEV-108872. resourcePrefix intersections. Return error, when create…
Browse files Browse the repository at this point in the history
… database with duplicate prefix
  • Loading branch information
alty1224 committed Dec 26, 2024
1 parent 7a74d43 commit b684ecc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ func (bp BaseProvider) createDatabase(requestOnCreateDb DbCreateRequest, ctx con
}
}

if ok, err := common.CheckPrefixUniqueness(prefix, ctx, bp.opensearch.Client); !ok {
if err != nil {
return nil, err
}
}

resourcesToCreate := requestOnCreateDb.Settings.CreateOnly
if len(resourcesToCreate) == 0 {
resourcesToCreate = []string{common.UserKind, common.IndexKind}
Expand Down
31 changes: 30 additions & 1 deletion common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/json"
"fmt"
"git.netcracker.com/PROD.Platform.ElasticStack/dbaas-opensearch-adapter/api"
"io"
"io/fs"
"log"
Expand Down Expand Up @@ -243,3 +242,33 @@ func CheckPrefixUniqueness(prefix string, ctx context.Context, opensearchcli Cli
}
return true, nil
}

func CheckPrefixUniqueness(prefix string, ctx context.Context, opensearchcli Client) (bool, error) {
logger.InfoContext(ctx, "Checking user prefix uniqueness during restoration with renaming")
getUsersRequest := api.GetUsersRequest{}
response, err := getUsersRequest.Do(context.Background(), opensearchcli)
if err != nil {
return false, fmt.Errorf("failed to receive users: %+v", err)
}
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
var users map[string]User
err = ProcessBody(response.Body, &users)
if err != nil {
return false, err
}
for element, user := range users {
if strings.HasPrefix(element, prefix) {
logger.ErrorContext(ctx, fmt.Sprintf("provided prefix already exists or a part of another prefix: %+v", prefix))
return false, fmt.Errorf("provided prefix already exists or a part of another prefix: %+v", prefix)
}
if user.Attributes[resourcePrefixAttributeName] != "" && strings.HasPrefix(user.Attributes[resourcePrefixAttributeName], prefix) {
logger.ErrorContext(ctx, fmt.Sprintf("provided prefix already exists or a part of another prefix: %+v", prefix))
return false, fmt.Errorf("provided prefix already exists or a part of another prefix: %+v", prefix)
}
}
} else if response.StatusCode == http.StatusNotFound {
return true, nil
}
return true, nil
}

0 comments on commit b684ecc

Please sign in to comment.