-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
helpers.go
178 lines (147 loc) · 5.55 KB
/
helpers.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package client
import (
"context"
"fmt"
"net/url"
"strings"
"sync"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2023-03-01/configurationstores"
)
var (
configurationStoreCache = map[string]ConfigurationStoreDetails{}
keysmith = &sync.RWMutex{}
lock = map[string]*sync.RWMutex{}
)
type ConfigurationStoreDetails struct {
configurationStoreId string
dataPlaneEndpoint string
}
func (c *Client) AddToCache(configurationStoreId configurationstores.ConfigurationStoreId, dataPlaneEndpoint string) {
cacheKey := c.cacheKeyForConfigurationStore(configurationStoreId.ConfigurationStoreName)
keysmith.Lock()
configurationStoreCache[cacheKey] = ConfigurationStoreDetails{
configurationStoreId: configurationStoreId.ID(),
dataPlaneEndpoint: dataPlaneEndpoint,
}
keysmith.Unlock()
}
func (c *Client) ConfigurationStoreIDFromEndpoint(ctx context.Context, subscriptionId commonids.SubscriptionId, configurationStoreEndpoint, domainSuffix string) (*string, error) {
configurationStoreName, err := c.parseNameFromEndpoint(configurationStoreEndpoint, domainSuffix)
if err != nil {
return nil, err
}
cacheKey := c.cacheKeyForConfigurationStore(*configurationStoreName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
defer lock[cacheKey].Unlock()
// first check the cache
if v, ok := configurationStoreCache[cacheKey]; ok {
return &v.configurationStoreId, nil
}
// If it's not present, populate the entire cache
configurationStores, err := c.ConfigurationStoresClient.ListComplete(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("retrieving the list of Configuration Stores in %s: %+v", subscriptionId, err)
}
for _, item := range configurationStores.Items {
if item.Id == nil || item.Properties == nil || item.Properties.Endpoint == nil {
continue
}
itemId := *item.Id
endpointUri := *item.Properties.Endpoint
configurationStoreId, err := configurationstores.ParseConfigurationStoreIDInsensitively(itemId)
if err != nil {
return nil, fmt.Errorf("parsing %q: %+v", itemId, err)
}
c.AddToCache(*configurationStoreId, endpointUri)
}
// finally try and pull this from the cache
if v, ok := configurationStoreCache[cacheKey]; ok {
return &v.configurationStoreId, nil
}
// we haven't found it, but Data Sources and Resources need to handle this error separately
return nil, nil
}
func (c *Client) EndpointForConfigurationStore(ctx context.Context, configurationStoreId configurationstores.ConfigurationStoreId) (*string, error) {
cacheKey := c.cacheKeyForConfigurationStore(configurationStoreId.ConfigurationStoreName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
defer lock[cacheKey].Unlock()
if v, ok := configurationStoreCache[cacheKey]; ok {
return &v.dataPlaneEndpoint, nil
}
resp, err := c.ConfigurationStoresClient.Get(ctx, configurationStoreId)
if err != nil {
return nil, fmt.Errorf("retrieving %s:%+v", configurationStoreId, err)
}
if resp.Model == nil || resp.Model.Properties == nil || resp.Model.Properties.Endpoint == nil {
return nil, fmt.Errorf("retrieving %s: `model.properties.Endpoint` was nil", configurationStoreId)
}
c.AddToCache(configurationStoreId, *resp.Model.Properties.Endpoint)
return resp.Model.Properties.Endpoint, nil
}
func (c *Client) Exists(ctx context.Context, configurationStoreId configurationstores.ConfigurationStoreId) (bool, error) {
cacheKey := c.cacheKeyForConfigurationStore(configurationStoreId.ConfigurationStoreName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
defer lock[cacheKey].Unlock()
if _, ok := configurationStoreCache[cacheKey]; ok {
return true, nil
}
resp, err := c.ConfigurationStoresClient.Get(ctx, configurationStoreId)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return false, nil
}
return false, fmt.Errorf("retrieving %s: %+v", configurationStoreId, err)
}
if resp.Model == nil || resp.Model.Properties == nil || resp.Model.Properties.Endpoint == nil {
return false, fmt.Errorf("retrieving %s: `model.properties.Endpoint` was nil", configurationStoreId)
}
c.AddToCache(configurationStoreId, *resp.Model.Properties.Endpoint)
return true, nil
}
func (c *Client) RemoveFromCache(configurationStoreId configurationstores.ConfigurationStoreId) {
cacheKey := c.cacheKeyForConfigurationStore(configurationStoreId.ConfigurationStoreName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
delete(configurationStoreCache, cacheKey)
lock[cacheKey].Unlock()
}
func (c *Client) cacheKeyForConfigurationStore(name string) string {
return strings.ToLower(name)
}
func (c *Client) parseNameFromEndpoint(input, domainSuffix string) (*string, error) {
uri, err := url.ParseRequestURI(input)
if err != nil {
return nil, err
}
// https://the-appconfiguration.azconfig.io
// https://the-appconfiguration.azconfig.azure.cn
// https://the-appconfiguration.azconfig.azure.us
if !strings.HasSuffix(uri.Host, domainSuffix) {
return nil, fmt.Errorf("expected a URI in the format `https://somename.%s` but got %q", domainSuffix, uri.Host)
}
segments := strings.Split(uri.Host, ".")
return &segments[0], nil
}