Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Snapshot support for SDK #22118

Merged
merged 15 commits into from
Dec 14, 2023
9 changes: 8 additions & 1 deletion sdk/data/azappconfig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Release History

## 1.0.1 (Unreleased)
## 1.1.1 (Unreleased)
MBSolomon marked this conversation as resolved.
Show resolved Hide resolved

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
MBSolomon marked this conversation as resolved.
Show resolved Hide resolved

## 1.1.0 (2024-01-16)

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

### Other Changes
* Updated to latest version of `azcore`.
* Enabled spans for distributed tracing.
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_6bb46d4f60"
}
285 changes: 285 additions & 0 deletions sdk/data/azappconfig/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,288 @@ func (c *Client) NewListSettingsPager(selector SettingSelector, options *ListSet
Tracer: c.appConfigClient.Tracer(),
})
}

// NewGetSnapshotsPager - Gets a list of key-value snapshots.
//
// - options - NewGetSnapshotsPagerOptions contains the optional parameters to retrieve a snapshot
// method.
func (c *Client) NewGetSnapshotsPager(options *NewGetSnapshotsPagerOptions) *runtime.Pager[ListGetSnapshotsPagerResponse] {
MBSolomon marked this conversation as resolved.
Show resolved Hide resolved
opts := (*generated.AzureAppConfigurationClientGetSnapshotsOptions)(options)
ssRespPager := c.appConfigClient.NewGetSnapshotsPager(opts)

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

snapshots := []Snapshot{}

for _, snapshot := range page.Items {

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

var convertedFilters []KeyValueFilter

for _, filter := range snapshot.Filters {
convertedFilters = append(convertedFilters, KeyValueFilter{
MBSolomon marked this conversation as resolved.
Show resolved Hide resolved
Key: filter.Key,
Label: filter.Label,
})
}

snapshots = append(snapshots, Snapshot{
MBSolomon marked this conversation as resolved.
Show resolved Hide resolved
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 ListGetSnapshotsPagerResponse{
Snapshots: snapshots,
SyncToken: SyncToken(*page.SyncToken),
}, nil
},
Tracer: c.appConfigClient.Tracer(),
})
}

// NewListConfigurationSettingsForSnapshotPager
//
// - snapshotName - The name of the snapshot to list configuration settings for
// - options - ListConfigurationSettingsForSnapshotOptions contains the optional parameters to retrieve Snapshot configuration settings
func (c *Client) NewListConfigurationSettingsForSnapshotPager(snapshotName string, options *ListConfigurationSettingsForSnapshotOptions) *runtime.Pager[ListConfigurationSettingsForSnapshotResponse] {
MBSolomon marked this conversation as resolved.
Show resolved Hide resolved
if options == nil {
options = &ListConfigurationSettingsForSnapshotOptions{}
}

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[ListConfigurationSettingsForSnapshotResponse]{
More: func(ListConfigurationSettingsForSnapshotResponse) bool {
return ssRespPager.More()
},
Fetcher: func(ctx context.Context, cur *ListConfigurationSettingsForSnapshotResponse) (ListConfigurationSettingsForSnapshotResponse, error) {
page, err := ssRespPager.NextPage(ctx)
if err != nil {
return ListConfigurationSettingsForSnapshotResponse{}, err
}

var settings []Setting

for _, s := range page.Items {
settings = append(settings, settingFromGenerated(s))
}

return ListConfigurationSettingsForSnapshotResponse{
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 - BeginCreateSnapshotOptions contains the optional parameters to create a Snapshot
func (c *Client) BeginCreateSnapshot(ctx context.Context, snapshotName string, keyLabelFilter []SettingFilter, options *BeginCreateSnapshotOptions) (*runtime.Poller[CreateSnapshotResponse], error) {
filter := []generated.KeyValueFilter{}

if options == nil {
options = &BeginCreateSnapshotOptions{}
}

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
Loading