github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/oneandone/resources_oneandone_shared_storage.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/1and1/oneandone-cloudserver-sdk-go"
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"strings"
     8  )
     9  
    10  func resourceOneandOneSharedStorage() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOneandOneSharedStorageCreate,
    13  		Read:   resourceOneandOneSharedStorageRead,
    14  		Update: resourceOneandOneSharedStorageUpdate,
    15  		Delete: resourceOneandOneSharedStorageDelete,
    16  		Schema: map[string]*schema.Schema{
    17  			"name": {
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  			},
    21  			"description": {
    22  				Type:     schema.TypeString,
    23  				Optional: true,
    24  			},
    25  			"size": {
    26  				Type:     schema.TypeInt,
    27  				Required: true,
    28  			},
    29  			"datacenter": {
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  			},
    33  			"storage_servers": {
    34  				Type: schema.TypeList,
    35  				Elem: &schema.Resource{
    36  					Schema: map[string]*schema.Schema{
    37  						"id": {
    38  							Type:     schema.TypeString,
    39  							Required: true,
    40  						},
    41  						"rights": {
    42  							Type:     schema.TypeString,
    43  							Required: true,
    44  						},
    45  					},
    46  				},
    47  				Optional: true,
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func resourceOneandOneSharedStorageCreate(d *schema.ResourceData, meta interface{}) error {
    54  	config := meta.(*Config)
    55  
    56  	req := oneandone.SharedStorageRequest{
    57  		Name: d.Get("name").(string),
    58  		Size: oneandone.Int2Pointer(d.Get("size").(int)),
    59  	}
    60  
    61  	if raw, ok := d.GetOk("description"); ok {
    62  		req.Description = raw.(string)
    63  
    64  	}
    65  
    66  	if raw, ok := d.GetOk("datacenter"); ok {
    67  		dcs, err := config.API.ListDatacenters()
    68  
    69  		if err != nil {
    70  			return fmt.Errorf("An error occured while fetching list of datacenters %s", err)
    71  
    72  		}
    73  
    74  		decenter := raw.(string)
    75  		for _, dc := range dcs {
    76  			if strings.ToLower(dc.CountryCode) == strings.ToLower(decenter) {
    77  				req.DatacenterId = dc.Id
    78  				break
    79  			}
    80  		}
    81  	}
    82  
    83  	ss_id, ss, err := config.API.CreateSharedStorage(&req)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	err = config.API.WaitForState(ss, "ACTIVE", 10, config.Retries)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	d.SetId(ss_id)
    93  
    94  	if raw, ok := d.GetOk("storage_servers"); ok {
    95  
    96  		storage_servers := []oneandone.SharedStorageServer{}
    97  
    98  		rawRights := raw.([]interface{})
    99  		for _, raws_ss := range rawRights {
   100  			ss := raws_ss.(map[string]interface{})
   101  			storage_server := oneandone.SharedStorageServer{
   102  				Id:     ss["id"].(string),
   103  				Rights: ss["rights"].(string),
   104  			}
   105  			storage_servers = append(storage_servers, storage_server)
   106  		}
   107  
   108  		ss, err := config.API.AddSharedStorageServers(ss_id, storage_servers)
   109  
   110  		if err != nil {
   111  			return err
   112  		}
   113  
   114  		err = config.API.WaitForState(ss, "ACTIVE", 10, 30)
   115  		if err != nil {
   116  			return err
   117  		}
   118  	}
   119  
   120  	return resourceOneandOneSharedStorageRead(d, meta)
   121  }
   122  
   123  func resourceOneandOneSharedStorageUpdate(d *schema.ResourceData, meta interface{}) error {
   124  	config := meta.(*Config)
   125  
   126  	if d.HasChange("name") || d.HasChange("description") || d.HasChange("size") {
   127  		ssu := oneandone.SharedStorageRequest{}
   128  		if d.HasChange("name") {
   129  			_, n := d.GetChange("name")
   130  			ssu.Name = n.(string)
   131  		}
   132  		if d.HasChange("description") {
   133  			_, n := d.GetChange("description")
   134  			ssu.Description = n.(string)
   135  		}
   136  		if d.HasChange("size") {
   137  			_, n := d.GetChange("size")
   138  			ssu.Size = oneandone.Int2Pointer(n.(int))
   139  		}
   140  
   141  		ss, err := config.API.UpdateSharedStorage(d.Id(), &ssu)
   142  
   143  		if err != nil {
   144  			return err
   145  		}
   146  		err = config.API.WaitForState(ss, "ACTIVE", 10, 30)
   147  		if err != nil {
   148  			return err
   149  		}
   150  
   151  	}
   152  
   153  	if d.HasChange("storage_servers") {
   154  
   155  		o, n := d.GetChange("storage_servers")
   156  
   157  		oldV := o.([]interface{})
   158  
   159  		for _, old := range oldV {
   160  			ol := old.(map[string]interface{})
   161  
   162  			ss, err := config.API.DeleteSharedStorageServer(d.Id(), ol["id"].(string))
   163  			if err != nil {
   164  				return err
   165  			}
   166  
   167  			err = config.API.WaitForState(ss, "ACTIVE", 10, config.Retries)
   168  
   169  			if err != nil {
   170  				return err
   171  			}
   172  
   173  		}
   174  
   175  		newV := n.([]interface{})
   176  
   177  		ids := []oneandone.SharedStorageServer{}
   178  		for _, newValue := range newV {
   179  			nn := newValue.(map[string]interface{})
   180  			ids = append(ids, oneandone.SharedStorageServer{
   181  				Id:     nn["id"].(string),
   182  				Rights: nn["rights"].(string),
   183  			})
   184  		}
   185  
   186  		if len(ids) > 0 {
   187  			ss, err := config.API.AddSharedStorageServers(d.Id(), ids)
   188  			if err != nil {
   189  				return err
   190  			}
   191  
   192  			err = config.API.WaitForState(ss, "ACTIVE", 10, config.Retries)
   193  
   194  			if err != nil {
   195  				return err
   196  			}
   197  		}
   198  
   199  		//DeleteSharedStorageServer
   200  
   201  	}
   202  
   203  	return resourceOneandOneSharedStorageRead(d, meta)
   204  }
   205  
   206  func resourceOneandOneSharedStorageRead(d *schema.ResourceData, meta interface{}) error {
   207  	config := meta.(*Config)
   208  
   209  	ss, err := config.API.GetSharedStorage(d.Id())
   210  	if err != nil {
   211  		if strings.Contains(err.Error(), "404") {
   212  			d.SetId("")
   213  			return nil
   214  		}
   215  		return err
   216  	}
   217  
   218  	d.Set("name", ss.Name)
   219  	d.Set("description", ss.Description)
   220  	d.Set("size", ss.Size)
   221  	d.Set("datacenter", ss.Datacenter.CountryCode)
   222  	d.Set("storage_servers", getStorageServers(ss.Servers))
   223  
   224  	return nil
   225  }
   226  
   227  func getStorageServers(servers []oneandone.SharedStorageServer) []map[string]interface{} {
   228  	raw := make([]map[string]interface{}, 0, len(servers))
   229  
   230  	for _, server := range servers {
   231  
   232  		toadd := map[string]interface{}{
   233  			"id":     server.Id,
   234  			"rights": server.Rights,
   235  		}
   236  
   237  		raw = append(raw, toadd)
   238  	}
   239  
   240  	return raw
   241  
   242  }
   243  func resourceOneandOneSharedStorageDelete(d *schema.ResourceData, meta interface{}) error {
   244  	config := meta.(*Config)
   245  
   246  	ss, err := config.API.DeleteSharedStorage(d.Id())
   247  	if err != nil {
   248  		return err
   249  	}
   250  	err = config.API.WaitUntilDeleted(ss)
   251  	if err != nil {
   252  		return err
   253  	}
   254  
   255  	return nil
   256  }