github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/opc/resource_sec_rule.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 resourceOPCSecRule() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOPCSecRuleCreate,
    13  		Read:   resourceOPCSecRuleRead,
    14  		Update: resourceOPCSecRuleUpdate,
    15  		Delete: resourceOPCSecRuleDelete,
    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  			"description": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  			},
    30  			"source_list": {
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  			"destination_list": {
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  			},
    40  			"application": {
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  				ForceNew: true,
    44  			},
    45  			"action": {
    46  				Type:     schema.TypeString,
    47  				Required: true,
    48  			},
    49  			"disabled": {
    50  				Type:     schema.TypeBool,
    51  				Optional: true,
    52  				Default:  false,
    53  			},
    54  		},
    55  	}
    56  }
    57  
    58  func resourceOPCSecRuleCreate(d *schema.ResourceData, meta interface{}) error {
    59  	client := meta.(*compute.Client).SecRules()
    60  
    61  	name := d.Get("name").(string)
    62  	sourceList := d.Get("source_list").(string)
    63  	destinationList := d.Get("destination_list").(string)
    64  	application := d.Get("application").(string)
    65  	action := d.Get("action").(string)
    66  	disabled := d.Get("disabled").(bool)
    67  
    68  	input := compute.CreateSecRuleInput{
    69  		Name:            name,
    70  		Action:          action,
    71  		SourceList:      sourceList,
    72  		DestinationList: destinationList,
    73  		Disabled:        disabled,
    74  		Application:     application,
    75  	}
    76  	desc, descOk := d.GetOk("description")
    77  	if descOk {
    78  		input.Description = desc.(string)
    79  	}
    80  
    81  	info, err := client.CreateSecRule(&input)
    82  	if err != nil {
    83  		return fmt.Errorf("Error creating sec rule %s: %s", name, err)
    84  	}
    85  
    86  	d.SetId(info.Name)
    87  
    88  	return resourceOPCSecRuleRead(d, meta)
    89  }
    90  
    91  func resourceOPCSecRuleRead(d *schema.ResourceData, meta interface{}) error {
    92  	client := meta.(*compute.Client).SecRules()
    93  
    94  	name := d.Id()
    95  
    96  	input := compute.GetSecRuleInput{
    97  		Name: name,
    98  	}
    99  	result, err := client.GetSecRule(&input)
   100  	if err != nil {
   101  		// Sec Rule does not exist
   102  		if compute.WasNotFoundError(err) {
   103  			d.SetId("")
   104  			return nil
   105  		}
   106  		return fmt.Errorf("Error reading sec list %s: %s", name, err)
   107  	}
   108  
   109  	d.Set("name", result.Name)
   110  	d.Set("description", result.Description)
   111  	d.Set("source_list", result.SourceList)
   112  	d.Set("destination_list", result.DestinationList)
   113  	d.Set("application", result.Application)
   114  	d.Set("action", result.Action)
   115  	d.Set("disabled", result.Disabled)
   116  
   117  	return nil
   118  }
   119  
   120  func resourceOPCSecRuleUpdate(d *schema.ResourceData, meta interface{}) error {
   121  	client := meta.(*compute.Client).SecRules()
   122  
   123  	name := d.Get("name").(string)
   124  	sourceList := d.Get("source_list").(string)
   125  	destinationList := d.Get("destination_list").(string)
   126  	application := d.Get("application").(string)
   127  	action := d.Get("action").(string)
   128  	disabled := d.Get("disabled").(bool)
   129  
   130  	input := compute.UpdateSecRuleInput{
   131  		Action:          action,
   132  		Application:     application,
   133  		DestinationList: destinationList,
   134  		Disabled:        disabled,
   135  		Name:            name,
   136  		SourceList:      sourceList,
   137  	}
   138  	desc, descOk := d.GetOk("description")
   139  	if descOk {
   140  		input.Description = desc.(string)
   141  	}
   142  
   143  	_, err := client.UpdateSecRule(&input)
   144  	if err != nil {
   145  		return fmt.Errorf("Error updating sec rule %s: %s", name, err)
   146  	}
   147  
   148  	return resourceOPCSecRuleRead(d, meta)
   149  }
   150  
   151  func resourceOPCSecRuleDelete(d *schema.ResourceData, meta interface{}) error {
   152  	client := meta.(*compute.Client).SecRules()
   153  	name := d.Id()
   154  
   155  	input := compute.DeleteSecRuleInput{
   156  		Name: name,
   157  	}
   158  	if err := client.DeleteSecRule(&input); err != nil {
   159  		return fmt.Errorf("Error deleting sec rule %s: %s", name, err)
   160  	}
   161  
   162  	return nil
   163  }