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_virtual_machine_automanage_configuration_assignment #25480

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ service/authorization:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(client_config|federated_identity_credential|marketplace_role_assignment|pim_|role_|user_assigned_identity)((.|\n)*)###'

service/automanage:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_automanage_configuration((.|\n)*)###'
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(automanage_configuration|virtual_machine_automanage_configuration_assignment)((.|\n)*)###'

service/automation:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_automation_((.|\n)*)###'
Expand Down
9 changes: 9 additions & 0 deletions internal/services/automanage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package client
import (
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofileassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofilehciassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofiles"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
Expand All @@ -14,6 +15,7 @@ import (
type Client struct {
ConfigurationProfilesClient *configurationprofiles.ConfigurationProfilesClient
ConfigurationProfileHCIAssignmentsClient *configurationprofilehciassignments.ConfigurationProfileHCIAssignmentsClient
ConfigurationProfileVMAssignmentsClient *configurationprofileassignments.ConfigurationProfileAssignmentsClient
}

func NewClient(o *common.ClientOptions) (*Client, error) {
Expand All @@ -29,8 +31,15 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(configurationProfileHCIAssignmentsClient.Client, o.Authorizers.ResourceManager)

configurationProfileVMAssignmentsClient, err := configurationprofileassignments.NewConfigurationProfileAssignmentsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ConfigurationProfilesHCIAssignments client: %+v", err)
}
o.Configure(configurationProfileVMAssignmentsClient.Client, o.Authorizers.ResourceManager)

return &Client{
ConfigurationProfilesClient: configurationProfilesClient,
ConfigurationProfileHCIAssignmentsClient: configurationProfileHCIAssignmentsClient,
ConfigurationProfileVMAssignmentsClient: configurationProfileVMAssignmentsClient,
}, nil
}
1 change: 1 addition & 0 deletions internal/services/automanage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ func (r Registration) DataSources() []sdk.DataSource {
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
AutoManageConfigurationResource{},
VirtualMachineConfigurationAssignment{},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package automanage

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-sdk/resource-manager/automanage/2022-05-04/configurationprofileassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofiles"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type VirtualMachineConfigurationAssignment struct {
VirtualMachineId string `tfschema:"virtual_machine_id"`
ConfigurationId string `tfschema:"configuration_id"`
}

func (v VirtualMachineConfigurationAssignment) Arguments() map[string]*schema.Schema {
return map[string]*schema.Schema{
"virtual_machine_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: configurationprofileassignments.ValidateVirtualMachineID,
},
"configuration_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: configurationprofiles.ValidateConfigurationProfileID,
},
}
}

func (v VirtualMachineConfigurationAssignment) Attributes() map[string]*schema.Schema {
return map[string]*pluginsdk.Schema{}
}

func (v VirtualMachineConfigurationAssignment) ModelObject() interface{} {
return &VirtualMachineConfigurationAssignment{}
}

func (v VirtualMachineConfigurationAssignment) ResourceType() string {
return "azurerm_virtual_machine_automanage_configuration_assignment"
}

func (v VirtualMachineConfigurationAssignment) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationProfileVMAssignmentsClient
subscriptionId := metadata.Client.Account.SubscriptionId

var model VirtualMachineConfigurationAssignment
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

vmId, err := configurationprofileassignments.ParseVirtualMachineID(model.VirtualMachineId)
if err != nil {
return err
}

configurationId, err := configurationprofiles.ParseConfigurationProfileID(model.ConfigurationId)
if err != nil {
return err
}

// Currently, the configuration profile assignment name has to be hardcoded to "default" by API requirement.
id := configurationprofileassignments.NewVirtualMachineProviders2ConfigurationProfileAssignmentID(subscriptionId, vmId.ResourceGroupName, vmId.VirtualMachineName, "default")
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(v.ResourceType(), id)
}

properties := configurationprofileassignments.ConfigurationProfileAssignment{
Name: pointer.To(id.ConfigurationProfileAssignmentName),
Properties: &configurationprofileassignments.ConfigurationProfileAssignmentProperties{
ConfigurationProfile: pointer.To(configurationId.ID()),
TargetId: pointer.To(vmId.ID()),
},
}

if _, respErr := client.CreateOrUpdate(ctx, id, properties); respErr != nil {
return fmt.Errorf("creating %s: %+v", id.String(), respErr)
}

metadata.SetID(id)
return nil
},
}
}

func (v VirtualMachineConfigurationAssignment) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationProfileVMAssignmentsClient
id, err := configurationprofileassignments.ParseVirtualMachineProviders2ConfigurationProfileAssignmentID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

state := VirtualMachineConfigurationAssignment{}

if model := resp.Model; model != nil {
configurationId, err := configurationprofiles.ParseConfigurationProfileID(*model.Properties.ConfigurationProfile)
if err != nil {
return err
}
state.ConfigurationId = configurationId.ID()

virtualMachineId, err := configurationprofileassignments.ParseVirtualMachineID(*model.Properties.TargetId)
if err != nil {
return err
}
state.VirtualMachineId = virtualMachineId.ID()
}

return metadata.Encode(&state)
},
}
}

func (v VirtualMachineConfigurationAssignment) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationProfileVMAssignmentsClient

id, err := configurationprofileassignments.ParseVirtualMachineProviders2ConfigurationProfileAssignmentID(metadata.ResourceData.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
},
}
}

func (v VirtualMachineConfigurationAssignment) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return configurationprofileassignments.ValidateVirtualMachineProviders2ConfigurationProfileAssignmentID
}

var _ sdk.Resource = &VirtualMachineConfigurationAssignment{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package automanage_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofileassignments"
"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/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type VirtualMachineConfigurationAssignmentResource struct{}

func TestAccVirtualMachineConfigurationAssignment_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_virtual_machine_automanage_configuration_assignment", "test")
r := VirtualMachineConfigurationAssignmentResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccVirtualMachineConfigurationAssignment_requireImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_virtual_machine_automanage_configuration_assignment", "test")
r := VirtualMachineConfigurationAssignmentResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func (r VirtualMachineConfigurationAssignmentResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
client := clients.Automanage.ConfigurationProfileVMAssignmentsClient

id, err := configurationprofileassignments.ParseVirtualMachineProviders2ConfigurationProfileAssignmentID(state.ID)
if err != nil {
return nil, err
}

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

func (r VirtualMachineConfigurationAssignmentResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctestnw-%d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_subnet" "test" {
name = "internal"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "test" {
name = "acctestnic-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.test.id
private_ip_address_allocation = "Dynamic"
}
}

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"
disable_password_authentication = false
network_interface_ids = [
azurerm_network_interface.test.id,
]

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
}

resource "azurerm_automanage_configuration" "test" {
name = "acctest-amcp-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_virtual_machine_automanage_configuration_assignment" "test" {
virtual_machine_id = azurerm_linux_virtual_machine.test.id
configuration_id = azurerm_automanage_configuration.test.id
}


`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (r VirtualMachineConfigurationAssignmentResource) requiresImport(data acceptance.TestData) string {
config := r.basic(data)
return fmt.Sprintf(`
%s

resource "azurerm_virtual_machine_automanage_configuration_assignment" "import" {
virtual_machine_id = azurerm_virtual_machine_automanage_configuration_assignment.test.virtual_machine_id
configuration_id = azurerm_virtual_machine_automanage_configuration_assignment.test.configuration_id
}
`, config)
}
Loading
Loading