Skip to content

Commit

Permalink
suppport remove in cce node
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 committed Jul 27, 2021
1 parent 6fb8d44 commit 18bcc96
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 35 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/huaweicloud/golangsdk v0.0.0-20210722084309-4039dc70b08a
github.com/huaweicloud/golangsdk v0.0.0-20210727014057-a2f63cd07e36
github.com/jen20/awspolicyequivalence v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
github.com/stretchr/testify v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
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/huaweicloud/golangsdk v0.0.0-20210722084309-4039dc70b08a h1:pkd1CUV6rI7CCzlZ+pQRQa8UdSOgxAwn2GtLZFsI34c=
github.com/huaweicloud/golangsdk v0.0.0-20210722084309-4039dc70b08a/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210727014057-a2f63cd07e36 h1:2oW1oYgSD2XWc3NymcBXrKm0laRiYXw2hCyMNML9j9E=
github.com/huaweicloud/golangsdk v0.0.0-20210727014057-a2f63cd07e36/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down
86 changes: 60 additions & 26 deletions huaweicloud/resource_huaweicloud_cce_node_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ func ResourceCCENodeV3() *schema.Resource {
Optional: true,
ForceNew: true,
},
"keep_ecs": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -760,43 +765,72 @@ func resourceCCENodeV3Delete(d *schema.ResourceData, meta interface{}) error {
}

clusterid := d.Get("cluster_id").(string)
// for prePaid node, firstly, we should unsubscribe the ecs server, and then delete it
if d.Get("charging_mode").(string) == "prePaid" || d.Get("billing_mode").(int) == 2 {
serverID := d.Get("server_id").(string)
publicIP := d.Get("public_ip").(string)
// remove node without deleting ecs
if d.Get("keep_ecs").(bool) {
var removeOpts nodes.RemoveOpts

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),
},
}
}
removeOpts.Spec.Login = loginSpec

resourceIDs := make([]string, 0, 2)
if serverID != "" {
resourceIDs = append(resourceIDs, serverID)
nodeItem := nodes.NodeItem{
Uid: d.Id(),
}
removeOpts.Spec.Nodes = append(removeOpts.Spec.Nodes, nodeItem)

// unsubscribe the eip if necessary
if _, ok := d.GetOk("iptype"); ok && publicIP != "" {
eipClient, err := config.NetworkingV1Client(GetRegion(d, config))
if err != nil {
return fmtp.Errorf("Error creating networking client: %s", err)
err = nodes.Remove(nodeClient, clusterid, removeOpts).ExtractErr()
if err != nil {
return fmtp.Errorf("Error removing HuaweiCloud CCE node: %s", err)
}
} else {
// for prePaid node, firstly, we should unsubscribe the ecs server, and then delete it
if d.Get("charging_mode").(string) == "prePaid" || d.Get("billing_mode").(int) == 2 {
serverID := d.Get("server_id").(string)
publicIP := d.Get("public_ip").(string)

resourceIDs := make([]string, 0, 2)
if serverID != "" {
resourceIDs = append(resourceIDs, serverID)
}

if eipID, err := getEipIDbyAddress(eipClient, publicIP); err == nil {
resourceIDs = append(resourceIDs, eipID)
} else {
logp.Printf("[WARN] Error fetching EIP ID of CCE Node (%s): %s", d.Id(), err)
// unsubscribe the eip if necessary
if _, ok := d.GetOk("iptype"); ok && publicIP != "" {
eipClient, err := config.NetworkingV1Client(GetRegion(d, config))
if err != nil {
return fmtp.Errorf("Error creating networking client: %s", err)
}

if eipID, err := getEipIDbyAddress(eipClient, publicIP); err == nil {
resourceIDs = append(resourceIDs, eipID)
} else {
logp.Printf("[WARN] Error fetching EIP ID of CCE Node (%s): %s", d.Id(), err)
}
}
}

if len(resourceIDs) > 0 {
if err := UnsubscribePrePaidResource(d, config, resourceIDs); err != nil {
return fmtp.Errorf("Error unsubscribing HuaweiCloud CCE node: %s", err)
if len(resourceIDs) > 0 {
if err := UnsubscribePrePaidResource(d, config, resourceIDs); err != nil {
return fmtp.Errorf("Error unsubscribing HuaweiCloud CCE node: %s", err)
}
}
}
}

time.Sleep(60 * time.Second) //lintignore:R018
err = nodes.Delete(nodeClient, clusterid, d.Id()).ExtractErr()
if err != nil {
return fmtp.Errorf("Error deleting HuaweiCloud CCE node: %s", err)
time.Sleep(60 * time.Second) //lintignore:R018
err = nodes.Delete(nodeClient, clusterid, d.Id()).ExtractErr()
if err != nil {
return fmtp.Errorf("Error deleting HuaweiCloud CCE node: %s", err)
}
}

stateConf := &resource.StateChangeConf{
Pending: []string{"Deleting"},
Target: []string{"Deleted"},
Expand Down
6 changes: 5 additions & 1 deletion vendor/github.com/huaweicloud/golangsdk/errors.go

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

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

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

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 @@ -267,7 +267,7 @@ github.com/hashicorp/terraform-svchost/auth
github.com/hashicorp/terraform-svchost/disco
# github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/hashicorp/yamux
# github.com/huaweicloud/golangsdk v0.0.0-20210722084309-4039dc70b08a
# github.com/huaweicloud/golangsdk v0.0.0-20210727014057-a2f63cd07e36
## explicit
github.com/huaweicloud/golangsdk
github.com/huaweicloud/golangsdk/internal
Expand Down

0 comments on commit 18bcc96

Please sign in to comment.