github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  // deleteRunningServer terminates the server and waits until it is removed.
    22  func deleteRunningServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
    23  	err := scaleway.PostServerAction(server.Identifier, "terminate")
    24  
    25  	if err != nil {
    26  		if serr, ok := err.(api.ScalewayAPIError); ok {
    27  			if serr.StatusCode == 404 {
    28  				return nil
    29  			}
    30  		}
    31  
    32  		return err
    33  	}
    34  
    35  	return resource.Retry(20*time.Minute, func() *resource.RetryError {
    36  		_, err := scaleway.GetServer(server.Identifier)
    37  
    38  		if err == nil {
    39  			return resource.RetryableError(fmt.Errorf("Waiting for server %q to be deleted", server.Identifier))
    40  		}
    41  
    42  		if serr, ok := err.(api.ScalewayAPIError); ok {
    43  			if serr.StatusCode == 404 {
    44  				return nil
    45  			}
    46  		}
    47  
    48  		return resource.RetryableError(err)
    49  	})
    50  }
    51  
    52  // deleteStoppedServer needs to cleanup attached root volumes. this is not done
    53  // automatically by Scaleway
    54  func deleteStoppedServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
    55  	if err := scaleway.DeleteServer(server.Identifier); err != nil {
    56  		return err
    57  	}
    58  
    59  	if rootVolume, ok := server.Volumes["0"]; ok {
    60  		if err := scaleway.DeleteVolume(rootVolume.Identifier); err != nil {
    61  			return err
    62  		}
    63  	}
    64  	return nil
    65  }
    66  
    67  // NOTE copied from github.com/scaleway/scaleway-cli/pkg/api/helpers.go
    68  // the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway
    69  
    70  func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error {
    71  	return resource.Retry(20*time.Minute, func() *resource.RetryError {
    72  		s, err := scaleway.GetServer(serverID)
    73  
    74  		if err != nil {
    75  			return resource.NonRetryableError(err)
    76  		}
    77  
    78  		if s.State != targetState {
    79  			return resource.RetryableError(fmt.Errorf("Waiting for server to enter %q state", targetState))
    80  		}
    81  
    82  		return nil
    83  	})
    84  }