Implemented get all keys (#1920)

Signed-off-by: cmendible <cmendible@gmail.com>

Co-authored-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
Co-authored-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>
This commit is contained in:
Carlos Mendible 2022-08-05 23:28:52 +02:00 committed by GitHub
parent 457d6c40b4
commit 8bb6ef7ed1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 64 additions and 17 deletions

View File

@ -159,26 +159,40 @@ func parseMetadata(meta configuration.Metadata) (metadata, error) {
func (r *ConfigurationStore) Get(ctx context.Context, req *configuration.GetRequest) (*configuration.GetResponse, error) {
keys := req.Keys
items := make([]*configuration.Item, 0, len(keys))
for _, key := range keys {
resp, err := r.client.GetSetting(
ctx,
key,
&azappconfig.GetSettingOptions{
Label: to.Ptr("label"),
})
if err != nil {
return nil, err
}
var items []*configuration.Item
item := &configuration.Item{
Metadata: map[string]string{},
if len(keys) == 0 {
var err error
if items, err = r.getAll(ctx); err != nil {
return &configuration.GetResponse{}, err
}
item.Key = key
item.Value = *resp.Value
item.Metadata["label"] = *resp.Label
} else {
items = make([]*configuration.Item, 0, len(keys))
i := 0
for _, key := range keys {
resp, err := r.client.GetSetting(
ctx,
key,
&azappconfig.GetSettingOptions{
Label: to.Ptr("*"),
})
if err != nil {
return &configuration.GetResponse{}, err
}
items = append(items, item)
item := &configuration.Item{
Metadata: map[string]string{},
}
item.Key = key
item.Value = *resp.Value
if resp.Label != nil {
item.Metadata["label"] = *resp.Label
}
items[i] = item
i++
}
items = items[:i]
}
return &configuration.GetResponse{
@ -186,6 +200,39 @@ func (r *ConfigurationStore) Get(ctx context.Context, req *configuration.GetRequ
}, nil
}
func (r *ConfigurationStore) getAll(ctx context.Context) ([]*configuration.Item, error) {
items := make([]*configuration.Item, 0)
revPgr := r.client.NewListRevisionsPager(
azappconfig.SettingSelector{
KeyFilter: to.Ptr("*"),
LabelFilter: to.Ptr("*"),
Fields: azappconfig.AllSettingFields(),
},
nil)
for revPgr.More() {
if revResp, err := revPgr.NextPage(ctx); err == nil {
for _, setting := range revResp.Settings {
item := &configuration.Item{
Metadata: map[string]string{},
}
item.Key = *setting.Key
item.Value = *setting.Value
if setting.Label != nil {
item.Metadata["label"] = *setting.Label
}
items = append(items, item)
}
} else {
return nil, fmt.Errorf("failed to load all keys, error is %s", err)
}
}
return items, nil
}
func (r *ConfigurationStore) Subscribe(ctx context.Context, req *configuration.SubscribeRequest, handler configuration.UpdateHandler) (string, error) {
return "", fmt.Errorf("Subscribe is not implemented by this configuration store")
}