-
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.
New Resource: `azurerm_virtual_machine_automanage_configuration_assig…
…nment` (#25480) * new resource `azurerm_virtual_machine_automanage_configuration_assignment` * add test cases for `azurerm_virtual_machine_automanage_configuration_assignment` * add documentation for `azurerm_virtual_machine_automanage_configuration_assignment` * add documentation for `azurerm_virtual_machine_automanage_configuration_assignment` * fix golint issue
- Loading branch information
1 parent
158636b
commit 27140fc
Showing
25 changed files
with
1,624 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
166 changes: 166 additions & 0 deletions
166
internal/services/automanage/virtual_machine_automanage_configuration_assignment_resource.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,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{} |
153 changes: 153 additions & 0 deletions
153
.../services/automanage/virtual_machine_automanage_configuration_assignment_resource_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,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) | ||
} |
Oops, something went wrong.