-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
software update configuration new resource
- Loading branch information
Showing
12 changed files
with
1,736 additions
and
0 deletions.
There are no files selected for viewing
952 changes: 952 additions & 0 deletions
952
internal/services/automation/automation_software_update_configuration.go
Large diffs are not rendered by default.
Oops, something went wrong.
231 changes: 231 additions & 0 deletions
231
internal/services/automation/automation_software_update_configuration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
package automation_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"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/automation" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
type SoftwareUpdateConfigurationResource struct { | ||
startTime string | ||
expireTime string | ||
} | ||
|
||
func newSoftwareUpdateConfigurationResource() SoftwareUpdateConfigurationResource { | ||
// The start time of the schedule must be at least 5 minutes after the time you create the schedule | ||
// so we cannot hardcode the time string. | ||
// we use timezone as UTC so the time string should be in UTC format | ||
ins := SoftwareUpdateConfigurationResource{ | ||
startTime: time.Now().Add(time.Hour * 10).In(time.UTC).Format(time.RFC3339), | ||
expireTime: time.Now().Add(time.Hour * 50).In(time.UTC).Format(time.RFC3339), | ||
} | ||
return ins | ||
} | ||
|
||
func (a SoftwareUpdateConfigurationResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { | ||
id, err := parse.SoftwareUpdateConfigurationID(state.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := client.Automation.SoftwareUpdateConfigClient.GetByName(ctx, id.ResourceGroup, id.AutomationAccountName, id.Name, "") | ||
if err != nil { | ||
return nil, fmt.Errorf("retrieving Type %s: %+v", id, err) | ||
} | ||
return utils.Bool(resp.SoftwareUpdateConfigurationProperties != nil), nil | ||
} | ||
|
||
func (a SoftwareUpdateConfigurationResource) template(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-auto-%[1]d" | ||
location = "%[2]s" | ||
} | ||
resource "azurerm_automation_account" "test" { | ||
name = "acctest-%[1]d" | ||
location = azurerm_resource_group.test.location | ||
resource_group_name = azurerm_resource_group.test.name | ||
sku_name = "Basic" | ||
} | ||
resource "azurerm_log_analytics_workspace" "test" { | ||
name = "acctestLAW-%[1]d" | ||
location = azurerm_resource_group.test.location | ||
resource_group_name = azurerm_resource_group.test.name | ||
sku = "PerGB2018" | ||
retention_in_days = 30 | ||
} | ||
resource "azurerm_log_analytics_linked_service" "test" { | ||
resource_group_name = azurerm_resource_group.test.name | ||
workspace_id = azurerm_log_analytics_workspace.test.id | ||
read_access_id = azurerm_automation_account.test.id | ||
} | ||
`, data.RandomInteger, data.Locations.Primary) | ||
} | ||
|
||
func (a SoftwareUpdateConfigurationResource) basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_automation_software_update_configuration" "test" { | ||
resource_group_name = azurerm_resource_group.test.name | ||
automation_account_name = azurerm_automation_account.test.name | ||
name = "acctest-suc-%[2]d" | ||
operating_system = "Linux" | ||
linux { | ||
classification = "Security" | ||
excluded_packages = ["apt"] | ||
included_packages = ["vim"] | ||
reboot_setting = "IfRequired" | ||
} | ||
duration = "PT1H1M1S" | ||
virtual_machines = [] | ||
targets { | ||
azure_queries { | ||
scope = [azurerm_resource_group.test.id] | ||
locations = [azurerm_resource_group.test.location] | ||
tags { | ||
tag = "foo" | ||
values = ["barbar2"] | ||
} | ||
tag_filter = "Any" | ||
} | ||
non_azure_queries { | ||
function_alias = "savedSearch1" | ||
workspace_id = azurerm_log_analytics_workspace.test.id | ||
} | ||
} | ||
schedule { | ||
description = "foo-schedule" | ||
start_time = "%[3]s" | ||
expiry_time = "%[4]s" | ||
is_enabled = true | ||
interval = 1 | ||
frequency = "Hour" | ||
time_zone = "Etc/UTC" | ||
advanced_week_days = ["Monday", "Tuesday"] | ||
advanced_month_days = [1, 10, 15] | ||
monthly_occurrence { | ||
occurrence = 1 | ||
day = "Tuesday" | ||
} | ||
} | ||
depends_on = [azurerm_log_analytics_linked_service.test] | ||
} | ||
`, a.template(data), data.RandomInteger, a.startTime, a.expireTime) | ||
} | ||
|
||
func (a SoftwareUpdateConfigurationResource) update(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_automation_software_update_configuration" "test" { | ||
resource_group_name = azurerm_resource_group.test.name | ||
automation_account_name = azurerm_automation_account.test.name | ||
name = "acctest-suc-%[2]d" | ||
operating_system = "Linux" | ||
linux { | ||
classification = "Security" | ||
excluded_packages = ["apt"] | ||
included_packages = ["vim"] | ||
reboot_setting = "IfRequired" | ||
} | ||
duration = "PT2H2M2S" | ||
virtual_machines = [] | ||
targets { | ||
azure_queries { | ||
scope = [azurerm_resource_group.test.id] | ||
locations = [azurerm_resource_group.test.location] | ||
tags { | ||
tag = "foo" | ||
values = ["barbar2"] | ||
} | ||
tag_filter = "Any" | ||
} | ||
non_azure_queries { | ||
function_alias = "savedSearch1" | ||
workspace_id = azurerm_log_analytics_workspace.test.id | ||
} | ||
} | ||
schedule { | ||
description = "foobar-schedule" | ||
start_time = "%[3]s" | ||
expiry_time = "%[4]s" | ||
is_enabled = true | ||
interval = 1 | ||
frequency = "Hour" | ||
time_zone = "Etc/UTC" | ||
advanced_week_days = ["Monday", "Tuesday"] | ||
} | ||
depends_on = [azurerm_log_analytics_linked_service.test] | ||
} | ||
`, a.template(data), data.RandomInteger, a.startTime, a.expireTime) | ||
} | ||
|
||
func TestAccSoftwareUpdateConfiguration_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, automation.SoftwareUpdateConfigurationResource{}.ResourceType(), "test") | ||
r := newSoftwareUpdateConfigurationResource() | ||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
// scheduleInfo.advancedSchedule always return null | ||
data.ImportStep("schedule.0.advanced", "schedule.0.monthly_occurrence"), | ||
}) | ||
} | ||
|
||
func TestAccSoftwareUpdateConfiguration_update(t *testing.T) { | ||
data := acceptance.BuildTestData(t, automation.SoftwareUpdateConfigurationResource{}.ResourceType(), "test") | ||
r := newSoftwareUpdateConfigurationResource() | ||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
// scheduleInfo.advancedSchedule always return null | ||
data.ImportStep("schedule.0.advanced", "schedule.0.monthly_occurrence"), | ||
{ | ||
Config: r.update(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
// scheduleInfo.advancedSchedule always return null | ||
data.ImportStep("schedule.0.advanced", "schedule.0.monthly_occurrence"), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
internal/services/automation/parse/software_update_configuration.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package parse | ||
|
||
// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" | ||
) | ||
|
||
type SoftwareUpdateConfigurationId struct { | ||
SubscriptionId string | ||
ResourceGroup string | ||
AutomationAccountName string | ||
Name string | ||
} | ||
|
||
func NewSoftwareUpdateConfigurationID(subscriptionId, resourceGroup, automationAccountName, name string) SoftwareUpdateConfigurationId { | ||
return SoftwareUpdateConfigurationId{ | ||
SubscriptionId: subscriptionId, | ||
ResourceGroup: resourceGroup, | ||
AutomationAccountName: automationAccountName, | ||
Name: name, | ||
} | ||
} | ||
|
||
func (id SoftwareUpdateConfigurationId) String() string { | ||
segments := []string{ | ||
fmt.Sprintf("Name %q", id.Name), | ||
fmt.Sprintf("Automation Account Name %q", id.AutomationAccountName), | ||
fmt.Sprintf("Resource Group %q", id.ResourceGroup), | ||
} | ||
segmentsStr := strings.Join(segments, " / ") | ||
return fmt.Sprintf("%s: (%s)", "Software Update Configuration", segmentsStr) | ||
} | ||
|
||
func (id SoftwareUpdateConfigurationId) ID() string { | ||
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Automation/automationAccounts/%s/softwareUpdateConfigurations/%s" | ||
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.AutomationAccountName, id.Name) | ||
} | ||
|
||
// SoftwareUpdateConfigurationID parses a SoftwareUpdateConfiguration ID into an SoftwareUpdateConfigurationId struct | ||
func SoftwareUpdateConfigurationID(input string) (*SoftwareUpdateConfigurationId, error) { | ||
id, err := resourceids.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resourceId := SoftwareUpdateConfigurationId{ | ||
SubscriptionId: id.SubscriptionID, | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if resourceId.SubscriptionId == "" { | ||
return nil, fmt.Errorf("ID was missing the 'subscriptions' element") | ||
} | ||
|
||
if resourceId.ResourceGroup == "" { | ||
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") | ||
} | ||
|
||
if resourceId.AutomationAccountName, err = id.PopSegment("automationAccounts"); err != nil { | ||
return nil, err | ||
} | ||
if resourceId.Name, err = id.PopSegment("softwareUpdateConfigurations"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resourceId, nil | ||
} |
Oops, something went wrong.