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 Data Source: azurerm_public_maintenance_configurations #16810

Merged
merged 5 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions internal/services/maintenance/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Client struct {
ConfigurationsClient *maintenance.ConfigurationsClient
ConfigurationAssignmentsClient *maintenance.ConfigurationAssignmentsClient
PublicConfigurationsClient *maintenance.PublicMaintenanceConfigurationsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -17,8 +18,12 @@ func NewClient(o *common.ClientOptions) *Client {
configurationAssignmentsClient := maintenance.NewConfigurationAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&configurationAssignmentsClient.Client, o.ResourceManagerAuthorizer)

publicConfigurationsClient := maintenance.NewPublicMaintenanceConfigurationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&publicConfigurationsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ConfigurationsClient: &configurationsClient,
ConfigurationAssignmentsClient: &configurationAssignmentsClient,
PublicConfigurationsClient: &publicConfigurationsClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package maintenance

import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/maintenance/mgmt/2021-05-01/maintenance"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

const recurMondayToThursday = "weekMondayToThursday"
const recurFridayToSunday = "weekFridayToSunday"

func dataSourcePublicMaintenanceConfigurations() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourcePublicMaintenanceConfigurationsRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{

"location_filter": {
Type: pluginsdk.TypeString,
Optional: true,
StateFunc: azure.NormalizeLocation,
},
catriona-m marked this conversation as resolved.
Show resolved Hide resolved

"scope_filter": {
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"All", // All is still accepted by the API
string(maintenance.ScopeExtension),
string(maintenance.ScopeHost),
string(maintenance.ScopeInGuestPatch),
string(maintenance.ScopeOSImage),
string(maintenance.ScopeSQLDB),
string(maintenance.ScopeSQLManagedInstance),
}, false),
},

"recur_every_filter": {
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
recurMondayToThursday,
recurFridayToSunday,
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
}, false),
},

"public_maintenance_configurations": {
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"location": {
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeString,
Computed: true,
},
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
"duration": {
Type: pluginsdk.TypeString,
Computed: true,
},
"maintenance_scope": {
Type: pluginsdk.TypeString,
Computed: true,
},
"time_zone": {
Type: pluginsdk.TypeString,
Computed: true,
},
"recur_every": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourcePublicMaintenanceConfigurationsRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Maintenance.PublicConfigurationsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

resp, err := client.List(ctx)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("no Public Maintenance Configurations were found")
}
return fmt.Errorf("retrieving Public Maintenance Configurations: %+v", err)
}

filteredPublicConfigs := make([]interface{}, 0)

recurEveryFilter := d.Get("recur_every_filter").(string)
if recurEveryFilter == recurFridayToSunday {
recurEveryFilter = "week Friday, Saturday, Sunday"
} else if recurEveryFilter == recurMondayToThursday {
recurEveryFilter = "week Monday, Tuesday, Wednesday, Thursday"
}

locationFilter := d.Get("location_filter").(string)
scopeFilter := d.Get("scope_filter").(string)

for _, maintenanceConfig := range *resp.Value {
catriona-m marked this conversation as resolved.
Show resolved Hide resolved

var configLocation, configRecurEvery, configScope string
if maintenanceConfig.Location != nil {
configLocation = *maintenanceConfig.Location
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
}
if props := maintenanceConfig.ConfigurationProperties; props != nil {
if props.Window != nil && props.Window.RecurEvery != nil {
configRecurEvery = *props.Window.RecurEvery
}
if string(props.MaintenanceScope) != "" {
configScope = string(props.MaintenanceScope)
}
}

if locationFilter == "" || locationFilter == configLocation {
if recurEveryFilter == "" || recurEveryFilter == configRecurEvery {
if scopeFilter == "" || scopeFilter == configScope {
filteredPublicConfigs = append(filteredPublicConfigs, flattenPublicMaintenanceConfiguration(maintenanceConfig))
}
}
}
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
}

d.Set("public_maintenance_configurations", filteredPublicConfigs)
catriona-m marked this conversation as resolved.
Show resolved Hide resolved

d.SetId(time.Now().UTC().String())
return nil
}

func flattenPublicMaintenanceConfiguration(config maintenance.Configuration) map[string]interface{} {
output := make(map[string]interface{})

output["name"] = *config.Name
output["id"] = *config.ID
output["location"] = *config.Location
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
output["maintenance_scope"] = string(config.MaintenanceScope)

var description, recurEvery, timeZone, duration string
if props := config.ConfigurationProperties; props != nil {
if props.ExtensionProperties != nil {
if configDescription, ok := props.ExtensionProperties["description"]; ok {
description = *configDescription
}
}
if props.Window != nil {
if props.Window.RecurEvery != nil {
recurEvery = *props.Window.RecurEvery
}
if props.Window.TimeZone != nil {
timeZone = *props.Window.TimeZone
}
if props.Window.Duration != nil {
duration = *props.Window.Duration
}
}
}

output["description"] = description
output["recur_every"] = recurEvery
output["time_zone"] = timeZone
output["duration"] = duration

return output
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package maintenance_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type PublicMaintenanceConfigurationsDataSource struct{}

func TestAccDataSourcePublicMaintenanceConfigurations_noFilters(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_public_maintenance_configurations", "test")
r := PublicMaintenanceConfigurationsDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.noFilters(),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("public_maintenance_configurations.30.name").Exists(),
),
},
})
}

func TestAccDataSourcePublicMaintenanceConfigurations_allFilters(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_public_maintenance_configurations", "test")
r := PublicMaintenanceConfigurationsDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.allFilters(),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("public_maintenance_configurations.#").HasValue("1"),
check.That(data.ResourceName).Key("public_maintenance_configurations.0.maintenance_scope").HasValue("SQLManagedInstance"),
check.That(data.ResourceName).Key("public_maintenance_configurations.0.name").HasValue("SQL_WestEurope_MI_1"),
check.That(data.ResourceName).Key("public_maintenance_configurations.0.recur_every").HasValue("week Monday, Tuesday, Wednesday, Thursday"),
),
},
})
}

func TestAccDataSourcePublicMaintenanceConfigurations_recurEvery(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_public_maintenance_configurations", "test")
r := PublicMaintenanceConfigurationsDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.recurEvery(),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("public_maintenance_configurations.0.maintenance_scope").HasValue("SQLManagedInstance"),
check.That(data.ResourceName).Key("public_maintenance_configurations.0.recur_every").HasValue("week Friday, Saturday, Sunday"),
),
},
})
}

func (PublicMaintenanceConfigurationsDataSource) allFilters() string {
return fmt.Sprintf(`
data "azurerm_public_maintenance_configurations" "test" {
location_filter = "westeurope"
scope_filter = "SQLManagedInstance"
recur_every_filter = "weekMondayToThursday"
}
`)
}

func (PublicMaintenanceConfigurationsDataSource) noFilters() string {
return fmt.Sprintf(`
data "azurerm_public_maintenance_configurations" "test" {

}
`)
}

func (PublicMaintenanceConfigurationsDataSource) recurEvery() string {
return fmt.Sprintf(`
data "azurerm_public_maintenance_configurations" "test" {
scope_filter = "SQLManagedInstance"
recur_every_filter = "weekFridayToSunday"
}
`)
}
3 changes: 2 additions & 1 deletion internal/services/maintenance/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func (r Registration) WebsiteCategories() []string {

func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
"azurerm_maintenance_configuration": dataSourceMaintenanceConfiguration(),
"azurerm_maintenance_configuration": dataSourceMaintenanceConfiguration(),
"azurerm_public_maintenance_configurations": dataSourcePublicMaintenanceConfigurations(),
}
}

Expand Down
65 changes: 65 additions & 0 deletions website/docs/d/public_maintenance_configurations.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
subcategory: "Maintenance"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_public_maintenance_configurations"
description: |-
Get information about existing Public Maintenance Configurations.
---

# Data Source: azurerm_public_maintenance_configurations

Use this data source to access information about existing Public Maintenance Configurations.

## Example Usage

```hcl
data "azurerm_public_maintenance_configurations" "existing" {
location_filter = "westeurope"
scope_filter = "SQLManagedInstance"
recur_every_filter = "weekMondayToThursday"
}

output "name" {
value = data.azurerm_public_maintenance_configurations.existing.public_maintenance_configurations[0].name
}
```

## Argument Reference

* `location_filter` - The Azure location to filter the list of Public Maintenance Configurations against.

* `scope_filter` - The scope to filter the list of Public Maintenance Configurations against. Possible values are `All`, `Extension`, `Host`, `InGuestPatch`, `OSImage`, `SQLDB` and `SQLManagedInstance`.

* `recur_every_filter` - The recurring window to filter the list of Public Maintenance Configurations against. Possible values are `weekMondayToThursday` and `weekFridayToSunday`

## Attributes Reference

* `public_maintenance_configurations` - A `public_maintenance_configurations` block as defined below.

---

A `public_maintenance_configurations` block exports the following:

* `name` - The name of the Public Maintenance Configuration.

* `id` - The id of the Public Maintenance Configuration.

* `location` - The Azure location of the Public Maintenance Configuration.

* `description` - A description of the Public Maintenance Configuration.

* `duration` - The duration of the Public Maintenance Configuration window.

* `maintenance_scope` - The scope of the Public Maintenance Configuration.

* `time_zone` - The time zone for the maintenance window.

* `recur_every` - The rate at which a maintenance window is expected to recur.

---

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Public Maintenance Configuration.
2 changes: 1 addition & 1 deletion website/docs/r/disk_sas_token.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ Disk SAS Token can be imported using the `resource id`, e.g.

```shell
terraform import azurerm_managed_disk_sas_token.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.compute/disks/manageddisk1
```
```