github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_ip_list.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceOPCSecurityIPList() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceOPCSecurityIPListCreate, 14 Read: resourceOPCSecurityIPListRead, 15 Update: resourceOPCSecurityIPListUpdate, 16 Delete: resourceOPCSecurityIPListDelete, 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 "ip_entries": { 28 Type: schema.TypeList, 29 Required: true, 30 Elem: &schema.Schema{Type: schema.TypeString}, 31 }, 32 "description": { 33 Type: schema.TypeString, 34 Optional: true, 35 }, 36 }, 37 } 38 } 39 40 func resourceOPCSecurityIPListCreate(d *schema.ResourceData, meta interface{}) error { 41 log.Printf("[DEBUG] Resource state: %#v", d.State()) 42 client := meta.(*compute.Client).SecurityIPLists() 43 44 ipEntries := d.Get("ip_entries").([]interface{}) 45 ipEntryStrings := []string{} 46 for _, entry := range ipEntries { 47 ipEntryStrings = append(ipEntryStrings, entry.(string)) 48 } 49 50 input := compute.CreateSecurityIPListInput{ 51 Name: d.Get("name").(string), 52 SecIPEntries: ipEntryStrings, 53 } 54 if description, ok := d.GetOk("description"); ok { 55 input.Description = description.(string) 56 } 57 58 log.Printf("[DEBUG] Creating security IP list with %+v", input) 59 info, err := client.CreateSecurityIPList(&input) 60 if err != nil { 61 return fmt.Errorf("Error creating security IP list %s: %s", input.Name, err) 62 } 63 64 d.SetId(info.Name) 65 return resourceOPCSecurityIPListRead(d, meta) 66 } 67 68 func resourceOPCSecurityIPListRead(d *schema.ResourceData, meta interface{}) error { 69 log.Printf("[DEBUG] Resource state: %#v", d.State()) 70 client := meta.(*compute.Client).SecurityIPLists() 71 name := d.Id() 72 73 log.Printf("[DEBUG] Reading state of security IP list %s", name) 74 input := compute.GetSecurityIPListInput{ 75 Name: name, 76 } 77 result, err := client.GetSecurityIPList(&input) 78 if err != nil { 79 // Security IP List does not exist 80 if compute.WasNotFoundError(err) { 81 d.SetId("") 82 return nil 83 } 84 return fmt.Errorf("Error reading security IP list %s: %s", name, err) 85 } 86 87 log.Printf("[DEBUG] Read state of security IP list %s: %#v", name, result) 88 d.Set("name", result.Name) 89 d.Set("ip_entries", result.SecIPEntries) 90 d.Set("description", result.Description) 91 return nil 92 } 93 94 func resourceOPCSecurityIPListUpdate(d *schema.ResourceData, meta interface{}) error { 95 log.Printf("[DEBUG] Resource state: %#v", d.State()) 96 97 client := meta.(*compute.Client).SecurityIPLists() 98 99 ipEntries := d.Get("ip_entries").([]interface{}) 100 ipEntryStrings := []string{} 101 for _, entry := range ipEntries { 102 ipEntryStrings = append(ipEntryStrings, entry.(string)) 103 } 104 input := compute.UpdateSecurityIPListInput{ 105 Name: d.Get("name").(string), 106 SecIPEntries: ipEntryStrings, 107 } 108 if description, ok := d.GetOk("description"); ok { 109 input.Description = description.(string) 110 } 111 112 log.Printf("[DEBUG] Updating security IP list with %+v", input) 113 info, err := client.UpdateSecurityIPList(&input) 114 if err != nil { 115 return fmt.Errorf("Error updating security IP list %s: %s", input.Name, err) 116 } 117 d.SetId(info.Name) 118 119 return resourceOPCSecurityIPListRead(d, meta) 120 } 121 122 func resourceOPCSecurityIPListDelete(d *schema.ResourceData, meta interface{}) error { 123 log.Printf("[DEBUG] Resource state: %#v", d.State()) 124 client := meta.(*compute.Client).SecurityIPLists() 125 name := d.Id() 126 127 log.Printf("[DEBUG] Deleting security IP list %s", name) 128 input := compute.DeleteSecurityIPListInput{ 129 Name: name, 130 } 131 if err := client.DeleteSecurityIPList(&input); err != nil { 132 return fmt.Errorf("Error deleting security IP list %s: %s", name, err) 133 } 134 return nil 135 }