diff --git a/basic/basic.go b/basic/basic.go index a52a56e..26b0b71 100644 --- a/basic/basic.go +++ b/basic/basic.go @@ -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} diff --git a/common/common.go b/common/common.go index deee308..1235069 100644 --- a/common/common.go +++ b/common/common.go @@ -18,7 +18,6 @@ import ( "context" "encoding/json" "fmt" - "git.netcracker.com/PROD.Platform.ElasticStack/dbaas-opensearch-adapter/api" "io" "io/fs" "log" @@ -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 +}