Skip to content

Commit

Permalink
Fix PrePaid ECS instance issue (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored Nov 13, 2019
1 parent 93b2d63 commit a7f45ee
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/huaweicloud/golangsdk v0.0.0-20191105032058-c1a523be9414
github.com/huaweicloud/golangsdk v0.0.0-20191107120439-abaa653f0fb5
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/mitchellh/go-homedir v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +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-20191105032058-c1a523be9414/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20191107120439-abaa653f0fb5 h1:yr8Dlj8uPOLQ1ZYZSwHr9IVVOpVixeRvInJIU/7xQ4g=
github.com/huaweicloud/golangsdk v0.0.0-20191107120439-abaa653f0fb5/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
6 changes: 6 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,12 @@ func (c *Config) CdnV1Client(region string) (*golangsdk.ServiceClient, error) {
})
}

func (c *Config) BssV1Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewBSSV1(c.HwClient, golangsdk.EndpointOpts{
Availability: c.getHwEndpointType(),
})
}

func (c *Config) sdkClient(region, serviceType string, level string) (*golangsdk.ServiceClient, error) {
client := c.HwClient
if level == serviceDomainLevel {
Expand Down
131 changes: 96 additions & 35 deletions huaweicloud/resource_huaweicloud_ecs_instance_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,38 +244,61 @@ func resourceEcsInstanceV1Create(d *schema.ResourceData, meta interface{}) error

log.Printf("[DEBUG] Create Options: %#v", createOpts)

n, err := cloudservers.Create(computeClient, createOpts).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud server: %s", err)
}
var instance_id string
if d.Get("charging_mode") == "prePaid" {
bssV1Client, err := config.BssV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud bss V1 client: %s", err)
}
n, err := cloudservers.CreatePrePaid(computeClient, createOpts).ExtractOrderResponse()
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud server: %s", err)
}

if err := cloudservers.WaitForJobSuccess(computeV1Client, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
}
if err := cloudservers.WaitForOrderSuccess(bssV1Client, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.OrderID); err != nil {
return err
}

entity, err := cloudservers.GetJobEntity(computeV1Client, n.JobID, "server_id")
if err != nil {
return err
resource, err := cloudservers.GetOrderResource(bssV1Client, n.OrderID)
if err != nil {
return err
}
instance_id = resource.(string)
} else {
n, err := cloudservers.Create(computeClient, createOpts).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud server: %s", err)
}

if err := cloudservers.WaitForJobSuccess(computeV1Client, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
}

entity, err := cloudservers.GetJobEntity(computeV1Client, n.JobID, "server_id")
if err != nil {
return err
}
instance_id = entity.(string)
}

if id, ok := entity.(string); ok {
d.SetId(id)
if instance_id != "" {
d.SetId(instance_id)

if hasFilledOpt(d, "tags") {
tagmap := d.Get("tags").(map[string]interface{})
log.Printf("[DEBUG] Setting tags: %v", tagmap)
err = setTagForInstance(d, meta, id, tagmap)
err = setTagForInstance(d, meta, instance_id, tagmap)
if err != nil {
log.Printf("[WARN] Error setting tags of instance:%s, err=%s", id, err)
log.Printf("[WARN] Error setting tags of instance:%s, err=%s", instance_id, err)
}
}

if hasFilledOpt(d, "auto_recovery") {
ar := d.Get("auto_recovery").(bool)
log.Printf("[DEBUG] Set auto recovery of instance to %t", ar)
err = setAutoRecoveryForInstance(d, meta, id, ar)
err = setAutoRecoveryForInstance(d, meta, instance_id, ar)
if err != nil {
log.Printf("[WARN] Error setting auto recovery of instance:%s, err=%s", id, err)
log.Printf("[WARN] Error setting auto recovery of instance:%s, err=%s", instance_id, err)
}
}

Expand Down Expand Up @@ -306,12 +329,6 @@ func resourceEcsInstanceV1Read(d *schema.ResourceData, meta interface{}) error {
d.Set("vpc_id", server.Metadata.VpcID)
d.Set("availability_zone", server.AvailabilityZone)

secGrpNames := []string{}
for _, sg := range server.SecurityGroups {
secGrpNames = append(secGrpNames, sg.Name)
}
d.Set("security_groups", secGrpNames)

// Get the instance network and address information
nics := flattenInstanceNicsV1(d, meta, server.Addresses)
d.Set("nics", nics)
Expand Down Expand Up @@ -489,25 +506,69 @@ func resourceEcsInstanceV1Delete(d *schema.ResourceData, meta interface{}) error
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud compute client: %s", err)
}

var serverRequests []cloudservers.Server
server := cloudservers.Server{
Id: d.Id(),
computeV2Client, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud compute V2 client: %s", err)
}
serverRequests = append(serverRequests, server)

deleteOpts := cloudservers.DeleteOpts{
Servers: serverRequests,
DeleteVolume: d.Get("delete_disks_on_termination").(bool),
if d.Get("charging_mode") == "prePaid" {
bssV1Client, err := config.BssV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud bss V1 client: %s", err)
}

resourceIds := []string{d.Id()}
deleteOrderOpts := cloudservers.DeleteOrderOpts{
ResourceIds: resourceIds,
UnSubType: 1,
}
n, err := cloudservers.DeleteOrder(bssV1Client, deleteOrderOpts).ExtractDeleteOrderResponse()
if err != nil {
return fmt.Errorf("Error deleting HuaweiCloud server: %s", err)
}

if err := cloudservers.WaitForOrderDeleteSuccess(bssV1Client, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.OrderIDs[0]); err != nil {
return err
}
} else {
var serverRequests []cloudservers.Server
server := cloudservers.Server{
Id: d.Id(),
}
serverRequests = append(serverRequests, server)

deleteOpts := cloudservers.DeleteOpts{
Servers: serverRequests,
DeleteVolume: d.Get("delete_disks_on_termination").(bool),
}

n, err := cloudservers.Delete(computeV1Client, deleteOpts).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error deleting HuaweiCloud server: %s", err)
}

if err := cloudservers.WaitForJobSuccess(computeV1Client, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
}
}

n, err := cloudservers.Delete(computeV1Client, deleteOpts).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error deleting HuaweiCloud server: %s", err)
// Wait for the instance to delete before moving on.
log.Printf("[DEBUG] Waiting for instance (%s) to delete", d.Id())

stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE", "SHUTOFF"},
Target: []string{"DELETED", "SOFT_DELETED"},
Refresh: ServerV2StateRefreshFunc(computeV2Client, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

if err := cloudservers.WaitForJobSuccess(computeV1Client, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for instance (%s) to delete: %s",
d.Id(), err)
}

d.SetId("")
Expand Down

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.

16 changes: 16 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.

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

Loading

0 comments on commit a7f45ee

Please sign in to comment.