Skip to content

Commit

Permalink
Merge pull request #988 from ThisIsClark/master
Browse files Browse the repository at this point in the history
Do not allow two profiles have same name
  • Loading branch information
xxwjj committed Aug 26, 2019
2 parents 30487b3 + 4df90cb commit 3bcecde
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions install/devsds/lib/opensds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ osds::opensds::install(){

export OPENSDS_AUTH_STRATEGY=$OPENSDS_AUTH_STRATEGY
export OPENSDS_ENDPOINT=http://localhost:50040
build/out/bin/osdsctl profile create '{"name": "default", "description": "default policy", "storageType": "block"}'
build/out/bin/osdsctl profile create '{"name": "default", "description": "default policy", "storageType": "file", "provisioningProperties":{"ioConnectivity": {"accessProtocol": "NFS"},"DataStorage":{"StorageAccessCapability":["Read","Write","Execute"]}}}'
build/out/bin/osdsctl profile create '{"name": "default_block", "description": "default policy", "storageType": "block"}'
build/out/bin/osdsctl profile create '{"name": "default_file", "description": "default policy", "storageType": "file", "provisioningProperties":{"ioConnectivity": {"accessProtocol": "NFS"},"DataStorage":{"StorageAccessCapability":["Read","Write","Execute"]}}}'

if [ $? == 0 ]; then
osds::echo_summary devsds installed successfully !!
Expand Down
38 changes: 29 additions & 9 deletions pkg/db/drivers/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ import (
)

const (
defaultLimit = 50
defaultOffset = 0
defaultSortDir = "desc"
defaultSortKey = "ID"
defaultLimit = 50
defaultOffset = 0
defaultSortDir = "desc"
defaultSortKey = "ID"
defaultBlockProfileName = "default_block"
defaultFileProfileName = "default_file"
typeBlock = "block"
typeFile = "file"
)

var validKey = []string{"limit", "offset", "sortDir", "sortKey"}
Expand Down Expand Up @@ -1434,8 +1438,8 @@ func (c *Client) CreateProfile(ctx *c.Context, prf *model.ProfileSpec) (*model.P
prf.CreatedAt = time.Now().Format(constants.TimeFormat)
}

// profile name must be unique with the same storage type.
if _, err := c.getProfileByName(ctx, prf.Name, prf.StorageType); err == nil {
// profile name must be unique.
if _, err := c.getProfileByName(ctx, prf.Name); err == nil {
return nil, fmt.Errorf("the profile name '%s' already exists", prf.Name)
}

Expand Down Expand Up @@ -1476,7 +1480,23 @@ func (c *Client) GetProfile(ctx *c.Context, prfID string) (*model.ProfileSpec, e
return prf, nil
}

func (c *Client) getProfileByName(ctx *c.Context, name, storageType string) (*model.ProfileSpec, error) {
func (c *Client) getProfileByName(ctx *c.Context, name string) (*model.ProfileSpec, error) {
profiles, err := c.ListProfiles(ctx)
if err != nil {
log.Error("List profile failed: ", err)
return nil, err
}

for _, profile := range profiles {
if profile.Name == name {
return profile, nil
}
}
var msg = fmt.Sprintf("can't find profile(name: %s)", name)
return nil, model.NewNotFoundError(msg)
}

func (c *Client) getProfileByNameAndType(ctx *c.Context, name, storageType string) (*model.ProfileSpec, error) {
profiles, err := c.ListProfiles(ctx)
if err != nil {
log.Error("List profile failed: ", err)
Expand All @@ -1494,12 +1514,12 @@ func (c *Client) getProfileByName(ctx *c.Context, name, storageType string) (*mo

// GetDefaultProfile
func (c *Client) GetDefaultProfile(ctx *c.Context) (*model.ProfileSpec, error) {
return c.getProfileByName(ctx, "default", "block")
return c.getProfileByNameAndType(ctx, defaultBlockProfileName, typeBlock)
}

// GetDefaultProfileFileShare
func (c *Client) GetDefaultProfileFileShare(ctx *c.Context) (*model.ProfileSpec, error) {
return c.getProfileByName(ctx, "default", "file")
return c.getProfileByNameAndType(ctx, defaultFileProfileName, typeFile)
}

// ListProfiles
Expand Down

0 comments on commit 3bcecde

Please sign in to comment.