Skip to content

Commit

Permalink
fix(mysql_databases): Fix import error (#497)
Browse files Browse the repository at this point in the history
* fix(mysql_databases): Change to a private function

* fix(mysql_databases): Fix import error
  • Loading branch information
youngmn authored Jan 3, 2025
1 parent d58cec1 commit 5908a89
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
8 changes: 4 additions & 4 deletions docs/resources/mysql_databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ In addition to all arguments above, the following attributes are exported

### `terraform import` command

* MySQL Database can be imported using the `id`. For example:
* MySQL Database can be imported using the `id`:`name`:`name`:... . For example:

```console
$ terraform import ncloud_mysql_databases.rsc_name 12345
$ terraform import ncloud_mysql_databases.rsc_name 12345:name1:name2
```

### `import` block

* In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import MySQL Database using the `id`. For example:
* In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import MySQL Database using the `id`:`name`:`name`:... . For example:

```terraform
import {
to = ncloud_mysql_databases.rsc_name
id = "12345"
id = "12345:name1:name2"
}
```
35 changes: 31 additions & 4 deletions internal/service/mysql/mysql_databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"fmt"
"regexp"
"strings"

"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/vmysql"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"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/listplanmodifier"
Expand Down Expand Up @@ -41,7 +41,34 @@ type mysqlDatabasesResource struct {
}

func (r *mysqlDatabasesResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
var plan mysqlDatabasesResourceModel
var dbList []mysqlDatabase
idParts := strings.Split(req.ID, ":")

if len(idParts) < 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: id:name1:name2:... Got: %q", req.ID),
)
return
}

for idx, v := range idParts {
if idx == 0 {
plan.ID = types.StringValue(v)
plan.MysqlInstanceNo = types.StringValue(v)
} else {
db := mysqlDatabase{
DatabaseName: types.StringValue(v),
}
dbList = append(dbList, db)
}
}

mysqlDatabases, _ := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: mysqlDatabase{}.attrTypes()}, dbList)
plan.MysqlDatabaseList = mysqlDatabases

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

func (r *mysqlDatabasesResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down Expand Up @@ -289,7 +316,7 @@ type mysqlDatabase struct {
DatabaseName types.String `tfsdk:"name"`
}

func (r mysqlDatabase) AttrTypes() map[string]attr.Type {
func (r mysqlDatabase) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"name": types.StringType,
}
Expand All @@ -307,7 +334,7 @@ func (r *mysqlDatabasesResourceModel) refreshFromOutput(ctx context.Context, out
databaseList = append(databaseList, mysqlDb)
}

mysqlDatabases, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: mysqlDatabase{}.AttrTypes()}, databaseList)
mysqlDatabases, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: mysqlDatabase{}.attrTypes()}, databaseList)
if diags.HasError() {
return diags
}
Expand Down
26 changes: 23 additions & 3 deletions internal/service/mysql/mysql_databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func TestAccResourceNcloudMysqlDatabases_vpc_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "mysql_database_list.1.name", "testdb2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccMysqlAssociationImportStateIDFunc(resourceName),
ImportStateVerify: true,
},
},
})
}
Expand All @@ -60,11 +66,11 @@ resource "ncloud_subnet" "test_subnet" {
resource "ncloud_mysql" "mysql" {
subnet_no = ncloud_subnet.test_subnet.id
service_name = "%[1]s"
server_name_prefix = "testprefix"
user_name = "testusername"
server_name_prefix = "rprefix"
user_name = "rusername"
user_password = "t123456789!a"
host_ip = "192.168.0.1"
database_name = "test_db"
database_name = "admin_r_db"
}
resource "ncloud_mysql_databases" "mysql_dbs" {
Expand Down Expand Up @@ -100,3 +106,17 @@ func testAccCheckMysqlDatabasesDestroy(s *terraform.State) error {

return nil
}

func testAccMysqlAssociationImportStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}
id := rs.Primary.Attributes["id"]
name1 := rs.Primary.Attributes["mysql_database_list.0.name"]
name2 := rs.Primary.Attributes["mysql_database_list.1.name"]

return fmt.Sprintf("%s:%s:%s", id, name1, name2), nil
}
}

0 comments on commit 5908a89

Please sign in to comment.