github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/scaleway/helpers.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/scaleway/scaleway-cli/pkg/api"
     9  )
    10  
    11  // Bool returns a pointer to of the bool value passed in.
    12  func Bool(val bool) *bool {
    13  	return &val
    14  }
    15  
    16  // String returns a pointer to of the string value passed in.
    17  func String(val string) *string {
    18  	return &val
    19  }
    20  
    21  func validateVolumeType(v interface{}, k string) (ws []string, errors []error) {
    22  	value := v.(string)
    23  	if value != "l_ssd" {
    24  		errors = append(errors, fmt.Errorf("%q must be l_ssd", k))
    25  	}
    26  	return
    27  }
    28  
    29  func validateVolumeSize(v interface{}, k string) (ws []string, errors []error) {
    30  	value := v.(int)
    31  	if value < 1 || value > 150 {
    32  		errors = append(errors, fmt.Errorf("%q be more than 1 and less than 150", k))
    33  	}
    34  	return
    35  }
    36  
    37  // deleteRunningServer terminates the server and waits until it is removed.
    38  func deleteRunningServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
    39  	err := scaleway.PostServerAction(server.Identifier, "terminate")
    40  
    41  	if err != nil {
    42  		if serr, ok := err.(api.ScalewayAPIError); ok {
    43  			if serr.StatusCode == 404 {
    44  				return nil
    45  			}
    46  		}
    47  
    48  		return err
    49  	}
    50  
    51  	return waitForServerState(scaleway, server.Identifier, "stopped")
    52  }
    53  
    54  // deleteStoppedServer needs to cleanup attached root volumes. this is not done
    55  // automatically by Scaleway
    56  func deleteStoppedServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
    57  	if err := scaleway.DeleteServer(server.Identifier); err != nil {
    58  		return err
    59  	}
    60  
    61  	if rootVolume, ok := server.Volumes["0"]; ok {
    62  		if err := scaleway.DeleteVolume(rootVolume.Identifier); err != nil {
    63  			return err
    64  		}
    65  	}
    66  	return nil
    67  }
    68  
    69  // NOTE copied from github.com/scaleway/scaleway-cli/pkg/api/helpers.go
    70  // the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway
    71  
    72  var allStates = []string{"starting", "running", "stopping", "stopped"}
    73  
    74  func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error {
    75  	pending := []string{}
    76  	for _, state := range allStates {
    77  		if state != targetState {
    78  			pending = append(pending, state)
    79  		}
    80  	}
    81  	stateConf := &resource.StateChangeConf{
    82  		Pending: pending,
    83  		Target:  []string{targetState},
    84  		Refresh: func() (interface{}, string, error) {
    85  			s, err := scaleway.GetServer(serverID)
    86  
    87  			if err == nil {
    88  				return 42, s.State, nil
    89  			}
    90  
    91  			if serr, ok := err.(api.ScalewayAPIError); ok {
    92  				if serr.StatusCode == 404 {
    93  					return 42, "stopped", nil
    94  				}
    95  			}
    96  
    97  			return 42, s.State, err
    98  		},
    99  		Timeout:    60 * time.Minute,
   100  		MinTimeout: 5 * time.Second,
   101  		Delay:      5 * time.Second,
   102  	}
   103  	_, err := stateConf.WaitForState()
   104  	return err
   105  }