From c78a3239c37032de92021c24f427f85bef8cddad Mon Sep 17 00:00:00 2001 From: tunm4 <8-tunm4_2@users.noreply.git.vngcloud.dev> Date: Fri, 21 Jun 2024 09:18:10 +0700 Subject: [PATCH] allow import vngcloud_vserver_volume_attach --- docs/resources/vserver_server.md | 2 +- resource/vserver/resource_acttach_volume.go | 60 +++++++++++++++++++++ resource/vserver/resource_server.go | 2 +- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/docs/resources/vserver_server.md b/docs/resources/vserver_server.md index 00ec8c6..a26c5f5 100644 --- a/docs/resources/vserver_server.md +++ b/docs/resources/vserver_server.md @@ -53,7 +53,7 @@ The following arguments are supported: - **ssh_key** (String, Optional) ID of SSH key - **user_name** (String, Optional) Name of user - **user_password** (String, Optional) Password of user -- **server_group** (String, Optional) ID of Server Group. +- **server_group_id** (String, Optional) ID of Server Group. - **user_data** (String, Optional) User data to provide when launching the server. - **user_data_base64_encode** (Boolean, Optional) Can be used instead of user_data to pass base64-encoded binary data directly. Use this instead of user_data whenever the value is not a valid UTF-8 string. diff --git a/resource/vserver/resource_acttach_volume.go b/resource/vserver/resource_acttach_volume.go index a5af497..8e5c663 100644 --- a/resource/vserver/resource_acttach_volume.go +++ b/resource/vserver/resource_acttach_volume.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "log" + "strings" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -18,6 +19,9 @@ func ResourceAttachVolume() *schema.Resource { Create: resourceVolumeAttach, Delete: resourceVolumeDetach, Read: resourceVolumeRead, + Importer: &schema.ResourceImporter{ + StateContext: resourceAttachVolumeImport, + }, Schema: map[string]*schema.Schema{ "project_id": { Type: schema.TypeString, @@ -39,6 +43,62 @@ func ResourceAttachVolume() *schema.Resource { }, } } + +func resourceAttachVolumeImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { + idParts := strings.Split(d.Id(), ":") + if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { + return nil, fmt.Errorf("Unexpected format of ID (%q), expected ProjectID:VolumeID:ServerID", d.Id()) + } + projectID := idParts[0] + volumeID := idParts[1] + cli := m.(*client.Client) + resp, httpResponse, _ := cli.VserverClient.VolumeRestControllerApi.GetVolumeUsingGET2(context.TODO(), projectID, volumeID) + if CheckErrorResponse(httpResponse) { + responseBody := GetResponseBody(httpResponse) + err := fmt.Errorf("Get Volume fail with errMsg : %s", responseBody) + return nil, err + } + respJSON, _ := json.Marshal(resp) + log.Printf("-------------------------------------\n") + log.Printf("%s\n", string(respJSON)) + log.Printf("-------------------------------------\n") + volume := resp.Data + if volume.Status != "IN-USE" { + err := fmt.Errorf("Volume status %s unexpected (IN-USE)", volume.Status) + return nil, err + } + serverID := idParts[2] + _, httpResponse, _ = cli.VserverClient.ServerRestControllerApi.GetServerUsingGET1(context.TODO(), projectID, serverID) + if CheckErrorResponse(httpResponse) { + responseBody := GetResponseBody(httpResponse) + err := fmt.Errorf("Get Server fail with errMsg : %s", responseBody) + return nil, err + } + if volume.MultiAttach { + if !contains(volume.ServerIdList, serverID) { + err := fmt.Errorf("Server is not attached in this volume") + return nil, err + } + } else if volume.ServerId != serverID { + err := fmt.Errorf("Server is not attached in this volume") + return nil, err + } + d.SetId(volumeID) + d.Set("project_id", projectID) + d.Set("server_id", serverID) + d.Set("volume_id", volumeID) + return []*schema.ResourceData{d}, nil +} + +func contains(list []string, target string) bool { + for _, item := range list { + if item == target { + return true + } + } + return false +} + func resourceVolumeAttach(d *schema.ResourceData, m interface{}) error { projectID := d.Get("project_id").(string) volumeID := d.Get("volume_id").(string) diff --git a/resource/vserver/resource_server.go b/resource/vserver/resource_server.go index ed582d9..02b6935 100644 --- a/resource/vserver/resource_server.go +++ b/resource/vserver/resource_server.go @@ -278,7 +278,6 @@ func resourceServerRead(d *schema.ResourceData, m interface{}) error { d.Set("ssh_key_name", server.SshKeyName) d.Set("server_group_id", server.ServerGroupId) d.Set("root_disk_id", server.BootVolumeId) - d.Set("image_id", server.Image.Id) d.Set("os_licence", server.Licence) _, ok := d.GetOk("host_group_id") if ok { @@ -366,6 +365,7 @@ func resourceServerImport(ctx context.Context, d *schema.ResourceData, m interfa d.Set("network_id", "") d.Set("subnet_id", "") } + d.Set("image_id", serverResp.Data.Image.Id) return resourceServerReadForImport(d, volumeResp) }