Skip to content

Commit

Permalink
Merge pull request #300 from opensds/development
Browse files Browse the repository at this point in the history
Merge development to master branch so as to publish next release
  • Loading branch information
leonwanghui committed Mar 10, 2018
2 parents a193a1a + c169f18 commit b52f1b6
Show file tree
Hide file tree
Showing 72 changed files with 3,284 additions and 654 deletions.
70 changes: 63 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"github.com/opensds/opensds/pkg/utils/constants"
)

const (
Keystone = "keystone" // Keystone == 0
Noauth = "noauth"
)

// Client is a struct for exposing some operations of opensds resources.
type Client struct {
*ProfileMgr
Expand All @@ -31,26 +36,77 @@ type Client struct {
cfg *Config
}

type AuthOptions interface {
GetTenantId() string
}

func NewKeystoneAuthOptions() *KeystoneAuthOptions {
return &KeystoneAuthOptions{}
}

type KeystoneAuthOptions struct {
IdentityEndpoint string
Username string
UserID string
Password string
DomainID string
DomainName string
TenantID string
TenantName string
AllowReauth bool
TokenID string
}

func (k *KeystoneAuthOptions) GetTenantId() string {
return k.TenantID
}

func NewNoauthOptions(tenantId string) *NoAuthOptions {
return &NoAuthOptions{TenantID: tenantId}
}

type NoAuthOptions struct {
TenantID string
}

func (n *NoAuthOptions) GetTenantId() string {
return n.TenantID
}

// Config is a struct that defines some options for calling the Client.
type Config struct {
Endpoint string
Endpoint string
AuthOptions AuthOptions
}

// NewClient method creates a new Client.
func NewClient(c *Config) *Client {
// If endpoint field not specified,use the default value localhost.
if c.Endpoint == "" {
c.Endpoint = constants.DefaultOpensdsEndpoint
log.Printf("OpenSDS Endpoint is not specified using the default value(%s)", c.Endpoint)
log.Printf("Warnning: OpenSDS Endpoint is not specified using the default value(%s)", c.Endpoint)
}

var r Receiver
switch c.AuthOptions.(type) {
case *NoAuthOptions:
r = NewReceiver()
case *KeystoneAuthOptions:
r = NewKeystoneReciver(c.AuthOptions.(*KeystoneAuthOptions))
default:
log.Printf("Warnning: Not support auth options, use default")
r = NewReceiver()
c.AuthOptions = NewNoauthOptions(constants.DefaultTenantId)
}

t := c.AuthOptions.GetTenantId()
return &Client{
cfg: c,
ProfileMgr: NewProfileMgr(c.Endpoint),
DockMgr: NewDockMgr(c.Endpoint),
PoolMgr: NewPoolMgr(c.Endpoint),
VolumeMgr: NewVolumeMgr(c.Endpoint),
VersionMgr: NewVersionMgr(c.Endpoint),
ProfileMgr: NewProfileMgr(r, c.Endpoint, t),
DockMgr: NewDockMgr(r, c.Endpoint, t),
PoolMgr: NewPoolMgr(r, c.Endpoint, t),
VolumeMgr: NewVolumeMgr(r, c.Endpoint, t),
VersionMgr: NewVersionMgr(r, c.Endpoint, t),
}
}

Expand Down
15 changes: 8 additions & 7 deletions client/dock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@ import (
"github.com/opensds/opensds/pkg/utils/urls"
)

func NewDockMgr(edp string) *DockMgr {
func NewDockMgr(r Receiver, edp string, tenantId string) *DockMgr {
return &DockMgr{
Receiver: NewReceiver(),
Receiver: r,
Endpoint: edp,
TenantId: tenantId,
}
}

type DockMgr struct {
Receiver

Endpoint string
TenantId string
}

func (d *DockMgr) GetDock(dckID string) (*model.DockSpec, error) {
var res model.DockSpec
url := strings.Join([]string{
d.Endpoint,
urls.GenerateDockURL(dckID)}, "/")
urls.GenerateDockURL(urls.Client, d.TenantId, dckID)}, "/")

if err := d.Recv(request, url, "GET", nil, &res); err != nil {
if err := d.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

Expand All @@ -51,9 +52,9 @@ func (d *DockMgr) ListDocks() ([]*model.DockSpec, error) {
var res []*model.DockSpec
url := strings.Join([]string{
d.Endpoint,
urls.GenerateDockURL()}, "/")
urls.GenerateDockURL(urls.Client, d.TenantId)}, "/")

if err := d.Recv(request, url, "GET", nil, &res); err != nil {
if err := d.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

Expand Down
1 change: 0 additions & 1 deletion client/dock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func NewFakeDockReceiver() Receiver {
type fakeDockReceiver struct{}

func (*fakeDockReceiver) Recv(
f ReqFunc,
string,
method string,
in interface{},
Expand Down
15 changes: 8 additions & 7 deletions client/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,29 @@ import (
)

// NewPoolMgr
func NewPoolMgr(edp string) *PoolMgr {
func NewPoolMgr(r Receiver, edp string, tenantId string) *PoolMgr {
return &PoolMgr{
Receiver: NewReceiver(),
Receiver: r,
Endpoint: edp,
TenantId: tenantId,
}
}

// PoolMgr
type PoolMgr struct {
Receiver

Endpoint string
TenantId string
}

// GetPool
func (p *PoolMgr) GetPool(polID string) (*model.StoragePoolSpec, error) {
var res model.StoragePoolSpec
url := strings.Join([]string{
p.Endpoint,
urls.GeneratePoolURL(polID)}, "/")
urls.GeneratePoolURL(urls.Client, p.TenantId, polID)}, "/")

if err := p.Recv(request, url, "GET", nil, &res); err != nil {
if err := p.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

Expand All @@ -55,9 +56,9 @@ func (p *PoolMgr) ListPools() ([]*model.StoragePoolSpec, error) {
var res []*model.StoragePoolSpec
url := strings.Join([]string{
p.Endpoint,
urls.GeneratePoolURL()}, "/")
urls.GeneratePoolURL(urls.Client, p.TenantId)}, "/")

if err := p.Recv(request, url, "GET", nil, &res); err != nil {
if err := p.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

Expand Down
1 change: 0 additions & 1 deletion client/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func NewFakePoolReceiver() Receiver {
type fakePoolReceiver struct{}

func (*fakePoolReceiver) Recv(
f ReqFunc,
string,
method string,
in interface{},
Expand Down
35 changes: 18 additions & 17 deletions client/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,29 @@ type ProfileBuilder *model.ProfileSpec
type ExtraBuilder *model.ExtraSpec

// NewProfileMgr
func NewProfileMgr(edp string) *ProfileMgr {
func NewProfileMgr(r Receiver, edp string, tenantId string) *ProfileMgr {
return &ProfileMgr{
Receiver: NewReceiver(),
Receiver: r,
Endpoint: edp,
TenantId: tenantId,
}
}

// ProfileMgr
type ProfileMgr struct {
Receiver

Endpoint string
TenantId string
}

// CreateProfile
func (p *ProfileMgr) CreateProfile(body ProfileBuilder) (*model.ProfileSpec, error) {
var res model.ProfileSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateProfileURL()}, "/")
urls.GenerateProfileURL(urls.Client, p.TenantId)}, "/")

if err := p.Recv(request, url, "POST", body, &res); err != nil {
if err := p.Recv(url, "POST", body, &res); err != nil {
return nil, err
}

Expand All @@ -65,9 +66,9 @@ func (p *ProfileMgr) GetProfile(prfID string) (*model.ProfileSpec, error) {
var res model.ProfileSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateProfileURL(prfID)}, "/")
urls.GenerateProfileURL(urls.Client, p.TenantId, prfID)}, "/")

if err := p.Recv(request, url, "GET", nil, &res); err != nil {
if err := p.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

Expand All @@ -79,9 +80,9 @@ func (p *ProfileMgr) ListProfiles() ([]*model.ProfileSpec, error) {
var res []*model.ProfileSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateProfileURL()}, "/")
urls.GenerateProfileURL(urls.Client, p.TenantId)}, "/")

if err := p.Recv(request, url, "GET", nil, &res); err != nil {
if err := p.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

Expand All @@ -92,20 +93,20 @@ func (p *ProfileMgr) ListProfiles() ([]*model.ProfileSpec, error) {
func (p *ProfileMgr) DeleteProfile(prfID string) error {
url := strings.Join([]string{
p.Endpoint,
urls.GenerateProfileURL(prfID)}, "/")
urls.GenerateProfileURL(urls.Client, p.TenantId, prfID)}, "/")

return p.Recv(request, url, "DELETE", nil, nil)
return p.Recv(url, "DELETE", nil, nil)
}

// AddExtraProperty
func (p *ProfileMgr) AddExtraProperty(prfID string, body ExtraBuilder) (*model.ExtraSpec, error) {
var res model.ExtraSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateProfileURL(prfID),
urls.GenerateProfileURL(urls.Client, p.TenantId, prfID),
"extras"}, "/")

if err := p.Recv(request, url, "POST", body, &res); err != nil {
if err := p.Recv(url, "POST", body, &res); err != nil {
return nil, err
}

Expand All @@ -117,10 +118,10 @@ func (p *ProfileMgr) ListExtraProperties(prfID string) (*model.ExtraSpec, error)
var res model.ExtraSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateProfileURL(prfID),
urls.GenerateProfileURL(urls.Client, p.TenantId, prfID),
"extras"}, "/")

if err := p.Recv(request, url, "GET", nil, &res); err != nil {
if err := p.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

Expand All @@ -131,8 +132,8 @@ func (p *ProfileMgr) ListExtraProperties(prfID string) (*model.ExtraSpec, error)
func (p *ProfileMgr) RemoveExtraProperty(prfID, extraKey string) error {
url := strings.Join([]string{
p.Endpoint,
urls.GenerateProfileURL(prfID),
urls.GenerateProfileURL(urls.Client, p.TenantId, prfID),
"extras", extraKey}, "/")

return p.Recv(request, url, "DELETE", nil, nil)
return p.Recv(url, "DELETE", nil, nil)
}
1 change: 0 additions & 1 deletion client/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func NewFakeProfileReceiver() Receiver {
type fakeProfileReceiver struct{}

func (*fakeProfileReceiver) Recv(
f ReqFunc,
string,
method string,
in interface{},
Expand Down

0 comments on commit b52f1b6

Please sign in to comment.