github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_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 resourceOPCSecurityRule() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceOPCSecurityRuleCreate, 13 Read: resourceOPCSecurityRuleRead, 14 Update: resourceOPCSecurityRuleUpdate, 15 Delete: resourceOPCSecurityRuleDelete, 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 "flow_direction": { 27 Type: schema.TypeString, 28 Required: true, 29 }, 30 "acl": { 31 Type: schema.TypeString, 32 Optional: true, 33 }, 34 "dst_ip_address_prefixes": { 35 Type: schema.TypeList, 36 Optional: true, 37 Elem: &schema.Schema{Type: schema.TypeString}, 38 }, 39 "src_ip_address_prefixes": { 40 Type: schema.TypeList, 41 Optional: true, 42 Elem: &schema.Schema{Type: schema.TypeString}, 43 }, 44 "security_protocols": { 45 Type: schema.TypeList, 46 Optional: true, 47 Elem: &schema.Schema{Type: schema.TypeString}, 48 }, 49 "dst_vnic_set": { 50 Type: schema.TypeString, 51 Optional: true, 52 }, 53 "src_vnic_set": { 54 Type: schema.TypeString, 55 Optional: true, 56 }, 57 "enabled": { 58 Type: schema.TypeBool, 59 Optional: true, 60 Default: true, 61 }, 62 "description": { 63 Type: schema.TypeString, 64 Optional: true, 65 }, 66 "tags": tagsOptionalSchema(), 67 "uri": { 68 Type: schema.TypeString, 69 Computed: true, 70 }, 71 }, 72 } 73 } 74 75 func resourceOPCSecurityRuleCreate(d *schema.ResourceData, meta interface{}) error { 76 client := meta.(*compute.Client).SecurityRules() 77 input := compute.CreateSecurityRuleInput{ 78 Name: d.Get("name").(string), 79 FlowDirection: d.Get("flow_direction").(string), 80 Enabled: d.Get("enabled").(bool), 81 } 82 83 if acl, ok := d.GetOk("acl"); ok { 84 input.ACL = acl.(string) 85 } 86 87 if srcVNicSet, ok := d.GetOk("src_vnic_set"); ok { 88 input.SrcVnicSet = srcVNicSet.(string) 89 } 90 91 if dstVNicSet, ok := d.GetOk("dst_vnic_set"); ok { 92 input.DstVnicSet = dstVNicSet.(string) 93 } 94 95 securityProtocols := getStringList(d, "security_protocols") 96 if len(securityProtocols) != 0 { 97 input.SecProtocols = securityProtocols 98 } 99 100 srcIPAdressPrefixes := getStringList(d, "src_ip_address_prefixes") 101 if len(srcIPAdressPrefixes) != 0 { 102 input.SrcIpAddressPrefixSets = srcIPAdressPrefixes 103 } 104 105 dstIPAdressPrefixes := getStringList(d, "dst_ip_address_prefixes") 106 if len(dstIPAdressPrefixes) != 0 { 107 input.DstIpAddressPrefixSets = dstIPAdressPrefixes 108 } 109 110 tags := getStringList(d, "tags") 111 if len(tags) != 0 { 112 input.Tags = tags 113 } 114 115 if description, ok := d.GetOk("description"); ok { 116 input.Description = description.(string) 117 } 118 119 info, err := client.CreateSecurityRule(&input) 120 if err != nil { 121 return fmt.Errorf("Error creating Security Rule: %s", err) 122 } 123 124 d.SetId(info.Name) 125 return resourceOPCSecurityRuleRead(d, meta) 126 } 127 128 func resourceOPCSecurityRuleRead(d *schema.ResourceData, meta interface{}) error { 129 client := meta.(*compute.Client).SecurityRules() 130 131 getInput := compute.GetSecurityRuleInput{ 132 Name: d.Id(), 133 } 134 result, err := client.GetSecurityRule(&getInput) 135 if err != nil { 136 // SecurityRule does not exist 137 if compute.WasNotFoundError(err) { 138 d.SetId("") 139 return nil 140 } 141 return fmt.Errorf("Error reading security rule %s: %s", d.Id(), err) 142 } 143 144 d.Set("name", result.Name) 145 d.Set("flow_direction", result.FlowDirection) 146 d.Set("enabled", result.Enabled) 147 d.Set("acl", result.ACL) 148 d.Set("src_vnic_set", result.SrcVnicSet) 149 d.Set("dst_vnic_set", result.DstVnicSet) 150 d.Set("description", result.Description) 151 d.Set("uri", result.Uri) 152 153 if err := setStringList(d, "security_protocols", result.SecProtocols); err != nil { 154 return err 155 } 156 if err := setStringList(d, "dst_ip_address_prefixes", result.DstIpAddressPrefixSets); err != nil { 157 return err 158 } 159 if err := setStringList(d, "src_ip_address_prefixes", result.SrcIpAddressPrefixSets); err != nil { 160 return err 161 } 162 if err := setStringList(d, "tags", result.Tags); err != nil { 163 return err 164 } 165 return nil 166 } 167 168 func resourceOPCSecurityRuleUpdate(d *schema.ResourceData, meta interface{}) error { 169 client := meta.(*compute.Client).SecurityRules() 170 input := compute.UpdateSecurityRuleInput{ 171 Name: d.Get("name").(string), 172 FlowDirection: d.Get("flow_direction").(string), 173 Enabled: d.Get("enabled").(bool), 174 } 175 176 if acl, ok := d.GetOk("acl"); ok { 177 input.ACL = acl.(string) 178 } 179 180 if srcVNicSet, ok := d.GetOk("src_vnic_set"); ok { 181 input.SrcVnicSet = srcVNicSet.(string) 182 } 183 184 if dstVNicSet, ok := d.GetOk("dst_vnic_set"); ok { 185 input.DstVnicSet = dstVNicSet.(string) 186 } 187 188 securityProtocols := getStringList(d, "security_protocols") 189 if len(securityProtocols) != 0 { 190 input.SecProtocols = securityProtocols 191 } 192 193 srcIPAdressPrefixes := getStringList(d, "src_ip_address_prefixes") 194 if len(srcIPAdressPrefixes) != 0 { 195 input.SrcIpAddressPrefixSets = srcIPAdressPrefixes 196 } 197 198 dstIPAdressPrefixes := getStringList(d, "dst_ip_address_prefixes") 199 if len(dstIPAdressPrefixes) != 0 { 200 input.DstIpAddressPrefixSets = dstIPAdressPrefixes 201 } 202 203 tags := getStringList(d, "tags") 204 if len(tags) != 0 { 205 input.Tags = tags 206 } 207 208 if description, ok := d.GetOk("description"); ok { 209 input.Description = description.(string) 210 } 211 info, err := client.UpdateSecurityRule(&input) 212 if err != nil { 213 return fmt.Errorf("Error updating Security Rule: %s", err) 214 } 215 216 d.SetId(info.Name) 217 return resourceOPCSecurityRuleRead(d, meta) 218 } 219 220 func resourceOPCSecurityRuleDelete(d *schema.ResourceData, meta interface{}) error { 221 client := meta.(*compute.Client).SecurityRules() 222 name := d.Id() 223 224 input := compute.DeleteSecurityRuleInput{ 225 Name: name, 226 } 227 if err := client.DeleteSecurityRule(&input); err != nil { 228 return fmt.Errorf("Error deleting Security Rule: %s", err) 229 } 230 return nil 231 }