/
data_source_openstack_fw_policy_v1.go
111 lines (89 loc) · 2.46 KB
/
data_source_openstack_fw_policy_v1.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
package nhncloud
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
)
func dataSourceFWPolicyV1() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFWPolicyV1Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"policy_id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"audited": {
Type: schema.TypeBool,
Computed: true,
},
"shared": {
Type: schema.TypeBool,
Computed: true,
},
"rules": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceFWPolicyV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
listOpts := policies.ListOpts{
ID: d.Get("policy_id").(string),
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
}
pages, err := policies.List(networkingClient, listOpts).AllPages()
if err != nil {
return diag.FromErr(err)
}
allFWPolicies, err := policies.ExtractPolicies(pages)
if err != nil {
return diag.Errorf("Unable to retrieve openstack_fw_policy_v1: %s", err)
}
if len(allFWPolicies) < 1 {
return diag.Errorf("No openstack_fw_policy_v1 found with name: %s", d.Get("name"))
}
if len(allFWPolicies) > 1 {
return diag.Errorf("More than one openstack_fw_policy_v1 found with name: %s", d.Get("name"))
}
policy := allFWPolicies[0]
log.Printf("[DEBUG] Retrieved openstack_fw_policy_v1 %s: %#v", policy.ID, policy)
d.SetId(policy.ID)
d.Set("name", policy.Name)
d.Set("tenant_id", policy.TenantID)
d.Set("description", policy.Description)
d.Set("audited", policy.Audited)
d.Set("shared", policy.Shared)
d.Set("rules", policy.Rules)
d.Set("region", GetRegion(d, config))
return nil
}