-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_viettelidc_keymanager_container_v1.go
175 lines (142 loc) · 4.16 KB
/
data_source_viettelidc_keymanager_container_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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package viettelidc
import (
"context"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/keymanager/v1/acls"
"github.com/gophercloud/gophercloud/openstack/keymanager/v1/containers"
)
func dataSourceKeyManagerContainerV1() *schema.Resource {
ret := &schema.Resource{
ReadContext: dataSourceKeyManagerContainerV1Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"secret_refs": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"secret_ref": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"container_ref": {
Type: schema.TypeString,
Computed: true,
},
"creator_id": {
Type: schema.TypeString,
Computed: true,
},
"consumers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"url": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"acl": {
Type: schema.TypeList,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
elem := &schema.Resource{
Schema: make(map[string]*schema.Schema),
}
for _, aclOp := range getSupportedACLOperations() {
elem.Schema[aclOp] = getACLSchema()
}
ret.Schema["acl"].Elem = elem
return ret
}
func dataSourceKeyManagerContainerV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
kmClient, err := config.KeyManagerV1Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack barbican client: %s", err)
}
listOpts := containers.ListOpts{
Name: d.Get("name").(string),
}
log.Printf("[DEBUG] Containers List Options: %#v", listOpts)
allPages, err := containers.List(kmClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to query openstack_keymanager_container_v1 containers: %s", err)
}
allContainers, err := containers.ExtractContainers(allPages)
if err != nil {
return diag.Errorf("Unable to retrieve openstack_keymanager_container_v1 containers: %s", err)
}
if len(allContainers) < 1 {
return diag.Errorf("Your query returned no openstack_keymanager_container_v1 results. " +
"Please change your search criteria and try again.")
}
if len(allContainers) > 1 {
log.Printf("[DEBUG] Multiple openstack_keymanager_container_v1 results found: %#v", allContainers)
return diag.Errorf("Your query returned more than one result. Please try a more " +
"specific search criteria.")
}
container := allContainers[0]
log.Printf("[DEBUG] Retrieved openstack_keymanager_container_v1 %s: %#v", d.Id(), container)
uuid := keyManagerContainerV1GetUUIDfromContainerRef(container.ContainerRef)
d.SetId(uuid)
d.Set("name", container.Name)
d.Set("creator_id", container.CreatorID)
d.Set("container_ref", container.ContainerRef)
d.Set("type", container.Type)
d.Set("status", container.Status)
d.Set("created_at", container.Created.Format(time.RFC3339))
d.Set("updated_at", container.Updated.Format(time.RFC3339))
d.Set("consumers", flattenKeyManagerContainerV1Consumers(container.Consumers))
d.Set("secret_refs", flattenKeyManagerContainerV1SecretRefs(container.SecretRefs))
acl, err := acls.GetContainerACL(kmClient, d.Id()).Extract()
if err != nil {
log.Printf("[DEBUG] Unable to get %s container acls: %s", uuid, err)
}
d.Set("acl", flattenKeyManagerV1ACLs(acl))
// Set the region
d.Set("region", GetRegion(d, config))
return nil
}