Skip to content

Commit

Permalink
Merge pull request #26 from vngcloud/iam-vserver
Browse files Browse the repository at this point in the history
allow import vngcloud_vserver_volume_attach
  • Loading branch information
manhtu1997 authored Jun 21, 2024
2 parents 5247e1e + c78a323 commit d18f312
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/resources/vserver_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
60 changes: 60 additions & 0 deletions resource/vserver/resource_acttach_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -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,
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion resource/vserver/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit d18f312

Please sign in to comment.