Skip to content

Commit

Permalink
arc_machine support identity and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
teowa committed Nov 12, 2024
1 parent cde9785 commit 949296a
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 4 deletions.
68 changes: 64 additions & 4 deletions internal/services/hybridcompute/arc_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/hybridcompute/2024-07-10/machines"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
Expand All @@ -19,10 +20,12 @@ import (
)

type ArcMachineResourceModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Kind string `tfschema:"kind"`
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Kind string `tfschema:"kind"`
Identity []identity.ModelSystemAssigned `tfschema:"identity"`
Tags map[string]string `tfschema:"tags"`
}

type ArcMachineResource struct{}
Expand Down Expand Up @@ -58,6 +61,10 @@ func (r ArcMachineResource) Arguments() map[string]*pluginsdk.Schema {
ForceNew: true,
ValidateFunc: validation.StringInSlice(machines.PossibleValuesForArcKindEnum(), false),
},

"identity": commonschema.SystemAssignedIdentityOptional(),

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

Expand Down Expand Up @@ -92,6 +99,12 @@ func (r ArcMachineResource) Create() sdk.ResourceFunc {
parameters := machines.Machine{
Location: location.Normalize(model.Location),
Kind: pointer.To(machines.ArcKindEnum(model.Kind)),
Tags: pointer.To(model.Tags),
}

parameters.Identity, err = identity.ExpandSystemAssignedFromModel(model.Identity)
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

if _, err := client.CreateOrUpdate(ctx, id, parameters, machines.DefaultCreateOrUpdateOperationOptions()); err != nil {
Expand Down Expand Up @@ -130,13 +143,60 @@ func (r ArcMachineResource) Read() sdk.ResourceFunc {
if model := resp.Model; model != nil {
state.Location = location.Normalize(model.Location)
state.Kind = string(pointer.From(model.Kind))
state.Identity = identity.FlattenSystemAssignedToModel(model.Identity)
state.Tags = pointer.From(model.Tags)
}

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

func (r ArcMachineResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.HybridCompute.HybridComputeClient_v2024_07_10.Machines

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

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

resp, err := client.Get(ctx, *id, machines.DefaultGetOperationOptions())
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
if resp.Model == nil {
return fmt.Errorf("retrieving %s: `model` was nil", *id)
}
existing := resp.Model

if metadata.ResourceData.HasChange("identity") {
existing.Identity, err = identity.ExpandSystemAssignedFromModel(model.Identity)
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
}

if metadata.ResourceData.HasChange("tags") {
existing.Tags = pointer.To(model.Tags)
}

if _, err := client.CreateOrUpdate(ctx, *id, *existing, machines.DefaultCreateOrUpdateOperationOptions()); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

return nil
},
}
}

func (r ArcMachineResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Expand Down
90 changes: 90 additions & 0 deletions internal/services/hybridcompute/arc_machine_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,50 @@ func TestAccArcMachineResource_requiresImport(t *testing.T) {
})
}

func TestAccArcMachineResource_identity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_arc_machine", "test")
r := ArcMachineResource{}

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.identity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccArcMachineResource_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_arc_machine", "test")
r := ArcMachineResource{}

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r ArcMachineResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := machines.ParseMachineID(state.ID)
if err != nil {
Expand Down Expand Up @@ -92,6 +136,52 @@ resource "azurerm_arc_machine" "import" {
`, r.basic(data))
}

func (r ArcMachineResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
provider "azurerm" {
features {}
}
resource "azurerm_arc_machine" "test" {
name = "acctest-hcm-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
kind = "SCVMM"
identity {
type = "SystemAssigned"
}
tags = {
foo = "bar"
}
}
`, r.template(data), data.RandomInteger)
}

func (r ArcMachineResource) identity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
provider "azurerm" {
features {}
}
resource "azurerm_arc_machine" "test" {
name = "acctest-hcm-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
kind = "SCVMM"
identity {
type = "SystemAssigned"
}
}
`, r.template(data), data.RandomInteger)
}

func (r ArcMachineResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
29 changes: 29 additions & 0 deletions website/docs/r/arc_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ resource "azurerm_arc_machine" "example" {
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
kind = "SCVMM"
identity {
type = "SystemAssigned"
}
tags = {
environment = "example"
}
}
```

Expand All @@ -38,18 +46,39 @@ The following arguments are supported:

* `kind` - (Required) The kind of the Arc Machine. Possible values are `AVS`, `AWS`, `EPS`, `GCP`, `HCI`, `SCVMM` and `VMware`. Changing this forces a new resource to be created.

* `identity` - (Optional) An `identity` block as defined below.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---

* An `identity` block supports the following:

* `type` - (Required) Specifies the type of Managed Service Identity assigned to this Arc Machine. At this time the only possible value is `SystemAssigned`. Changing this forces a new resource to be created.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Arc Machine.

* `identity` - An `identity` block as defined below.

---

An `identity` block exports the following:

* `principal_id` - The Principal ID associated with this Managed Service Identity.

* `tenant_id` - The Tenant ID associated with this Managed Service Identity.

## Timeouts

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

* `create` - (Defaults to 30 minutes) Used when creating this Arc Machine.
* `read` - (Defaults to 5 minutes) Used when retrieving this Arc Machine.
* `update` - (Defaults to 30 minutes) Used when updating this Arc Machine.
* `delete` - (Defaults to 30 minutes) Used when deleting this Arc Machine.

## Import
Expand Down

0 comments on commit 949296a

Please sign in to comment.