github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/ecs/v1/cloudservers/results_order.go (about)

     1  package cloudservers
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  )
     9  
    10  type OrderResponse struct {
    11  	OrderID string `json:"order_id"`
    12  	JobID   string `json:"job_id"`
    13  }
    14  
    15  type OrderStatus struct {
    16  	ErrorCode string     `json:"error_code"`
    17  	ErrorMsg  string     `json:"error_msg"`
    18  	Resources []Resource `json:"resources"`
    19  }
    20  
    21  type Resource struct {
    22  	Status     int    `json:"status"`
    23  	ResourceId string `json:"resourceId"`
    24  }
    25  
    26  type DeleteOrderResponse struct {
    27  	OrderIDs []string `json:"orderIds"`
    28  }
    29  
    30  type OrderResult struct {
    31  	golangsdk.Result
    32  }
    33  
    34  type DeleteOrderResult struct {
    35  	golangsdk.Result
    36  }
    37  
    38  func (r OrderResult) ExtractOrderResponse() (*OrderResponse, error) {
    39  	order := new(OrderResponse)
    40  	err := r.ExtractInto(order)
    41  	return order, err
    42  }
    43  
    44  func (r DeleteOrderResult) ExtractDeleteOrderResponse() (*DeleteOrderResponse, error) {
    45  	order := new(DeleteOrderResponse)
    46  	err := r.ExtractInto(order)
    47  	return order, err
    48  }
    49  
    50  func (r OrderResult) ExtractOrderStatus() (*OrderStatus, error) {
    51  	order := new(OrderStatus)
    52  	err := r.ExtractInto(order)
    53  	return order, err
    54  }
    55  
    56  func WaitForOrderSuccess(client *golangsdk.ServiceClient, secs int, orderID string) error {
    57  	return golangsdk.WaitFor(secs, func() (bool, error) {
    58  		order := new(OrderStatus)
    59  		_, err := client.Get(orderURL(client, orderID), &order, nil)
    60  		if err != nil {
    61  			return false, err
    62  		}
    63  		time.Sleep(5 * time.Second)
    64  
    65  		if len(order.Resources) == 0 {
    66  			return false, nil
    67  		}
    68  		instance := order.Resources[0]
    69  
    70  		if instance.Status == 1 {
    71  			return true, nil
    72  		}
    73  		if instance.Status == 2 {
    74  			err = fmt.Errorf("Order failed: %s.", orderID)
    75  			return false, err
    76  		}
    77  
    78  		return false, nil
    79  	})
    80  }
    81  
    82  func WaitForOrderDeleteSuccess(client *golangsdk.ServiceClient, secs int, orderID string) error {
    83  	return golangsdk.WaitFor(secs, func() (bool, error) {
    84  		order := new(OrderStatus)
    85  		_, err := client.Get(orderURL(client, orderID), &order, nil)
    86  		if err != nil {
    87  			return false, err
    88  		}
    89  		time.Sleep(5 * time.Second)
    90  
    91  		if len(order.Resources) == 0 {
    92  			return false, nil
    93  		}
    94  		instance := order.Resources[0]
    95  
    96  		if instance.Status == 8 {
    97  			return true, nil
    98  		}
    99  		if instance.Status == 2 {
   100  			err = fmt.Errorf("Order failed: %s.", orderID)
   101  			return false, err
   102  		}
   103  
   104  		return false, nil
   105  	})
   106  }
   107  
   108  func GetOrderResource(client *golangsdk.ServiceClient, orderID string) (interface{}, error) {
   109  	order := new(OrderStatus)
   110  	_, err := client.Get(orderURL(client, orderID), &order, nil)
   111  	if err != nil {
   112  		return false, err
   113  	}
   114  	instance := order.Resources[0]
   115  	if instance.Status == 1 {
   116  		if e := instance.ResourceId; e != "" {
   117  			return e, nil
   118  		}
   119  	}
   120  
   121  	return nil, fmt.Errorf("Get Order resource ID error.")
   122  }