/
data_source_flexibleengine_vpcep_public_services.go
100 lines (90 loc) · 2.28 KB
/
data_source_flexibleengine_vpcep_public_services.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
package flexibleengine
import (
"fmt"
"github.com/chnsz/golangsdk/openstack/vpcep/v1/services"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceVPCEPPublicServices() *schema.Resource {
return &schema.Resource{
Read: dataSourceVpcepPublicRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"service_name": {
Type: schema.TypeString,
Optional: true,
},
"service_id": {
Type: schema.TypeString,
Optional: true,
},
"services": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"service_name": {
Type: schema.TypeString,
Computed: true,
},
"service_type": {
Type: schema.TypeString,
Computed: true,
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
"is_charge": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataSourceVpcepPublicRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region := GetRegion(d, config)
vpcepClient, err := config.VPCEPClient(region)
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine VPC endpoint client: %s", err)
}
listOpts := services.ListOpts{
ServiceName: d.Get("service_name").(string),
ID: d.Get("service_id").(string),
}
allServices, err := services.ListPublic(vpcepClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve vpc endpoint public services: %s", err)
}
if len(allServices) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
d.SetId(allServices[0].ID)
d.Set("region", region)
services := make([]map[string]interface{}, len(allServices))
for i, v := range allServices {
services[i] = map[string]interface{}{
"id": v.ID,
"service_name": v.ServiceName,
"service_type": v.ServiceType,
"owner": v.Owner,
"is_charge": v.IsChange,
}
}
if err := d.Set("services", services); err != nil {
return err
}
return nil
}