Skip to content

Commit

Permalink
introducing models for model transformations
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Sep 2, 2023
1 parent 8a69896 commit 93a60e8
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 62 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ response format:
response format:
```json
{
"ContentType": "...",
"ETag": "...",
"IsReadOnly": false,
"Key":" ...",
"Label": null,
"LastModified": null,
"SyncToken": "...",
"Tags": {},
"Value": "..."
"contentType": "...",
"eTag": "...",
"isReadOnly": false,
"key":" ...",
"label": null,
"lastModified": null,
"syncToken": "...",
"tags": {},
"value": "..."
}

```
Expand Down
4 changes: 3 additions & 1 deletion azuretpl/azure.appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/data/azappconfig"
"github.com/webdevops/go-common/azuresdk/cloudconfig"
"github.com/webdevops/go-common/utils/to"

"github.com/webdevops/helm-azure-tpl/azuretpl/models"
)

// buildAppConfigUrl builds Azure AppConfig url in case value is supplied as AppConfig name only
Expand Down Expand Up @@ -108,6 +110,6 @@ func (e *AzureTemplateExecutor) azAppConfigSetting(appConfigUrl string, settingN
appConfigValue.Value = to.StringPtr(secretMap["value"].(string))
}

return transformToInterface(appConfigValue)
return transformToInterface(models.NewAzAppconfigSettingFromReponse(appConfigValue))
})
}
56 changes: 4 additions & 52 deletions azuretpl/azure.keyvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,10 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
"github.com/webdevops/go-common/azuresdk/cloudconfig"
"github.com/webdevops/go-common/utils/to"
)

type (
AzKeyvaultSecretItem struct {
// The secret management attributes.
Attributes *azsecrets.SecretAttributes `json:"attributes"`

// The content type of the secret.
ContentType *string `json:"contentType"`

// The secret id.
ID string `json:"id"`

// Application specific metadata in the form of key-value pairs.
Tags map[string]*string `json:"tags"`

// The secret value.
Value *string `json:"value"`

Managed bool `json:"managed"`

Version string `json:"version" yaml:"version"`
Name string `json:"name" yaml:"name"`
}
"github.com/webdevops/helm-azure-tpl/azuretpl/models"
)

func newAzKeyvaultSecretItem(secret azsecrets.Secret) *AzKeyvaultSecretItem {
return &AzKeyvaultSecretItem{
Attributes: secret.Attributes,
ContentType: secret.ContentType,
ID: string(*secret.ID),
Tags: secret.Tags,
Value: secret.Value,
Managed: to.Bool(secret.Managed),
Version: secret.ID.Version(),
Name: secret.ID.Name(),
}
}

func newAzKeyvaultSecretListItem(secret azsecrets.SecretProperties) *AzKeyvaultSecretItem {
return &AzKeyvaultSecretItem{
Attributes: secret.Attributes,
ContentType: secret.ContentType,
ID: string(*secret.ID),
Tags: secret.Tags,
Value: nil,
Managed: to.Bool(secret.Managed),
Version: secret.ID.Version(),
Name: secret.ID.Name(),
}
}

// buildAzKeyVaulUrl builds Azure KeyVault url in case value is supplied as KeyVault name only
func (e *AzureTemplateExecutor) buildAzKeyVaulUrl(vaultUrl string) (string, error) {
// do not build keyvault url in lint mode
Expand Down Expand Up @@ -156,7 +108,7 @@ func (e *AzureTemplateExecutor) azKeyVaultSecret(vaultUrl string, secretName str
e.logger.Infof(`using Azure KeyVault secret '%v' -> '%v' (version: %v)`, vaultUrl, secretName, secret.ID.Version())
e.handleCicdMaskSecret(to.String(secret.Secret.Value))

return transformToInterface(newAzKeyvaultSecretItem(secret.Secret))
return transformToInterface(models.NewAzSecretItem(secret.Secret))
})
}

Expand Down Expand Up @@ -219,7 +171,7 @@ func (e *AzureTemplateExecutor) azKeyVaultSecretVersions(vaultUrl string, secret

e.handleCicdMaskSecret(to.String(secret.Secret.Value))

if val, err := transformToInterface(newAzKeyvaultSecretItem(secret.Secret)); err == nil {
if val, err := transformToInterface(models.NewAzSecretItem(secret.Secret)); err == nil {
ret = append(ret, val)
} else {
return nil, err
Expand Down Expand Up @@ -270,7 +222,7 @@ func (e *AzureTemplateExecutor) azKeyVaultSecretList(vaultUrl string, secretName
}

for _, secret := range result.Value {
secretData, err := transformToInterface(newAzKeyvaultSecretListItem(*secret))
secretData, err := transformToInterface(models.NewAzSecretItemFromSecretproperties(*secret))
if err != nil {
return nil, fmt.Errorf(`unable to transform KeyVault secret '%v': %w`, secret.ID.Name(), err)
}
Expand Down
64 changes: 64 additions & 0 deletions azuretpl/models/appconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package models

import (
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/data/azappconfig"
"github.com/webdevops/go-common/utils/to"
)

type (
AzAppconfigSetting struct {
// The primary identifier of the configuration setting.
// A Key is used together with a Label to uniquely identify a configuration setting.
Key *string `json:"key"`

// The configuration setting's value.
Value *string `json:"value"`

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

// 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 `json:"contentType"`

// An ETag indicating the state of a configuration setting within a configuration store.
ETag *azcore.ETag `json:"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 `json:"tags"`

// The last time a modifying operation was performed on the given configuration setting.
LastModified *time.Time `json:"lastModified"`

// 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 `json:"isReadOnly"`

// Sync token for the Azure App Configuration client, corresponding to the current state of the client.
SyncToken *string `json:"syncToken"`
}
)

func NewAzAppconfigSetting(setting azappconfig.Setting) *AzAppconfigSetting {
return &AzAppconfigSetting{
Key: setting.Key,
Value: setting.Value,
Label: setting.Label,
ContentType: setting.ContentType,
ETag: setting.ETag,
Tags: setting.Tags,
LastModified: setting.LastModified,
IsReadOnly: to.Bool(setting.IsReadOnly),
}
}

func NewAzAppconfigSettingFromReponse(setting azappconfig.GetSettingResponse) *AzAppconfigSetting {
ret := NewAzAppconfigSetting(setting.Setting)
ret.SyncToken = setting.SyncToken
return ret
}
56 changes: 56 additions & 0 deletions azuretpl/models/keyvault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package models

import (
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
"github.com/webdevops/go-common/utils/to"
)

type (
AzSecret struct {
// The secret management attributes.
Attributes *azsecrets.SecretAttributes `json:"attributes"`

// The content type of the secret.
ContentType *string `json:"contentType"`

// The secret id.
ID string `json:"id"`

// Application specific metadata in the form of key-value pairs.
Tags map[string]*string `json:"tags"`

// The secret value.
Value *string `json:"value"`

Managed bool `json:"managed"`

Version string `json:"version" yaml:"version"`
Name string `json:"name" yaml:"name"`
}
)

func NewAzSecretItem(secret azsecrets.Secret) *AzSecret {
return &AzSecret{
Attributes: secret.Attributes,
ContentType: secret.ContentType,
ID: string(*secret.ID),
Tags: secret.Tags,
Value: secret.Value,
Managed: to.Bool(secret.Managed),
Version: secret.ID.Version(),
Name: secret.ID.Name(),
}
}

func NewAzSecretItemFromSecretproperties(secret azsecrets.SecretProperties) *AzSecret {
return &AzSecret{
Attributes: secret.Attributes,
ContentType: secret.ContentType,
ID: string(*secret.ID),
Tags: secret.Tags,
Value: nil,
Managed: to.Bool(secret.Managed),
Version: secret.ID.Version(),
Name: secret.ID.Name(),
}
}

0 comments on commit 93a60e8

Please sign in to comment.