diff --git a/internal/services/mssqlmanagedinstance/client/client.go b/internal/services/mssqlmanagedinstance/client/client.go index dce0f0881c1d..e21711c5bd49 100644 --- a/internal/services/mssqlmanagedinstance/client/client.go +++ b/internal/services/mssqlmanagedinstance/client/client.go @@ -18,6 +18,7 @@ import ( "github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/managedinstances" "github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/managedinstancevulnerabilityassessments" "github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/managedserversecurityalertpolicies" + "github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules" "github.com/hashicorp/terraform-provider-azurerm/internal/common" ) @@ -33,6 +34,7 @@ type Client struct { ManagedInstanceEncryptionProtectorClient *managedinstanceencryptionprotectors.ManagedInstanceEncryptionProtectorsClient ManagedInstanceFailoverGroupsClient *instancefailovergroups.InstanceFailoverGroupsClient ManagedInstanceKeysClient *managedinstancekeys.ManagedInstanceKeysClient + ManagedInstanceStartStopSchedulesClient *startstopmanagedinstanceschedules.StartStopManagedInstanceSchedulesClient options *common.ClientOptions } @@ -104,6 +106,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) { } o.Configure(managedInstanceServerSecurityAlertPoliciesClient.Client, o.Authorizers.ResourceManager) + managedInstanceStartStopSchedulesClient, err := startstopmanagedinstanceschedules.NewStartStopManagedInstanceSchedulesClientWithBaseURI(o.Environment.ResourceManager) + if err != nil { + return nil, fmt.Errorf("building Managed Instance Start Stop Schedules Client: %+v", err) + } + o.Configure(managedInstanceStartStopSchedulesClient.Client, o.Authorizers.ResourceManager) + return &Client{ ManagedDatabasesClient: managedDatabasesClient, ManagedInstanceAdministratorsClient: managedInstancesAdministratorsClient, @@ -116,6 +124,7 @@ func NewClient(o *common.ClientOptions) (*Client, error) { ManagedInstancesShortTermRetentionPoliciesClient: managedInstancesShortTermRetentionPoliciesClient, ManagedInstanceVulnerabilityAssessmentsClient: managedInstanceVulnerabilityAssessmentsClient, ManagedInstancesClient: managedInstancesClient, + ManagedInstanceStartStopSchedulesClient: managedInstanceStartStopSchedulesClient, options: o, }, nil diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_instance_start_stop_schedule_resource.go b/internal/services/mssqlmanagedinstance/mssql_managed_instance_start_stop_schedule_resource.go new file mode 100644 index 000000000000..892d0b97cf4d --- /dev/null +++ b/internal/services/mssqlmanagedinstance/mssql_managed_instance_start_stop_schedule_resource.go @@ -0,0 +1,330 @@ +package mssqlmanagedinstance + +import ( + "context" + "fmt" + "time" + + "github.com/hashicorp/go-azure-helpers/lang/pointer" + "github.com/hashicorp/go-azure-helpers/lang/response" + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + schedule "github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules" + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/mssqlmanagedinstance/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/mssqlmanagedinstance/validate" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" +) + +type SqlManagedInstanceStartStopScheduleModel struct { + SqlManagedInstanceId string `tfschema:"managed_instance_id"` + Description string `tfschema:"description"` + Schedule []ScheduleItemModel `tfschema:"schedule"` + TimeZoneId string `tfschema:"timezone_id"` + NextExecutionTime string `tfschema:"next_execution_time"` + NextRunAction string `tfschema:"next_run_action"` +} + +type ScheduleItemModel struct { + StartDay schedule.DayOfWeek `tfschema:"start_day"` + StartTime string `tfschema:"start_time"` + StopDay schedule.DayOfWeek `tfschema:"stop_day"` + StopTime string `tfschema:"stop_time"` +} + +type MsSqlManagedInstanceStartStopScheduleResource struct{} + +var _ sdk.ResourceWithUpdate = MsSqlManagedInstanceStartStopScheduleResource{} + +func (r MsSqlManagedInstanceStartStopScheduleResource) ResourceType() string { + return "azurerm_mssql_managed_instance_start_stop_schedule" +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) ModelObject() interface{} { + return &SqlManagedInstanceStartStopScheduleModel{} +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { + return validate.ManagedInstanceStartStopScheduleID +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) Arguments() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "managed_instance_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: commonids.ValidateSqlManagedInstanceID, + }, + + "description": { + Type: pluginsdk.TypeString, + Optional: true, + }, + + "schedule": { + Type: pluginsdk.TypeList, + Required: true, + MinItems: 1, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "start_day": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice(schedule.PossibleValuesForDayOfWeek(), false), + }, + + "start_time": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "stop_day": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice(schedule.PossibleValuesForDayOfWeek(), false), + }, + + "stop_time": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + }, + }, + }, + + "timezone_id": { + Type: pluginsdk.TypeString, + Optional: true, + Default: "UTC", + ValidateFunc: validation.StringIsNotEmpty, + }, + } +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "next_execution_time": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "next_run_action": { + Type: pluginsdk.TypeString, + Computed: true, + }, + } +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) Create() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + var model SqlManagedInstanceStartStopScheduleModel + if err := metadata.Decode(&model); err != nil { + return fmt.Errorf("decoding: %+v", err) + } + + client := metadata.Client.MSSQLManagedInstance.ManagedInstanceStartStopSchedulesClient + + managedInstanceId, err := commonids.ParseSqlManagedInstanceID(model.SqlManagedInstanceId) + if err != nil { + return err + } + + id := *managedInstanceId + + existing, err := client.Get(ctx, id) + if err != nil && !response.WasNotFound(existing.HttpResponse) { + return fmt.Errorf("checking for existing %s: %+v", id, err) + } + + if !response.WasNotFound(existing.HttpResponse) { + return metadata.ResourceRequiresImport(r.ResourceType(), id) + } + + properties := &schedule.StartStopManagedInstanceSchedule{ + Properties: &schedule.StartStopManagedInstanceScheduleProperties{}, + } + + if model.Description != "" { + properties.Properties.Description = &model.Description + } + + properties.Properties.ScheduleList = expandScheduleItemModelArray(model.Schedule) + + if model.TimeZoneId != "" { + properties.Properties.TimeZoneId = &model.TimeZoneId + } + + if _, err := client.CreateOrUpdate(ctx, id, *properties); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) + } + + scheduleID := parse.NewManagedInstanceStartStopScheduleID(id.SubscriptionId, id.ResourceGroupName, id.ManagedInstanceName, "default") + metadata.SetID(scheduleID) + + return nil + }, + } +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) Update() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.MSSQLManagedInstance.ManagedInstanceStartStopSchedulesClient + + id, err := parse.ManagedInstanceStartStopScheduleID(metadata.ResourceData.Id()) + if err != nil { + return err + } + + managedInstanceID := commonids.NewSqlManagedInstanceID(id.SubscriptionId, id.ResourceGroup, id.ManagedInstanceName) + + var model SqlManagedInstanceStartStopScheduleModel + if err := metadata.Decode(&model); err != nil { + return fmt.Errorf("decoding: %+v", err) + } + + resp, err := client.Get(ctx, managedInstanceID) + if err != nil { + return fmt.Errorf("retrieving %s: %+v", managedInstanceID, err) + } + + if resp.Model == nil { + return fmt.Errorf("retrieving %s:`model` was nil", managedInstanceID) + } + if resp.Model.Properties == nil { + return fmt.Errorf("retrieving %s:`properties` was nil", managedInstanceID) + } + + properties := resp.Model + + if metadata.ResourceData.HasChange("description") { + properties.Properties.Description = pointer.To(model.Description) + } + + if metadata.ResourceData.HasChange("schedule") { + properties.Properties.ScheduleList = expandScheduleItemModelArray(model.Schedule) + } + + if metadata.ResourceData.HasChange("timezone_id") { + properties.Properties.TimeZoneId = pointer.To(model.TimeZoneId) + } + + if _, err := client.CreateOrUpdate(ctx, managedInstanceID, *properties); err != nil { + return fmt.Errorf("updating %s: %+v", managedInstanceID, err) + } + + return nil + }, + } +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) Read() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.MSSQLManagedInstance.ManagedInstanceStartStopSchedulesClient + + id, err := parse.ManagedInstanceStartStopScheduleID(metadata.ResourceData.Id()) + if err != nil { + return err + } + + managedInstanceID := commonids.NewSqlManagedInstanceID(id.SubscriptionId, id.ResourceGroup, id.ManagedInstanceName) + + resp, err := client.Get(ctx, managedInstanceID) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + return metadata.MarkAsGone(managedInstanceID) + } + + return fmt.Errorf("retrieving %s: %+v", managedInstanceID, err) + } + + state := SqlManagedInstanceStartStopScheduleModel{ + SqlManagedInstanceId: managedInstanceID.ID(), + } + + if model := resp.Model; model != nil { + if properties := model.Properties; properties != nil { + state.Description = pointer.From(properties.Description) + + state.NextExecutionTime = pointer.From(properties.NextExecutionTime) + + state.NextRunAction = pointer.From(properties.NextRunAction) + + if properties.ScheduleList != nil { + state.Schedule = flattenScheduleItemModelArray(properties.ScheduleList) + } + + state.TimeZoneId = pointer.From(properties.TimeZoneId) + } + } + + return metadata.Encode(&state) + }, + } +} + +func (r MsSqlManagedInstanceStartStopScheduleResource) Delete() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.MSSQLManagedInstance.ManagedInstanceStartStopSchedulesClient + + id, err := parse.ManagedInstanceStartStopScheduleID(metadata.ResourceData.Id()) + if err != nil { + return err + } + + managedInstanceID := commonids.NewSqlManagedInstanceID(id.SubscriptionId, id.ResourceGroup, id.ManagedInstanceName) + + if _, err := client.Delete(ctx, managedInstanceID); err != nil { + return fmt.Errorf("deleting %s: %+v", id, err) + } + + return nil + }, + } +} + +func expandScheduleItemModelArray(inputList []ScheduleItemModel) []schedule.ScheduleItem { + outputList := make([]schedule.ScheduleItem, 0, len(inputList)) + + for _, v := range inputList { + input := v + output := schedule.ScheduleItem{ + StartDay: input.StartDay, + StartTime: input.StartTime, + StopDay: input.StopDay, + StopTime: input.StopTime, + } + + outputList = append(outputList, output) + } + return outputList +} + +func flattenScheduleItemModelArray(inputList []schedule.ScheduleItem) []ScheduleItemModel { + outputList := make([]ScheduleItemModel, 0, len(inputList)) + + if inputList == nil { + return outputList + } + for _, input := range inputList { + output := ScheduleItemModel{ + StartDay: input.StartDay, + StartTime: input.StartTime, + StopDay: input.StopDay, + StopTime: input.StopTime, + } + + outputList = append(outputList, output) + } + return outputList +} diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_instance_start_stop_schedule_resource_test.go b/internal/services/mssqlmanagedinstance/mssql_managed_instance_start_stop_schedule_resource_test.go new file mode 100644 index 000000000000..dcb0d8968ed0 --- /dev/null +++ b/internal/services/mssqlmanagedinstance/mssql_managed_instance_start_stop_schedule_resource_test.go @@ -0,0 +1,152 @@ +package mssqlmanagedinstance_test + +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/go-azure-helpers/lang/pointer" + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/mssqlmanagedinstance/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +type sqlManagedInstanceStartStopScheduleResource struct{} + +func TestAccMsSqlManagedInstanceStartStopSchedule_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_mssql_managed_instance_start_stop_schedule", "test") + r := sqlManagedInstanceStartStopScheduleResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccMsSqlManagedInstanceStartStopSchedule_complete(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_mssql_managed_instance_start_stop_schedule", "test") + r := sqlManagedInstanceStartStopScheduleResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccMsSqlManagedInstanceStartStopSchedule_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_mssql_managed_instance_start_stop_schedule", "test") + r := sqlManagedInstanceStartStopScheduleResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.update(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func (r sqlManagedInstanceStartStopScheduleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := parse.ManagedInstanceStartStopScheduleID(state.ID) + if err != nil { + return nil, err + } + + client := clients.MSSQLManagedInstance.ManagedInstanceStartStopSchedulesClient + + managedInstanceId := commonids.NewSqlManagedInstanceID(id.SubscriptionId, id.ResourceGroup, id.ManagedInstanceName) + + resp, err := client.Get(ctx, managedInstanceId) + if err != nil { + return nil, fmt.Errorf("retrieving %s: %+v", id, err) + } + return pointer.To(resp.Model != nil), nil +} + +func (r sqlManagedInstanceStartStopScheduleResource) template(data acceptance.TestData) string { + return MsSqlManagedInstanceResource{}.basic(data) +} + +func (r sqlManagedInstanceStartStopScheduleResource) basic(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` + %s + +resource "azurerm_mssql_managed_instance_start_stop_schedule" "test" { + managed_instance_id = azurerm_mssql_managed_instance.test.id + schedule { + start_day = "Wednesday" + start_time = "11:00" + stop_day = "Wednesday" + stop_time = "23:00" + } + +} +`, template) +} + +func (r sqlManagedInstanceStartStopScheduleResource) complete(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` + %s + +resource "azurerm_mssql_managed_instance_start_stop_schedule" "test" { + managed_instance_id = azurerm_mssql_managed_instance.test.id + description = "test description" + timezone_id = "Central European Standard Time" + schedule { + start_day = "Wednesday" + start_time = "11:00" + stop_day = "Wednesday" + stop_time = "23:00" + } + +} +`, template) +} + +func (r sqlManagedInstanceStartStopScheduleResource) update(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` + %s + +resource "azurerm_mssql_managed_instance_start_stop_schedule" "test" { + managed_instance_id = azurerm_mssql_managed_instance.test.id + description = "updated test description" + timezone_id = "Central European Standard Time" + schedule { + start_day = "Wednesday" + start_time = "10:00" + stop_day = "Wednesday" + stop_time = "22:00" + } + + schedule { + start_day = "Thursday" + start_time = "11:00" + stop_day = "Thursday" + stop_time = "23:00" + } + +} +`, template) +} diff --git a/internal/services/mssqlmanagedinstance/parse/managed_instance_start_stop_schedule.go b/internal/services/mssqlmanagedinstance/parse/managed_instance_start_stop_schedule.go new file mode 100644 index 000000000000..9e2618846041 --- /dev/null +++ b/internal/services/mssqlmanagedinstance/parse/managed_instance_start_stop_schedule.go @@ -0,0 +1,79 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "errors" + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +type ManagedInstanceStartStopScheduleId struct { + SubscriptionId string + ResourceGroup string + ManagedInstanceName string + StartStopScheduleName string +} + +func NewManagedInstanceStartStopScheduleID(subscriptionId, resourceGroup, managedInstanceName, startStopScheduleName string) ManagedInstanceStartStopScheduleId { + return ManagedInstanceStartStopScheduleId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + ManagedInstanceName: managedInstanceName, + StartStopScheduleName: startStopScheduleName, + } +} + +func (id ManagedInstanceStartStopScheduleId) String() string { + segments := []string{ + fmt.Sprintf("Start Stop Schedule Name %q", id.StartStopScheduleName), + fmt.Sprintf("Managed Instance Name %q", id.ManagedInstanceName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Managed Instance Start Stop Schedule", segmentsStr) +} + +func (id ManagedInstanceStartStopScheduleId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Sql/managedInstances/%s/startStopSchedules/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ManagedInstanceName, id.StartStopScheduleName) +} + +// ManagedInstanceStartStopScheduleID parses a ManagedInstanceStartStopSchedule ID into an ManagedInstanceStartStopScheduleId struct +func ManagedInstanceStartStopScheduleID(input string) (*ManagedInstanceStartStopScheduleId, error) { + id, err := resourceids.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("parsing %q as an ManagedInstanceStartStopSchedule ID: %+v", input, err) + } + + resourceId := ManagedInstanceStartStopScheduleId{ + SubscriptionId: id.SubscriptionID, + ResourceGroup: id.ResourceGroup, + } + + if resourceId.SubscriptionId == "" { + return nil, errors.New("ID was missing the 'subscriptions' element") + } + + if resourceId.ResourceGroup == "" { + return nil, errors.New("ID was missing the 'resourceGroups' element") + } + + if resourceId.ManagedInstanceName, err = id.PopSegment("managedInstances"); err != nil { + return nil, err + } + if resourceId.StartStopScheduleName, err = id.PopSegment("startStopSchedules"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/internal/services/mssqlmanagedinstance/parse/managed_instance_start_stop_schedule_test.go b/internal/services/mssqlmanagedinstance/parse/managed_instance_start_stop_schedule_test.go new file mode 100644 index 000000000000..abc435b1d786 --- /dev/null +++ b/internal/services/mssqlmanagedinstance/parse/managed_instance_start_stop_schedule_test.go @@ -0,0 +1,130 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "testing" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +var _ resourceids.Id = ManagedInstanceStartStopScheduleId{} + +func TestManagedInstanceStartStopScheduleIDFormatter(t *testing.T) { + actual := NewManagedInstanceStartStopScheduleID("12345678-1234-9876-4563-123456789012", "resGroup1", "instance1", "default").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/startStopSchedules/default" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestManagedInstanceStartStopScheduleID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *ManagedInstanceStartStopScheduleId + }{ + { + // empty + Input: "", + Error: true, + }, + + { + // missing SubscriptionId + Input: "/", + Error: true, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Error: true, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Error: true, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Error: true, + }, + + { + // missing ManagedInstanceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/", + Error: true, + }, + + { + // missing value for ManagedInstanceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/", + Error: true, + }, + + { + // missing StartStopScheduleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/", + Error: true, + }, + + { + // missing value for StartStopScheduleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/startStopSchedules/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/startStopSchedules/default", + Expected: &ManagedInstanceStartStopScheduleId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ManagedInstanceName: "instance1", + StartStopScheduleName: "default", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.SQL/MANAGEDINSTANCES/INSTANCE1/STARTSTOPSCHEDULES/DEFAULT", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ManagedInstanceStartStopScheduleID(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %s", err) + } + if v.Error { + t.Fatal("Expect an error but didn't get one") + } + + if actual.SubscriptionId != v.Expected.SubscriptionId { + t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + } + if actual.ManagedInstanceName != v.Expected.ManagedInstanceName { + t.Fatalf("Expected %q but got %q for ManagedInstanceName", v.Expected.ManagedInstanceName, actual.ManagedInstanceName) + } + if actual.StartStopScheduleName != v.Expected.StartStopScheduleName { + t.Fatalf("Expected %q but got %q for StartStopScheduleName", v.Expected.StartStopScheduleName, actual.StartStopScheduleName) + } + } +} diff --git a/internal/services/mssqlmanagedinstance/registration.go b/internal/services/mssqlmanagedinstance/registration.go index 1297d742f4c0..d639c86ab8bd 100644 --- a/internal/services/mssqlmanagedinstance/registration.go +++ b/internal/services/mssqlmanagedinstance/registration.go @@ -60,5 +60,6 @@ func (r Registration) Resources() []sdk.Resource { MsSqlManagedInstanceActiveDirectoryAdministratorResource{}, MsSqlManagedInstanceFailoverGroupResource{}, MsSqlManagedInstanceResource{}, + MsSqlManagedInstanceStartStopScheduleResource{}, } } diff --git a/internal/services/mssqlmanagedinstance/resourceids.go b/internal/services/mssqlmanagedinstance/resourceids.go index 65465eb92b91..78e37d41aaae 100644 --- a/internal/services/mssqlmanagedinstance/resourceids.go +++ b/internal/services/mssqlmanagedinstance/resourceids.go @@ -10,3 +10,4 @@ package mssqlmanagedinstance //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedInstanceFailoverGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/locations/Location/instanceFailoverGroups/failoverGroup1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedInstanceVulnerabilityAssessment -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/vulnerabilityAssessments/assessment1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedInstancesSecurityAlertPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Sql/managedInstances/instance1/securityAlertPolicies/Default +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedInstanceStartStopSchedule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/startStopSchedules/default diff --git a/internal/services/mssqlmanagedinstance/validate/managed_instance_start_stop_schedule_id.go b/internal/services/mssqlmanagedinstance/validate/managed_instance_start_stop_schedule_id.go new file mode 100644 index 000000000000..aa9345922e53 --- /dev/null +++ b/internal/services/mssqlmanagedinstance/validate/managed_instance_start_stop_schedule_id.go @@ -0,0 +1,26 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + + "github.com/hashicorp/terraform-provider-azurerm/internal/services/mssqlmanagedinstance/parse" +) + +func ManagedInstanceStartStopScheduleID(input interface{}, key string) (warnings []string, errors []error) { + v, ok := input.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected %q to be a string", key)) + return + } + + if _, err := parse.ManagedInstanceStartStopScheduleID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/mssqlmanagedinstance/validate/managed_instance_start_stop_schedule_id_test.go b/internal/services/mssqlmanagedinstance/validate/managed_instance_start_stop_schedule_id_test.go new file mode 100644 index 000000000000..d89afe7c8fb2 --- /dev/null +++ b/internal/services/mssqlmanagedinstance/validate/managed_instance_start_stop_schedule_id_test.go @@ -0,0 +1,90 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestManagedInstanceStartStopScheduleID(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + { + // empty + Input: "", + Valid: false, + }, + + { + // missing SubscriptionId + Input: "/", + Valid: false, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Valid: false, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Valid: false, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Valid: false, + }, + + { + // missing ManagedInstanceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/", + Valid: false, + }, + + { + // missing value for ManagedInstanceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/", + Valid: false, + }, + + { + // missing StartStopScheduleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/", + Valid: false, + }, + + { + // missing value for StartStopScheduleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/startStopSchedules/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/instance1/startStopSchedules/default", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.SQL/MANAGEDINSTANCES/INSTANCE1/STARTSTOPSCHEDULES/DEFAULT", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := ManagedInstanceStartStopScheduleID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/README.md b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/README.md new file mode 100644 index 000000000000..91bfab115a4c --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/README.md @@ -0,0 +1,91 @@ + +## `github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules` Documentation + +The `startstopmanagedinstanceschedules` SDK allows for interaction with Azure Resource Manager `sql` (API Version `2023-08-01-preview`). + +This readme covers example usages, but further information on [using this SDK can be found in the project root](https://github.com/hashicorp/go-azure-sdk/tree/main/docs). + +### Import Path + +```go +import "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" +import "github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules" +``` + + +### Client Initialization + +```go +client := startstopmanagedinstanceschedules.NewStartStopManagedInstanceSchedulesClientWithBaseURI("https://management.azure.com") +client.Client.Authorizer = authorizer +``` + + +### Example Usage: `StartStopManagedInstanceSchedulesClient.CreateOrUpdate` + +```go +ctx := context.TODO() +id := commonids.NewSqlManagedInstanceID("12345678-1234-9876-4563-123456789012", "example-resource-group", "managedInstanceName") + +payload := startstopmanagedinstanceschedules.StartStopManagedInstanceSchedule{ + // ... +} + + +read, err := client.CreateOrUpdate(ctx, id, payload) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` + + +### Example Usage: `StartStopManagedInstanceSchedulesClient.Delete` + +```go +ctx := context.TODO() +id := commonids.NewSqlManagedInstanceID("12345678-1234-9876-4563-123456789012", "example-resource-group", "managedInstanceName") + +read, err := client.Delete(ctx, id) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` + + +### Example Usage: `StartStopManagedInstanceSchedulesClient.Get` + +```go +ctx := context.TODO() +id := commonids.NewSqlManagedInstanceID("12345678-1234-9876-4563-123456789012", "example-resource-group", "managedInstanceName") + +read, err := client.Get(ctx, id) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` + + +### Example Usage: `StartStopManagedInstanceSchedulesClient.ListByInstance` + +```go +ctx := context.TODO() +id := commonids.NewSqlManagedInstanceID("12345678-1234-9876-4563-123456789012", "example-resource-group", "managedInstanceName") + +// alternatively `client.ListByInstance(ctx, id)` can be used to do batched pagination +items, err := client.ListByInstanceComplete(ctx, id) +if err != nil { + // handle the error +} +for _, item := range items { + // do something +} +``` diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/client.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/client.go new file mode 100644 index 000000000000..aa4c62fed82d --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/client.go @@ -0,0 +1,26 @@ +package startstopmanagedinstanceschedules + +import ( + "fmt" + + "github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager" + sdkEnv "github.com/hashicorp/go-azure-sdk/sdk/environments" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type StartStopManagedInstanceSchedulesClient struct { + Client *resourcemanager.Client +} + +func NewStartStopManagedInstanceSchedulesClientWithBaseURI(sdkApi sdkEnv.Api) (*StartStopManagedInstanceSchedulesClient, error) { + client, err := resourcemanager.NewClient(sdkApi, "startstopmanagedinstanceschedules", defaultApiVersion) + if err != nil { + return nil, fmt.Errorf("instantiating StartStopManagedInstanceSchedulesClient: %+v", err) + } + + return &StartStopManagedInstanceSchedulesClient{ + Client: client, + }, nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/constants.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/constants.go new file mode 100644 index 000000000000..d6f3effb1efc --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/constants.go @@ -0,0 +1,66 @@ +package startstopmanagedinstanceschedules + +import ( + "encoding/json" + "fmt" + "strings" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type DayOfWeek string + +const ( + DayOfWeekFriday DayOfWeek = "Friday" + DayOfWeekMonday DayOfWeek = "Monday" + DayOfWeekSaturday DayOfWeek = "Saturday" + DayOfWeekSunday DayOfWeek = "Sunday" + DayOfWeekThursday DayOfWeek = "Thursday" + DayOfWeekTuesday DayOfWeek = "Tuesday" + DayOfWeekWednesday DayOfWeek = "Wednesday" +) + +func PossibleValuesForDayOfWeek() []string { + return []string{ + string(DayOfWeekFriday), + string(DayOfWeekMonday), + string(DayOfWeekSaturday), + string(DayOfWeekSunday), + string(DayOfWeekThursday), + string(DayOfWeekTuesday), + string(DayOfWeekWednesday), + } +} + +func (s *DayOfWeek) UnmarshalJSON(bytes []byte) error { + var decoded string + if err := json.Unmarshal(bytes, &decoded); err != nil { + return fmt.Errorf("unmarshaling: %+v", err) + } + out, err := parseDayOfWeek(decoded) + if err != nil { + return fmt.Errorf("parsing %q: %+v", decoded, err) + } + *s = *out + return nil +} + +func parseDayOfWeek(input string) (*DayOfWeek, error) { + vals := map[string]DayOfWeek{ + "friday": DayOfWeekFriday, + "monday": DayOfWeekMonday, + "saturday": DayOfWeekSaturday, + "sunday": DayOfWeekSunday, + "thursday": DayOfWeekThursday, + "tuesday": DayOfWeekTuesday, + "wednesday": DayOfWeekWednesday, + } + if v, ok := vals[strings.ToLower(input)]; ok { + return &v, nil + } + + // otherwise presume it's an undefined value and best-effort it + out := DayOfWeek(input) + return &out, nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_createorupdate.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_createorupdate.go new file mode 100644 index 000000000000..641d1d373894 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_createorupdate.go @@ -0,0 +1,60 @@ +package startstopmanagedinstanceschedules + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type CreateOrUpdateOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *StartStopManagedInstanceSchedule +} + +// CreateOrUpdate ... +func (c StartStopManagedInstanceSchedulesClient) CreateOrUpdate(ctx context.Context, id commonids.SqlManagedInstanceId, input StartStopManagedInstanceSchedule) (result CreateOrUpdateOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusCreated, + http.StatusOK, + }, + HttpMethod: http.MethodPut, + Path: fmt.Sprintf("%s/startStopSchedules/default", id.ID()), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + if err = req.Marshal(input); err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + var model StartStopManagedInstanceSchedule + result.Model = &model + if err = resp.Unmarshal(result.Model); err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_delete.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_delete.go new file mode 100644 index 000000000000..c49b841ccb44 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_delete.go @@ -0,0 +1,49 @@ +package startstopmanagedinstanceschedules + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type DeleteOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData +} + +// Delete ... +func (c StartStopManagedInstanceSchedulesClient) Delete(ctx context.Context, id commonids.SqlManagedInstanceId) (result DeleteOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusNoContent, + http.StatusOK, + }, + HttpMethod: http.MethodDelete, + Path: fmt.Sprintf("%s/startStopSchedules/default", id.ID()), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_get.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_get.go new file mode 100644 index 000000000000..bc3bc1326f93 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_get.go @@ -0,0 +1,55 @@ +package startstopmanagedinstanceschedules + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type GetOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *StartStopManagedInstanceSchedule +} + +// Get ... +func (c StartStopManagedInstanceSchedulesClient) Get(ctx context.Context, id commonids.SqlManagedInstanceId) (result GetOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusOK, + }, + HttpMethod: http.MethodGet, + Path: fmt.Sprintf("%s/startStopSchedules/default", id.ID()), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + var model StartStopManagedInstanceSchedule + result.Model = &model + if err = resp.Unmarshal(result.Model); err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_listbyinstance.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_listbyinstance.go new file mode 100644 index 000000000000..ea88a26dfa80 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/method_listbyinstance.go @@ -0,0 +1,106 @@ +package startstopmanagedinstanceschedules + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type ListByInstanceOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *[]StartStopManagedInstanceSchedule +} + +type ListByInstanceCompleteResult struct { + LatestHttpResponse *http.Response + Items []StartStopManagedInstanceSchedule +} + +type ListByInstanceCustomPager struct { + NextLink *odata.Link `json:"nextLink"` +} + +func (p *ListByInstanceCustomPager) NextPageLink() *odata.Link { + defer func() { + p.NextLink = nil + }() + + return p.NextLink +} + +// ListByInstance ... +func (c StartStopManagedInstanceSchedulesClient) ListByInstance(ctx context.Context, id commonids.SqlManagedInstanceId) (result ListByInstanceOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusOK, + }, + HttpMethod: http.MethodGet, + Pager: &ListByInstanceCustomPager{}, + Path: fmt.Sprintf("%s/startStopSchedules", id.ID()), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.ExecutePaged(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + var values struct { + Values *[]StartStopManagedInstanceSchedule `json:"value"` + } + if err = resp.Unmarshal(&values); err != nil { + return + } + + result.Model = values.Values + + return +} + +// ListByInstanceComplete retrieves all the results into a single object +func (c StartStopManagedInstanceSchedulesClient) ListByInstanceComplete(ctx context.Context, id commonids.SqlManagedInstanceId) (ListByInstanceCompleteResult, error) { + return c.ListByInstanceCompleteMatchingPredicate(ctx, id, StartStopManagedInstanceScheduleOperationPredicate{}) +} + +// ListByInstanceCompleteMatchingPredicate retrieves all the results and then applies the predicate +func (c StartStopManagedInstanceSchedulesClient) ListByInstanceCompleteMatchingPredicate(ctx context.Context, id commonids.SqlManagedInstanceId, predicate StartStopManagedInstanceScheduleOperationPredicate) (result ListByInstanceCompleteResult, err error) { + items := make([]StartStopManagedInstanceSchedule, 0) + + resp, err := c.ListByInstance(ctx, id) + if err != nil { + result.LatestHttpResponse = resp.HttpResponse + err = fmt.Errorf("loading results: %+v", err) + return + } + if resp.Model != nil { + for _, v := range *resp.Model { + if predicate.Matches(v) { + items = append(items, v) + } + } + } + + result = ListByInstanceCompleteResult{ + LatestHttpResponse: resp.HttpResponse, + Items: items, + } + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_scheduleitem.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_scheduleitem.go new file mode 100644 index 000000000000..bea9c28f0f7e --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_scheduleitem.go @@ -0,0 +1,11 @@ +package startstopmanagedinstanceschedules + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type ScheduleItem struct { + StartDay DayOfWeek `json:"startDay"` + StartTime string `json:"startTime"` + StopDay DayOfWeek `json:"stopDay"` + StopTime string `json:"stopTime"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_startstopmanagedinstanceschedule.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_startstopmanagedinstanceschedule.go new file mode 100644 index 000000000000..eab78ea62f21 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_startstopmanagedinstanceschedule.go @@ -0,0 +1,16 @@ +package startstopmanagedinstanceschedules + +import ( + "github.com/hashicorp/go-azure-helpers/resourcemanager/systemdata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type StartStopManagedInstanceSchedule struct { + Id *string `json:"id,omitempty"` + Name *string `json:"name,omitempty"` + Properties *StartStopManagedInstanceScheduleProperties `json:"properties,omitempty"` + SystemData *systemdata.SystemData `json:"systemData,omitempty"` + Type *string `json:"type,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_startstopmanagedinstancescheduleproperties.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_startstopmanagedinstancescheduleproperties.go new file mode 100644 index 000000000000..5e556bd211e0 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/model_startstopmanagedinstancescheduleproperties.go @@ -0,0 +1,12 @@ +package startstopmanagedinstanceschedules + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type StartStopManagedInstanceScheduleProperties struct { + Description *string `json:"description,omitempty"` + NextExecutionTime *string `json:"nextExecutionTime,omitempty"` + NextRunAction *string `json:"nextRunAction,omitempty"` + ScheduleList []ScheduleItem `json:"scheduleList"` + TimeZoneId *string `json:"timeZoneId,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/predicates.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/predicates.go new file mode 100644 index 000000000000..60606bc2f22e --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/predicates.go @@ -0,0 +1,27 @@ +package startstopmanagedinstanceschedules + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type StartStopManagedInstanceScheduleOperationPredicate struct { + Id *string + Name *string + Type *string +} + +func (p StartStopManagedInstanceScheduleOperationPredicate) Matches(input StartStopManagedInstanceSchedule) bool { + + if p.Id != nil && (input.Id == nil || *p.Id != *input.Id) { + return false + } + + if p.Name != nil && (input.Name == nil || *p.Name != *input.Name) { + return false + } + + if p.Type != nil && (input.Type == nil || *p.Type != *input.Type) { + return false + } + + return true +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/version.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/version.go new file mode 100644 index 000000000000..971ededeb718 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules/version.go @@ -0,0 +1,10 @@ +package startstopmanagedinstanceschedules + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +const defaultApiVersion = "2023-08-01-preview" + +func userAgent() string { + return "hashicorp/go-azure-sdk/startstopmanagedinstanceschedules/2023-08-01-preview" +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 47c3a44135ce..7d32452028fc 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1084,6 +1084,7 @@ github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/server github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/servers github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/serversecurityalertpolicies github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/servervulnerabilityassessments +github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/startstopmanagedinstanceschedules github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/transparentdataencryptions github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/virtualnetworkrules github.com/hashicorp/go-azure-sdk/resource-manager/sqlvirtualmachine/2023-10-01/availabilitygrouplisteners diff --git a/website/docs/r/mssql_managed_instance_sql_start_stop_schedule.html.markdown b/website/docs/r/mssql_managed_instance_sql_start_stop_schedule.html.markdown new file mode 100644 index 000000000000..33f5c96670ab --- /dev/null +++ b/website/docs/r/mssql_managed_instance_sql_start_stop_schedule.html.markdown @@ -0,0 +1,271 @@ +--- +subcategory: "Database" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_mssql_managed_instance_start_stop_schedule" +description: |- + Manages Start Stop Schedules for an MS SQL Managed Instance. +--- + +# azurerm_mssql_managed_instance_start_stop_schedule + +Manages Start Stop Schedules for an MS SQL Managed Instance. + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "database-rg" + location = "West Europe" +} + +resource "azurerm_network_security_group" "example" { + name = "mi-security-group" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name +} + +resource "azurerm_network_security_rule" "allow_management_inbound" { + name = "allow_management_inbound" + priority = 106 + direction = "Inbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_ranges = ["9000", "9003", "1438", "1440", "1452"] + source_address_prefix = "*" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_network_security_rule" "allow_misubnet_inbound" { + name = "allow_misubnet_inbound" + priority = 200 + direction = "Inbound" + access = "Allow" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + source_address_prefix = "10.0.0.0/24" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_network_security_rule" "allow_health_probe_inbound" { + name = "allow_health_probe_inbound" + priority = 300 + direction = "Inbound" + access = "Allow" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + source_address_prefix = "AzureLoadBalancer" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_network_security_rule" "allow_tds_inbound" { + name = "allow_tds_inbound" + priority = 1000 + direction = "Inbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_range = "1433" + source_address_prefix = "VirtualNetwork" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_network_security_rule" "deny_all_inbound" { + name = "deny_all_inbound" + priority = 4096 + direction = "Inbound" + access = "Deny" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + source_address_prefix = "*" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_network_security_rule" "allow_management_outbound" { + name = "allow_management_outbound" + priority = 102 + direction = "Outbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_ranges = ["80", "443", "12000"] + source_address_prefix = "*" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_network_security_rule" "allow_misubnet_outbound" { + name = "allow_misubnet_outbound" + priority = 200 + direction = "Outbound" + access = "Allow" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + source_address_prefix = "10.0.0.0/24" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_network_security_rule" "deny_all_outbound" { + name = "deny_all_outbound" + priority = 4096 + direction = "Outbound" + access = "Deny" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + source_address_prefix = "*" + destination_address_prefix = "*" + resource_group_name = azurerm_resource_group.example.name + network_security_group_name = azurerm_network_security_group.example.name +} + +resource "azurerm_virtual_network" "example" { + name = "vnet-mi" + resource_group_name = azurerm_resource_group.example.name + address_space = ["10.0.0.0/16"] + location = azurerm_resource_group.example.location +} + +resource "azurerm_subnet" "example" { + name = "subnet-mi" + resource_group_name = azurerm_resource_group.example.name + virtual_network_name = azurerm_virtual_network.example.name + address_prefixes = ["10.0.0.0/24"] + + delegation { + name = "managedinstancedelegation" + + service_delegation { + name = "Microsoft.Sql/managedInstances" + actions = ["Microsoft.Network/virtualNetworks/subnets/join/action", "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action", "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action"] + } + } +} + +resource "azurerm_subnet_network_security_group_association" "example" { + subnet_id = azurerm_subnet.example.id + network_security_group_id = azurerm_network_security_group.example.id +} + +resource "azurerm_route_table" "example" { + name = "routetable-mi" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + disable_bgp_route_propagation = false + depends_on = [ + azurerm_subnet.example, + ] +} + +resource "azurerm_subnet_route_table_association" "example" { + subnet_id = azurerm_subnet.example.id + route_table_id = azurerm_route_table.example.id +} + +resource "azurerm_mssql_managed_instance" "example" { + name = "managedsqlinstance" + resource_group_name = azurerm_resource_group.example.name + location = azurerm_resource_group.example.location + + license_type = "BasePrice" + sku_name = "GP_Gen5" + storage_size_in_gb = 32 + subnet_id = azurerm_subnet.example.id + vcores = 4 + + administrator_login = "mradministrator" + administrator_login_password = "thisIsDog11" + + depends_on = [ + azurerm_subnet_network_security_group_association.example, + azurerm_subnet_route_table_association.example, + ] +} + +resource "azurerm_mssql_managed_instance_start_stop_schedule" "example" { + managed_instance_id = azurerm_mssql_managed_instance.example.id + timezone_id = "Central European Standard Time" + + schedule { + start_day = "Monday" + start_time = "08:00" + stop_day = "Monday" + stop_time = "11:00" + } + + schedule { + start_day = "Tuesday" + start_time = "12:00" + stop_day = "Tuesday" + stop_time = "18:00" + } +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `managed_instance_id` - (Required) Specifies the ID of the Sql Start Stop Managed Instance Schedule. Changing this forces a new Sql Start Stop Managed Instance Schedule to be created. + +* `schedule` - (Required) A `schedule` block as defined below. + +* `description` - (Optional) Specifies the description of the schedule. + +* `timezone_id` - (Optional) Specifies the time zone of the schedule. Defaults to `UTC`. + +--- + +A `schedule` block supports the following: + +* `start_day` - (Required) Start day of the schedule. Possible values are `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday`, `Saturday`, `Sunday`. + +* `start_time` - (Required) Start time of the schedule in 24-hour format (e.g., `08:00`). + +* `stop_day` - (Required) Stop day of the schedule. Possible values are `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday`, `Saturday`, `Sunday`. + +* `stop_time` - (Required) Stop time of the schedule in 24-hour format (e.g., `17:00`). + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the MS SQL Managed Instance Start Stop Schedule. + +* `next_execution_time` - Timestamp when the next action will be executed in the corresponding schedule time zone. + +* `next_run_action` - Next action to be executed (Start or Stop). + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the MS SQL Managed Instance Start Stop Schedule. +* `read` - (Defaults to 5 minutes) Used when retrieving the MS SQL Managed Instance Start Stop Schedule. +* `update` - (Defaults to 30 minutes) Used when updating the MS SQL Managed Instance Start Stop Schedule. +* `delete` - (Defaults to 30 minutes) Used when deleting the MS SQL Managed Instance Start Stop Schedule. + +## Import + +MS SQL Managed Instance Start Stop Schedule can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_mssql_managed_instance_start_stop_schedule.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resourceGroup1/providers/Microsoft.Sql/managedInstances/managedInstance1/startStopSchedules/default +```