github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_ip_address_association.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 resourceOPCIPAddressAssociation() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceOPCIPAddressAssociationCreate, 13 Read: resourceOPCIPAddressAssociationRead, 14 Update: resourceOPCIPAddressAssociationUpdate, 15 Delete: resourceOPCIPAddressAssociationDelete, 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 "ip_address_reservation": { 27 Type: schema.TypeString, 28 Optional: true, 29 }, 30 "vnic": { 31 Type: schema.TypeString, 32 Optional: true, 33 }, 34 "description": { 35 Type: schema.TypeString, 36 Optional: true, 37 }, 38 "tags": tagsOptionalSchema(), 39 "uri": { 40 Type: schema.TypeString, 41 Computed: true, 42 }, 43 }, 44 } 45 } 46 47 func resourceOPCIPAddressAssociationCreate(d *schema.ResourceData, meta interface{}) error { 48 client := meta.(*compute.Client).IPAddressAssociations() 49 50 input := compute.CreateIPAddressAssociationInput{ 51 Name: d.Get("name").(string), 52 } 53 54 if ipAddressReservation, ok := d.GetOk("ip_address_reservation"); ok { 55 input.IPAddressReservation = ipAddressReservation.(string) 56 } 57 58 if vnic, ok := d.GetOk("vnic"); ok { 59 input.Vnic = vnic.(string) 60 } 61 62 tags := getStringList(d, "tags") 63 if len(tags) != 0 { 64 input.Tags = tags 65 } 66 67 if description, ok := d.GetOk("description"); ok { 68 input.Description = description.(string) 69 } 70 71 info, err := client.CreateIPAddressAssociation(&input) 72 if err != nil { 73 return fmt.Errorf("Error creating IP Address Association: %s", err) 74 } 75 76 d.SetId(info.Name) 77 return resourceOPCIPAddressAssociationRead(d, meta) 78 } 79 80 func resourceOPCIPAddressAssociationRead(d *schema.ResourceData, meta interface{}) error { 81 client := meta.(*compute.Client).IPAddressAssociations() 82 name := d.Id() 83 84 getInput := compute.GetIPAddressAssociationInput{ 85 name, 86 } 87 result, err := client.GetIPAddressAssociation(&getInput) 88 if err != nil { 89 // IP Address Association does not exist 90 if compute.WasNotFoundError(err) { 91 d.SetId("") 92 return nil 93 } 94 return fmt.Errorf("Error reading IP Address Association %s: %s", name, err) 95 } 96 if result == nil { 97 d.SetId("") 98 return fmt.Errorf("Error reading IP Address Association %s: %s", name, err) 99 } 100 101 d.Set("name", result.Name) 102 d.Set("ip_address_reservation", result.IPAddressReservation) 103 d.Set("vnic", result.Vnic) 104 d.Set("description", result.Description) 105 d.Set("uri", result.Uri) 106 if err := setStringList(d, "tags", result.Tags); err != nil { 107 return err 108 } 109 return nil 110 } 111 112 func resourceOPCIPAddressAssociationUpdate(d *schema.ResourceData, meta interface{}) error { 113 client := meta.(*compute.Client).IPAddressAssociations() 114 115 input := compute.UpdateIPAddressAssociationInput{ 116 Name: d.Get("name").(string), 117 } 118 119 if ipAddressReservation, ok := d.GetOk("ip_address_reservation"); ok { 120 input.IPAddressReservation = ipAddressReservation.(string) 121 } 122 123 if vnic, ok := d.GetOk("vnic"); ok { 124 input.Vnic = vnic.(string) 125 } 126 127 tags := getStringList(d, "tags") 128 if len(tags) != 0 { 129 input.Tags = tags 130 } 131 132 if description, ok := d.GetOk("description"); ok { 133 input.Description = description.(string) 134 } 135 136 info, err := client.UpdateIPAddressAssociation(&input) 137 if err != nil { 138 return fmt.Errorf("Error updating IP Address Association: %s", err) 139 } 140 141 d.SetId(info.Name) 142 return resourceOPCIPAddressAssociationRead(d, meta) 143 } 144 145 func resourceOPCIPAddressAssociationDelete(d *schema.ResourceData, meta interface{}) error { 146 client := meta.(*compute.Client).IPAddressAssociations() 147 name := d.Id() 148 149 input := compute.DeleteIPAddressAssociationInput{ 150 Name: name, 151 } 152 if err := client.DeleteIPAddressAssociation(&input); err != nil { 153 return fmt.Errorf("Error deleting IP Address Association: %s", err) 154 } 155 return nil 156 }