yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/operation.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package google
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/log"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  )
    24  
    25  const (
    26  	OPERATION_STATUS_RUNNING = "RUNNING"
    27  	OPERATION_STATUS_DONE    = "DONE"
    28  )
    29  
    30  type SOperation struct {
    31  	Id            string
    32  	Name          string
    33  	OperationType string
    34  	TargetLink    string
    35  	TargetId      string
    36  	Status        string
    37  	User          string
    38  	Progress      int
    39  	InsertTime    time.Time
    40  	StartTime     time.Time
    41  	EndTime       time.Time
    42  	SelfLink      string
    43  	Region        string
    44  	Kind          string
    45  }
    46  
    47  func (self *SGoogleClient) GetOperation(id string) (*SOperation, error) {
    48  	operation := &SOperation{}
    49  	err := self.GetBySelfId(id, &operation)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return operation, nil
    54  }
    55  
    56  func (self *SGoogleClient) WaitOperation(id string, resource, action string) (string, error) {
    57  	targetLink := ""
    58  	err := cloudprovider.Wait(time.Second*5, time.Minute*5, func() (bool, error) {
    59  		operation, err := self.GetOperation(id)
    60  		if err != nil {
    61  			return false, err
    62  		}
    63  		log.Debugf("%s %s operation status: %s expect %s", action, resource, operation.Status, OPERATION_STATUS_DONE)
    64  		if operation.Status == OPERATION_STATUS_DONE {
    65  			targetLink = operation.TargetLink
    66  			return true, nil
    67  		}
    68  		return false, nil
    69  	})
    70  	return targetLink, err
    71  }
    72  
    73  func (region *SRegion) GetRdsOperation(id string) (*SOperation, error) {
    74  	operation := &SOperation{}
    75  	err := region.rdsGet(id, &operation)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	return operation, nil
    80  }
    81  
    82  func (region *SRegion) WaitRdsOperation(id string, resource, action string) (string, error) {
    83  	targetLink := ""
    84  	err := cloudprovider.Wait(time.Second*5, time.Minute*20, func() (bool, error) {
    85  		operation, err := region.GetRdsOperation(id)
    86  		if err != nil {
    87  			return false, err
    88  		}
    89  		log.Debugf("%s %s operation status: %s expect %s", action, resource, operation.Status, OPERATION_STATUS_DONE)
    90  		if operation.Status == OPERATION_STATUS_DONE {
    91  			targetLink = operation.TargetLink
    92  			return true, nil
    93  		}
    94  		return false, nil
    95  	})
    96  	return targetLink, err
    97  }