github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/ovh/resource_ovh_publiccloud_private_network_subnet.go (about)

     1  package ovh
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	"net"
    10  
    11  	"github.com/ovh/go-ovh/ovh"
    12  )
    13  
    14  func resourcePublicCloudPrivateNetworkSubnet() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourcePublicCloudPrivateNetworkSubnetCreate,
    17  		Read:   resourcePublicCloudPrivateNetworkSubnetRead,
    18  		Delete: resourcePublicCloudPrivateNetworkSubnetDelete,
    19  		Importer: &schema.ResourceImporter{
    20  			State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
    21  				return []*schema.ResourceData{d}, nil
    22  			},
    23  		},
    24  
    25  		Schema: map[string]*schema.Schema{
    26  			"project_id": &schema.Schema{
    27  				Type:        schema.TypeString,
    28  				Required:    true,
    29  				ForceNew:    true,
    30  				DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", ""),
    31  			},
    32  			"network_id": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  			"dhcp": &schema.Schema{
    38  				Type:     schema.TypeBool,
    39  				Optional: true,
    40  				ForceNew: true,
    41  				Default:  false,
    42  			},
    43  			"start": &schema.Schema{
    44  				Type:         schema.TypeString,
    45  				Required:     true,
    46  				ForceNew:     true,
    47  				ValidateFunc: resourcePubliccloudPrivateNetworkSubnetValidateIP,
    48  			},
    49  			"end": &schema.Schema{
    50  				Type:         schema.TypeString,
    51  				Required:     true,
    52  				ForceNew:     true,
    53  				ValidateFunc: resourcePubliccloudPrivateNetworkSubnetValidateIP,
    54  			},
    55  			"network": &schema.Schema{
    56  				Type:         schema.TypeString,
    57  				Required:     true,
    58  				ForceNew:     true,
    59  				ValidateFunc: resourcePubliccloudPrivateNetworkSubnetValidateNetwork,
    60  			},
    61  			"region": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Required: true,
    64  				ForceNew: true,
    65  			},
    66  			"no_gateway": &schema.Schema{
    67  				Type:     schema.TypeBool,
    68  				Optional: true,
    69  				ForceNew: true,
    70  				Default:  false,
    71  			},
    72  			"gateway_ip": &schema.Schema{
    73  				Type:     schema.TypeString,
    74  				Computed: true,
    75  			},
    76  
    77  			"cidr": &schema.Schema{
    78  				Type:     schema.TypeString,
    79  				Computed: true,
    80  			},
    81  
    82  			"ip_pools": &schema.Schema{
    83  				Type:     schema.TypeSet,
    84  				Computed: true,
    85  				Elem: &schema.Resource{
    86  					Schema: map[string]*schema.Schema{
    87  						"network": &schema.Schema{
    88  							Type:     schema.TypeString,
    89  							Computed: true,
    90  						},
    91  						"region": &schema.Schema{
    92  							Type:     schema.TypeString,
    93  							Computed: true,
    94  						},
    95  						"dhcp": &schema.Schema{
    96  							Type:     schema.TypeBool,
    97  							Computed: true,
    98  						},
    99  						"end": &schema.Schema{
   100  							Type:     schema.TypeString,
   101  							Computed: true,
   102  						},
   103  						"start": &schema.Schema{
   104  							Type:     schema.TypeString,
   105  							Computed: true,
   106  						},
   107  					},
   108  				},
   109  			},
   110  		},
   111  	}
   112  }
   113  
   114  func resourcePublicCloudPrivateNetworkSubnetCreate(d *schema.ResourceData, meta interface{}) error {
   115  	config := meta.(*Config)
   116  
   117  	projectId := d.Get("project_id").(string)
   118  	networkId := d.Get("network_id").(string)
   119  
   120  	params := &PublicCloudPrivateNetworksCreateOpts{
   121  		ProjectId: projectId,
   122  		NetworkId: networkId,
   123  		Dhcp:      d.Get("dhcp").(bool),
   124  		NoGateway: d.Get("no_gateway").(bool),
   125  		Start:     d.Get("start").(string),
   126  		End:       d.Get("end").(string),
   127  		Network:   d.Get("network").(string),
   128  		Region:    d.Get("region").(string),
   129  	}
   130  
   131  	r := &PublicCloudPrivateNetworksResponse{}
   132  
   133  	log.Printf("[DEBUG] Will create public cloud private network subnet: %s", params)
   134  
   135  	endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet", projectId, networkId)
   136  
   137  	err := config.OVHClient.Post(endpoint, params, r)
   138  	if err != nil {
   139  		return fmt.Errorf("calling %s with params %s:\n\t %q", endpoint, params, err)
   140  	}
   141  
   142  	log.Printf("[DEBUG] Created Private Network Subnet %s", r)
   143  
   144  	//set id
   145  	d.SetId(r.Id)
   146  
   147  	return nil
   148  }
   149  
   150  func resourcePublicCloudPrivateNetworkSubnetRead(d *schema.ResourceData, meta interface{}) error {
   151  	config := meta.(*Config)
   152  
   153  	projectId := d.Get("project_id").(string)
   154  	networkId := d.Get("network_id").(string)
   155  
   156  	r := []*PublicCloudPrivateNetworksResponse{}
   157  
   158  	log.Printf("[DEBUG] Will read public cloud private network subnet for project: %s, network: %s, id: %s", projectId, networkId, d.Id())
   159  
   160  	endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet", projectId, networkId)
   161  
   162  	err := config.OVHClient.Get(endpoint, &r)
   163  	if err != nil {
   164  		return fmt.Errorf("calling %s:\n\t %q", endpoint, err)
   165  	}
   166  
   167  	err = readPublicCloudPrivateNetworkSubnet(d, r)
   168  	if err != nil {
   169  		return err
   170  	}
   171  
   172  	log.Printf("[DEBUG] Read Public Cloud Private Network %v", r)
   173  	return nil
   174  }
   175  
   176  func resourcePublicCloudPrivateNetworkSubnetDelete(d *schema.ResourceData, meta interface{}) error {
   177  	config := meta.(*Config)
   178  
   179  	projectId := d.Get("project_id").(string)
   180  	networkId := d.Get("network_id").(string)
   181  	id := d.Id()
   182  
   183  	log.Printf("[DEBUG] Will delete public cloud private network subnet for project: %s, network: %s, id: %s", projectId, networkId, id)
   184  
   185  	endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet/%s", projectId, id, id)
   186  
   187  	err := config.OVHClient.Delete(endpoint, nil)
   188  	if err != nil {
   189  		return fmt.Errorf("calling %s:\n\t %q", endpoint, err)
   190  	}
   191  
   192  	d.SetId("")
   193  
   194  	log.Printf("[DEBUG] Deleted Public Cloud %s Private Network %s Subnet %s", projectId, networkId, id)
   195  	return nil
   196  }
   197  
   198  func publicCloudPrivateNetworkSubnetExists(projectId, networkId, id string, c *ovh.Client) error {
   199  	r := []*PublicCloudPrivateNetworksResponse{}
   200  
   201  	log.Printf("[DEBUG] Will read public cloud private network subnet for project: %s, network: %s, id: %s", projectId, networkId, id)
   202  
   203  	endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s/subnet", projectId, networkId)
   204  
   205  	err := c.Get(endpoint, &r)
   206  	if err != nil {
   207  		return fmt.Errorf("calling %s:\n\t %q", endpoint, err)
   208  	}
   209  
   210  	s := findPublicCloudPrivateNetworkSubnet(r, id)
   211  	if s == nil {
   212  		return fmt.Errorf("Subnet %s doesn't exists for project %s and network %s", id, projectId, networkId)
   213  	}
   214  
   215  	return nil
   216  }
   217  
   218  func findPublicCloudPrivateNetworkSubnet(rs []*PublicCloudPrivateNetworksResponse, id string) *PublicCloudPrivateNetworksResponse {
   219  	for i := range rs {
   220  		if rs[i].Id == id {
   221  			return rs[i]
   222  		}
   223  	}
   224  
   225  	return nil
   226  }
   227  
   228  func readPublicCloudPrivateNetworkSubnet(d *schema.ResourceData, rs []*PublicCloudPrivateNetworksResponse) error {
   229  	r := findPublicCloudPrivateNetworkSubnet(rs, d.Id())
   230  	if r == nil {
   231  		return fmt.Errorf("%s subnet not found", d.Id())
   232  	}
   233  
   234  	d.Set("gateway_ip", r.GatewayIp)
   235  	d.Set("cidr", r.Cidr)
   236  
   237  	ippools := make([]map[string]interface{}, 0)
   238  	for i := range r.IPPools {
   239  		ippool := make(map[string]interface{})
   240  		ippool["network"] = r.IPPools[i].Network
   241  		ippool["region"] = r.IPPools[i].Region
   242  		ippool["dhcp"] = r.IPPools[i].Dhcp
   243  		ippool["start"] = r.IPPools[i].Start
   244  		ippool["end"] = r.IPPools[i].End
   245  		ippools = append(ippools, ippool)
   246  	}
   247  
   248  	d.Set("network", ippools[0]["network"])
   249  	d.Set("region", ippools[0]["region"])
   250  	d.Set("dhcp", ippools[0]["dhcp"])
   251  	d.Set("start", ippools[0]["start"])
   252  	d.Set("end", ippools[0]["end"])
   253  	d.Set("ip_pools", ippools)
   254  
   255  	if r.GatewayIp == "" {
   256  		d.Set("no_gateway", true)
   257  	} else {
   258  		d.Set("no_gateway", false)
   259  	}
   260  
   261  	d.SetId(r.Id)
   262  	return nil
   263  }
   264  
   265  func resourcePubliccloudPrivateNetworkSubnetValidateIP(v interface{}, k string) (ws []string, errors []error) {
   266  	value := v.(string)
   267  	ip := net.ParseIP(value)
   268  	if ip == nil {
   269  		errors = append(errors, fmt.Errorf("%q must be a valid IP", k))
   270  	}
   271  	return
   272  }
   273  
   274  func resourcePubliccloudPrivateNetworkSubnetValidateNetwork(v interface{}, k string) (ws []string, errors []error) {
   275  	value := v.(string)
   276  	_, _, err := net.ParseCIDR(value)
   277  	if err != nil {
   278  		errors = append(errors, fmt.Errorf("%q is not a valid network value: %#v", k, err))
   279  	}
   280  	return
   281  }