github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_protocol.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 resourceOPCSecurityProtocol() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceOPCSecurityProtocolCreate, 13 Read: resourceOPCSecurityProtocolRead, 14 Update: resourceOPCSecurityProtocolUpdate, 15 Delete: resourceOPCSecurityProtocolDelete, 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 "dst_ports": { 27 Type: schema.TypeList, 28 Optional: true, 29 Elem: &schema.Schema{Type: schema.TypeString}, 30 }, 31 "description": { 32 Type: schema.TypeString, 33 Optional: true, 34 ForceNew: true, 35 }, 36 "ip_protocol": { 37 Type: schema.TypeString, 38 Optional: true, 39 Default: string(compute.All), 40 ValidateFunc: validateIPProtocol, 41 }, 42 "src_ports": { 43 Type: schema.TypeList, 44 Optional: true, 45 Elem: &schema.Schema{Type: schema.TypeString}, 46 }, 47 "tags": tagsForceNewSchema(), 48 "uri": { 49 Type: schema.TypeString, 50 Computed: true, 51 }, 52 }, 53 } 54 } 55 56 func resourceOPCSecurityProtocolCreate(d *schema.ResourceData, meta interface{}) error { 57 client := meta.(*compute.Client).SecurityProtocols() 58 input := compute.CreateSecurityProtocolInput{ 59 Name: d.Get("name").(string), 60 IPProtocol: d.Get("ip_protocol").(string), 61 } 62 dstPorts := getStringList(d, "dst_ports") 63 if len(dstPorts) != 0 { 64 input.DstPortSet = dstPorts 65 } 66 srcPorts := getStringList(d, "src_ports") 67 if len(srcPorts) != 0 { 68 input.SrcPortSet = srcPorts 69 } 70 tags := getStringList(d, "tags") 71 if len(tags) != 0 { 72 input.Tags = tags 73 } 74 75 if description, ok := d.GetOk("description"); ok { 76 input.Description = description.(string) 77 } 78 79 info, err := client.CreateSecurityProtocol(&input) 80 if err != nil { 81 return fmt.Errorf("Error creating Security Protocol: %s", err) 82 } 83 84 d.SetId(info.Name) 85 return resourceOPCSecurityProtocolRead(d, meta) 86 } 87 88 func resourceOPCSecurityProtocolRead(d *schema.ResourceData, meta interface{}) error { 89 client := meta.(*compute.Client).SecurityProtocols() 90 getInput := compute.GetSecurityProtocolInput{ 91 Name: d.Id(), 92 } 93 result, err := client.GetSecurityProtocol(&getInput) 94 if err != nil { 95 // Security Protocol does not exist 96 if compute.WasNotFoundError(err) { 97 d.SetId("") 98 return nil 99 } 100 return fmt.Errorf("Error reading security protocol %s: %s", d.Id(), err) 101 } 102 103 d.Set("name", result.Name) 104 d.Set("ip_protocol", result.IPProtocol) 105 d.Set("description", result.Description) 106 if err := setStringList(d, "dst_ports", result.DstPortSet); err != nil { 107 return err 108 } 109 if err := setStringList(d, "src_ports", result.SrcPortSet); err != nil { 110 return err 111 } 112 if err := setStringList(d, "tags", result.Tags); err != nil { 113 return err 114 } 115 return nil 116 } 117 118 func resourceOPCSecurityProtocolUpdate(d *schema.ResourceData, meta interface{}) error { 119 client := meta.(*compute.Client).SecurityProtocols() 120 input := compute.UpdateSecurityProtocolInput{ 121 Name: d.Get("name").(string), 122 IPProtocol: d.Get("ip_protocol").(string), 123 } 124 dstPorts := getStringList(d, "dst_ports") 125 if len(dstPorts) != 0 { 126 input.DstPortSet = dstPorts 127 } 128 srcPorts := getStringList(d, "src_ports") 129 if len(srcPorts) != 0 { 130 input.SrcPortSet = srcPorts 131 } 132 tags := getStringList(d, "tags") 133 if len(tags) != 0 { 134 input.Tags = tags 135 } 136 if description, ok := d.GetOk("description"); ok { 137 input.Description = description.(string) 138 } 139 140 info, err := client.UpdateSecurityProtocol(&input) 141 if err != nil { 142 return fmt.Errorf("Error updating Security Protocol: %s", err) 143 } 144 145 d.SetId(info.Name) 146 return resourceOPCSecurityProtocolRead(d, meta) 147 } 148 149 func resourceOPCSecurityProtocolDelete(d *schema.ResourceData, meta interface{}) error { 150 client := meta.(*compute.Client).SecurityProtocols() 151 name := d.Id() 152 153 input := compute.DeleteSecurityProtocolInput{ 154 Name: name, 155 } 156 if err := client.DeleteSecurityProtocol(&input); err != nil { 157 return fmt.Errorf("Error deleting Security Protocol: %s", err) 158 } 159 return nil 160 }