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

storage - allow azurerm_storage_account to be used in Data Plane restrictive environments #27818

Merged
merged 8 commits into from
Nov 5, 2024
Next Next commit
add feature flag for toggling storage data plane availability
  • Loading branch information
jackofallops committed Oct 25, 2024
commit 36e5d2d0ced99ee4f0b6e7ae42a30fb410ef3a41
3 changes: 3 additions & 0 deletions internal/features/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func Default() UserFeatures {
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
Storage: StorageFeatures{
DataPlaneAvailable: true,
},
Subscription: SubscriptionFeatures{
PreventCancellationOnDestroy: false,
},
Expand Down
5 changes: 5 additions & 0 deletions internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type UserFeatures struct {
ResourceGroup ResourceGroupFeatures
RecoveryServicesVault RecoveryServicesVault
ManagedDisk ManagedDiskFeatures
Storage StorageFeatures
Subscription SubscriptionFeatures
PostgresqlFlexibleServer PostgresqlFlexibleServerFeatures
MachineLearning MachineLearningFeatures
Expand Down Expand Up @@ -84,6 +85,10 @@ type AppConfigurationFeatures struct {
RecoverSoftDeleted bool
}

type StorageFeatures struct {
DataPlaneAvailable bool
}

type SubscriptionFeatures struct {
PreventCancellationOnDestroy bool
}
Expand Down
24 changes: 24 additions & 0 deletions internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
},
},

"storage": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"data_plane_available": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
},
},
},

"subscription": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down Expand Up @@ -580,6 +595,15 @@ func expandFeatures(input []interface{}) features.UserFeatures {
}
}
}
if raw, ok := val["storage"]; ok {
items := raw.([]interface{})
if len(items) > 0 {
storageRaw := items[0].(map[string]interface{})
if v, ok := storageRaw["data_plane_available"]; ok {
featuresMap.Storage.DataPlaneAvailable = v.(bool)
}
}
}

if raw, ok := val["subscription"]; ok {
items := raw.([]interface{})
Expand Down
67 changes: 67 additions & 0 deletions internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func TestExpandFeatures(t *testing.T) {
RecoveryServicesVault: features.RecoveryServicesVault{
RecoverSoftDeletedBackupProtectedVM: true,
},
Storage: features.StorageFeatures{
DataPlaneAvailable: true,
},
Subscription: features.SubscriptionFeatures{
PreventCancellationOnDestroy: false,
},
Expand Down Expand Up @@ -156,6 +159,11 @@ func TestExpandFeatures(t *testing.T) {
"recover_soft_deleted_backup_protected_vm": true,
},
},
"storage": []interface{}{
map[string]interface{}{
"data_plane_available": true,
},
},
"subscription": []interface{}{
map[string]interface{}{
"prevent_cancellation_on_destroy": true,
Expand Down Expand Up @@ -235,6 +243,9 @@ func TestExpandFeatures(t *testing.T) {
RecoveryServicesVault: features.RecoveryServicesVault{
RecoverSoftDeletedBackupProtectedVM: true,
},
Storage: features.StorageFeatures{
DataPlaneAvailable: true,
},
Subscription: features.SubscriptionFeatures{
PreventCancellationOnDestroy: true,
},
Expand Down Expand Up @@ -331,6 +342,11 @@ func TestExpandFeatures(t *testing.T) {
"recover_soft_deleted_backup_protected_vm": false,
},
},
"storage": []interface{}{
map[string]interface{}{
"data_plane_available": false,
},
},
"subscription": []interface{}{
map[string]interface{}{
"prevent_cancellation_on_destroy": false,
Expand Down Expand Up @@ -410,6 +426,9 @@ func TestExpandFeatures(t *testing.T) {
RecoveryServicesVault: features.RecoveryServicesVault{
RecoverSoftDeletedBackupProtectedVM: false,
},
Storage: features.StorageFeatures{
DataPlaneAvailable: false,
},
Subscription: features.SubscriptionFeatures{
PreventCancellationOnDestroy: false,
},
Expand Down Expand Up @@ -1431,6 +1450,54 @@ func TestExpandFeaturesManagedDisk(t *testing.T) {
}
}

func TestExpandFeaturesStorage(t *testing.T) {
testData := []struct {
Name string
Input []interface{}
EnvVars map[string]interface{}
Expected features.UserFeatures
}{
{
Name: "Empty Block",
Input: []interface{}{
map[string]interface{}{
"storage": []interface{}{},
},
},
Expected: features.UserFeatures{
Storage: features.StorageFeatures{
DataPlaneAvailable: true,
},
},
},
{
Name: "Storage Data Plane on Create is Disabled",
Input: []interface{}{
map[string]interface{}{
"storage": []interface{}{
map[string]interface{}{
"data_plane_available": false,
},
},
},
},
Expected: features.UserFeatures{
Storage: features.StorageFeatures{
DataPlaneAvailable: false,
},
},
},
}

for _, testCase := range testData {
t.Logf("[DEBUG] Test Case: %q", testCase.Name)
result := expandFeatures(testCase.Input)
if !reflect.DeepEqual(result.Storage, testCase.Expected.Storage) {
t.Fatalf("Expected %+v but got %+v", result.Storage, testCase.Expected.Storage)
}
}
}

func TestExpandFeaturesSubscription(t *testing.T) {
testData := []struct {
Name string
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/framework/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,19 @@ func (p *ProviderConfig) Load(ctx context.Context, data *ProviderModel, tfVersio
f.ManagedDisk.ExpandWithoutDowntime = true
}

if !features.Storage.IsNull() && !features.Storage.IsUnknown() {
var feature []Storage
d := features.Storage.ElementsAs(ctx, &feature, true)
diags.Append(d...)
if diags.HasError() {
return
}
f.Storage.DataPlaneAvailable = true
if !feature[0].DataPlaneAvailable.IsNull() && !feature[0].DataPlaneAvailable.IsUnknown() {
f.Storage.DataPlaneAvailable = feature[0].DataPlaneAvailable.ValueBool()
}
}

if !features.Subscription.IsNull() && !features.Subscription.IsUnknown() {
var feature []Subscription
d := features.Subscription.ElementsAs(ctx, &feature, true)
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/framework/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ func defaultFeaturesList() types.List {
})
managedDiskList, _ := basetypes.NewListValue(types.ObjectType{}.WithAttributeTypes(ManagedDiskAttributes), []attr.Value{managedDisk})

storage, _ := basetypes.NewObjectValueFrom(context.Background(), StorageAttributes, map[string]attr.Value{
"data_plane_available": basetypes.NewBoolNull(),
})
storageList, _ := basetypes.NewListValue(types.ObjectType{}.WithAttributeTypes(StorageAttributes), []attr.Value{storage})

subscription, _ := basetypes.NewObjectValueFrom(context.Background(), SubscriptionAttributes, map[string]attr.Value{
"prevent_cancellation_on_destroy": basetypes.NewBoolNull(),
})
Expand Down Expand Up @@ -314,6 +319,7 @@ func defaultFeaturesList() types.List {
"virtual_machine_scale_set": virtualMachineScaleSetList,
"resource_group": resourceGroupList,
"managed_disk": managedDiskList,
"storage": storageList,
"subscription": subscriptionList,
"postgresql_flexible_server": postgresqlFlexibleServerList,
"machine_learning": machineLearningList,
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/framework/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Features struct {
VirtualMachineScaleSet types.List `tfsdk:"virtual_machine_scale_set"`
ResourceGroup types.List `tfsdk:"resource_group"`
ManagedDisk types.List `tfsdk:"managed_disk"`
Storage types.List `tfsdk:"storage"`
Subscription types.List `tfsdk:"subscription"`
PostgresqlFlexibleServer types.List `tfsdk:"postgresql_flexible_server"`
MachineLearning types.List `tfsdk:"machine_learning"`
Expand All @@ -73,6 +74,7 @@ var FeaturesAttributes = map[string]attr.Type{
"virtual_machine_scale_set": types.ListType{}.WithElementType(types.ObjectType{}.WithAttributeTypes(VirtualMachineScaleSetAttributes)),
"resource_group": types.ListType{}.WithElementType(types.ObjectType{}.WithAttributeTypes(ResourceGroupAttributes)),
"managed_disk": types.ListType{}.WithElementType(types.ObjectType{}.WithAttributeTypes(ManagedDiskAttributes)),
"storage": types.ListType{}.WithElementType(types.ObjectType{}.WithAttributeTypes(StorageAttributes)),
"subscription": types.ListType{}.WithElementType(types.ObjectType{}.WithAttributeTypes(SubscriptionAttributes)),
"postgresql_flexible_server": types.ListType{}.WithElementType(types.ObjectType{}.WithAttributeTypes(PostgresqlFlexibleServerAttributes)),
"machine_learning": types.ListType{}.WithElementType(types.ObjectType{}.WithAttributeTypes(MachineLearningAttributes)),
Expand Down Expand Up @@ -204,6 +206,14 @@ var ManagedDiskAttributes = map[string]attr.Type{
"expand_without_downtime": types.BoolType,
}

type Storage struct {
DataPlaneAvailable types.Bool `tfsdk:"data_plane_available"`
}

var StorageAttributes = map[string]attr.Type{
"data_plane_available": types.BoolType,
}

type Subscription struct {
PreventCancellationOnDestroy types.Bool `tfsdk:"prevent_cancellation_on_destroy"`
}
Expand Down
9 changes: 9 additions & 0 deletions internal/provider/framework/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,15 @@ func (p *azureRmFrameworkProvider) Schema(_ context.Context, _ provider.SchemaRe
},
},
},
"storage": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"data_plane_available": schema.BoolAttribute{
Optional: true,
},
},
},
},
"subscription": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
Expand Down