Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #716 "Managing projects requires admin role" by using the users… #833

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions openstack/data_source_openstack_identity_project_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"

"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/gophercloud/gophercloud/openstack/identity/v3/users"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -56,9 +58,20 @@ func dataSourceIdentityProjectV3() *schema.Resource {
}
}

func filterProjects(allProjects []projects.Project, listOpts projects.ListOpts) (results []projects.Project) {
for _, p := range allProjects {
// TODO: Test for all fields :)
if p.Name == listOpts.Name {
results = append(results, p)
}
}
return
}

// dataSourceIdentityProjectV3Read performs the project lookup.
func dataSourceIdentityProjectV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
Expand All @@ -74,17 +87,43 @@ func dataSourceIdentityProjectV3Read(d *schema.ResourceData, meta interface{}) e
ParentID: d.Get("parent_id").(string),
}

log.Printf("[DEBUG] openstack_identity_project_v3 list options: %#v", listOpts)

var project projects.Project
var allProjects []projects.Project
allPages, err := projects.List(identityClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to query openstack_identity_project_v3: %s", err)
}

allProjects, err := projects.ExtractProjects(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve openstack_identity_project_v3: %s", err)
userID := config.UserID
log.Printf("[DEBUG] Will try to find project with users.ListProjects as I am unable to query openstack_identity_project_v3: %s. Trying listing userprojects.", err)
if userID == "" {
// If we don't have the ID for the user we will try to fetch it from the Token
tokenID := config.OsClient.TokenID
d.SetId(d.Get("name").(string))
result := tokens.Get(identityClient, tokenID)
if result.Err != nil {
log.Printf("[ERROR] Error when getting token info %s", result)
return result.Err
}
user, err := result.ExtractUser()
if err != nil {
log.Printf("[ERROR] Error when extracting user: %s", err)
return err
}
userID = user.ID
}
// Search for all the projects using the users.ListProjects API call and filter them
allPages, err = users.ListProjects(identityClient, userID).AllPages()
if err != nil {
return fmt.Errorf("Unable to query openstack_identity_project_v3: %s", err)
}
allProjects, err = projects.ExtractProjects(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve openstack_identity_project_v3: %s", err)
}
allProjects = filterProjects(allProjects, listOpts)
} else {
allProjects, err = projects.ExtractProjects(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve openstack_identity_project_v3: %s", err)
}
}

if len(allProjects) < 1 {
Expand Down