github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/scaleway/helpers.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"time"
     8  
     9  	"github.com/scaleway/scaleway-cli/pkg/api"
    10  )
    11  
    12  // Bool returns a pointer to of the bool value passed in.
    13  func Bool(val bool) *bool {
    14  	return &val
    15  }
    16  
    17  // String returns a pointer to of the string value passed in.
    18  func String(val string) *string {
    19  	return &val
    20  }
    21  
    22  // DetachIP detaches an IP from a server
    23  func DetachIP(s *api.ScalewayAPI, ipID string) error {
    24  	var update struct {
    25  		Address      string `json:"address"`
    26  		ID           string `json:"id"`
    27  		Organization string `json:"organization"`
    28  	}
    29  
    30  	ip, err := s.GetIP(ipID)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	update.Address = ip.IP.Address
    35  	update.ID = ip.IP.ID
    36  	update.Organization = ip.IP.Organization
    37  
    38  	resp, err := s.PutResponse(api.ComputeAPI, fmt.Sprintf("ips/%s", ipID), update)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	if resp.StatusCode != http.StatusOK {
    43  		return err
    44  	}
    45  	resp.Body.Close()
    46  	return nil
    47  }
    48  
    49  // NOTE copied from github.com/scaleway/scaleway-cli/pkg/api/helpers.go
    50  // the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway
    51  
    52  func deleteServerSafe(s *api.ScalewayAPI, serverID string) error {
    53  	server, err := s.GetServer(serverID)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	if server.State != "stopped" {
    59  		if err := s.PostServerAction(serverID, "poweroff"); err != nil {
    60  			return err
    61  		}
    62  		if err := waitForServerState(s, serverID, "stopped"); err != nil {
    63  			return err
    64  		}
    65  	}
    66  
    67  	if err := s.DeleteServer(serverID); err != nil {
    68  		return err
    69  	}
    70  	if rootVolume, ok := server.Volumes["0"]; ok {
    71  		if err := s.DeleteVolume(rootVolume.Identifier); err != nil {
    72  			return err
    73  		}
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  func waitForServerState(s *api.ScalewayAPI, serverID string, targetState string) error {
    80  	var server *api.ScalewayServer
    81  	var err error
    82  
    83  	var currentState string
    84  
    85  	for {
    86  		server, err = s.GetServer(serverID)
    87  		if err != nil {
    88  			return err
    89  		}
    90  		if currentState != server.State {
    91  			log.Printf("[DEBUG] Server changed state to %q\n", server.State)
    92  			currentState = server.State
    93  		}
    94  		if server.State == targetState {
    95  			break
    96  		}
    97  		time.Sleep(1 * time.Second)
    98  	}
    99  
   100  	return nil
   101  }