Skip to content

Commit

Permalink
digitaltwins: updating to account for the changes in 2023-01-31
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Aug 2, 2023
1 parent 2243e95 commit 092ff91
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceDigitalTwinsEndpointEventGrid() *pluginsdk.Resource {
Expand Down Expand Up @@ -114,10 +113,10 @@ func resourceDigitalTwinsEndpointEventGridCreateUpdate(d *pluginsdk.ResourceData
payload := endpoints.DigitalTwinsEndpointResource{
Properties: &endpoints.EventGrid{
AuthenticationType: pointer.To(endpoints.AuthenticationTypeKeyBased),
TopicEndpoint: utils.String(d.Get("eventgrid_topic_endpoint").(string)),
AccessKey1: utils.String(d.Get("eventgrid_topic_primary_access_key").(string)),
AccessKey2: utils.String(d.Get("eventgrid_topic_secondary_access_key").(string)),
DeadLetterSecret: utils.String(d.Get("dead_letter_storage_secret").(string)),
TopicEndpoint: d.Get("eventgrid_topic_endpoint").(string),
AccessKey1: d.Get("eventgrid_topic_primary_access_key").(string),
AccessKey2: pointer.To(d.Get("eventgrid_topic_secondary_access_key").(string)),
DeadLetterSecret: pointer.To(d.Get("dead_letter_storage_secret").(string)),
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func resourceDigitalTwinsInstance() *pluginsdk.Resource {
Computed: true,
},

"identity": commonschema.SystemAssignedIdentityOptional(),
"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),

"tags": commonschema.Tags(),
},
Expand All @@ -81,7 +81,7 @@ func resourceDigitalTwinsInstanceCreate(d *pluginsdk.ResourceData, meta interfac
return tf.ImportAsExistsError("azurerm_digital_twins_instance", id.ID())
}

expandedIdentity, err := identity.ExpandSystemAssigned(d.Get("identity").([]interface{}))
expandedIdentity, err := identity.ExpandLegacySystemAndUserAssignedMap(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
Expand Down Expand Up @@ -130,7 +130,11 @@ func resourceDigitalTwinsInstanceRead(d *pluginsdk.ResourceData, meta interface{
d.Set("host_name", props.HostName)
}

if err := d.Set("identity", identity.FlattenSystemAssigned(model.Identity)); err != nil {
flattenedIdentity, err := identity.FlattenLegacySystemAndUserAssignedMap(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
if err := d.Set("identity", flattenedIdentity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

Expand All @@ -155,7 +159,7 @@ func resourceDigitalTwinsInstanceUpdate(d *pluginsdk.ResourceData, meta interfac
props := digitaltwinsinstance.DigitalTwinsPatchDescription{}

if d.HasChange("identity") {
expandedIdentity, err := identity.ExpandSystemAssigned(d.Get("identity").([]interface{}))
expandedIdentity, err := identity.ExpandLegacySystemAndUserAssignedMap(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func TestAccDigitalTwinsInstance_update(t *testing.T) {
})
}

func TestAccDigitalTwinsInstance_identity(t *testing.T) {
func TestAccDigitalTwinsInstance_identitySystemAssigned(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_digital_twins_instance", "test")
r := DigitalTwinsInstanceResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicWithIdentity(data),
Config: r.basicWithIdentitySystemAssigned(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
Expand All @@ -125,7 +125,7 @@ func TestAccDigitalTwinsInstance_identity(t *testing.T) {
},
data.ImportStep(),
{
Config: r.basicWithIdentity(data),
Config: r.basicWithIdentitySystemAssigned(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
Expand All @@ -136,6 +136,40 @@ func TestAccDigitalTwinsInstance_identity(t *testing.T) {
})
}

func TestAccDigitalTwinsInstance_identityUserAssigned(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_digital_twins_instance", "test")
r := DigitalTwinsInstanceResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicWithIdentityUserAssigned(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.principal_id").IsEmpty(),
check.That(data.ResourceName).Key("identity.0.tenant_id").IsEmpty(),
),
},
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.basicWithIdentityUserAssigned(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.principal_id").IsEmpty(),
check.That(data.ResourceName).Key("identity.0.tenant_id").IsEmpty(),
),
},
data.ImportStep(),
})
}

func (DigitalTwinsInstanceResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := digitaltwinsinstance.ParseDigitalTwinsInstanceID(state.ID)
if err != nil {
Expand Down Expand Up @@ -219,7 +253,7 @@ resource "azurerm_digital_twins_instance" "test" {
`, r.template(data), data.RandomInteger)
}

func (r DigitalTwinsInstanceResource) basicWithIdentity(data acceptance.TestData) string {
func (r DigitalTwinsInstanceResource) basicWithIdentitySystemAssigned(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand All @@ -233,3 +267,27 @@ resource "azurerm_digital_twins_instance" "test" {
}
`, r.template(data), data.RandomInteger)
}

func (r DigitalTwinsInstanceResource) basicWithIdentityUserAssigned(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
resource "azurerm_user_assigned_identity" "test" {
name = "acctest%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}
resource "azurerm_digital_twins_instance" "test" {
name = "acctest-DT-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
identity {
type = "UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.test.id
]
}
}
`, r.template(data), data.RandomInteger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (m TimeSeriesDatabaseConnectionResource) Delete() sdk.ResourceFunc {
}

client := meta.Client.DigitalTwins.TimeSeriesDatabaseConnectionsClient
if err = client.DeleteThenPoll(ctx, *id); err != nil {
if err = client.DeleteThenPoll(ctx, *id, timeseriesdatabaseconnections.DefaultDeleteOperationOptions()); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}
return nil
Expand Down
18 changes: 16 additions & 2 deletions website/docs/r/digital_twins_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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.
Expand All @@ -51,7 +51,11 @@ The following arguments are supported:

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`.
* `type` - (Required) Specifies the type of Managed Service Identity that should be configured on this Digital Twins instance. Possible values are `SystemAssigned`, `UserAssigned`, `SystemAssigned, UserAssigned` (to enable both).

* `identity_ids` - (Optional) A list of User Assigned Managed Identity IDs to be assigned to this Digital Twins instance.

~> **NOTE:** This is required when `type` is set to `UserAssigned` or `SystemAssigned, UserAssigned`.

## Attributes Reference

Expand All @@ -61,6 +65,16 @@ In addition to the Arguments listed above - the following Attributes are exporte

* `host_name` - The API endpoint to work with this Digital Twins instance.

* `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/language/resources/syntax#operation-timeouts) for certain actions:
Expand Down

0 comments on commit 092ff91

Please sign in to comment.