forked from hashicorp/terraform-provider-azurerm
/
app_configuration_keys_data_source.go
197 lines (177 loc) · 6.03 KB
/
app_configuration_keys_data_source.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package appconfiguration
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2022-05-01/configurationstores"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
type KeysDataSource struct{}
var _ sdk.DataSource = KeysDataSource{}
type KeyDataSourceModel struct {
Key string `tfschema:"key"`
ContentType string `tfschema:"content_type"`
Etag string `tfschema:"etag"`
Label string `tfschema:"label"`
Value string `tfschema:"value"`
Locked bool `tfschema:"locked"`
Tags map[string]interface{} `tfschema:"tags"`
Type string `tfschema:"type"`
VaultKeyReference string `tfschema:"vault_key_reference"`
}
type KeysDataSourceModel struct {
ConfigurationStoreId string `tfschema:"configuration_store_id"`
Key string `tfschema:"key"`
Label string `tfschema:"label"`
Items []KeyDataSourceModel `tfschema:"items"`
}
func (k KeysDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"configuration_store_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: configurationstores.ValidateConfigurationStoreID,
},
"key": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "",
},
"label": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "",
},
}
}
func (k KeysDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"items": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"key": {
Type: pluginsdk.TypeString,
Computed: true,
},
"label": {
Type: pluginsdk.TypeString,
Computed: true,
},
"content_type": {
Type: pluginsdk.TypeString,
Computed: true,
},
"etag": {
Type: pluginsdk.TypeString,
Computed: true,
},
"value": {
Type: pluginsdk.TypeString,
Computed: true,
},
"locked": {
Type: pluginsdk.TypeBool,
Computed: true,
},
"type": {
Type: pluginsdk.TypeString,
Computed: true,
},
"vault_key_reference": {
Type: pluginsdk.TypeString,
Computed: true,
},
"tags": tags.SchemaDataSource(),
},
},
},
}
}
func (k KeysDataSource) ModelObject() interface{} {
return &KeysDataSourceModel{}
}
func (k KeysDataSource) ResourceType() string {
return "azurerm_app_configuration_keys"
}
func (k KeysDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model KeysDataSourceModel
if err := metadata.Decode(&model); err != nil {
return err
}
configurationStoreId, err := configurationstores.ParseConfigurationStoreID(model.ConfigurationStoreId)
if err != nil {
return err
}
configurationStoreEndpoint, err := metadata.Client.AppConfiguration.EndpointForConfigurationStore(ctx, *configurationStoreId)
if err != nil {
return fmt.Errorf("retrieving Endpoint for feature %q in %q: %s", model.Key, *configurationStoreId, err)
}
// @favoretti: API returns pagination nextLink (Link header) without complete URI, only path:
// Link: "</kv?somepath...>; rel=next;"
// whereas the client expects a complete URI to be present and therefore fails to fetch all results if
// store contains more than 100 entries
client, err := metadata.Client.AppConfiguration.LinkWorkaroundDataPlaneClientWithEndpoint(*configurationStoreEndpoint)
if err != nil {
return err
}
nestedItemId, err := parse.NewNestedItemID(*configurationStoreEndpoint, model.Key, model.Label)
if err != nil {
return err
}
iter, err := client.GetKeyValuesComplete(ctx, model.Key, model.Label, "", "", []string{})
if err != nil {
if v, ok := err.(autorest.DetailedError); ok {
if utils.ResponseWasNotFound(autorest.Response{Response: v.Response}) {
return fmt.Errorf("key %s was not found", model.Key)
}
} else {
return fmt.Errorf("while checking for key's %q existence: %+v", model.Key, err)
}
return fmt.Errorf("while checking for key's %q existence: %+v", model.Key, err)
}
for iter.NotDone() {
kv := iter.Value()
var krmodel KeyDataSourceModel
krmodel.Key = utils.NormalizeNilableString(kv.Key)
krmodel.Label = utils.NormalizeNilableString(kv.Label)
if contentType := utils.NormalizeNilableString(kv.ContentType); contentType != VaultKeyContentType {
krmodel.Type = KeyTypeKV
krmodel.ContentType = contentType
krmodel.Value = utils.NormalizeNilableString(kv.Value)
} else {
var ref VaultKeyReference
refBytes := []byte(utils.NormalizeNilableString(kv.Value))
err := json.Unmarshal(refBytes, &ref)
if err != nil {
return fmt.Errorf("while unmarshalling vault reference: %+v", err)
}
krmodel.Type = KeyTypeVault
krmodel.VaultKeyReference = ref.URI
krmodel.ContentType = VaultKeyContentType
krmodel.Value = ref.URI
}
if kv.Locked != nil {
krmodel.Locked = *kv.Locked
}
krmodel.Etag = utils.NormalizeNilableString(kv.Etag)
model.Items = append(model.Items, krmodel)
if err := iter.NextWithContext(ctx); err != nil {
return fmt.Errorf("fetching keys for %q: %+v", nestedItemId, err)
}
}
metadata.SetID(nestedItemId)
return metadata.Encode(&model)
},
}
}