Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Enhance plan schemas #665

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/schemas/enabled_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package schemas

import "github.com/Azure/open-service-broker-azure/pkg/service"

const (
// EnabledParamString defines "enabled" value
EnabledParamString = "enabled"
// DisabledParamString defines "enabled" value
DisabledParamString = "disabled"
)

// EnabledDisabledValues returns "enabled" and "disabled" EnumValues
func EnabledDisabledValues() []service.EnumValue {
return []service.EnumValue{
{
Value: EnabledParamString,
Title: "Enabled",
},
{
Value: DisabledParamString,
Title: "Disabled",
},
}
}
54 changes: 27 additions & 27 deletions pkg/schemas/location_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ func GetLocationSchema() *service.StringPropertySchema {
Title: "Location",
Description: "The Azure region in which to provision" +
" applicable resources.",
AllowedValues: []string{
"australiaeast",
"australiasoutheast",
"brazilsouth",
"canadacentral",
"canadaeast",
"centralindia",
"centralus",
"eastasia",
"eastus",
"eastus2",
"japaneast",
"japanwest",
"koreacentral",
"koreasouth",
"northcentralus",
"northeurope",
"southcentralus",
"southeastasia",
"southindia",
"uksouth",
"ukwest",
"westcentralus",
"westeurope",
"westindia",
"westus",
"westus2",
OneOf: []service.EnumValue{
{Value: "australiaeast", Title: "Australia East"},
{Value: "australiasoutheast", Title: "Australia Southeast"},
{Value: "brazilsouth", Title: "Brazil South"},
{Value: "canadacentral", Title: "Canada Central"},
{Value: "canadaeast", Title: "Canada East"},
{Value: "centralindia", Title: "Central India"},
{Value: "centralus", Title: "Central US"},
{Value: "eastasia", Title: "East Asia"},
{Value: "eastus", Title: "East US"},
{Value: "eastus2", Title: "East US 2"},
{Value: "japaneast", Title: "Japan East"},
{Value: "japanwest", Title: "Japan West"},
{Value: "koreacentral", Title: "Korea Central"},
{Value: "koreasouth", Title: "Korea South"},
{Value: "northcentralus", Title: "North Central US"},
{Value: "northeurope", Title: "North Europe"},
{Value: "southcentralus", Title: "South Central US"},
{Value: "southeastasia", Title: "Southeast Asia"},
{Value: "southindia", Title: "South India"},
{Value: "uksouth", Title: "UK South"},
{Value: "ukwest", Title: "UK West"},
{Value: "westcentralus", Title: "West Central US"},
{Value: "westeurope", Title: "West Europe"},
{Value: "westindia", Title: "West India"},
{Value: "westus", Title: "West US"},
{Value: "westus2", Title: "West US 2"},
},
}
}
20 changes: 20 additions & 0 deletions pkg/schemas/redundancy_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package schemas

import "github.com/Azure/open-service-broker-azure/pkg/service"

// LocalRedundancy provides an EnumValue slice with "local" value.
func LocalRedundancy() []service.EnumValue {
return []service.EnumValue{{
Value: "local",
Title: "Local",
}}
}

// LocalAndGeoRedundancy provides an EnumValue slice with
// "local" and "geo" value.
func LocalAndGeoRedundancy() []service.EnumValue {
return []service.EnumValue{
{Value: "local", Title: "Local"},
{Value: "geo", Title: "Geo"},
}
}
33 changes: 33 additions & 0 deletions pkg/service/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type StringPropertySchema struct {
AllowedPattern string `json:"pattern,omitempty"` // nolint: lll
CustomPropertyValidator CustomStringPropertyValidator `json:"-"`
DefaultValue string `json:"default,omitempty"` // nolint: lll
OneOf []EnumValue `json:"oneOf,omitempty"` // nolint: lll
}

// MarshalJSON provides functionality to marshal a StringPropertySchema to JSON
Expand Down Expand Up @@ -163,6 +164,18 @@ func (s StringPropertySchema) validate(
return NewValidationError(context, "field value is invalid")
}
}
if len(s.OneOf) > 0 {
var found bool
for _, allowedValue := range s.OneOf {
if val == allowedValue.Value {
found = true
break
}
}
if !found {
return NewValidationError(context, "field value is invalid")
}
}
if s.AllowedPattern != "" {
pattern := regexp.MustCompile(s.AllowedPattern)
if !pattern.MatchString(val) {
Expand All @@ -175,6 +188,26 @@ func (s StringPropertySchema) validate(
return nil
}

// EnumValue represents an enum item in the oneOf JSON schema collection
type EnumValue struct {
Value string
Title string
}

// MarshalJSON provides functionality to marshal an EnumValue to JSON
// according to JSON schema definition.
func (v EnumValue) MarshalJSON() ([]byte, error) {
return json.Marshal(
struct {
Enum []string `json:"enum"`
Title string `json:"title"`
}{
Title: v.Title,
Enum: []string{v.Value},
},
)
}

// CustomIntPropertyValidator is a function type that describes the signature
// for functions that provide custom validation logic for integer properties.
type CustomIntPropertyValidator func(context string, value int64) error
Expand Down
17 changes: 17 additions & 0 deletions pkg/service/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ func TestValidateInputParametersSchema(t *testing.T) {
assert.Nil(t, err)
}

func TestValidateInputParameterSchemaWithOneOfField(t *testing.T) {
ips := InputParametersSchema{
PropertySchemas: map[string]PropertySchema{
"foo": StringPropertySchema{
OneOf: []EnumValue{{Value: "bar", Title: ""}, {Value: "bat", Title: ""}},
},
},
}

assert.NotNil(t, ips.Validate(map[string]interface{}{
"foo": "wrong",
}))
assert.Nil(t, ips.Validate(map[string]interface{}{
"foo": "bar",
}))
}

func TestStringPropertySchemaToJSON(t *testing.T) {
fooSps := StringPropertySchema{
Title: "bar",
Expand Down
15 changes: 15 additions & 0 deletions pkg/services/cosmosdb/commons_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cosmosdb

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidateReadLocations(t *testing.T) {
assert.NotNil(t, validateReadLocations("", []string{"ukwest", "no-existing"}))
assert.NotNil(t, validateReadLocations("", []string{"ukwest", "ukwest"}))

assert.Nil(t, validateReadLocations("", []string{"ukwest", "eastasia"}))
assert.Nil(t, validateReadLocations("", []string{}))
}
63 changes: 34 additions & 29 deletions pkg/services/cosmosdb/cosmos-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,14 @@ func validateReadLocations(
context string,
regions []string,
) error {
allowedLocations := make(map[string]bool)
for _, item := range allowedReadLocations() {
allowedLocations[item.Value] = true
}
occurred := make(map[string]bool)
for i := range regions {
region := regions[i]
if !allowedReadLocations[region] {
if !allowedLocations[region] {
return service.NewValidationError(
fmt.Sprintf("%s.readRegions", context),
fmt.Sprintf("given read region %s is not allowed", region),
Expand All @@ -274,34 +278,35 @@ func validateReadLocations(
}

// Allowed CosmosDB read locations, it is different from Azure regions.
// We use a map here to record all allowed regions.
var allowedReadLocations = map[string]bool{
"westus2": true,
"westus": true,
"southcentralus": true,
"centralus": true,
"northcentralus": true,
"canadacentral": true,
"eastus": true,
"eastus2": true,
"canadaeast": true,
"brazilsouth": true,
"northeurope": true,
"ukwest": true,
"uksouth": true,
"francecentral": true,
"westeurope": true,
"westindia": true,
"centralindia": true,
"southindia": true,
"southeastasia": true,
"eastasia": true,
"koreacentral": true,
"koreasouth": true,
"japaneast": true,
"japanwest": true,
"australiasoutheast": true,
"australiaeast": true,
func allowedReadLocations() []service.EnumValue {
return []service.EnumValue{
{Value: "westus2", Title: "West US 2"},
{Value: "westus", Title: "West US"},
{Value: "southcentralus", Title: "South Central US"},
{Value: "centralus", Title: "Central US"},
{Value: "northcentralus", Title: "North Central US"},
{Value: "canadacentral", Title: "Canada Central"},
{Value: "eastus", Title: "East US"},
{Value: "eastus2", Title: "East US 2"},
{Value: "canadaeast", Title: "Canada East"},
{Value: "brazilsouth", Title: "Brazil South"},
{Value: "northeurope", Title: "North Europe"},
{Value: "ukwest", Title: "UK West"},
{Value: "uksouth", Title: "UK South"},
{Value: "francecentral", Title: "France Central"},
{Value: "westeurope", Title: "West Europe"},
{Value: "westindia", Title: "West India"},
{Value: "centralindia", Title: "Central India"},
{Value: "southindia", Title: "South India"},
{Value: "southeastasia", Title: "Southeast Asia"},
{Value: "eastasia", Title: "East Asia"},
{Value: "koreacentral", Title: "Korea Central"},
{Value: "koreasouth", Title: "Korea South"},
{Value: "japaneast", Title: "Japan East"},
{Value: "japanwest", Title: "Japan West"},
{Value: "australiasoutheast", Title: "Australia Southeast"},
{Value: "australiaeast", Title: "Australia East"},
}
}

func (c *cosmosAccountManager) buildGoTemplateParamsCore(
Expand Down
33 changes: 18 additions & 15 deletions pkg/services/cosmosdb/plan_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func generateUpdatingParamsSchema() service.InputParametersSchema {
" will be synchronized across these regions.",
DefaultValue: []interface{}{},
CustomPropertyValidator: readLocationsValidator,
ItemsSchema: &service.StringPropertySchema{
OneOf: allowedReadLocations(),
},
},
"autoFailoverEnabled": &service.StringPropertySchema{
Title: "Auto failover enabled",
Description: "Specifies if you want Cosmos DB to perform" +
" automatic failover of the write region to one of" +
" the read regions in the rare event of a data center outage.",
DefaultValue: "disabled",
AllowedValues: []string{"enabled", "disabled"},
DefaultValue: "disabled",
OneOf: schemas.EnabledDisabledValues(),
},
"ipFilters": &service.ObjectPropertySchema{
Title: "IP filters",
Expand All @@ -45,16 +48,16 @@ func generateUpdatingParamsSchema() service.InputParametersSchema {
Title: "Allow access from Azure",
Description: "Specifies if Azure Services should be able to access" +
" the CosmosDB account.",
AllowedValues: []string{"enabled", "disabled"},
DefaultValue: "enabled",
OneOf: schemas.EnabledDisabledValues(),
DefaultValue: schemas.EnabledParamString,
},
"allowAccessFromPortal": &service.StringPropertySchema{
Title: "Allow access From Portal",
Description: "Specifies if the Azure Portal should be able to" +
" access the CosmosDB account. If `allowAccessFromAzure` is" +
" set to enabled, this value is ignored.",
AllowedValues: []string{"enabled", "disabled"},
DefaultValue: "enabled",
OneOf: schemas.EnabledDisabledValues(),
DefaultValue: schemas.EnabledParamString,
},
"allowedIPRanges": &service.ArrayPropertySchema{
Title: "Allowed IP ranges",
Expand All @@ -67,7 +70,7 @@ func generateUpdatingParamsSchema() service.InputParametersSchema {
},
},
DefaultValue: map[string]interface{}{
"allowAccessFromAzure": "enabled",
"allowAccessFromAzure": schemas.EnabledParamString,
},
},
"consistencyPolicy": &service.ObjectPropertySchema{
Expand All @@ -81,12 +84,12 @@ func generateUpdatingParamsSchema() service.InputParametersSchema {
Title: "Default consistency level",
Description: "The default consistency level and" +
" configuration settings of the Cosmos DB account.",
AllowedValues: []string{
"Eventual",
"Session",
"BoundedStaleness",
"Strong",
"ConsistentPrefix",
OneOf: []service.EnumValue{
{Value: "Eventual", Title: "Eventual"},
{Value: "Session", Title: "Session"},
{Value: "BoundedStaleness", Title: "Bounded staleness"},
{Value: "Strong", Title: "Strong"},
{Value: "ConsistentPrefix", Title: "Consistent prefix"},
},
},
"boundedStaleness": &service.ObjectPropertySchema{
Expand Down Expand Up @@ -136,8 +139,8 @@ func generateProvisioningParamsSchema() service.InputParametersSchema {
Title: "Multiple write regions enabled",
Description: "Specifies if you want the account to write " +
"in multiple regions.",
AllowedValues: []string{"enabled", "disabled"},
DefaultValue: "disabled",
OneOf: schemas.EnabledDisabledValues(),
DefaultValue: schemas.DisabledParamString,
},
}
sharedSchema := generateUpdatingParamsSchema()
Expand Down
7 changes: 4 additions & 3 deletions pkg/services/mysql/catalog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mysql

import (
"github.com/Azure/open-service-broker-azure/pkg/schemas"
"github.com/Azure/open-service-broker-azure/pkg/service"
)

Expand All @@ -13,7 +14,7 @@ func createBasicPlan(
allowedCores: []int64{1, 2},
defaultCores: 1,
maxStorage: 1024,
allowedBackupRedundancy: []string{"local"},
allowedBackupRedundancy: schemas.LocalRedundancy(),
}

return service.PlanProperties{
Expand Down Expand Up @@ -49,7 +50,7 @@ func createGPPlan(
allowedCores: []int64{2, 4, 8, 16, 32},
defaultCores: 2,
maxStorage: 2048,
allowedBackupRedundancy: []string{"local", "geo"},
allowedBackupRedundancy: schemas.LocalAndGeoRedundancy(),
}
extendedPlanData := map[string]interface{}{
"tierDetails": td,
Expand Down Expand Up @@ -90,7 +91,7 @@ func createMemoryOptimizedPlan(
allowedCores: []int64{2, 4, 8, 16},
defaultCores: 2,
maxStorage: 2048,
allowedBackupRedundancy: []string{"local", "geo"},
allowedBackupRedundancy: schemas.LocalAndGeoRedundancy(),
}
extendedPlanData := map[string]interface{}{
"tierDetails": td,
Expand Down
Loading