github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_list.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 resourceOPCSecurityList() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceOPCSecurityListCreate, 14 Read: resourceOPCSecurityListRead, 15 Update: resourceOPCSecurityListUpdate, 16 Delete: resourceOPCSecurityListDelete, 17 Importer: &schema.ResourceImporter{ 18 State: schema.ImportStatePassthrough, 19 }, 20 21 Schema: map[string]*schema.Schema{ 22 "name": { 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 28 "description": { 29 Type: schema.TypeString, 30 Optional: true, 31 }, 32 33 "policy": { 34 Type: schema.TypeString, 35 Optional: true, 36 Default: "deny", 37 ValidateFunc: validation.StringInSlice([]string{ 38 string(compute.SecurityListPolicyDeny), 39 string(compute.SecurityListPolicyPermit), 40 string(compute.SecurityListPolicyReject), 41 }, true), 42 }, 43 44 "outbound_cidr_policy": { 45 Type: schema.TypeString, 46 Optional: true, 47 Default: "permit", 48 ValidateFunc: validation.StringInSlice([]string{ 49 string(compute.SecurityListPolicyDeny), 50 string(compute.SecurityListPolicyPermit), 51 string(compute.SecurityListPolicyReject), 52 }, true), 53 }, 54 }, 55 } 56 } 57 58 func resourceOPCSecurityListCreate(d *schema.ResourceData, meta interface{}) error { 59 name := d.Get("name").(string) 60 description := d.Get("description").(string) 61 policy := d.Get("policy").(string) 62 outboundCIDRPolicy := d.Get("outbound_cidr_policy").(string) 63 64 client := meta.(*compute.Client).SecurityLists() 65 input := compute.CreateSecurityListInput{ 66 Name: name, 67 Description: description, 68 Policy: compute.SecurityListPolicy(policy), 69 OutboundCIDRPolicy: compute.SecurityListPolicy(outboundCIDRPolicy), 70 } 71 info, err := client.CreateSecurityList(&input) 72 if err != nil { 73 return fmt.Errorf("Error creating security list %s: %s", name, err) 74 } 75 76 d.SetId(info.Name) 77 78 return resourceOPCSecurityListRead(d, meta) 79 } 80 81 func resourceOPCSecurityListUpdate(d *schema.ResourceData, meta interface{}) error { 82 client := meta.(*compute.Client).SecurityLists() 83 84 name := d.Get("name").(string) 85 description := d.Get("description").(string) 86 policy := d.Get("policy").(string) 87 outboundCIDRPolicy := d.Get("outbound_cidr_policy").(string) 88 89 input := compute.UpdateSecurityListInput{ 90 Name: name, 91 Description: description, 92 Policy: compute.SecurityListPolicy(policy), 93 OutboundCIDRPolicy: compute.SecurityListPolicy(outboundCIDRPolicy), 94 } 95 _, err := client.UpdateSecurityList(&input) 96 if err != nil { 97 return fmt.Errorf("Error updating security list %s: %s", name, err) 98 } 99 100 return resourceOPCSecurityListRead(d, meta) 101 } 102 103 func resourceOPCSecurityListRead(d *schema.ResourceData, meta interface{}) error { 104 client := meta.(*compute.Client).SecurityLists() 105 106 name := d.Id() 107 108 input := compute.GetSecurityListInput{ 109 Name: name, 110 } 111 result, err := client.GetSecurityList(&input) 112 if err != nil { 113 // Security List does not exist 114 if compute.WasNotFoundError(err) { 115 d.SetId("") 116 return nil 117 } 118 return fmt.Errorf("Error reading security list %s: %s", name, err) 119 } 120 121 d.Set("name", result.Name) 122 d.Set("description", result.Description) 123 d.Set("policy", string(result.Policy)) 124 d.Set("outbound_cidr_policy", string(result.OutboundCIDRPolicy)) 125 126 return nil 127 } 128 129 func resourceOPCSecurityListDelete(d *schema.ResourceData, meta interface{}) error { 130 client := meta.(*compute.Client).SecurityLists() 131 132 name := d.Id() 133 input := compute.DeleteSecurityListInput{ 134 Name: name, 135 } 136 if err := client.DeleteSecurityList(&input); err != nil { 137 return fmt.Errorf("Error deleting security list %s: %s", name, err) 138 } 139 140 return nil 141 }