github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_application.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  	"github.com/hashicorp/terraform/helper/validation"
     9  )
    10  
    11  func resourceOPCSecurityApplication() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceOPCSecurityApplicationCreate,
    14  		Read:   resourceOPCSecurityApplicationRead,
    15  		Delete: resourceOPCSecurityApplicationDelete,
    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  				ForceNew: true,
    25  			},
    26  
    27  			"description": {
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"protocol": {
    34  				Type:         schema.TypeString,
    35  				Required:     true,
    36  				ValidateFunc: validateIPProtocol,
    37  				ForceNew:     true,
    38  			},
    39  
    40  			"dport": {
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  				ForceNew: true,
    44  			},
    45  
    46  			"icmptype": {
    47  				Type:     schema.TypeString,
    48  				Optional: true,
    49  				ValidateFunc: validation.StringInSlice([]string{
    50  					string(compute.Echo),
    51  					string(compute.Reply),
    52  					string(compute.TTL),
    53  					string(compute.TraceRoute),
    54  					string(compute.Unreachable),
    55  				}, true),
    56  				ForceNew: true,
    57  			},
    58  
    59  			"icmpcode": {
    60  				Type:     schema.TypeString,
    61  				Optional: true,
    62  				ValidateFunc: validation.StringInSlice([]string{
    63  					string(compute.Admin),
    64  					string(compute.Df),
    65  					string(compute.Host),
    66  					string(compute.Network),
    67  					string(compute.Port),
    68  					string(compute.Protocol),
    69  				}, true),
    70  				ForceNew: true,
    71  			},
    72  		},
    73  	}
    74  }
    75  
    76  func resourceOPCSecurityApplicationCreate(d *schema.ResourceData, meta interface{}) error {
    77  	name := d.Get("name").(string)
    78  	protocol := d.Get("protocol").(string)
    79  	dport := d.Get("dport").(string)
    80  	icmptype := d.Get("icmptype").(string)
    81  	icmpcode := d.Get("icmpcode").(string)
    82  	description := d.Get("description").(string)
    83  
    84  	client := meta.(*compute.Client).SecurityApplications()
    85  	input := compute.CreateSecurityApplicationInput{
    86  		Name:        name,
    87  		Description: description,
    88  		Protocol:    compute.SecurityApplicationProtocol(protocol),
    89  		DPort:       dport,
    90  		ICMPCode:    compute.SecurityApplicationICMPCode(icmpcode),
    91  		ICMPType:    compute.SecurityApplicationICMPType(icmptype),
    92  	}
    93  	info, err := client.CreateSecurityApplication(&input)
    94  	if err != nil {
    95  		return fmt.Errorf("Error creating security application %s: %s", name, err)
    96  	}
    97  
    98  	d.SetId(info.Name)
    99  
   100  	return resourceOPCSecurityApplicationRead(d, meta)
   101  }
   102  
   103  func resourceOPCSecurityApplicationRead(d *schema.ResourceData, meta interface{}) error {
   104  	client := meta.(*compute.Client).SecurityApplications()
   105  	name := d.Id()
   106  
   107  	input := compute.GetSecurityApplicationInput{
   108  		Name: name,
   109  	}
   110  	result, err := client.GetSecurityApplication(&input)
   111  	if err != nil {
   112  		if compute.WasNotFoundError(err) {
   113  			d.SetId("")
   114  			return nil
   115  		}
   116  		return fmt.Errorf("Error reading security application %s: %s", name, err)
   117  	}
   118  
   119  	d.Set("name", result.Name)
   120  	d.Set("protocol", result.Protocol)
   121  	d.Set("dport", result.DPort)
   122  	d.Set("icmptype", result.ICMPType)
   123  	d.Set("icmpcode", result.ICMPCode)
   124  	d.Set("description", result.Description)
   125  
   126  	return nil
   127  }
   128  
   129  func resourceOPCSecurityApplicationDelete(d *schema.ResourceData, meta interface{}) error {
   130  	client := meta.(*compute.Client).SecurityApplications()
   131  	name := d.Id()
   132  
   133  	input := compute.DeleteSecurityApplicationInput{
   134  		Name: name,
   135  	}
   136  	if err := client.DeleteSecurityApplication(&input); err != nil {
   137  		return fmt.Errorf("Error deleting security application '%s': %s", name, err)
   138  	}
   139  	return nil
   140  }