Skip to content

Commit

Permalink
support tags in cce node pool (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 authored Apr 2, 2021
1 parent acc4749 commit 814ef9f
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/resources/cce_node_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ The following arguments are supported:

* `labels` - (Optional, Map, ForceNew) Tags of a Kubernetes node, key/value pair format. Changing this parameter will create a new resource.

* `tags` - (Optional, Map) Tags of a VM node, key/value pair format.

* `root_volume` - (Required, List, ForceNew) It corresponds to the system disk related configuration. Changing this parameter will create a new resource.

* `data_volumes` - (Required, List, ForceNew) Represents the data disk to be created. Changing this parameter will create a new resource.
Expand Down
35 changes: 35 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/nodepools"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes"
"github.com/huaweicloud/golangsdk/openstack/common/tags"
)

func ResourceCCENodePool() *schema.Resource {
Expand Down Expand Up @@ -167,6 +168,7 @@ func ResourceCCENodePool() *schema.Resource {
},
}},
},
"tags": tagsSchema(),
"billing_mode": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -250,6 +252,11 @@ func ResourceCCENodePool() *schema.Resource {
}
}

func resourceCCENodePoolTags(d *schema.ResourceData) []tags.ResourceTag {
tagRaw := d.Get("tags").(map[string]interface{})
return expandResourceTags(tagRaw)
}

func resourceCCENodePoolCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
nodePoolClient, err := config.CceV3Client(GetRegion(d, config))
Expand Down Expand Up @@ -296,6 +303,7 @@ func resourceCCENodePoolCreate(d *schema.ResourceData, meta interface{}) error {
},
ExtendParam: resourceCCEExtendParam(d),
Taints: resourceCCETaint(d),
UserTags: resourceCCENodePoolTags(d),
},
Autoscaling: nodepools.AutoscalingSpec{
Enable: d.Get("scall_enable").(bool),
Expand Down Expand Up @@ -429,6 +437,13 @@ func resourceCCENodePoolRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("[DEBUG] Error saving root Volume to state for HuaweiCloud Node Pool (%s): %s", d.Id(), err)
}

tagmap := tagsToMap(s.Spec.NodeTemplate.UserTags)
// ignore "CCE-Dynamic-Provisioning-Node"
delete(tagmap, "CCE-Dynamic-Provisioning-Node")
if err := d.Set("tags", tagmap); err != nil {
return fmt.Errorf("Error saving tags to state for CCE Node Pool(%s): %s", d.Id(), err)
}

d.Set("status", s.Status.Phase)

return nil
Expand All @@ -442,6 +457,17 @@ func resourceCCENodePoolUpdate(d *schema.ResourceData, meta interface{}) error {
}

initialNodeCount := d.Get("initial_node_count").(int)
var loginSpec nodes.LoginSpec
if hasFilledOpt(d, "key_pair") {
loginSpec = nodes.LoginSpec{SshKey: d.Get("key_pair").(string)}
} else if hasFilledOpt(d, "password") {
loginSpec = nodes.LoginSpec{
UserPassword: nodes.UserPassword{
Username: "root",
Password: d.Get("password").(string),
},
}
}

updateOpts := nodepools.UpdateOpts{
Kind: "NodePool",
Expand All @@ -458,6 +484,15 @@ func resourceCCENodePoolUpdate(d *schema.ResourceData, meta interface{}) error {
ScaleDownCooldownTime: d.Get("scale_down_cooldown_time").(int),
Priority: d.Get("priority").(int),
},
NodeTemplate: nodes.Spec{
Flavor: d.Get("flavor_id").(string),
Az: d.Get("availability_zone").(string),
Login: loginSpec,
RootVolume: resourceCCERootVolume(d),
DataVolumes: resourceCCEDataVolume(d),
Count: 1,
UserTags: resourceCCENodePoolTags(d),
},
Type: d.Get("type").(string),
},
}
Expand Down
107 changes: 107 additions & 0 deletions huaweicloud/resource_huaweicloud_cce_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ func TestAccCCENodePool_basic(t *testing.T) {
})
}

func TestAccCCENodePool_tags(t *testing.T) {
var nodePool nodepools.NodePool

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "huaweicloud_cce_node_pool.test"
//clusterName here is used to provide the cluster id to fetch cce node pool.
clusterName := "huaweicloud_cce_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCCENodePoolDestroy,
Steps: []resource.TestStep{
{
Config: testAccCCENodePool_tags(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCENodePoolExists(resourceName, clusterName, &nodePool),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.test1", "val1"),
resource.TestCheckResourceAttr(resourceName, "tags.test2", "val2"),
),
},
{
Config: testAccCCENodePool_tags_update(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCENodePoolExists(resourceName, clusterName, &nodePool),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.test1", "val1_update"),
resource.TestCheckResourceAttr(resourceName, "tags.test2_update", "val2_update"),
),
},
},
})
}

func testAccCheckCCENodePoolDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
cceClient, err := config.CceV3Client(HW_REGION_NAME)
Expand Down Expand Up @@ -258,3 +293,75 @@ resource "huaweicloud_cce_node_pool" "test" {
}
`, testAccCCENodePool_Base(rName), rName)
}

func testAccCCENodePool_tags(rName string) string {
return fmt.Sprintf(`
%s
resource "huaweicloud_cce_node_pool" "test" {
cluster_id = huaweicloud_cce_cluster.test.id
name = "%s"
os = "EulerOS 2.5"
flavor_id = "s6.large.2"
initial_node_count = 1
availability_zone = data.huaweicloud_availability_zones.test.names[0]
key_pair = huaweicloud_compute_keypair.test.name
scall_enable = false
min_node_count = 0
max_node_count = 0
scale_down_cooldown_time = 0
priority = 0
type = "vm"
root_volume {
size = 40
volumetype = "SSD"
}
data_volumes {
size = 100
volumetype = "SSD"
}
tags = {
test1 = "val1"
test2 = "val2"
}
}
`, testAccCCENodePool_Base(rName), rName)
}

func testAccCCENodePool_tags_update(rName string) string {
return fmt.Sprintf(`
%s
resource "huaweicloud_cce_node_pool" "test" {
cluster_id = huaweicloud_cce_cluster.test.id
name = "%s"
os = "EulerOS 2.5"
flavor_id = "s6.large.2"
initial_node_count = 1
availability_zone = data.huaweicloud_availability_zones.test.names[0]
key_pair = huaweicloud_compute_keypair.test.name
scall_enable = false
min_node_count = 0
max_node_count = 0
scale_down_cooldown_time = 0
priority = 0
type = "vm"
root_volume {
size = 40
volumetype = "SSD"
}
data_volumes {
size = 100
volumetype = "SSD"
}
tags = {
test1 = "val1_update"
test2_update = "val2_update"
}
}
`, testAccCCENodePool_Base(rName), rName)
}

0 comments on commit 814ef9f

Please sign in to comment.