Skip to content

Commit

Permalink
refactor resourceId & make use of iotcentral_application_id
Browse files Browse the repository at this point in the history
  • Loading branch information
KenSpur committed Sep 14, 2023
1 parent ac65855 commit 0c4a487
Show file tree
Hide file tree
Showing 12 changed files with 485 additions and 400 deletions.
101 changes: 72 additions & 29 deletions internal/services/iotcentral/iotcentral_organization_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/iotcentral/2021-11-01-preview/apps"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iotcentral/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iotcentral/validate"
Expand All @@ -22,24 +23,25 @@ var (
)

type IotCentralOrganizationModel struct {
SubDomain string `tfschema:"sub_domain"`
OrganizationId string `tfschema:"organization_id"`
DisplayName string `tfschema:"display_name"`
ParentOrganizationId string `tfschema:"parent_organization_id"`
IotCentralApplicationId string `tfschema:"iotcentral_application_id"`
OrganizationId string `tfschema:"organization_id"`
DisplayName string `tfschema:"display_name"`
ParentOrganizationId string `tfschema:"parent_organization_id"`
}

func (r IotCentralOrganizationResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"sub_domain": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
"iotcentral_application_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: apps.ValidateIotAppID,
},
"organization_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.OrganizationID,
ValidateFunc: validate.OrganizationOrganizationID,
},
"display_name": {
Type: pluginsdk.TypeString,
Expand All @@ -49,7 +51,7 @@ func (r IotCentralOrganizationResource) Arguments() map[string]*pluginsdk.Schema
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validate.OrganizationID,
ValidateFunc: validate.OrganizationOrganizationID,
},
}
}
Expand All @@ -67,7 +69,7 @@ func (r IotCentralOrganizationResource) ModelObject() interface{} {
}

func (r IotCentralOrganizationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.ID
return validate.OrganizationID
}

func (r IotCentralOrganizationResource) Create() sdk.ResourceFunc {
Expand All @@ -79,7 +81,17 @@ func (r IotCentralOrganizationResource) Create() sdk.ResourceFunc {
return err
}

orgClient, err := client.OrganizationsClient(ctx, state.SubDomain)
appId, err := apps.ParseIotAppID(state.IotCentralApplicationId)
if err != nil {
return err
}

app, err := client.AppsClient.Get(ctx, *appId)
if err != nil || app.Model == nil {
return fmt.Errorf("checking for the presence of existing %q: %+v", appId, err)
}

orgClient, err := client.OrganizationsClient(ctx, *app.Model.Properties.Subdomain)
if err != nil {
return fmt.Errorf("creating organization client: %+v", err)
}
Expand All @@ -97,10 +109,7 @@ func (r IotCentralOrganizationResource) Create() sdk.ResourceFunc {
return fmt.Errorf("creating %s: %+v", state.OrganizationId, err)
}

orgId, err := parse.NewOrganizationID(state.SubDomain, client.Endpoint.Name(), *org.ID)
if err != nil {
return err
}
orgId := parse.NewOrganizationID(appId.SubscriptionId, appId.ResourceGroupName, appId.IotAppName, *org.ID)

metadata.SetID(orgId)
return nil
Expand All @@ -113,17 +122,27 @@ func (r IotCentralOrganizationResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.IoTCentral
id, err := parse.ParseOrganizationID(metadata.ResourceData.Id(), metadata.ResourceData.Get("sub_domain").(string), client.Endpoint.Name())
id, err := parse.OrganizationID(metadata.ResourceData.Id())
if err != nil {
return err
}

appId := apps.NewIotAppID(id.SubscriptionId, id.ResourceGroup, id.IotAppName)
if err != nil {
return err
}

orgClient, err := client.OrganizationsClient(ctx, id.SubDomain)
app, err := client.AppsClient.Get(ctx, appId)
if err != nil || app.Model == nil {
return metadata.MarkAsGone(id)
}

orgClient, err := client.OrganizationsClient(ctx, *app.Model.Properties.Subdomain)
if err != nil {
return fmt.Errorf("creating organization client: %+v", err)
}

org, err := orgClient.Get(ctx, id.OrganizationId)
org, err := orgClient.Get(ctx, id.Name)
if err != nil {
if org.ID == nil || *org.ID == "" {
return metadata.MarkAsGone(id)
Expand All @@ -133,9 +152,9 @@ func (r IotCentralOrganizationResource) Read() sdk.ResourceFunc {
}

state := IotCentralOrganizationModel{
SubDomain: id.SubDomain,
OrganizationId: id.OrganizationId,
DisplayName: *org.DisplayName,
IotCentralApplicationId: appId.ID(),
OrganizationId: id.Name,
DisplayName: *org.DisplayName,
}

if org.Parent != nil {
Expand All @@ -157,19 +176,33 @@ func (r IotCentralOrganizationResource) Update() sdk.ResourceFunc {
return err
}

id, err := parse.ParseOrganizationID(metadata.ResourceData.Id(), state.SubDomain, client.Endpoint.Name())
id, err := parse.OrganizationID(metadata.ResourceData.Id())
if err != nil {
return err
}

orgClient, err := client.OrganizationsClient(ctx, id.SubDomain)
appId := apps.NewIotAppID(id.SubscriptionId, id.ResourceGroup, id.IotAppName)
if err != nil {
return err
}

app, err := client.AppsClient.Get(ctx, appId)
if err != nil || app.Model == nil {
return metadata.MarkAsGone(id)
}

orgClient, err := client.OrganizationsClient(ctx, *app.Model.Properties.Subdomain)
if err != nil {
return fmt.Errorf("creating organization client: %+v", err)
}

existing, err := orgClient.Get(ctx, id.OrganizationId)
existing, err := orgClient.Get(ctx, id.Name)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id, err)
if existing.ID == nil || *existing.ID == "" {
return metadata.MarkAsGone(id)
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if metadata.ResourceData.HasChange("display_name") {
Expand All @@ -196,17 +229,27 @@ func (r IotCentralOrganizationResource) Delete() sdk.ResourceFunc {
return err
}

id, err := parse.ParseOrganizationID(metadata.ResourceData.Id(), state.SubDomain, client.Endpoint.Name())
id, err := parse.OrganizationID(metadata.ResourceData.Id())
if err != nil {
return err
}

orgClient, err := client.OrganizationsClient(ctx, id.SubDomain)
appId := apps.NewIotAppID(id.SubscriptionId, id.ResourceGroup, id.IotAppName)
if err != nil {
return err
}

app, err := client.AppsClient.Get(ctx, appId)
if err != nil || app.Model == nil {
return metadata.MarkAsGone(id)
}

orgClient, err := client.OrganizationsClient(ctx, *app.Model.Properties.Subdomain)
if err != nil {
return fmt.Errorf("creating organization client: %+v", err)
}

_, err = orgClient.Remove(ctx, id.OrganizationId)
_, err = orgClient.Remove(ctx, id.Name)
if err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/iotcentral/2021-11-01-preview/apps"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -143,17 +144,27 @@ func TestAccIoTCentralOrganization_unsetParent(t *testing.T) {
}

func (IoTCentralOrganizationResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ParseOrganizationID(state.ID, state.Attributes["sub_domain"], clients.IoTCentral.Endpoint.Name())
id, err := parse.OrganizationID(state.ID)
if err != nil {
return nil, err
}

orgClient, err := clients.IoTCentral.OrganizationsClient(ctx, id.SubDomain)
appId, err := apps.ParseIotAppID(state.Attributes["iotcentral_application_id"])
if err != nil {
return nil, err
}

app, err := clients.IoTCentral.AppsClient.Get(ctx, *appId)
if err != nil || app.Model == nil {
return nil, fmt.Errorf("checking for the presence of existing %q: %+v", appId, err)
}

orgClient, err := clients.IoTCentral.OrganizationsClient(ctx, *app.Model.Properties.Subdomain)
if err != nil {
return nil, fmt.Errorf("creating organization client: %+v", err)
}

resp, err := orgClient.Get(ctx, id.OrganizationId)
resp, err := orgClient.Get(ctx, id.Name)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
Expand All @@ -168,9 +179,9 @@ provider "azurerm" {
}
%s
resource "azurerm_iotcentral_organization" "test" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-id"
display_name = "Org basic"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-id"
display_name = "Org basic"
}
`, r.template(data))
}
Expand All @@ -182,14 +193,14 @@ provider "azurerm" {
}
%s
resource "azurerm_iotcentral_organization" "test_parent" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-parent-id"
display_name = "Org parent"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-parent-id"
display_name = "Org parent"
}
resource "azurerm_iotcentral_organization" "test" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-id"
display_name = "Org child"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-id"
display_name = "Org child"
parent_organization_id = azurerm_iotcentral_organization.test_parent.organization_id
}
Expand All @@ -203,9 +214,9 @@ provider "azurerm" {
}
%s
resource "azurerm_iotcentral_organization" "test" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-id"
display_name = "Org basic updated"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-id"
display_name = "Org basic updated"
}
`, r.template(data))
}
Expand All @@ -217,19 +228,19 @@ provider "azurerm" {
}
%s
resource "azurerm_iotcentral_organization" "test_parent" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-parent-id"
display_name = "Org parent"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-parent-id"
display_name = "Org parent"
}
resource "azurerm_iotcentral_organization" "test_parent_2" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-parent-2-id"
display_name = "Org parent 2"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-parent-2-id"
display_name = "Org parent 2"
}
resource "azurerm_iotcentral_organization" "test" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-id"
display_name = "Org child"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-id"
display_name = "Org child"
parent_organization_id = azurerm_iotcentral_organization.test_parent_2.organization_id
}
Expand All @@ -243,14 +254,14 @@ provider "azurerm" {
}
%s
resource "azurerm_iotcentral_organization" "test_parent" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-parent-id"
display_name = "Org parent"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-parent-id"
display_name = "Org parent"
}
resource "azurerm_iotcentral_organization" "test" {
sub_domain = azurerm_iotcentral_application.test.sub_domain
organization_id = "org-test-id"
display_name = "Org child"
iotcentral_application_id = azurerm_iotcentral_application.test.id
organization_id = "org-test-id"
display_name = "Org child"
}
`, r.template(data))
}
Expand Down
Loading

0 comments on commit 0c4a487

Please sign in to comment.