This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_source_openstack_identity_endpoint_v3.go
147 lines (125 loc) · 4.09 KB
/
data_source_openstack_identity_endpoint_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/identity/v3/endpoints"
"github.com/gophercloud/gophercloud/openstack/identity/v3/services"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceIdentityEndpointV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceIdentityEndpointV3Read,
Schema: map[string]*schema.Schema{
"service_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"service_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"interface": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "public",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value != "public" && value != "internal" && value != "admin" {
errors = append(errors, fmt.Errorf(
"Only 'public', 'internal', 'public' are supported values for 'interface'"))
}
return
},
},
"url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
// dataSourceIdentityEndpointV3Read performs the endpoint lookup.
func dataSourceIdentityEndpointV3Read(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)
}
availability := gophercloud.AvailabilityPublic
switch d.Get("interface") {
case "internal":
availability = gophercloud.AvailabilityInternal
case "admin":
availability = gophercloud.AvailabilityAdmin
}
listOpts := endpoints.ListOpts{
Availability: availability,
ServiceID: d.Get("service_id").(string),
}
log.Printf("[DEBUG] List Options: %#v", listOpts)
var endpoint endpoints.Endpoint
allPages, err := endpoints.List(identityClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to query endpoints: %s", err)
}
allEndpoints, err := endpoints.ExtractEndpoints(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve endpoints: %s", err)
}
serviceName := d.Get("service_name").(string)
if len(allEndpoints) > 1 && serviceName != "" {
var filteredEndpoints []endpoints.Endpoint
// Query all services to further filter results
allServicePages, err := services.List(identityClient, nil).AllPages()
if err != nil {
return fmt.Errorf("Unable to query services: %s", err)
}
allServices, err := services.ExtractServices(allServicePages)
if err != nil {
return fmt.Errorf("Unable to retrieve services: %s", err)
}
for _, endpoint := range allEndpoints {
for _, service := range allServices {
if v, ok := service.Extra["name"].(string); ok {
if endpoint.ServiceID == service.ID && serviceName == v {
endpoint.Name = v
filteredEndpoints = append(filteredEndpoints, endpoint)
}
}
}
}
allEndpoints = filteredEndpoints
}
if len(allEndpoints) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(allEndpoints) > 1 {
log.Printf("[DEBUG] Multiple results found: %#v", allEndpoints)
return fmt.Errorf("Your query returned more than one result")
}
endpoint = allEndpoints[0]
log.Printf("[DEBUG] Single endpoint found: %s", endpoint.ID)
return dataSourceIdentityEndpointV3Attributes(d, &endpoint)
}
// dataSourceIdentityEndpointV3Attributes populates the fields of an Endpoint resource.
func dataSourceIdentityEndpointV3Attributes(d *schema.ResourceData, endpoint *endpoints.Endpoint) error {
log.Printf("[DEBUG] openstack_identity_endpoint_v3 details: %#v", endpoint)
d.SetId(endpoint.ID)
d.Set("interface", endpoint.Availability)
d.Set("region", endpoint.Region)
d.Set("service_id", endpoint.ServiceID)
d.Set("service_name", endpoint.Name)
d.Set("url", endpoint.URL)
return nil
}