github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/oneandone/resource_oneandone_private_network.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 resourceOneandOnePrivateNetwork() *schema.Resource {
    11  	return &schema.Resource{
    12  
    13  		Create: resourceOneandOnePrivateNetworkCreate,
    14  		Read:   resourceOneandOnePrivateNetworkRead,
    15  		Update: resourceOneandOnePrivateNetworkUpdate,
    16  		Delete: resourceOneandOnePrivateNetworkDelete,
    17  		Schema: map[string]*schema.Schema{
    18  			"name": {
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  			},
    22  			"description": {
    23  				Type:     schema.TypeString,
    24  				Optional: true,
    25  			},
    26  			"datacenter": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  			},
    30  			"network_address": {
    31  				Type:     schema.TypeString,
    32  				Optional: true,
    33  			},
    34  			"subnet_mask": {
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  			},
    38  			"server_ids": {
    39  				Type:     schema.TypeSet,
    40  				Elem:     &schema.Schema{Type: schema.TypeString},
    41  				Optional: true,
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func resourceOneandOnePrivateNetworkCreate(d *schema.ResourceData, meta interface{}) error {
    48  	config := meta.(*Config)
    49  
    50  	req := oneandone.PrivateNetworkRequest{
    51  		Name: d.Get("name").(string),
    52  	}
    53  
    54  	if raw, ok := d.GetOk("description"); ok {
    55  		req.Description = raw.(string)
    56  	}
    57  
    58  	if raw, ok := d.GetOk("network_address"); ok {
    59  		req.NetworkAddress = raw.(string)
    60  	}
    61  
    62  	if raw, ok := d.GetOk("subnet_mask"); ok {
    63  		req.SubnetMask = raw.(string)
    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  	prn_id, prn, err := config.API.CreatePrivateNetwork(&req)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	err = config.API.WaitForState(prn, "ACTIVE", 30, config.Retries)
    88  
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	d.SetId(prn_id)
    94  
    95  	var ids []string
    96  	if raw, ok := d.GetOk("server_ids"); ok {
    97  
    98  		rawIps := raw.(*schema.Set).List()
    99  
   100  		for _, raw := range rawIps {
   101  			ids = append(ids, raw.(string))
   102  			server, err := config.API.ShutdownServer(raw.(string), false)
   103  			if err != nil {
   104  				return err
   105  			}
   106  			err = config.API.WaitForState(server, "POWERED_OFF", 10, config.Retries)
   107  			if err != nil {
   108  				return err
   109  			}
   110  
   111  		}
   112  	}
   113  
   114  	prn, err = config.API.AttachPrivateNetworkServers(d.Id(), ids)
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	err = config.API.WaitForState(prn, "ACTIVE", 30, config.Retries)
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	for _, id := range ids {
   125  		server, err := config.API.StartServer(id)
   126  		if err != nil {
   127  			return err
   128  		}
   129  
   130  		err = config.API.WaitForState(server, "POWERED_ON", 10, config.Retries)
   131  		if err != nil {
   132  			return err
   133  		}
   134  	}
   135  
   136  	return resourceOneandOnePrivateNetworkRead(d, meta)
   137  }
   138  
   139  func resourceOneandOnePrivateNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
   140  	config := meta.(*Config)
   141  	if d.HasChange("name") || d.HasChange("description") || d.HasChange("network_address") || d.HasChange("subnet_mask") {
   142  		pnset := oneandone.PrivateNetworkRequest{}
   143  
   144  		pnset.Name = d.Get("name").(string)
   145  
   146  		pnset.Description = d.Get("description").(string)
   147  		pnset.NetworkAddress = d.Get("network_address").(string)
   148  		pnset.SubnetMask = d.Get("subnet_mask").(string)
   149  
   150  		prn, err := config.API.UpdatePrivateNetwork(d.Id(), &pnset)
   151  
   152  		if err != nil {
   153  			return err
   154  		}
   155  
   156  		err = config.API.WaitForState(prn, "ACTIVE", 30, config.Retries)
   157  		if err != nil {
   158  			return err
   159  		}
   160  	}
   161  
   162  	if d.HasChange("server_ids") {
   163  		o, n := d.GetChange("server_ids")
   164  
   165  		newValues := n.(*schema.Set).List()
   166  		oldValues := o.(*schema.Set).List()
   167  
   168  		var ids []string
   169  		for _, newV := range oldValues {
   170  			ids = append(ids, newV.(string))
   171  		}
   172  		for _, id := range ids {
   173  			server, err := config.API.ShutdownServer(id, false)
   174  			if err != nil {
   175  				return err
   176  			}
   177  			err = config.API.WaitForState(server, "POWERED_OFF", 10, config.Retries)
   178  			if err != nil {
   179  				return err
   180  			}
   181  
   182  			_, err = config.API.RemoveServerPrivateNetwork(id, d.Id())
   183  			if err != nil {
   184  				return err
   185  			}
   186  
   187  			prn, _ := config.API.GetPrivateNetwork(d.Id())
   188  
   189  			err = config.API.WaitForState(prn, "ACTIVE", 10, config.Retries)
   190  			if err != nil {
   191  				return err
   192  			}
   193  
   194  		}
   195  
   196  		var newids []string
   197  
   198  		for _, newV := range newValues {
   199  			newids = append(newids, newV.(string))
   200  		}
   201  		pn, err := config.API.AttachPrivateNetworkServers(d.Id(), newids)
   202  
   203  		if err != nil {
   204  			return err
   205  		}
   206  		err = config.API.WaitForState(pn, "ACTIVE", 30, config.Retries)
   207  		if err != nil {
   208  			return err
   209  		}
   210  
   211  		for _, id := range newids {
   212  			server, err := config.API.StartServer(id)
   213  			if err != nil {
   214  				return err
   215  			}
   216  
   217  			err = config.API.WaitForState(server, "POWERED_ON", 10, config.Retries)
   218  			if err != nil {
   219  				return err
   220  			}
   221  		}
   222  	}
   223  
   224  	return resourceOneandOnePrivateNetworkRead(d, meta)
   225  }
   226  
   227  func resourceOneandOnePrivateNetworkRead(d *schema.ResourceData, meta interface{}) error {
   228  	config := meta.(*Config)
   229  
   230  	pn, err := config.API.GetPrivateNetwork(d.Id())
   231  	if err != nil {
   232  		if strings.Contains(err.Error(), "404") {
   233  			d.SetId("")
   234  			return nil
   235  		}
   236  		return err
   237  	}
   238  
   239  	d.Set("name", pn.Name)
   240  	d.Set("description", pn.Description)
   241  	d.Set("network_address", pn.NetworkAddress)
   242  	d.Set("subnet_mask", pn.SubnetMask)
   243  	d.Set("datacenter", pn.Datacenter.CountryCode)
   244  
   245  	var toAdd []string
   246  	for _, s := range pn.Servers {
   247  		toAdd = append(toAdd, s.Id)
   248  	}
   249  	d.Set("server_ids", toAdd)
   250  	return nil
   251  }
   252  
   253  func resourceOneandOnePrivateNetworkDelete(d *schema.ResourceData, meta interface{}) error {
   254  	config := meta.(*Config)
   255  
   256  	pn, err := config.API.GetPrivateNetwork(d.Id())
   257  
   258  	for _, server := range pn.Servers {
   259  		srv, err := config.API.ShutdownServer(server.Id, false)
   260  		if err != nil {
   261  			return err
   262  		}
   263  		err = config.API.WaitForState(srv, "POWERED_OFF", 10, config.Retries)
   264  		if err != nil {
   265  			return err
   266  		}
   267  	}
   268  
   269  	pn, err = config.API.DeletePrivateNetwork(d.Id())
   270  	if err != nil {
   271  		return err
   272  	}
   273  
   274  	err = config.API.WaitUntilDeleted(pn)
   275  	if err != nil {
   276  		return err
   277  	}
   278  
   279  	for _, server := range pn.Servers {
   280  		srv, err := config.API.StartServer(server.Id)
   281  		if err != nil {
   282  			return err
   283  		}
   284  		err = config.API.WaitForState(srv, "POWERED_ON", 10, config.Retries)
   285  		if err != nil {
   286  			return err
   287  		}
   288  	}
   289  
   290  	return nil
   291  }