github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_ip_address_reservation.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 resourceOPCIPAddressReservation() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceOPCIPAddressReservationCreate, 14 Read: resourceOPCIPAddressReservationRead, 15 Update: resourceOPCIPAddressReservationUpdate, 16 Delete: resourceOPCIPAddressReservationDelete, 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_address_pool": { 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 ValidateFunc: validation.StringInSlice([]string{ 32 string(compute.PublicIPAddressPool), 33 string(compute.PrivateIPAddressPool), 34 }, true), 35 }, 36 "description": { 37 Type: schema.TypeString, 38 Optional: true, 39 }, 40 "tags": tagsOptionalSchema(), 41 "ip_address": { 42 Type: schema.TypeString, 43 Computed: true, 44 }, 45 "uri": { 46 Type: schema.TypeString, 47 Computed: true, 48 }, 49 }, 50 } 51 } 52 53 func resourceOPCIPAddressReservationCreate(d *schema.ResourceData, meta interface{}) error { 54 client := meta.(*compute.Client).IPAddressReservations() 55 56 input := compute.CreateIPAddressReservationInput{ 57 Name: d.Get("name").(string), 58 IPAddressPool: d.Get("ip_address_pool").(string), 59 } 60 tags := getStringList(d, "tags") 61 if len(tags) != 0 { 62 input.Tags = tags 63 } 64 if description, ok := d.GetOk("description"); ok { 65 input.Description = description.(string) 66 } 67 68 info, err := client.CreateIPAddressReservation(&input) 69 if err != nil { 70 return fmt.Errorf("Error creating IP Address Reservation: %s", err) 71 } 72 d.SetId(info.Name) 73 return resourceOPCIPAddressReservationRead(d, meta) 74 } 75 76 func resourceOPCIPAddressReservationRead(d *schema.ResourceData, meta interface{}) error { 77 client := meta.(*compute.Client).IPAddressReservations() 78 79 getInput := compute.GetIPAddressReservationInput{ 80 Name: d.Id(), 81 } 82 result, err := client.GetIPAddressReservation(&getInput) 83 if err != nil { 84 // IP Address Reservation does not exist 85 if compute.WasNotFoundError(err) { 86 d.SetId("") 87 return nil 88 } 89 return fmt.Errorf("Error reading ip address reservation %s: %s", d.Id(), err) 90 } 91 92 d.Set("name", result.Name) 93 d.Set("description", result.Description) 94 d.Set("ip_address_pool", result.IPAddressPool) 95 d.Set("uri", result.Uri) 96 97 if err := setStringList(d, "tags", result.Tags); err != nil { 98 return err 99 } 100 return nil 101 } 102 103 func resourceOPCIPAddressReservationUpdate(d *schema.ResourceData, meta interface{}) error { 104 client := meta.(*compute.Client).IPAddressReservations() 105 106 input := compute.UpdateIPAddressReservationInput{ 107 Name: d.Get("name").(string), 108 IPAddressPool: d.Get("ip_address_pool").(string), 109 } 110 tags := getStringList(d, "tags") 111 if len(tags) != 0 { 112 input.Tags = tags 113 } 114 if description, ok := d.GetOk("description"); ok { 115 input.Description = description.(string) 116 } 117 118 info, err := client.UpdateIPAddressReservation(&input) 119 if err != nil { 120 return fmt.Errorf("Error updating IP Address Reservation: %s", err) 121 } 122 d.SetId(info.Name) 123 return resourceOPCIPAddressReservationRead(d, meta) 124 } 125 126 func resourceOPCIPAddressReservationDelete(d *schema.ResourceData, meta interface{}) error { 127 client := meta.(*compute.Client).IPAddressReservations() 128 name := d.Id() 129 130 input := compute.DeleteIPAddressReservationInput{ 131 Name: name, 132 } 133 if err := client.DeleteIPAddressReservation(&input); err != nil { 134 return fmt.Errorf("Error deleting IP Address Reservation: %+v", err) 135 } 136 return nil 137 }