Skip to content

Commit 4f4f94d

Browse files
committed
Give specific domain id for user/project
Keystone supports "domain" and users, groups and projects belong to a specific domain, and it's possible that multiple entries with the same name exist in different domains. This introduces the interface to specify the domain where the user or the project should exist, to avoid unexpected behavior caused by such "duplicate" entities.
1 parent 8305912 commit 4f4f94d

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

modules/openstack/project.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
type Project struct {
2828
Name string
2929
Description string
30+
DomainID string
3031
}
3132

3233
// CreateProject - creates project with projectName and projectDescription if it does not exist
@@ -35,7 +36,7 @@ func (o *OpenStack) CreateProject(
3536
p Project,
3637
) (string, error) {
3738
var projectID string
38-
allPages, err := projects.List(o.osclient, projects.ListOpts{Name: p.Name}).AllPages()
39+
allPages, err := projects.List(o.osclient, projects.ListOpts{Name: p.Name, DomainID: p.DomainID}).AllPages()
3940
if err != nil {
4041
return projectID, err
4142
}
@@ -49,8 +50,9 @@ func (o *OpenStack) CreateProject(
4950
createOpts := projects.CreateOpts{
5051
Name: p.Name,
5152
Description: p.Description,
53+
DomainID: p.DomainID,
5254
}
53-
log.Info(fmt.Sprintf("Creating project %s", p.Name))
55+
log.Info(fmt.Sprintf("Creating project %s in %s", p.Name, p.DomainID))
5456
project, err := projects.Create(o.osclient, createOpts).Extract()
5557
if err != nil {
5658
return projectID, err

modules/openstack/user.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type User struct {
3232
Name string
3333
Password string
3434
ProjectID string
35+
DomainID string
3536
}
3637

3738
// CreateUser - creates user with userName, password and default project projectID
@@ -44,6 +45,7 @@ func (o *OpenStack) CreateUser(
4445
user, err := o.GetUser(
4546
log,
4647
u.Name,
48+
u.DomainID,
4749
)
4850
// If the user is not found, don't count that as an error here
4951
if err != nil && !strings.Contains(err.Error(), UserNotFound) {
@@ -59,6 +61,7 @@ func (o *OpenStack) CreateUser(
5961
Name: u.Name,
6062
DefaultProjectID: u.ProjectID,
6163
Password: u.Password,
64+
DomainID: u.DomainID,
6265
}
6366
user, err := users.Create(o.GetOSClient(), createOpts).Extract()
6467
if err != nil {
@@ -76,8 +79,9 @@ func (o *OpenStack) CreateUser(
7679
func (o *OpenStack) GetUser(
7780
log logr.Logger,
7881
userName string,
82+
domainID string,
7983
) (*users.User, error) {
80-
allPages, err := users.List(o.GetOSClient(), users.ListOpts{Name: userName}).AllPages()
84+
allPages, err := users.List(o.GetOSClient(), users.ListOpts{Name: userName, DomainID: domainID}).AllPages()
8185
if err != nil {
8286
return nil, err
8387
}
@@ -97,18 +101,20 @@ func (o *OpenStack) GetUser(
97101
func (o *OpenStack) DeleteUser(
98102
log logr.Logger,
99103
userName string,
104+
domainID string,
100105
) error {
101106
user, err := o.GetUser(
102107
log,
103108
userName,
109+
domainID,
104110
)
105111
// If the user is not found, don't count that as an error here
106112
if err != nil && !strings.Contains(err.Error(), "user not found in keystone") {
107113
return err
108114
}
109115

110116
if user != nil {
111-
log.Info(fmt.Sprintf("Deleting user %s", user.Name))
117+
log.Info(fmt.Sprintf("Deleting user %s in %s", user.Name, user.DomainID))
112118
err = users.Delete(o.GetOSClient(), user.ID).ExtractErr()
113119
if err != nil {
114120
return err

0 commit comments

Comments
 (0)