azappconfig

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2022 License: MIT Imports: 17 Imported by: 0

README

Azure App Configuration client library for Go

Azure App Configuration is a managed service that helps developers centralize their application and feature settings simply and securely. It allows you to create and manage application configuration settings and retrieve their revisions from a specific point in time.

Source code | Package (pkg.go.dev) | Product documentation

Getting started

Install packages

Install azappconfig with go get:

go get github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig
Prerequisites
  • An Azure subscription

  • Go version 1.16 or higher

  • An App Configuration store. If you need to create one, you can use the Azure Cloud Shell to create one with these commands (replace "my-resource-group" and "my-app-config" with your own, unique names):

    (Optional) if you want a new resource group to hold the Azure App Configuration:

    az group create --name my-resource-group --location westus2
    

    Create the Key Vault:

    az appconfig create --resource-group my-resource-group --name my-app-config --location westus2
    

    Output:

    {
        "creationDate": "...",
        "endpoint": "https://my-app-config.azconfig.io",
        "id": "/subscriptions/.../resourceGroups/my-resource-group/providers/Microsoft.AppConfiguration/configurationStores/my-app-config",
        "location": "westus2",
        "name": "my-app-config",
        "provisioningState": "Succeeded",
        "resourceGroup": "my-resource-group",
        "tags": {},
        "type": "Microsoft.AppConfiguration/configurationStores"
    }
    

    The "endpoint" property is the endpointUrl used by Client

Authenticate the client

This document demonstrates using the connection string. However, Client accepts any azidentity credential. See the azidentity documentation for more information about other credentials.

Create a client

Constructing the client requires your App Configuration connection string, which you can get from the Azure Portal.

export APPCONFIGURATION_CONNECTION_STRING="Endpoint=https://my-app-config.azconfig.io;Id=...;Secret=..."
import (
    "os"
    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
)

client, err = azappconfig.NewClientFromConnectionString(os.Getenv("APPCONFIGURATION_CONNECTION_STRING"), nil)

Or, using Default Azure Credential from Azure Identity:

import (
    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

credential, err := azidentity.NewDefaultAzureCredential(nil)

client, err = azappconfig.NewClient("https://my-app-config.azconfig.io", credential, nil)

Examples

This section contains code snippets covering common tasks:

Add a configuration setting
import (
    "fmt"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func ExampleAddConfigurationSetting() {
    connectionString := os.Getenv("APPCONFIGURATION_CONNECTION_STRING")
    client, err := azappconfig.NewClientFromConnectionString(connectionString, nil)
    if err != nil {
        panic(err)
    }

    // Create configuration setting
    resp, err := client.AddSetting(
        context.TODO(),
        "key",
        to.Ptr("value"),
        &azappconfig.AddSettingOptions{
            Label: to.Ptr("label"),
        })

    if err != nil {
        panic(err)
    }

    fmt.Println(*resp.Key)
    fmt.Println(*resp.Label)
    fmt.Println(*resp.Value)
}
Get a configuration setting
import (
    "fmt"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func ExampleGetConfigurationSetting() {
    connectionString := os.Getenv("APPCONFIGURATION_CONNECTION_STRING")
    client, err := azappconfig.NewClientFromConnectionString(connectionString, nil)
    if err != nil {
        panic(err)
    }

    // Get configuration setting
    resp, err := client.GetSetting(
        context.TODO(),
        "key"
        &azappconfig.GetSettingOptions{
            Label: to.Ptr("label")
        })

    if err != nil {
        panic(err)
    }

    fmt.Println(*resp.Key)
    fmt.Println(*resp.Label)
    fmt.Println(*resp.Value)
}
Set a configuration setting
import (
    "fmt"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func ExampleSetConfigurationSetting() {
    connectionString := os.Getenv("APPCONFIGURATION_CONNECTION_STRING")
    client, err := azappconfig.NewClientFromConnectionString(connectionString, nil)
    if err != nil {
        panic(err)
    }

    // Set configuration setting
    resp, err := client.SetSetting(
        context.TODO(),
        "key",
        to.Ptr("new_value"),
        &azappconfig.SetSettingOptions{
            Label: to.Ptr("label"),
        })

    if err != nil {
        panic(err)
    }

    fmt.Println(*resp.Key)
    fmt.Println(*resp.Label)
    fmt.Println(*resp.Value)
}
Set a configuration setting read only
import (
    "fmt"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func ExampleSetConfigurationSettingReadOnly() {
    connectionString := os.Getenv("APPCONFIGURATION_CONNECTION_STRING")
    client, err := azappconfig.NewClientFromConnectionString(connectionString, nil)
    if err != nil {
        panic(err)
    }

    // Set configuration setting read only
    resp, err := client.SetReadOnly(
        context.TODO(),
        "key",
        true,
        &azappconfig.SetReadOnlyOptions{
            Label: to.Ptr("label")
        })

    if err != nil {
        panic(err)
    }

    fmt.Println(*resp.Key)
    fmt.Println(*resp.Label)
    fmt.Println(*resp.Value)
    fmt.Println(*resp.IsReadOnly)

    // Remove read only status
    resp, err := client.SetReadOnly(
        context.TODO(),
        "key",
        false,
        &azappconfig.SetReadOnlyOptions{
            Label: to.Ptr("label")
        })

    if err != nil {
        panic(err)
    }

    fmt.Println(*resp.Key)
    fmt.Println(*resp.Label)
    fmt.Println(*resp.Value)
    fmt.Println(*resp.IsReadOnly)
}
List configuration setting revisions
import (
    "fmt"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func ExampleListRevisions() {
    connectionString := os.Getenv("APPCONFIGURATION_CONNECTION_STRING")
    client, err := azappconfig.NewClientFromConnectionString(connectionString, nil)
    if err != nil {
        panic(err)
    }

    revPgr := client.NewListRevisionsPager(
        azappconfig.SettingSelector{
            KeyFilter: to.Ptr("*"),
            LabelFilter: to.Ptr("*"),
            Fields: azappconfig.AllSettingFields()
        },
        nil)

    for revPgr.More() {
        if revResp, revErr := revPgr.NextPage(context.TODO()); revErr == nil {
            for _, setting := range revResp.Settings {
                fmt.Println(*setting.Key)
                fmt.Println(*setting.Label)
                fmt.Println(*setting.Value)
                fmt.Println(*setting.IsReadOnly)
            }
        }
    }
}
Delete a configuration setting
import (
    "fmt"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/appconfig/azappconfig"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func ExampleDeleteConfigurationSetting() {
    connectionString := os.Getenv("APPCONFIGURATION_CONNECTION_STRING")
    client, err := azappconfig.NewClientFromConnectionString(connectionString, nil)
    if err != nil {
        panic(err)
    }

    // Delete configuration setting
    resp, err := client.DeleteSetting(
        context.TODO(),
        "key",
        &azappconfig.DeleteSettingOptions{
            Label: to.Ptr("label")
        })

    if err != nil {
        panic(err)
    }
    fmt.Println(*resp.Key)
    fmt.Println(*resp.Label)
}

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions

Documentation

Index

Constants

View Source
const (
	// The primary identifier of a configuration setting.
	SettingFieldsKey = SettingFields(generated.Enum6Key)

	// A label used to group configuration settings.
	SettingFieldsLabel = SettingFields(generated.Enum6Label)

	// The value of the configuration setting.
	SettingFieldsValue = SettingFields(generated.Enum6Value)

	// The content type of the configuration setting's value.
	SettingFieldsContentType = SettingFields(generated.Enum6ContentType)

	// An ETag indicating the version of a configuration setting within a configuration store.
	SettingFieldsETag = SettingFields(generated.Enum6Etag)

	// The last time a modifying operation was performed on the given configuration setting.
	SettingFieldsLastModified = SettingFields(generated.Enum6LastModified)

	// A value indicating whether the configuration setting is read-only.
	SettingFieldsIsReadOnly = SettingFields(generated.Enum6Locked)

	// A list of tags that can help identify what a configuration setting may be applicable for.
	SettingFieldsTags = SettingFields(generated.Enum6Tags)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AddSettingOptions

type AddSettingOptions struct {
	// Configuration setting label.
	Label *string
}

AddSettingOptions contains the optional parameters for the AddSetting method.

type AddSettingResponse

type AddSettingResponse struct {
	Setting

	// Sync token for the Azure App Configuration client, corresponding to the current state of the client.
	SyncToken *string
}

AddSettingResponse contains the response from AddSetting method.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the struct for interacting with an Azure App Configuration instance.

func NewClient

func NewClient(endpointUrl string, cred azcore.TokenCredential, options *ClientOptions) (*Client, error)

NewClient returns a pointer to a Client object affinitized to an endpointUrl.

func NewClientFromConnectionString

func NewClientFromConnectionString(connectionString string, options *ClientOptions) (*Client, error)

NewClientFromConnectionString parses the connection string and returns a pointer to a Client object.

func (*Client) AddSetting

func (c *Client) AddSetting(ctx context.Context, key string, value *string, options *AddSettingOptions) (AddSettingResponse, error)

AddSetting creates a configuration setting only if the setting does not already exist in the configuration store.

func (*Client) DeleteSetting

func (c *Client) DeleteSetting(ctx context.Context, key string, options *DeleteSettingOptions) (DeleteSettingResponse, error)

DeleteSetting deletes a configuration setting from the configuration store.

func (*Client) GetSetting

func (c *Client) GetSetting(ctx context.Context, key string, options *GetSettingOptions) (GetSettingResponse, error)

GetSetting retrieves an existing configuration setting from the configuration store.

func (*Client) NewListRevisionsPager added in v0.2.0

func (c *Client) NewListRevisionsPager(selector SettingSelector, options *ListRevisionsOptions) *runtime.Pager[ListRevisionsPage]

NewListRevisionsPager creates a pager that retrieves the revisions of one or more configuration setting entities that match the specified setting selector.

func (*Client) SetReadOnly

func (c *Client) SetReadOnly(ctx context.Context, key string, isReadOnly bool, options *SetReadOnlyOptions) (SetReadOnlyResponse, error)

SetReadOnly sets an existing configuration setting to read only or read write state in the configuration store.

func (*Client) SetSetting

func (c *Client) SetSetting(ctx context.Context, key string, value *string, options *SetSettingOptions) (SetSettingResponse, error)

SetSetting creates a configuration setting if it doesn't exist or overwrites the existing setting in the configuration store.

func (*Client) UpdateSyncToken

func (c *Client) UpdateSyncToken(token string)

UpdateSyncToken sets an external synchronization token to ensure service requests receive up-to-date values.

type ClientOptions

type ClientOptions struct {
	azcore.ClientOptions
}

ClientOptions are the configurable options on a Client.

type DeleteSettingOptions

type DeleteSettingOptions struct {
	// Configuration setting label.
	Label *string

	// If set to true and the configuration setting exists in the configuration store,
	// delete the setting if the passed-in configuration setting is the same version as the one in the configuration store.
	// The setting versions are the same if their ETag fields match.
	OnlyIfUnchanged bool
}

DeleteSettingOptions contains the optional parameters for the DeleteSetting method.

type DeleteSettingResponse

type DeleteSettingResponse struct {
	Setting

	// Sync token for the Azure App Configuration client, corresponding to the current state of the client.
	SyncToken *string
}

DeleteSettingResponse contains the response from DeleteSetting method.

type GetSettingOptions

type GetSettingOptions struct {
	// Configuration setting label.
	Label *string

	// If set to true, only retrieve the setting from the configuration store if it has changed since the client last retrieved it.
	// It is determined to have changed if the ETag field on the passed-in configuration setting is different from the ETag
	// of the setting in the configuration store.
	OnlyIfChanged bool

	// The setting will be retrieved exactly as it existed at the provided time.
	AcceptDateTime *time.Time
}

GetSettingOptions contains the optional parameters for the GetSetting method.

type GetSettingResponse

type GetSettingResponse struct {
	Setting

	// Sync token for the Azure App Configuration client, corresponding to the current state of the client.
	SyncToken *string

	// Contains the timestamp of when the configuration setting was last modified.
	LastModified *time.Time
}

GetSettingResponse contains the configuration setting retrieved by GetSetting method.

type ListRevisionsOptions

type ListRevisionsOptions struct {
}

ListRevisionsOptions contains the optional parameters for the ListRevisions method.

type ListRevisionsPage

type ListRevisionsPage struct {
	// Contains the configuration settings returned that match the setting selector provided.
	Settings []Setting

	// Sync token for the Azure App Configuration client, corresponding to the current state of the client.
	SyncToken *string
}

ListRevisionsPage contains the configuration settings returned by ListRevisionsPager.

type SetReadOnlyOptions

type SetReadOnlyOptions struct {
	// Configuration setting label.
	Label *string

	// If set to true and the configuration setting exists in the configuration store, update the setting
	// if the passed-in configuration setting is the same version as the one in the configuration store.
	// The setting versions are the same if their ETag fields match.
	OnlyIfUnchanged bool
}

SetReadOnlyOptions contains the optional parameters for the SetReadOnly method.

type SetReadOnlyResponse

type SetReadOnlyResponse struct {
	Setting

	// Sync token for the Azure App Configuration client, corresponding to the current state of the client.
	SyncToken *string
}

SetReadOnlyResponse contains the response from SetReadOnly method.

type SetSettingOptions

type SetSettingOptions struct {
	// Configuration setting label.
	Label *string

	// If set to true and the configuration setting exists in the configuration store, overwrite the setting
	// if the passed-in configuration setting is the same version as the one in the configuration store.
	// The setting versions are the same if their ETag fields match.
	OnlyIfUnchanged bool
}

SetSettingOptions contains the optional parameters for the SetSetting method.

type SetSettingResponse

type SetSettingResponse struct {
	Setting

	// Sync token for the Azure App Configuration client, corresponding to the current state of the client.
	SyncToken *string
}

SetSettingResponse contains the response from SetSetting method.

type Setting

type Setting struct {
	// The primary identifier of the configuration setting.
	// A Key is used together with a Label to uniquely identify a configuration setting.
	Key *string

	// The configuration setting's value.
	Value *string

	// A value used to group configuration settings.
	// A Label is used together with a Key to uniquely identify a configuration setting.
	Label *string

	// The content type of the configuration setting's value.
	// Providing a proper content-type can enable transformations of values when they are retrieved by applications.
	ContentType *string

	// An ETag indicating the state of a configuration setting within a configuration store.
	ETag *azcore.ETag

	// A dictionary of tags used to assign additional properties to a configuration setting.
	// These can be used to indicate how a configuration setting may be applied.
	Tags map[string]string

	// The last time a modifying operation was performed on the given configuration setting.
	LastModified *time.Time

	// A value indicating whether the configuration setting is read only.
	// A read only configuration setting may not be modified until it is made writable.
	IsReadOnly *bool
}

Setting is a setting, defined by a unique combination of a Key and Label.

type SettingFields

type SettingFields string

SettingFields are fields to retrieve from a configuration setting.

func AllSettingFields

func AllSettingFields() []SettingFields

AllSettingFields returns a collection of all setting fields to use in SettingSelector.

type SettingSelector

type SettingSelector struct {
	// Key filter that will be used to select a set of configuration setting entities.
	KeyFilter *string

	// Label filter that will be used to select a set of configuration setting entities.
	LabelFilter *string

	// Indicates the point in time in the revision history of the selected configuration setting entities to retrieve.
	// If set, all properties of the configuration setting entities in the returned group will be exactly what they were at this time.
	AcceptDateTime *time.Time

	// The fields of the configuration setting to retrieve for each setting in the retrieved group.
	Fields []SettingFields
}

SettingSelector is a set of options that allows selecting a filtered set of configuration setting entities from the configuration store, and optionally allows indicating which fields of each setting to retrieve.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL