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