github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_ip_reservation.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 resourceOPCIPReservation() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceOPCIPReservationCreate, 14 Read: resourceOPCIPReservationRead, 15 Delete: resourceOPCIPReservationDelete, 16 Importer: &schema.ResourceImporter{ 17 State: schema.ImportStatePassthrough, 18 }, 19 20 Schema: map[string]*schema.Schema{ 21 "name": { 22 Type: schema.TypeString, 23 Optional: true, 24 Computed: true, 25 }, 26 "permanent": { 27 Type: schema.TypeBool, 28 Required: true, 29 ForceNew: true, 30 }, 31 "parent_pool": { 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 }, 36 "tags": tagsForceNewSchema(), 37 "ip": { 38 Type: schema.TypeString, 39 Computed: true, 40 }, 41 }, 42 } 43 } 44 45 func resourceOPCIPReservationCreate(d *schema.ResourceData, meta interface{}) error { 46 log.Printf("[DEBUG] Resource state: %#v", d.State()) 47 48 reservation := compute.CreateIPReservationInput{ 49 Name: d.Get("name").(string), 50 ParentPool: compute.IPReservationPool(d.Get("parent_pool").(string)), 51 Permanent: d.Get("permanent").(bool), 52 } 53 54 tags := getStringList(d, "tags") 55 if len(tags) != 0 { 56 reservation.Tags = tags 57 } 58 59 log.Printf("[DEBUG] Creating ip reservation from parent_pool %s with tags=%s", 60 reservation.ParentPool, reservation.Tags) 61 62 client := meta.(*compute.Client).IPReservations() 63 info, err := client.CreateIPReservation(&reservation) 64 if err != nil { 65 return fmt.Errorf("Error creating ip reservation from parent_pool %s with tags=%s: %s", 66 reservation.ParentPool, reservation.Tags, err) 67 } 68 69 d.SetId(info.Name) 70 return resourceOPCIPReservationRead(d, meta) 71 } 72 73 func resourceOPCIPReservationRead(d *schema.ResourceData, meta interface{}) error { 74 log.Printf("[DEBUG] Resource state: %#v", d.State()) 75 client := meta.(*compute.Client).IPReservations() 76 77 log.Printf("[DEBUG] Reading state of ip reservation %s", d.Id()) 78 getInput := compute.GetIPReservationInput{ 79 Name: d.Id(), 80 } 81 result, err := client.GetIPReservation(&getInput) 82 if err != nil { 83 // IP Reservation does not exist 84 if compute.WasNotFoundError(err) { 85 d.SetId("") 86 return nil 87 } 88 return fmt.Errorf("Error reading ip reservation %s: %s", d.Id(), err) 89 } 90 91 log.Printf("[DEBUG] Read state of ip reservation %s: %#v", d.Id(), result) 92 d.Set("name", result.Name) 93 d.Set("parent_pool", result.ParentPool) 94 d.Set("permanent", result.Permanent) 95 96 if err := setStringList(d, "tags", result.Tags); err != nil { 97 return err 98 } 99 100 d.Set("ip", result.IP) 101 return nil 102 } 103 104 func resourceOPCIPReservationDelete(d *schema.ResourceData, meta interface{}) error { 105 log.Printf("[DEBUG] Resource state: %#v", d.State()) 106 client := meta.(*compute.Client).IPReservations() 107 108 log.Printf("[DEBUG] Deleting ip reservation %s", d.Id()) 109 110 input := compute.DeleteIPReservationInput{ 111 Name: d.Id(), 112 } 113 if err := client.DeleteIPReservation(&input); err != nil { 114 return fmt.Errorf("Error deleting ip reservation %s", d.Id()) 115 } 116 return nil 117 }