Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
minosmlee authored Dec 6, 2022
2 parents 4929a23 + f9d6f3e commit 566d3f7
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 11 deletions.
47 changes: 47 additions & 0 deletions docs/data-sources/servers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Data Source: ncloud_servers

Use this data source to get multiple `ncloud_server` ids

## Example Usage

#### Basic usage

```hcl
data "ncloud_servers" "servers" {
ids = [ncloud_server.example1.id, ncloud_server.example2.id]
}
```

#### Usage of using filter

```hcl
data "ncloud_servers" "servers" {
filter {
name = "subnet_no"
values = [ncloud_subnet.example1.id, ncloud_subnet.example2.id]
}
}
```

#### Usage of `ncloud_servers` data source in `ncloud_nas_volume`

```hcl
data "ncloud_servers" "servers" {
ids = [ncloud_server.example1.id, ncloud_server.example2.id]
}
resource "ncloud_nas_volume" "vol" {
volume_name_postfix = "vol"
volume_size = "500"
volume_allotment_protocol_type = "NFS"
server_instance_no_list = data.ncloud_servers.servers.ids
}
```

The following arguments are supported:

* `ids` - (Optional) The set of ID of the Server instances.
* `filter` - (Optional) Custom filter block as described below.
* `name` - (Required) The name of the field to filter by
* `values` - (Required) Set of values that are accepted for the given field.
* `regex` - (Optional) is `values` treated as a regular expression.
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/NaverCloudPlatform/ncloud-sdk-go-v2 v1.5.3 h1:0xKNV67NN6Dnra8Ycvdrd7jig2IrNlQAjsk6Iq9ICPA=
github.com/NaverCloudPlatform/ncloud-sdk-go-v2 v1.5.3/go.mod h1:KWd9AT+YSM6qgsMzPnE23h2/r0bsPSIdJzZIg3BUcfI=
github.com/NaverCloudPlatform/ncloud-sdk-go-v2 v1.5.5 h1:HJijRCjpGph/VXR5X4/u8D0++RmugpGg54KlB5CKJ9M=
github.com/NaverCloudPlatform/ncloud-sdk-go-v2 v1.5.5/go.mod h1:KWd9AT+YSM6qgsMzPnE23h2/r0bsPSIdJzZIg3BUcfI=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
Expand Down
5 changes: 3 additions & 2 deletions ncloud/data_source_ncloud_server_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ func getVpcServerImageProductList(d *schema.ResourceData, config *ProviderConfig
regionCode := config.RegionCode

reqParams := &vserver.GetServerImageProductListRequest{
ProductCode: StringPtrOrNil(d.GetOk("product_code")),
RegionCode: &regionCode,
ProductCode: StringPtrOrNil(d.GetOk("product_code")),
RegionCode: &regionCode,
InfraResourceDetailTypeCode: StringPtrOrNil(d.GetOk("infra_resource_detail_type_code")),
}

if v, ok := d.GetOk("platform_type"); ok {
Expand Down
85 changes: 85 additions & 0 deletions ncloud/data_source_ncloud_servers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ncloud

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func init() {
RegisterDataSource("ncloud_servers", dataSourceNcloudServers())
}

func dataSourceNcloudServers() *schema.Resource {
return &schema.Resource{
Read: dataSourceNcloudServersRead,
Schema: map[string]*schema.Schema{
"ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filter": dataSourceFiltersSchema(),
},
}
}

func dataSourceNcloudServersRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*ProviderConfig)

instances, err := getServerList(d, config)
if err != nil {
return err
}

if len(instances) < 1 {
return fmt.Errorf("no results. there is no available server resource")
}

if values, ok := d.GetOk("ids"); ok {
return readServersIDs(d, values.(*schema.Set).List(), instances)
}

resources := ConvertToArrayMap(instances)
if f, ok := d.GetOk("filter"); ok {
resources = ApplyFilters(f.(*schema.Set), resources, dataSourceNcloudServer().Schema)
}

if len(resources) == 0 {
return fmt.Errorf("no results with filter. there is no available server resource")
}

var ids []string
for _, r := range resources {
for k, v := range r {
if k == "instance_no" {
ids = append(ids, v.(string))
}
}
}

d.SetId(dataResourceIdHash(ids))
d.Set("ids", ids)
return nil
}

func readServersIDs(d *schema.ResourceData, values []interface{}, serverInstances []*ServerInstance) error {
var ids []string
for _, id := range values {
for _, s := range serverInstances {
if *s.ServerInstanceNo == id.(string) {
ids = append(ids, id.(string))
break
}
}
}

if len(values) != len(ids) {
return fmt.Errorf("invalid server id specified")
}

d.SetId(dataResourceIdHash(ids))
d.Set("ids", ids)
return nil
}
144 changes: 144 additions & 0 deletions ncloud/data_source_ncloud_servers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ncloud

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
dataName = "data.ncloud_servers.by_id"
dataName2 = "data.ncloud_servers.by_filter"
serverName = "ncloud_server.test"
)

func TestAccDataSourceNcloudServers_vpc_basic(t *testing.T) {
testServerName := getTestServerName()
testServerName2 := getTestServerName()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceServersVpcConfig(testServerName, testServerName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceID(dataName),
resource.TestMatchResourceAttr(dataName, "id", regexp.MustCompile(`^\d+$`)),
resource.TestCheckResourceAttr(dataName, "ids.#", "2"),
testAccCheckDataSourceID(dataName2),
resource.TestCheckResourceAttr(dataName2, "ids.#", "1"),
resource.TestCheckResourceAttrPair(dataName2, "ids.0", serverName, "id"),
),
},
},
})
}

func TestAccDataSourceNcloudServers_classic_basic(t *testing.T) {
testServerName := getTestServerName()
testServerName2 := getTestServerName()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccClassicProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceServersClassicConfig(testServerName, testServerName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceID(dataName),
resource.TestMatchResourceAttr(dataName, "id", regexp.MustCompile(`^\d+$`)),
resource.TestCheckResourceAttr(dataName, "ids.#", "2"),
testAccCheckDataSourceID(dataName2),
resource.TestCheckResourceAttr(dataName2, "ids.#", "1"),
resource.TestCheckResourceAttrPair(dataName2, "ids.0", serverName, "id"),
),
},
},
})
}

func testAccDataSourceServersVpcConfig(testServerName, testServerName2 string) string {
return fmt.Sprintf(`
resource "ncloud_login_key" "loginkey" {
key_name = "%[1]s-key"
}
resource "ncloud_vpc" "test" {
name = "%[1]s"
ipv4_cidr_block = "10.5.0.0/16"
}
resource "ncloud_subnet" "test" {
vpc_no = ncloud_vpc.test.vpc_no
name = "%[1]s"
subnet = "10.5.0.0/24"
zone = "KR-2"
network_acl_no = ncloud_vpc.test.default_network_acl_no
subnet_type = "PUBLIC"
usage_type = "GEN"
}
resource "ncloud_server" "test" {
subnet_no = ncloud_subnet.test.id
name = "%[1]s"
server_image_product_code = "SW.VSVR.OS.LNX64.CNTOS.0703.B050"
server_product_code = "SVR.VSVR.STAND.C002.M008.NET.HDD.B050.G002"
login_key_name = ncloud_login_key.loginkey.key_name
}
resource "ncloud_server" "test2" {
subnet_no = ncloud_subnet.test.id
name = "%[2]s"
server_image_product_code = "SW.VSVR.OS.LNX64.CNTOS.0703.B050"
server_product_code = "SVR.VSVR.STAND.C002.M008.NET.HDD.B050.G002"
login_key_name = ncloud_login_key.loginkey.key_name
}
data "ncloud_servers" "by_id" {
ids = [ncloud_server.test.id, ncloud_server.test2.id]
}
data "ncloud_servers" "by_filter" {
filter {
name = "instance_no"
values = [ncloud_server.test.id]
}
}
`, testServerName, testServerName2)
}

func testAccDataSourceServersClassicConfig(testServerName, testServerName2 string) string {
return fmt.Sprintf(`
resource "ncloud_login_key" "loginkey" {
key_name = "%[1]s-key"
}
resource "ncloud_server" "test" {
name = "%[1]s"
server_image_product_code = "SPSW0LINUX000139"
server_product_code = "SPSVRSSD00000003"
login_key_name = "${ncloud_login_key.loginkey.key_name}"
}
resource "ncloud_server" "test2" {
name = "%[2]s"
server_image_product_code = "SPSW0LINUX000139"
server_product_code = "SPSVRSSD00000003"
login_key_name = "${ncloud_login_key.loginkey.key_name}"
}
data "ncloud_servers" "by_id" {
ids = [ncloud_server.test.id, ncloud_server.test2.id]
}
data "ncloud_servers" "by_filter" {
filter {
name = "instance_no"
values = [ncloud_server.test.id]
}
}
`, testServerName, testServerName2)
}
35 changes: 28 additions & 7 deletions ncloud/resource_ncloud_access_control_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
"regexp"
"strconv"
"time"
)

Expand All @@ -34,9 +36,12 @@ func resourceNcloudAccessControlGroupRule() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.StringInSlice([]string{"TCP", "UDP", "ICMP"}, false)),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.All(
validation.StringMatch(regexp.MustCompile(`TCP|UDP|ICMP|\b([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-2])\b`), "only TCP, UDP, ICMP and 1-252 are valid values."),
validation.StringNotInSlice([]string{"1", "6", "17"}, false),
)),
},
"port_range": {
Type: schema.TypeString,
Expand Down Expand Up @@ -71,9 +76,12 @@ func resourceNcloudAccessControlGroupRule() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.StringInSlice([]string{"TCP", "UDP", "ICMP"}, false)),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.All(
validation.StringMatch(regexp.MustCompile(`TCP|UDP|ICMP|\b([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-2])\b`), "only TCP, UDP, ICMP and 1-252 are valid values."),
validation.StringNotInSlice([]string{"1", "6", "17"}, false),
)),
},
"port_range": {
Type: schema.TypeString,
Expand Down Expand Up @@ -148,8 +156,15 @@ func resourceNcloudAccessControlGroupRuleRead(d *schema.ResourceData, meta inter
oSet := schema.NewSet(schema.HashResource(resourceNcloudAccessControlGroupRule().Schema["outbound"].Elem.(*schema.Resource)), []interface{}{})

for _, r := range rules {
var protocol string
if allowedProtocolCodes[*r.ProtocolType.Code] {
protocol = *r.ProtocolType.Code
} else {
protocol = strconv.Itoa(int(*r.ProtocolType.Number))
}

m := map[string]interface{}{
"protocol": *r.ProtocolType.Code,
"protocol": protocol,
"port_range": *r.PortRange,
"ip_block": *r.IpBlock,
"source_access_control_group_no": *r.AccessControlGroupSequence,
Expand Down Expand Up @@ -444,3 +459,9 @@ func expandRemoveAccessControlGroupRule(rules []interface{}) []*vserver.RemoveAc

return acgRuleList
}

var allowedProtocolCodes = map[string]bool{
"TCP": true,
"UDP": true,
"ICMP": true,
}

0 comments on commit 566d3f7

Please sign in to comment.