Skip to content

Commit

Permalink
cce node add k8s_tags and user_tags (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 authored Oct 19, 2020
1 parent 1de85b5 commit 0558326
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 36 deletions.
79 changes: 65 additions & 14 deletions flexibleengine/resource_flexibleengine_cce_node_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/clusters"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes"
"github.com/huaweicloud/golangsdk/openstack/common/tags"
)

func resourceCCENodeV3() *schema.Resource {
Expand Down Expand Up @@ -56,11 +57,19 @@ func resourceCCENodeV3() *schema.Resource {
Optional: true,
Computed: true,
},
"labels": {
"labels": { //(k8s_tags)
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
},
"server_id": {
Type: schema.TypeString,
Computed: true,
},
"annotations": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -245,13 +254,6 @@ func resourceCCENodeV3() *schema.Resource {
}
}

func resourceCCENodeLabelsV2(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("labels").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}
func resourceCCENodeAnnotationsV2(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("annotations").(map[string]interface{}) {
Expand Down Expand Up @@ -290,6 +292,20 @@ func resourceCCEEipIDs(d *schema.ResourceData) []string {
}
return id
}

func resourceCCENodeK8sTags(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("labels").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}

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

func resourceCCENodeV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
nodeClient, err := config.cceV3Client(GetRegion(d, config))
Expand All @@ -310,7 +326,6 @@ func resourceCCENodeV3Create(d *schema.ResourceData, meta interface{}) error {
ApiVersion: "v3",
Metadata: nodes.CreateMetaData{
Name: d.Get("name").(string),
Labels: resourceCCENodeLabelsV2(d),
Annotations: resourceCCENodeAnnotationsV2(d),
},
Spec: nodes.Spec{
Expand All @@ -320,6 +335,8 @@ func resourceCCENodeV3Create(d *schema.ResourceData, meta interface{}) error {
Login: nodes.LoginSpec{SshKey: d.Get("key_pair").(string)},
RootVolume: resourceCCERootVolume(d),
DataVolumes: resourceCCEDataVolume(d),
UserTags: resourceCCENodeUserTags(d),
K8sTags: resourceCCENodeK8sTags(d),
PublicIP: nodes.PublicIPSpec{
Ids: resourceCCEEipIDs(d),
Count: d.Get("eip_count").(int),
Expand Down Expand Up @@ -475,6 +492,27 @@ func resourceCCENodeV3Read(d *schema.ResourceData, meta interface{}) error {
d.Set("private_ip", s.Status.PrivateIP)
d.Set("public_ip", s.Status.PublicIP)

serverId := s.Status.ServerID
d.Set("server_id", serverId)

// fetch tags from ECS instance
computeClient, err := config.loadECSV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Flexibleengine compute client: %s", err)
}

resourceTags, err := tags.Get(computeClient, "servers", serverId).Extract()
if err != nil {
return fmt.Errorf("Error fetching Flexibleengine instance tags: %s", err)
}

tagmap := tagsToMap(resourceTags.Tags)
//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 of cce node: %s", err)
}

return nil
}

Expand All @@ -489,14 +527,27 @@ func resourceCCENodeV3Update(d *schema.ResourceData, meta interface{}) error {

if d.HasChange("name") {
updateOpts.Metadata.Name = d.Get("name").(string)
}

clusterid := d.Get("cluster_id").(string)
_, err = nodes.Update(nodeClient, clusterid, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating flexibleengine Node: %s", err)
clusterid := d.Get("cluster_id").(string)
_, err = nodes.Update(nodeClient, clusterid, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating flexibleengine Node: %s", err)
}
}

// update tags
if d.HasChange("tags") {
computeClient, err := config.loadECSV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Flexibleengine compute client: %s", err)
}

serverId := d.Get("server_id").(string)
tagErr := UpdateResourceTags(computeClient, d, "servers", serverId)
if tagErr != nil {
return fmt.Errorf("Error updateing tags of cce node %s: %s", d.Id(), tagErr)
}
}
return resourceCCENodeV3Read(d, meta)
}

Expand Down
2 changes: 1 addition & 1 deletion flexibleengine/resource_flexibleengine_css_cluster_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func resourceCssClusterV1Update(d *schema.ResourceData, meta interface{}) error
}

if d.HasChange("tags") {
tagErr := UpdateResourceTags(client, d, "css-cluster")
tagErr := UpdateResourceTags(client, d, "css-cluster", d.Id())
if tagErr != nil {
return fmt.Errorf("Error updating tags of CSS cluster:%s, err:%s", d.Id(), tagErr)
}
Expand Down
6 changes: 3 additions & 3 deletions flexibleengine/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func tagsSchema() *schema.Schema {

// UpdateResourceTags is a helper to update the tags for a resource.
// It expects the tags field to be named "tags"
func UpdateResourceTags(conn *golangsdk.ServiceClient, d *schema.ResourceData, resourceType string) error {
func UpdateResourceTags(conn *golangsdk.ServiceClient, d *schema.ResourceData, resourceType, id string) error {
if d.HasChange("tags") {
oRaw, nRaw := d.GetChange("tags")
oMap := oRaw.(map[string]interface{})
Expand All @@ -26,7 +26,7 @@ func UpdateResourceTags(conn *golangsdk.ServiceClient, d *schema.ResourceData, r
// remove old tags
if len(oMap) > 0 {
taglist := expandResourceTags(oMap)
err := tags.Delete(conn, resourceType, d.Id(), taglist).ExtractErr()
err := tags.Delete(conn, resourceType, id, taglist).ExtractErr()
if err != nil {
return err
}
Expand All @@ -35,7 +35,7 @@ func UpdateResourceTags(conn *golangsdk.ServiceClient, d *schema.ResourceData, r
// set new tags
if len(nMap) > 0 {
taglist := expandResourceTags(nMap)
err := tags.Create(conn, resourceType, d.Id(), taglist).ExtractErr()
err := tags.Create(conn, resourceType, id, taglist).ExtractErr()
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/terraform-providers/terraform-provider-flexibleengine

go 1.14

require (
github.com/aws/aws-sdk-go v1.22.0
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/huaweicloud/golangsdk v0.0.0-20200901024739-8d01f23cf75e
github.com/huaweicloud/golangsdk v0.0.0-20201013050319-683f75dc8263
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/mitchellh/go-homedir v1.1.0
Expand Down
16 changes: 2 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKe
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huaweicloud/golangsdk v0.0.0-20200514023300-6a3455c8bf4f h1:CGRXQmJk5T7FMfKiJKQhRVRnxJvKCyQ2KZjoWfaN88s=
github.com/huaweicloud/golangsdk v0.0.0-20200514023300-6a3455c8bf4f/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200706081112-ca97f4502e33 h1:ZIk5YmC7v05x+HoQZksJI95PK6hs9t2VBta+8qPEu+A=
github.com/huaweicloud/golangsdk v0.0.0-20200706081112-ca97f4502e33/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200715023612-8da2d504ff78 h1:HF4ahjN+FUAmdBAVtCgQF2lQS7ClX6FUxKeMlSlIv1M=
github.com/huaweicloud/golangsdk v0.0.0-20200715023612-8da2d504ff78/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200725024337-bef093aea95e h1:v8VaVyyiDDRYiMYj/ywbGhaCv8UZwRQ3tabSGX8cit4=
github.com/huaweicloud/golangsdk v0.0.0-20200725024337-bef093aea95e/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200725094037-6532743262f0 h1:ZIG/cjRR5ir0JP1R88tKcW/QERqeIqJE5f8YSo38kPI=
github.com/huaweicloud/golangsdk v0.0.0-20200725094037-6532743262f0/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200818112044-35329fdf4d04 h1:mrqU5znCeRRcN4CInII8AOlDpqWd407Gaw3w1cu71Hs=
github.com/huaweicloud/golangsdk v0.0.0-20200818112044-35329fdf4d04/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200901024739-8d01f23cf75e h1:wvr6UKMzpbGlQfr+ua8rK1g2vCSbEKqF09+MoEHV3O4=
github.com/huaweicloud/golangsdk v0.0.0-20200901024739-8d01f23cf75e/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20201013050319-683f75dc8263 h1:PaCpxjILNlQJkHqbETpTqjbcejjlphCeYMcxWAVnxKk=
github.com/huaweicloud/golangsdk v0.0.0-20201013050319-683f75dc8263/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a h1:FyS/ubzBR5xJlnJGRTwe7GUHpJOR4ukYK3y+LFNffuA=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a/go.mod h1:uoIMjNxUfXi48Ci40IXkPRbghZ1vbti6v9LCbNqRgHY=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/huaweicloud/golangsdk/openstack/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ github.com/hashicorp/terraform-plugin-sdk/plugin
github.com/hashicorp/terraform-plugin-sdk/terraform
# github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/hashicorp/yamux
# github.com/huaweicloud/golangsdk v0.0.0-20200901024739-8d01f23cf75e
# github.com/huaweicloud/golangsdk v0.0.0-20201013050319-683f75dc8263
## explicit
github.com/huaweicloud/golangsdk
github.com/huaweicloud/golangsdk/internal
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/cce_nodes_v3.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ The following arguments are supported:

* `name` - (Optional) Node Name.

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

* `tags` - (Optional) VM tag, key/value pair format.

* `annotations` - (Optional) Node annotation, key/value pair format. Changing this parameter will create a new resource.

Expand Down

0 comments on commit 0558326

Please sign in to comment.