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_system_center_virtual_machine_manager_availability_set #24975

2 changes: 1 addition & 1 deletion .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ service/synapse:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_synapse_((.|\n)*)###'

service/systemcentervirtualmachinemanager:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_system_center_virtual_machine_manager_server((.|\n)*)###'
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_system_center_virtual_machine_manager_((.|\n)*)###'

service/traffic-manager:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_traffic_manager_((.|\n)*)###'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func (r Registration) DataSources() []sdk.DataSource {
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
SystemCenterVirtualMachineManagerServerResource{},
SystemCenterVirtualMachineManagerAvailabilitySetResource{},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package systemcentervirtualmachinemanager

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-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations"
"github.com/hashicorp/go-azure-sdk/resource-manager/systemcentervirtualmachinemanager/2023-10-07/availabilitysets"
"github.com/hashicorp/go-azure-sdk/resource-manager/systemcentervirtualmachinemanager/2023-10-07/vmmservers"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/systemcentervirtualmachinemanager/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type SystemCenterVirtualMachineManagerAvailabilitySetModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
CustomLocationId string `tfschema:"custom_location_id"`
SystemCenterVirtualMachineManagerServerId string `tfschema:"system_center_virtual_machine_manager_server_id"`
Tags map[string]string `tfschema:"tags"`
}

var _ sdk.Resource = SystemCenterVirtualMachineManagerAvailabilitySetResource{}
var _ sdk.ResourceWithUpdate = SystemCenterVirtualMachineManagerAvailabilitySetResource{}

type SystemCenterVirtualMachineManagerAvailabilitySetResource struct{}

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) ModelObject() interface{} {
return &SystemCenterVirtualMachineManagerAvailabilitySetModel{}
}

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return availabilitysets.ValidateAvailabilitySetID
}

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) ResourceType() string {
return "azurerm_system_center_virtual_machine_manager_availability_set"
}

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.SystemCenterVirtualMachineManagerAvailabilitySetName,
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

"custom_location_id": commonschema.ResourceIDReferenceRequiredForceNew(&customlocations.CustomLocationId{}),

"system_center_virtual_machine_manager_server_id": commonschema.ResourceIDReferenceRequiredForceNew(&vmmservers.VMmServerId{}),

"tags": commonschema.Tags(),
}
}

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
subscriptionId := metadata.Client.Account.SubscriptionId
client := metadata.Client.SystemCenterVirtualMachineManager.AvailabilitySets

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

scvmmServerId, err := vmmservers.ParseVMmServerID(model.SystemCenterVirtualMachineManagerServerId)
if err != nil {
return err
}

id := availabilitysets.NewAvailabilitySetID(subscriptionId, model.ResourceGroupName, model.Name)

existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for the presence of an existing %s: %+v", id, err)
}
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

parameters := availabilitysets.AvailabilitySet{
Location: location.Normalize(model.Location),
ExtendedLocation: availabilitysets.ExtendedLocation{
Type: utils.String("customLocation"),
Name: utils.String(model.CustomLocationId),
},
Properties: availabilitysets.AvailabilitySetProperties{
AvailabilitySetName: utils.String(id.AvailabilitySetName),
VMmServerId: utils.String(scvmmServerId.ID()),
},
Tags: pointer.To(model.Tags),
}

if err := client.CreateOrUpdateThenPoll(ctx, id, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

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

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.SystemCenterVirtualMachineManager.AvailabilitySets

id, err := availabilitysets.ParseAvailabilitySetID(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 := SystemCenterVirtualMachineManagerAvailabilitySetModel{}
if model := resp.Model; model != nil {
state.Name = id.AvailabilitySetName
state.ResourceGroupName = id.ResourceGroupName
state.Location = location.Normalize(model.Location)
state.CustomLocationId = pointer.From(model.ExtendedLocation.Name)
state.Tags = pointer.From(model.Tags)

scvmmServerId, err := vmmservers.ParseVMmServerID(pointer.From(model.Properties.VMmServerId))
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
state.SystemCenterVirtualMachineManagerServerId = scvmmServerId.ID()
}

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

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.SystemCenterVirtualMachineManager.AvailabilitySets

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

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

parameters := availabilitysets.ResourcePatch{
Tags: pointer.To(model.Tags),
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
}

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

return nil
},
}
}

func (r SystemCenterVirtualMachineManagerAvailabilitySetResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.SystemCenterVirtualMachineManager.AvailabilitySets

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

if err := client.DeleteThenPoll(ctx, *id, availabilitysets.DeleteOperationOptions{Force: pointer.To(availabilitysets.ForceTrue)}); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
},
}
}
Loading
Loading