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

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/go-oracle-terraform/compute"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceOPCIPNetwork() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOPCIPNetworkCreate,
    13  		Read:   resourceOPCIPNetworkRead,
    14  		Update: resourceOPCIPNetworkUpdate,
    15  		Delete: resourceOPCIPNetworkDelete,
    16  		Importer: &schema.ResourceImporter{
    17  			State: schema.ImportStatePassthrough,
    18  		},
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": {
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  			},
    25  
    26  			"ip_address_prefix": {
    27  				Type:         schema.TypeString,
    28  				Required:     true,
    29  				ValidateFunc: validateIPPrefixCIDR,
    30  			},
    31  
    32  			"ip_network_exchange": {
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  			},
    36  
    37  			"description": {
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  			},
    41  
    42  			"public_napt_enabled": {
    43  				Type:     schema.TypeBool,
    44  				Optional: true,
    45  				Default:  false,
    46  			},
    47  
    48  			"uri": {
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  
    53  			"tags": tagsOptionalSchema(),
    54  		},
    55  	}
    56  }
    57  
    58  func resourceOPCIPNetworkCreate(d *schema.ResourceData, meta interface{}) error {
    59  	client := meta.(*compute.Client).IPNetworks()
    60  
    61  	// Get required attributes
    62  	name := d.Get("name").(string)
    63  	ipPrefix := d.Get("ip_address_prefix").(string)
    64  	// public_napt_enabled is not required, but bool type allows it to be unspecified
    65  	naptEnabled := d.Get("public_napt_enabled").(bool)
    66  
    67  	input := &compute.CreateIPNetworkInput{
    68  		Name:              name,
    69  		IPAddressPrefix:   ipPrefix,
    70  		PublicNaptEnabled: naptEnabled,
    71  	}
    72  
    73  	// Get Optional attributes
    74  	if desc, ok := d.GetOk("description"); ok && desc != nil {
    75  		input.Description = desc.(string)
    76  	}
    77  
    78  	if ipEx, ok := d.GetOk("ip_network_exchange"); ok && ipEx != nil {
    79  		input.IPNetworkExchange = ipEx.(string)
    80  	}
    81  
    82  	tags := getStringList(d, "tags")
    83  	if len(tags) != 0 {
    84  		input.Tags = tags
    85  	}
    86  
    87  	info, err := client.CreateIPNetwork(input)
    88  	if err != nil {
    89  		return fmt.Errorf("Error creating IP Network '%s': %v", name, err)
    90  	}
    91  
    92  	d.SetId(info.Name)
    93  
    94  	return resourceOPCIPNetworkRead(d, meta)
    95  }
    96  
    97  func resourceOPCIPNetworkRead(d *schema.ResourceData, meta interface{}) error {
    98  	client := meta.(*compute.Client).IPNetworks()
    99  
   100  	name := d.Id()
   101  	input := &compute.GetIPNetworkInput{
   102  		Name: name,
   103  	}
   104  
   105  	res, err := client.GetIPNetwork(input)
   106  	if err != nil {
   107  		if compute.WasNotFoundError(err) {
   108  			d.SetId("")
   109  			return nil
   110  		}
   111  		return fmt.Errorf("Error reading IP Network '%s': %v", name, err)
   112  	}
   113  
   114  	d.Set("name", res.Name)
   115  	d.Set("ip_address_prefix", res.IPAddressPrefix)
   116  	d.Set("ip_network_exchanged", res.IPNetworkExchange)
   117  	d.Set("description", res.Description)
   118  	d.Set("public_napt_enabled", res.PublicNaptEnabled)
   119  	d.Set("uri", res.Uri)
   120  	if err := setStringList(d, "tags", res.Tags); err != nil {
   121  		return err
   122  	}
   123  	return nil
   124  }
   125  
   126  func resourceOPCIPNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
   127  	client := meta.(*compute.Client).IPNetworks()
   128  
   129  	// Get required attributes
   130  	name := d.Get("name").(string)
   131  	ipPrefix := d.Get("ip_address_prefix").(string)
   132  	// public_napt_enabled is not required, but bool type allows it to be unspecified
   133  	naptEnabled := d.Get("public_napt_enabled").(bool)
   134  
   135  	input := &compute.UpdateIPNetworkInput{
   136  		Name:              name,
   137  		IPAddressPrefix:   ipPrefix,
   138  		PublicNaptEnabled: naptEnabled,
   139  	}
   140  
   141  	// Get Optional attributes
   142  	desc, descOk := d.GetOk("description")
   143  	if descOk {
   144  		input.Description = desc.(string)
   145  	}
   146  
   147  	ipEx, ipExOk := d.GetOk("ip_network_exchange")
   148  	if ipExOk {
   149  		input.IPNetworkExchange = ipEx.(string)
   150  	}
   151  
   152  	tags := getStringList(d, "tags")
   153  	if len(tags) != 0 {
   154  		input.Tags = tags
   155  	}
   156  
   157  	info, err := client.UpdateIPNetwork(input)
   158  	if err != nil {
   159  		return fmt.Errorf("Error updating IP Network '%s': %v", name, err)
   160  	}
   161  
   162  	d.SetId(info.Name)
   163  
   164  	return resourceOPCIPNetworkRead(d, meta)
   165  }
   166  
   167  func resourceOPCIPNetworkDelete(d *schema.ResourceData, meta interface{}) error {
   168  	client := meta.(*compute.Client).IPNetworks()
   169  
   170  	name := d.Id()
   171  	input := &compute.DeleteIPNetworkInput{
   172  		Name: name,
   173  	}
   174  
   175  	if err := client.DeleteIPNetwork(input); err != nil {
   176  		return fmt.Errorf("Error deleting IP Network '%s': %v", name, err)
   177  	}
   178  	return nil
   179  }