Skip to content

Commit

Permalink
r\digital_twins_instance: support identity (#17076)
Browse files Browse the repository at this point in the history
  • Loading branch information
myc2h6o authored Jun 27, 2022
1 parent 2515d53 commit c0ddf4a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
53 changes: 53 additions & 0 deletions internal/services/digitaltwins/digital_twins_instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/digitaltwins/mgmt/2020-12-01/digitaltwins"
"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/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -54,6 +56,8 @@ func resourceDigitalTwinsInstance() *pluginsdk.Resource {
Computed: true,
},

"identity": commonschema.SystemAssignedIdentityOptional(),

"tags": tags.Schema(),
},
}
Expand All @@ -80,8 +84,14 @@ func resourceDigitalTwinsInstanceCreate(d *pluginsdk.ResourceData, meta interfac
return tf.ImportAsExistsError("azurerm_digital_twins_instance", id)
}

expandedIdentity, err := expandDigitalTwinsInstanceIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

properties := digitaltwins.Description{
Location: utils.String(location.Normalize(d.Get("location").(string))),
Identity: expandedIdentity,
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

Expand Down Expand Up @@ -130,6 +140,10 @@ func resourceDigitalTwinsInstanceRead(d *pluginsdk.ResourceData, meta interface{
d.Set("host_name", props.HostName)
}

if err := d.Set("identity", flattenDigitalTwinsInstanceIdentity(resp.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}

Expand All @@ -145,6 +159,14 @@ func resourceDigitalTwinsInstanceUpdate(d *pluginsdk.ResourceData, meta interfac

props := digitaltwins.PatchDescription{}

if d.HasChange("identity") {
expandedIdentity, err := expandDigitalTwinsInstanceIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
props.Identity = expandedIdentity
}

if d.HasChange("tags") {
props.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}
Expand Down Expand Up @@ -182,3 +204,34 @@ func resourceDigitalTwinsInstanceDelete(d *pluginsdk.ResourceData, meta interfac

return nil
}

func expandDigitalTwinsInstanceIdentity(input []interface{}) (*digitaltwins.Identity, error) {
expanded, err := identity.ExpandSystemAssigned(input)
if err != nil {
return nil, err
}

result := digitaltwins.Identity{
Type: digitaltwins.IdentityType(expanded.Type),
}

return &result, nil
}

func flattenDigitalTwinsInstanceIdentity(input *digitaltwins.Identity) []interface{} {
var transform *identity.SystemAssigned

if input != nil {
transform = &identity.SystemAssigned{
Type: identity.Type(string(input.Type)),
}
if input.PrincipalID != nil {
transform.PrincipalId = *input.PrincipalID
}
if input.TenantID != nil {
transform.TenantId = *input.TenantID
}
}

return identity.FlattenSystemAssigned(transform)
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ func TestAccDigitalTwinsInstance_update(t *testing.T) {
})
}

func TestAccDigitalTwinsInstance_identity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_digital_twins_instance", "test")
r := DigitalTwinsInstanceResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicWithIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
check.That(data.ResourceName).Key("identity.0.tenant_id").IsUUID(),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.principal_id").DoesNotExist(),
check.That(data.ResourceName).Key("identity.0.tenant_id").DoesNotExist(),
),
},
data.ImportStep(),
{
Config: r.basicWithIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
check.That(data.ResourceName).Key("identity.0.tenant_id").IsUUID(),
),
},
data.ImportStep(),
})
}

func (DigitalTwinsInstanceResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.DigitalTwinsInstanceID(state.ID)
if err != nil {
Expand Down Expand Up @@ -181,3 +215,18 @@ resource "azurerm_digital_twins_instance" "test" {
}
`, r.template(data), data.RandomInteger)
}

func (r DigitalTwinsInstanceResource) basicWithIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_digital_twins_instance" "test" {
name = "acctest-DT-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
identity {
type = "SystemAssigned"
}
}
`, r.template(data), data.RandomInteger)
}
8 changes: 8 additions & 0 deletions website/docs/r/digital_twins_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ The following arguments are supported:
* `resource_group_name` - (Required) The name of the Resource Group where the Digital Twins instance should exist. Changing this forces a new Digital Twins instance to be created.

* `location` - (Required) The Azure Region where the Digital Twins instance should exist. Changing this forces a new Digital Twins instance to be created.
*
* `identity` - (Optional) An `identity` block as defined below.

* `tags` - (Optional) A mapping of tags which should be assigned to the Digital Twins instance.

---

An `identity` block supports the following:

* `type` - (Required) The type of Managed Service Identity that is configured on this Digital Twins instance. The only possible value is `SystemAssigned`.

## Attributes Reference

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

0 comments on commit c0ddf4a

Please sign in to comment.