-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_openstack_networking_secgroup_v2.go
80 lines (65 loc) · 1.86 KB
/
data_source_openstack_networking_secgroup_v2.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
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceNetworkingSecGroupV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkingSecGroupV2Read,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"secgroup_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"tenant_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}
func dataSourceNetworkingSecGroupV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
listOpts := groups.ListOpts{
ID: d.Get("secgroup_id").(string),
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
}
pages, err := groups.List(networkingClient, listOpts).AllPages()
if err != nil {
return err
}
allSecGroups, err := groups.ExtractGroups(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve security groups: %s", err)
}
if len(allSecGroups) < 1 {
return fmt.Errorf("No Security Group found with name: %s", d.Get("name"))
}
if len(allSecGroups) > 1 {
return fmt.Errorf("More than one Security Group found with name: %s", d.Get("name"))
}
secGroup := allSecGroups[0]
log.Printf("[DEBUG] Retrieved Security Group %s: %+v", secGroup.ID, secGroup)
d.SetId(secGroup.ID)
d.Set("name", secGroup.Name)
d.Set("description", secGroup.Description)
d.Set("tenant_id", secGroup.TenantID)
d.Set("region", GetRegion(d, config))
return nil
}