Skip to content

Commit

Permalink
Merge pull request #2215 from okta/steveAG_2203
Browse files Browse the repository at this point in the history
@steveAG's fix for okta_email_template_settings import
  • Loading branch information
monde authored Feb 10, 2025
2 parents 6ed2925 + 318ef43 commit e521979
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions okta/resource_okta_email_template_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package okta
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
Expand Down Expand Up @@ -149,7 +149,17 @@ func (r *emailTemplateSettingsResource) Update(ctx context.Context, req resource
}

func (r *emailTemplateSettingsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
idParts := strings.Split(req.ID, "/")

if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError("Invalid Import Identifier", "Expected import identifier with format <brand_id>/<template_name>")
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &emailTemplateSettingsResourceModel{
ID: types.StringValue(req.ID),
BrandID: types.StringValue(idParts[0]),
TemplateName: types.StringValue(idParts[1]),
})...)
}

func (r *emailTemplateSettingsResource) put(ctx context.Context, plan emailTemplateSettingsResourceModel) error {
Expand Down
22 changes: 22 additions & 0 deletions okta/resource_okta_email_template_settings_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package okta

import (
"errors"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccResourceOktaEmailTemplateSettings_crud(t *testing.T) {
Expand Down Expand Up @@ -33,6 +35,26 @@ func TestAccResourceOktaEmailTemplateSettings_crud(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "template_name", "UserActivation"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("failed to find %s", resourceName)
}
brandID := rs.Primary.Attributes["brand_id"]
templateName := rs.Primary.Attributes["template_name"]
return fmt.Sprintf("%s/%s", brandID, templateName), nil
},
ImportStateCheck: func(s []*terraform.InstanceState) error {
if len(s) != 1 {
return errors.New("failed to import schema into state")
}
return nil
},
},
},
})
}

0 comments on commit e521979

Please sign in to comment.