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

azurerm_arc_machine - support identity and tags properties #27987

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but for consistency can we please call this

Suggested change
existing := resp.Model
payload := resp.Model

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


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(),
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_arc_machine", "test")
r := ArcMachineResource{}
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


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)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given only one identity type is available here, we can omit this specific identity config and rely on the complete config to test identity

Suggested change
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)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

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 Arc Machine.

---

* 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
Loading