Skip to content

Commit d18f312

Browse files
authored
Merge pull request #26 from vngcloud/iam-vserver
allow import vngcloud_vserver_volume_attach
2 parents 5247e1e + c78a323 commit d18f312

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

docs/resources/vserver_server.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The following arguments are supported:
5353
- **ssh_key** (String, Optional) ID of SSH key
5454
- **user_name** (String, Optional) Name of user
5555
- **user_password** (String, Optional) Password of user
56-
- **server_group** (String, Optional) ID of Server Group.
56+
- **server_group_id** (String, Optional) ID of Server Group.
5757
- **user_data** (String, Optional) User data to provide when launching the server.
5858
- **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.
5959

resource/vserver/resource_acttach_volume.go

+60
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"log"
8+
"strings"
89
"time"
910

1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -18,6 +19,9 @@ func ResourceAttachVolume() *schema.Resource {
1819
Create: resourceVolumeAttach,
1920
Delete: resourceVolumeDetach,
2021
Read: resourceVolumeRead,
22+
Importer: &schema.ResourceImporter{
23+
StateContext: resourceAttachVolumeImport,
24+
},
2125
Schema: map[string]*schema.Schema{
2226
"project_id": {
2327
Type: schema.TypeString,
@@ -39,6 +43,62 @@ func ResourceAttachVolume() *schema.Resource {
3943
},
4044
}
4145
}
46+
47+
func resourceAttachVolumeImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
48+
idParts := strings.Split(d.Id(), ":")
49+
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
50+
return nil, fmt.Errorf("Unexpected format of ID (%q), expected ProjectID:VolumeID:ServerID", d.Id())
51+
}
52+
projectID := idParts[0]
53+
volumeID := idParts[1]
54+
cli := m.(*client.Client)
55+
resp, httpResponse, _ := cli.VserverClient.VolumeRestControllerApi.GetVolumeUsingGET2(context.TODO(), projectID, volumeID)
56+
if CheckErrorResponse(httpResponse) {
57+
responseBody := GetResponseBody(httpResponse)
58+
err := fmt.Errorf("Get Volume fail with errMsg : %s", responseBody)
59+
return nil, err
60+
}
61+
respJSON, _ := json.Marshal(resp)
62+
log.Printf("-------------------------------------\n")
63+
log.Printf("%s\n", string(respJSON))
64+
log.Printf("-------------------------------------\n")
65+
volume := resp.Data
66+
if volume.Status != "IN-USE" {
67+
err := fmt.Errorf("Volume status %s unexpected (IN-USE)", volume.Status)
68+
return nil, err
69+
}
70+
serverID := idParts[2]
71+
_, httpResponse, _ = cli.VserverClient.ServerRestControllerApi.GetServerUsingGET1(context.TODO(), projectID, serverID)
72+
if CheckErrorResponse(httpResponse) {
73+
responseBody := GetResponseBody(httpResponse)
74+
err := fmt.Errorf("Get Server fail with errMsg : %s", responseBody)
75+
return nil, err
76+
}
77+
if volume.MultiAttach {
78+
if !contains(volume.ServerIdList, serverID) {
79+
err := fmt.Errorf("Server is not attached in this volume")
80+
return nil, err
81+
}
82+
} else if volume.ServerId != serverID {
83+
err := fmt.Errorf("Server is not attached in this volume")
84+
return nil, err
85+
}
86+
d.SetId(volumeID)
87+
d.Set("project_id", projectID)
88+
d.Set("server_id", serverID)
89+
d.Set("volume_id", volumeID)
90+
return []*schema.ResourceData{d}, nil
91+
}
92+
93+
func contains(list []string, target string) bool {
94+
for _, item := range list {
95+
if item == target {
96+
return true
97+
}
98+
}
99+
return false
100+
}
101+
42102
func resourceVolumeAttach(d *schema.ResourceData, m interface{}) error {
43103
projectID := d.Get("project_id").(string)
44104
volumeID := d.Get("volume_id").(string)

resource/vserver/resource_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ func resourceServerRead(d *schema.ResourceData, m interface{}) error {
278278
d.Set("ssh_key_name", server.SshKeyName)
279279
d.Set("server_group_id", server.ServerGroupId)
280280
d.Set("root_disk_id", server.BootVolumeId)
281-
d.Set("image_id", server.Image.Id)
282281
d.Set("os_licence", server.Licence)
283282
_, ok := d.GetOk("host_group_id")
284283
if ok {
@@ -366,6 +365,7 @@ func resourceServerImport(ctx context.Context, d *schema.ResourceData, m interfa
366365
d.Set("network_id", "")
367366
d.Set("subnet_id", "")
368367
}
368+
d.Set("image_id", serverResp.Data.Image.Id)
369369
return resourceServerReadForImport(d, volumeResp)
370370
}
371371

0 commit comments

Comments
 (0)