Skip to content

Commit

Permalink
Add Snapshot support for SDK (#22118)
Browse files Browse the repository at this point in the history
* remove generated

* Make base recording for snapshot tests. Customizable for local testing

* comment updates

* update test proxy

* Changes to Operation-Local header for test Proxy

* add comments

* add comments

* fix regional link

* changelog updates

* update changelog

* update version.go

* comment updates

* renamed variables/pre-make maps with known sizes for pager items

* omit pager from response/options
  • Loading branch information
MBSolomon authored Dec 14, 2023
1 parent 8867496 commit 178fe26
Show file tree
Hide file tree
Showing 16 changed files with 1,180 additions and 11 deletions.
7 changes: 2 additions & 5 deletions sdk/data/azappconfig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# Release History

## 1.0.1 (Unreleased)
## 1.1.0 (2024-01-16)

### Features Added

### Breaking Changes

### Bugs Fixed
* Added support for [`Snapshots`](https://learn.microsoft.com/azure/azure-app-configuration/concept-snapshots).

### Other Changes
* Updated to latest version of `azcore`.
Expand Down
2 changes: 1 addition & 1 deletion sdk/data/azappconfig/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/data/azappconfig",
"Tag": "go/data/azappconfig_ec954b99e7"
"Tag": "go/data/azappconfig_ff59e7b261"
}
289 changes: 289 additions & 0 deletions sdk/data/azappconfig/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,292 @@ func (c *Client) NewListSettingsPager(selector SettingSelector, options *ListSet
Tracer: c.appConfigClient.Tracer(),
})
}

// NewListSnapshotsPager - Gets a list of key-value snapshots.
//
// - options - NewListSnapshotsPagerOptions contains the optional parameters to retrieve a snapshot
// method.
func (c *Client) NewListSnapshotsPager(options *ListSnapshotsOptions) *runtime.Pager[ListSnapshotsResponse] {
opts := (*generated.AzureAppConfigurationClientGetSnapshotsOptions)(options)
ssRespPager := c.appConfigClient.NewGetSnapshotsPager(opts)

return runtime.NewPager(runtime.PagingHandler[ListSnapshotsResponse]{
More: func(ListSnapshotsResponse) bool {
return ssRespPager.More()
},
Fetcher: func(ctx context.Context, cur *ListSnapshotsResponse) (ListSnapshotsResponse, error) {
page, err := ssRespPager.NextPage(ctx)
if err != nil {
return ListSnapshotsResponse{}, err
}

snapshots := make([]Snapshot, len(page.Items))

for i := 0; i < len(page.Items); i++ {

snapshot := page.Items[i]

convertedETag := azcore.ETag(*snapshot.Etag)

convertedFilters := make([]KeyValueFilter, len(snapshot.Filters))

for j := 0; j < len(snapshot.Filters); j++ {
convertedFilters[j] = KeyValueFilter{
Key: snapshot.Filters[j].Key,
Label: snapshot.Filters[j].Label,
}
}

snapshots[i] = Snapshot{
Filters: convertedFilters,
CompositionType: snapshot.CompositionType,
RetentionPeriod: snapshot.RetentionPeriod,
Tags: snapshot.Tags,
Created: snapshot.Created,
ETag: &convertedETag,
Expires: snapshot.Expires,
ItemsCount: snapshot.ItemsCount,
Name: snapshot.Name,
Size: snapshot.Size,
Status: snapshot.Status,
}
}

return ListSnapshotsResponse{
Snapshots: snapshots,
SyncToken: SyncToken(*page.SyncToken),
}, nil
},
Tracer: c.appConfigClient.Tracer(),
})
}

// NewListSettingsForSnapshotPager
//
// - snapshotName - The name of the snapshot to list configuration settings for
// - options - ListSettingsForSnapshotOptions contains the optional parameters to retrieve Snapshot configuration settings
func (c *Client) NewListSettingsForSnapshotPager(snapshotName string, options *ListSettingsForSnapshotOptions) *runtime.Pager[ListSettingsForSnapshotResponse] {
if options == nil {
options = &ListSettingsForSnapshotOptions{}
}

opts := generated.AzureAppConfigurationClientGetKeyValuesOptions{
AcceptDatetime: options.AcceptDatetime,
After: options.After,
IfMatch: options.IfMatch,
IfNoneMatch: options.IfNoneMatch,
Select: options.Select,
Snapshot: &snapshotName,
Key: &options.Key,
Label: &options.Label,
}
ssRespPager := c.appConfigClient.NewGetKeyValuesPager(&opts)

return runtime.NewPager(runtime.PagingHandler[ListSettingsForSnapshotResponse]{
More: func(ListSettingsForSnapshotResponse) bool {
return ssRespPager.More()
},
Fetcher: func(ctx context.Context, cur *ListSettingsForSnapshotResponse) (ListSettingsForSnapshotResponse, error) {
page, err := ssRespPager.NextPage(ctx)
if err != nil {
return ListSettingsForSnapshotResponse{}, err
}

settings := make([]Setting, len(page.Items))

for i := 0; i < len(page.Items); i++ {
setting := page.Items[i]

settings[i] = settingFromGenerated(setting)
}

return ListSettingsForSnapshotResponse{
Settings: settings,
SyncToken: SyncToken(*page.SyncToken),
}, nil
},
Tracer: c.appConfigClient.Tracer(),
})
}

// BeginCreateSnapshot creates a snapshot of the configuration store.
//
// - snapshotName - The name of the snapshot to create.
// - keyLabelFilter - The filters to apply on the key-values.
// - options - CreateSnapshotOptions contains the optional parameters to create a Snapshot
func (c *Client) BeginCreateSnapshot(ctx context.Context, snapshotName string, keyLabelFilter []SettingFilter, options *CreateSnapshotOptions) (*runtime.Poller[CreateSnapshotResponse], error) {
filter := []generated.KeyValueFilter{}

if options == nil {
options = &CreateSnapshotOptions{}
}

for _, f := range keyLabelFilter {
filter = append(filter, generated.KeyValueFilter{
Key: f.KeyFilter,
Label: f.LabelFilter,
})
}

if len(filter) == 0 {
filter = append(filter, generated.KeyValueFilter{})
}

entity := generated.Snapshot{
Filters: filter,
CompositionType: options.CompositionType,
RetentionPeriod: options.RetentionPeriod,
Tags: options.Tags,
Name: &snapshotName,
}

opts := generated.AzureAppConfigurationClientBeginCreateSnapshotOptions{
ResumeToken: options.ResumeToken,
}

pollerSS, err := generated.NewCreateSnapshotPoller[CreateSnapshotResponse](ctx, c.appConfigClient, snapshotName, entity, &opts)

if err != nil {
return nil, err
}

return pollerSS, nil
}

// GetSnapshot gets a snapshot
//
// - snapshotName - The name of the snapshot to get.
// - options - GetSnapshotOptions contains the optional parameters to get a snapshot
func (c *Client) GetSnapshot(ctx context.Context, snapshotName string, options *GetSnapshotOptions) (GetSnapshotResponse, error) {
if options == nil {
options = &GetSnapshotOptions{}
}

opts := (*generated.AzureAppConfigurationClientGetSnapshotOptions)(options)

getResp, err := c.appConfigClient.GetSnapshot(ctx, snapshotName, opts)

if err != nil {
return GetSnapshotResponse{}, err
}

convertedETag := azcore.ETag(*getResp.Etag)

var convertedFilters []KeyValueFilter

for _, filter := range getResp.Filters {
convertedFilters = append(convertedFilters, KeyValueFilter{
Key: filter.Key,
Label: filter.Label,
})
}

resp := GetSnapshotResponse{
Snapshot: Snapshot{
Filters: convertedFilters,
CompositionType: getResp.CompositionType,
RetentionPeriod: getResp.RetentionPeriod,
Tags: getResp.Tags,
Created: getResp.Created,
ETag: &convertedETag,
Expires: getResp.Expires,
ItemsCount: getResp.ItemsCount,
Name: getResp.Snapshot.Name,
Size: getResp.Size,
Status: getResp.Snapshot.Status,
},
SyncToken: SyncToken(*getResp.SyncToken),
Link: getResp.Link,
}

return resp, nil
}

// ArchiveSnapshot archives a snapshot
//
// - snapshotName - The name of the snapshot to archive.
// - options - ArchiveSnapshotOptions contains the optional parameters to archive a snapshot
func (c *Client) ArchiveSnapshot(ctx context.Context, snapshotName string, options *ArchiveSnapshotOptions) (ArchiveSnapshotResponse, error) {
if options == nil {
options = &ArchiveSnapshotOptions{}
}

opts := updateSnapshotStatusOptions{
IfMatch: options.IfMatch,
IfNoneMatch: options.IfNoneMatch,
}
resp, err := c.updateSnapshotStatus(ctx, snapshotName, generated.SnapshotStatusArchived, &opts)

if err != nil {
return ArchiveSnapshotResponse{}, err
}

return (ArchiveSnapshotResponse)(resp), nil
}

// RecoverSnapshot recovers a snapshot
//
// - snapshotName - The name of the snapshot to recover.
// - options - RecoverSnapshotOptions contains the optional parameters to recover a snapshot
func (c *Client) RecoverSnapshot(ctx context.Context, snapshotName string, options *RecoverSnapshotOptions) (RecoverSnapshotResponse, error) {
if options == nil {
options = &RecoverSnapshotOptions{}
}

opts := updateSnapshotStatusOptions{
IfMatch: options.IfMatch,
IfNoneMatch: options.IfNoneMatch,
}
resp, err := c.updateSnapshotStatus(ctx, snapshotName, generated.SnapshotStatusReady, &opts)

if err != nil {
return RecoverSnapshotResponse{}, err
}

return (RecoverSnapshotResponse)(resp), nil
}

func (c *Client) updateSnapshotStatus(ctx context.Context, snapshotName string, status SnapshotStatus, options *updateSnapshotStatusOptions) (updateSnapshotStatusResponse, error) {
entity := generated.SnapshotUpdateParameters{
Status: &status,
}

opts := (*generated.AzureAppConfigurationClientUpdateSnapshotOptions)(options)

updateResp, err := c.appConfigClient.UpdateSnapshot(ctx, snapshotName, entity, opts)

if err != nil {
return updateSnapshotStatusResponse{}, err
}

convertedETag := azcore.ETag(*updateResp.Etag)

var convertedFilters []KeyValueFilter

for _, filter := range updateResp.Filters {
convertedFilters = append(convertedFilters, KeyValueFilter{
Key: filter.Key,
Label: filter.Label,
})
}

resp := updateSnapshotStatusResponse{
Snapshot: Snapshot{
Filters: convertedFilters,
CompositionType: updateResp.CompositionType,
RetentionPeriod: updateResp.RetentionPeriod,
Tags: updateResp.Tags,
Created: updateResp.Created,
ETag: &convertedETag,
Expires: updateResp.Expires,
ItemsCount: updateResp.ItemsCount,
Name: updateResp.Snapshot.Name,
Size: updateResp.Size,
Status: updateResp.Snapshot.Status,
},
SyncToken: SyncToken(*updateResp.SyncToken),
Link: updateResp.Link,
}

return resp, nil
}
Loading

0 comments on commit 178fe26

Please sign in to comment.