Skip to content

Commit

Permalink
review(vserver): Modify based on reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
youngmn committed Sep 27, 2024
1 parent 1458066 commit 502fbaa
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 158 deletions.
18 changes: 9 additions & 9 deletions docs/data-sources/server_image_numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ output "image_list" {
}
```

```terraform
data "ncloud_server_image_numbers" "example" {
filter {
name = "name"
values = ["rocky-8.10-base"]
}
}
```

Outputs:
```terraform
image_list = {
Expand Down Expand Up @@ -69,6 +60,15 @@ image_list = {
}
```

```terraform
data "ncloud_server_image_numbers" "example" {
filter {
name = "name"
values = ["rocky-8.10-base"]
}
}
```

## Argument Reference

The following arguments are supported:
Expand Down
18 changes: 9 additions & 9 deletions docs/data-sources/server_specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ output "spec_list" {
}
```

```terraform
data "ncloud_server_specs" "example" {
filter {
name = "server_spec_code"
values = ["c2-g3"]
}
}
```

Outputs:
```terraform
spec_list = {
Expand All @@ -53,6 +44,15 @@ spec_list = {
}
```

```terraform
data "ncloud_server_specs" "example" {
filter {
name = "server_spec_code"
values = ["c2-g3"]
}
}
```

## Argument Reference

The following arguments are supported:
Expand Down
20 changes: 0 additions & 20 deletions internal/common/convert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,6 @@ func Int64FromInt32OrDefault(value *int32) basetypes.Int64Value {
return basetypes.NewInt64Value(int64(*value))
}

// Int64FromInt64OrDefault converts an int64 pointer to a Framework Int64 value.
// A nil int64 pointer is converted to a zero Int64.
// Used when the optional and computed attribute have no response value
func Int64FromInt64OrDefault(value *int64) basetypes.Int64Value {
if value == nil {
return basetypes.NewInt64Value(0)
}
return basetypes.NewInt64Value(int64(*value))
}

// Int32FromInt32OrDefault converts an int32 pointer to a Framework Int32 value.
// A nil int32 pointer is converted to a zero Int32.
// Used when the optional and computed attribute have no response value
func Int32FromInt32OrDefault(value *int32) basetypes.Int32Value {
if value == nil {
return basetypes.NewInt32Value(0)
}
return basetypes.NewInt32Value(int32(*value))
}

// StringFrameworkOrDefault converts a Framework StringValue struct to a same Framework StringValue.
// A null or unknown state is converted to a default(not aloocated) string.
// Used when the optional and computed attribute have no response value
Expand Down
10 changes: 9 additions & 1 deletion internal/common/json.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common

import "encoding/json"
import (
"encoding/json"
"regexp"
)

// MarshalUnchecked return the JSON encoding of value
// It does not check for errors about marshaling failing, so be careful to use
Expand All @@ -12,3 +15,8 @@ func MarshalUnchecked(value interface{}) []byte {
func MarshalUncheckedString(value interface{}) string {
return string(MarshalUnchecked(value))
}

func ReplaceNull(s string) string {
re := regexp.MustCompile(`:<null>`)
return re.ReplaceAllString(s, ":null")
}
11 changes: 7 additions & 4 deletions internal/service/server/block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import (
)

const (
BlockStorageStatusCodeCreate = "CREAT"
BlockStorageStatusCodeInit = "INIT"
BlockStorageStatusCodeAttach = "ATTAC"
BlockStorageStatusNameAttach = "attached"
BlockStorageStatusCodeCreate = "CREAT"
BlockStorageStatusCodeInit = "INIT"
BlockStorageStatusCodeAttach = "ATTAC"
BlockStorageStatusNameInit = "initialized"
BlockStorageStatusNameOptimizing = "optimizing"
BlockStorageStatusNameAttach = "attached"
BlockStorageStatusNameDetach = "detached"
)

func ResourceNcloudBlockStorage() *schema.Resource {
Expand Down
114 changes: 76 additions & 38 deletions internal/service/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

Expand Down Expand Up @@ -88,11 +89,6 @@ func ResourceNcloudServer() *schema.Resource {
Computed: true,
ForceNew: true,
},
"associate_with_public_ip": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"is_protect_server_termination": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -342,7 +338,7 @@ func resourceNcloudServerDelete(d *schema.ResourceData, meta interface{}) error
return err
}

if err := waitForDisconnectBlockStorage(config, d, blockStorage); err != nil {
if err := waitForDisconnectBlockStorage(config, *blockStorage.BlockStorageInstanceNo); err != nil {
return err
}
}
Expand Down Expand Up @@ -477,7 +473,6 @@ func createVpcServerInstance(d *schema.ResourceData, config *conn.ProviderConfig
ServerDescription: StringPtrOrNil(d.GetOk("description")),
LoginKeyName: StringPtrOrNil(d.GetOk("login_key_name")),
IsProtectServerTermination: BoolPtrOrNil(d.GetOk("is_protect_server_termination")),
AssociateWithPublicIp: BoolPtrOrNil(d.GetOk("associate_with_public_ip")),
FeeSystemTypeCode: StringPtrOrNil(d.GetOk("fee_system_type_code")),
InitScriptNo: StringPtrOrNil(d.GetOk("init_script_no")),
VpcNo: subnet.VpcNo,
Expand Down Expand Up @@ -543,7 +538,7 @@ func createVpcServerInstance(d *schema.ResourceData, config *conn.ProviderConfig

if len(blockStorageList) > 0 {
for _, blockStorage := range blockStorageList {
if err := waitForAttachedBlockStorage(config, d, blockStorage); err != nil {
if err := waitForAttachedBlockStorage(config, *blockStorage.BlockStorageInstanceNo); err != nil {
return nil, err
}
}
Expand All @@ -553,7 +548,7 @@ func createVpcServerInstance(d *schema.ResourceData, config *conn.ProviderConfig
}

func waitStateNcloudServerForCreation(config *conn.ProviderConfig, id string) error {
stateConf := &resource.StateChangeConf{
stateConf := &retry.StateChangeConf{
Pending: []string{"INIT", "CREAT"},
Target: []string{"RUN"},
Refresh: func() (interface{}, string, error) {
Expand Down Expand Up @@ -613,7 +608,7 @@ func changeServerInstanceSpec(d *schema.ResourceData, config *conn.ProviderConfi
return err
}

stateConf := &resource.StateChangeConf{
stateConf := &retry.StateChangeConf{
Pending: []string{"CHNG"},
Target: []string{"NULL"},
Refresh: func() (interface{}, string, error) {
Expand Down Expand Up @@ -734,7 +729,7 @@ func startThenWaitServerInstance(config *conn.ProviderConfig, id string) error {
return err
}

stateConf := &resource.StateChangeConf{
stateConf := &retry.StateChangeConf{
Pending: []string{"NSTOP"},
Target: []string{"RUN"},
Refresh: func() (interface{}, string, error) {
Expand Down Expand Up @@ -957,7 +952,7 @@ func buildNetworkInterfaceList(config *conn.ProviderConfig, r *ServerInstance) e
func stopThenWaitServerInstance(config *conn.ProviderConfig, id string) error {
var err error

stateConf := &resource.StateChangeConf{
stateConf := &retry.StateChangeConf{
Pending: []string{"SETUP"},
Target: []string{"NULL"},
Refresh: func() (interface{}, string, error) {
Expand Down Expand Up @@ -988,7 +983,7 @@ func stopThenWaitServerInstance(config *conn.ProviderConfig, id string) error {
return err
}

stateConf = &resource.StateChangeConf{
stateConf = &retry.StateChangeConf{
Pending: []string{"RUN"},
Target: []string{"NSTOP"},
Refresh: func() (interface{}, string, error) {
Expand All @@ -1013,7 +1008,7 @@ func stopThenWaitServerInstance(config *conn.ProviderConfig, id string) error {
}

func detachThenWaitServerInstance(config *conn.ProviderConfig, id string) error {
stateConf := &resource.StateChangeConf{
stateConf := &retry.StateChangeConf{
Pending: []string{"SETUP"},
Target: []string{"NULL"},
Refresh: func() (interface{}, string, error) {
Expand Down Expand Up @@ -1080,7 +1075,7 @@ func terminateThenWaitServerInstance(config *conn.ProviderConfig, id string) err
return err
}

stateConf := &resource.StateChangeConf{
stateConf := &retry.StateChangeConf{
Pending: []string{"NSTOP"},
Target: []string{"TERMINATED"},
Refresh: func() (interface{}, string, error) {
Expand Down Expand Up @@ -1307,30 +1302,74 @@ func disconnectClassicBlockStorage(config *conn.ProviderConfig, storage *BlockSt
return nil
}

func waitForDisconnectBlockStorage(config *conn.ProviderConfig, d *schema.ResourceData, storage *BlockStorage) error {
return resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
blockStorage, err := GetBlockStorage(config, *storage.BlockStorageInstanceNo)
if err != nil {
return resource.RetryableError(err)
}
if *blockStorage.Status != BlockStorageStatusCodeCreate {
return resource.RetryableError(fmt.Errorf("sill connected block storage(%s)", *blockStorage.BlockStorageInstanceNo))
}
return nil
})
func waitForDisconnectBlockStorage(config *conn.ProviderConfig, no string) error {
stateConf := &retry.StateChangeConf{
Pending: []string{BlockStorageStatusNameAttach},
Target: []string{BlockStorageStatusNameDetach},
Refresh: func() (interface{}, string, error) {
resp, err := GetBlockStorage(config, no)
if err != nil {
return 0, "", err
}

if resp == nil {
return 0, "", fmt.Errorf("GetBlockStorage is nil")
}

if *resp.StatusName == BlockStorageStatusNameAttach {
return resp, BlockStorageStatusNameAttach, nil
} else if *resp.StatusName == BlockStorageStatusNameDetach {
return resp, BlockStorageStatusNameDetach, nil
}

return 0, "", fmt.Errorf("error occurred while waiting to detached")
},
Timeout: 6 * conn.DefaultTimeout,
Delay: 2 * time.Second,
MinTimeout: 3 * time.Second,
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for BlockStorage (%s) to become available: %s", no, err)
}

return nil
}

func waitForAttachedBlockStorage(config *conn.ProviderConfig, d *schema.ResourceData, storage *BlockStorage) error {
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
blockStorage, err := GetBlockStorage(config, *storage.BlockStorageInstanceNo)
if err != nil {
return resource.RetryableError(err)
}
if *blockStorage.StatusName != BlockStorageStatusNameAttach {
return resource.RetryableError(fmt.Errorf("sill optimizing block storage(%s)", *blockStorage.BlockStorageInstanceNo))
}
return nil
})
func waitForAttachedBlockStorage(config *conn.ProviderConfig, no string) error {
stateConf := &retry.StateChangeConf{
Pending: []string{BlockStorageStatusNameInit, BlockStorageStatusNameOptimizing},
Target: []string{BlockStorageStatusNameAttach},
Refresh: func() (interface{}, string, error) {
resp, err := GetBlockStorage(config, no)
if err != nil {
return 0, "", err
}

if resp == nil {
return 0, "", fmt.Errorf("GetBlockStorage is nil")
}

if *resp.StatusName == BlockStorageStatusNameInit {
return resp, BlockStorageStatusNameInit, nil
} else if *resp.StatusName == BlockStorageStatusNameOptimizing {
return resp, BlockStorageStatusNameOptimizing, nil
} else if *resp.StatusName == BlockStorageStatusNameAttach {
return resp, BlockStorageStatusNameAttach, nil
}

return 0, "", fmt.Errorf("error occurred while waiting to attached")
},
Timeout: 6 * conn.DefaultTimeout,
Delay: 2 * time.Second,
MinTimeout: 3 * time.Second,
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for BlockStorage (%s) to become available: %s", no, err)
}

return nil
}

func getServerZoneNo(config *conn.ProviderConfig, serverInstanceNo string) (string, error) {
Expand All @@ -1352,7 +1391,6 @@ type ServerInstance struct {
ServerDescription *string `json:"description,omitempty"`
LoginKeyName *string `json:"login_key_name,omitempty"`
IsProtectServerTermination *bool `json:"is_protect_server_termination,omitempty"`
AssociateWithPublicIp *bool `json:"associate_with_public_ip,omitempty"`
FeeSystemTypeCode *string `json:"fee_system_type_code,omitempty"`
UserData *string `json:"user_data,omitempty"`
RaidTypeName *string `json:"raid_type_name,omitempty"`
Expand Down
9 changes: 8 additions & 1 deletion internal/service/server/server_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,17 @@ resource "ncloud_subnet" "test" {
usage_type = "GEN"
}
data "ncloud_server_image_numbers" "server_images" {
filter {
name = "name"
values = ["ubuntu-22.04-base"]
}
}
resource "ncloud_server" "server" {
subnet_no = ncloud_subnet.test.id
name = "%[1]s"
server_image_number = "25495367"
server_image_number = data.ncloud_server_image_numbers.server_images.image_number_list.0.server_image_number
server_spec_code = "s2-g3"
login_key_name = ncloud_login_key.loginkey.key_name
}
Expand Down
Loading

0 comments on commit 502fbaa

Please sign in to comment.