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

r\digital_twins_instance: support identity #17076

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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