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

new resource azurerm_mssql_managed_instance_start_stop_schedule #26702

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

hqhqhqhqhqhqhqhqhqhqhq
Copy link
Contributor

Community Note

  • Please vote on this PR by adding a 👍 reaction to the original PR to help the community and maintainers prioritize for review
  • Please do not leave comments along the lines of "+1", "me too" or "any updates", they generate extra noise for PR followers and do not help prioritize for review

Description

Add new resource azurerm_mssql_managed_instance_start_stop_schedule -> setting up start stop schedule for mssql_managed_instance resource.

PR Checklist

  • I have followed the guidelines in our Contributing Documentation.
  • I have checked to ensure there aren't other open Pull Requests for the same update/change.
  • I have checked if my changes close any open issues. If so please include appropriate closing keywords below.
  • I have updated/added Documentation as required written in a helpful and kind way to assist users that may be unfamiliar with the resource / data source.
  • I have used a meaningful PR title to help maintainers and other users understand this change and help prevent duplicate work.
    For example: “resource_name_here - description of change e.g. adding property new_property_name_here

Changes to existing Resource / Data Source

  • I have added an explanation of what my changes do and why I'd like you to include them (This may be covered by linking to an issue above, but may benefit from additional explanation).
  • I have written new tests for my resource or datasource changes & updated any relevent documentation.
  • I have successfully run tests with my changes locally. If not, please provide details on testing challenges that prevented you running the tests.
  • (For changes that include a state migration only). I have manually tested the migration path between relevant versions of the provider.

Testing

  • My submission includes Test coverage as described in the Contribution Guide and the tests pass. (if this is not possible for any reason, please include details of why you did or could not add test coverage)

Done testing by building the repo and running terraform script with the setting. There needs some fix for mssql_managed_instance testing first as there are issues in related acc tests and this resource depends on the
mssql_managed_instance` resource.

Change Log

Below please provide what should go into the changelog (if anything) conforming to the Changelog Format documented here.

  • new resource azurerm_mssql_managed_instance_start_stop_schedule

This is a (please select all that apply):

  • Bug Fix
  • New Feature (ie adding a service, resource, or data source)
  • Enhancement
  • Breaking Change

Related Issue(s)

Note

If this PR changes meaningfully during the course of review please update the title and description as required.

Copy link
Contributor

@ms-henglu ms-henglu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I have some minor suggestions, please check


func (r MsSqlManagedInstanceStartStopScheduleResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary to output these fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}

func (r MsSqlManagedInstanceStartStopScheduleResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a name argument with a validation func which only allows "default".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have confirmed with the service team that this resource will have 1to1 relationship with the managedinstance and hence the name will always be "default", I think leaving this out so that user doesn't need to declare it is cleaner.

Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.MSSQLManagedInstance.ManagedInstanceStartStopSchedulesClient

managedInstanceId, err := commonids.ParseSqlManagedInstanceID(metadata.ResourceData.Get("managed_instance_id").(string))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of getting the managedInstanceId from the resource data, it's recommended to parse and build it like the following codes.

	                id, err := parse.ManagedInstanceStartStopScheduleID(metadata.ResourceData.Id())
			if err != nil {
				return err
			}
			
			managedInstanceID := commonids.NewSqlManagedInstanceID(id.SubscriptionId, id.ResourceGroup, id.ManagedInstanceName)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}

if model := resp.Model; model != nil {
if name := model.Name; name != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the name from the parsed ID instead of from the response, because the upstream API might have unexpected changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}

if properties := model.Properties; properties != nil {
if properties.Description != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be simplified to state.Description = pointer.From(properties.Description), please update other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@hqhqhqhqhqhqhqhqhqhqhq hqhqhqhqhqhqhqhqhqhqhq marked this pull request as ready for review July 26, 2024 05:07
Copy link
Member

@stephybun stephybun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR @hqhqhqhqhqhqhqhqhqhqhq. We just merged a PR that migrates all the managed instance resources over to hashicorp/go-azure-sdk so this will want to be rebased on top of that.

Overall this is off to a good start, I've left a detailed review in-line, once the comments have been resolved we can take another look through.

Optional: true,
},

"schedule_list": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_list is redundant here, can we rename this to

Suggested change
"schedule_list": {
"schedule": {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

},
},

"timezone_id": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be shortened to

Suggested change
"timezone_id": {
"timezone": {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the parent resource azurerm_mssql_managed_instance uses timezone_id, I think it would nice to have similar terminologies.

Comment on lines 123 to 132
managedInstanceId, err := commonids.ParseSqlManagedInstanceID(model.SqlManagedInstanceId)
if err != nil {
return err
}

if managedInstanceId == nil {
return fmt.Errorf("managedInstanceId is nil")
}

id := *managedInstanceId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model.SqlManagedInstanceId is a required property in the schema with strict validation on it so we can expect the managedInstanceId to not ever be nil with a high degree of confidence, which is why we don't nil check for this in any other resources in the provider.

For consistency can we please update this to

Suggested change
managedInstanceId, err := commonids.ParseSqlManagedInstanceID(model.SqlManagedInstanceId)
if err != nil {
return err
}
if managedInstanceId == nil {
return fmt.Errorf("managedInstanceId is nil")
}
id := *managedInstanceId
id, err := commonids.ParseSqlManagedInstanceID(model.SqlManagedInstanceId)
if err != nil {
return err
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines 192 to 195
properties := resp.Model
if properties == nil {
return fmt.Errorf("retrieving %s: properties was nil", managedInstanceID)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the update we usually nil check both Model and Properties to prevent the provider panicking in the unlikely event that these are nil (the SDK actually handles this for us).

For consistency can we please update this to

Suggested change
properties := resp.Model
if properties == nil {
return fmt.Errorf("retrieving %s: properties was nil", managedInstanceID)
}
properties := resp.Model
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)
}
payload := resp.Model

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}
}

properties.SystemData = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need to do this. If this is required then this is indicative of an API bug and an issue should be opened on the Rest API specs repo and linked in a comment above this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}

if model := resp.Model; model != nil {
state.Name = id.StartStopScheduleName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't in the schema, so we don't need to set a value for this into state

Suggested change
state.Name = id.StartStopScheduleName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines 299 to 313
func expandScheduleItemModelArray(inputList []ScheduleItemModel) *[]schedule.ScheduleItem {
var outputList []schedule.ScheduleItem
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
}
Copy link
Member

@stephybun stephybun Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model doesn't have this defined as a pointer so I don't see a need to make this return a pointer

Suggested change
func expandScheduleItemModelArray(inputList []ScheduleItemModel) *[]schedule.ScheduleItem {
var outputList []schedule.ScheduleItem
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 expandScheduleItemModelArray(inputList []ScheduleItemModel) []schedule.ScheduleItem {
var outputList []schedule.ScheduleItem
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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines 315 to 331
func flattenScheduleItemModelArray(inputList *[]schedule.ScheduleItem) []ScheduleItemModel {
var outputList []ScheduleItemModel
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Suggested change
func flattenScheduleItemModelArray(inputList *[]schedule.ScheduleItem) []ScheduleItemModel {
var outputList []ScheduleItemModel
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
}
func flattenScheduleItemModelArray(inputList []schedule.ScheduleItem) []ScheduleItemModel {
var outputList []ScheduleItemModel
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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines 81 to 86
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return utils.Bool(resp.Model != nil), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get any error while running this function to check whether the resource exists we should return it

Suggested change
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return utils.Bool(resp.Model != nil), nil
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return pointer.To(resp.Model != nil), nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines 238 to 244
* `start_day` - (Required) Start day.

* `start_time` - (Required) Start time.

* `stop_day` - (Required) Stop day.

* `stop_time` - (Required) Stop time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start_day and stop_day have validation for accepted values, can we extend the description here to include the possible values for these properties.

Also for start_time and stop_time, an example of the format we're expecting here would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@hqhqhqhqhqhqhqhqhqhqhq hqhqhqhqhqhqhqhqhqhqhq force-pushed the feat/sql-StartStopManagedInstanceSchedules branch from 836c0fe to 72f2f41 Compare September 3, 2024 10:26
@katbyte katbyte requested review from katbyte and a team as code owners November 14, 2024 00:08
Copy link
Member

@stephybun stephybun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hqhqhqhqhqhqhqhqhqhqhq,

could you take a look at the test failure:

------- Stdout: -------
=== RUN   TestAccMsSqlManagedInstanceStartStopSchedule_update
=== PAUSE TestAccMsSqlManagedInstanceStartStopSchedule_update
=== CONT  TestAccMsSqlManagedInstanceStartStopSchedule_update
    testcase.go:173: Step 4/6 error: Error running apply: exit status 1
        Error: updating Managed Instance (Subscription: "*******"
        Resource Group Name: "acctestRG1-sql-250110074135832701"
        Managed Instance Name: "acctestsqlserver250110074135832701"): unexpected status 400 (400 Bad Request) with error: ManagementServiceStartStopManagedInstanceScheduleOverlap: One or multiple schedule items have a time span overlap. Please correct your schedule and submit again.
          with azurerm_mssql_managed_instance_start_stop_schedule.test,
          on terraform_plugin_test.tf line 304, in resource "azurerm_mssql_managed_instance_start_stop_schedule" "test":
         304: resource "azurerm_mssql_managed_instance_start_stop_schedule" "test" {
        updating Managed Instance (Subscription:
        "*******"
        Resource Group Name: "acctestRG1-sql-250110074135832701"
        Managed Instance Name: "acctestsqlserver250110074135832701"): unexpected
        status 400 (400 Bad Request) with error:
        ManagementServiceStartStopManagedInstanceScheduleOverlap: One or multiple
        schedule items have a time span overlap. Please correct your schedule and
        submit again.
--- FAIL: TestAccMsSqlManagedInstanceStartStopSchedule_update (6545.41s)
FAIL

Copy link

This PR is being labeled as "stale" because it has not been updated for 30 or more days.

If this PR is still valid, please remove the "stale" label. If this PR is blocked, please add it to the "Blocked" milestone.

If you need some help completing this PR, please leave a comment letting us know. Thank you!

@github-actions github-actions bot added the stale label Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants