This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_source_flexibleengine_identity_project_v3.go
106 lines (87 loc) · 2.64 KB
/
data_source_flexibleengine_identity_project_v3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package flexibleengine
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/identity/v3/projects"
)
func dataSourceIdentityProjectV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceIdentityProjectV3Read,
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
},
"domain_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"parent_id": {
Type: schema.TypeString,
Optional: true,
},
"is_domain": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}
// 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)
}
listOpts := projects.ListOpts{
DomainID: d.Get("domain_id").(string),
Name: d.Get("name").(string),
ParentID: d.Get("parent_id").(string),
}
log.Printf("[DEBUG] List Options: %#v", listOpts)
var project projects.Project
allPages, err := projects.List(identityClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to query projects: %s", err)
}
allProjects, err := projects.ExtractProjects(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve projects: %s", err)
}
if len(allProjects) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(allProjects) > 1 {
log.Printf("[DEBUG] Multiple results found: %#v", allProjects)
return fmt.Errorf("Your query returned more than one result")
}
project = allProjects[0]
log.Printf("[DEBUG] Single project found: %s", project.ID)
return dataSourceIdentityProjectV3Attributes(d, &project)
}
// dataSourceIdentityProjectV3Attributes populates the fields of an Project resource.
func dataSourceIdentityProjectV3Attributes(d *schema.ResourceData, project *projects.Project) error {
log.Printf("[DEBUG] flexibleengine_identity_project_v3 details: %#v", project)
d.SetId(project.ID)
d.Set("is_domain", project.IsDomain)
d.Set("description", project.Description)
d.Set("domain_id", project.DomainID)
d.Set("enabled", project.Enabled)
d.Set("name", project.Name)
d.Set("parent_id", project.ParentID)
return nil
}