github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_vroute_entry.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "github.com/denverdino/aliyungo/ecs" 6 "github.com/hashicorp/terraform/helper/schema" 7 "strings" 8 ) 9 10 func resourceAliyunRouteEntry() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceAliyunRouteEntryCreate, 13 Read: resourceAliyunRouteEntryRead, 14 Delete: resourceAliyunRouteEntryDelete, 15 16 Schema: map[string]*schema.Schema{ 17 "router_id": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 ForceNew: true, 21 }, 22 "route_table_id": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 "destination_cidrblock": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 ForceNew: true, 31 }, 32 "nexthop_type": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 ForceNew: true, 36 ValidateFunc: validateRouteEntryNextHopType, 37 }, 38 "nexthop_id": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 ForceNew: true, 42 }, 43 }, 44 } 45 } 46 47 func resourceAliyunRouteEntryCreate(d *schema.ResourceData, meta interface{}) error { 48 conn := meta.(*AliyunClient).ecsconn 49 50 rtId := d.Get("route_table_id").(string) 51 rId := d.Get("router_id").(string) 52 cidr := d.Get("destination_cidrblock").(string) 53 nt := d.Get("nexthop_type").(string) 54 ni := d.Get("nexthop_id").(string) 55 56 args, err := buildAliyunRouteEntryArgs(d, meta) 57 if err != nil { 58 return err 59 } 60 err = conn.CreateRouteEntry(args) 61 62 if err != nil { 63 return err 64 } 65 // route_table_id:router_id:destination_cidrblock:nexthop_type:nexthop_id 66 d.SetId(rtId + ":" + rId + ":" + cidr + ":" + nt + ":" + ni) 67 d.Set("router_id", rId) 68 69 if err := conn.WaitForAllRouteEntriesAvailable(rId, rtId, defaultTimeout); err != nil { 70 return fmt.Errorf("WaitFor route entry got error: %#v", err) 71 } 72 return resourceAliyunRouteEntryRead(d, meta) 73 } 74 75 func resourceAliyunRouteEntryRead(d *schema.ResourceData, meta interface{}) error { 76 client := meta.(*AliyunClient) 77 parts := strings.Split(d.Id(), ":") 78 rtId := parts[0] 79 //rId := parts[1] 80 cidr := parts[2] 81 nexthop_type := parts[3] 82 nexthop_id := parts[4] 83 84 en, err := client.QueryRouteEntry(rtId, cidr, nexthop_type, nexthop_id) 85 86 if err != nil { 87 if notFoundError(err) { 88 d.SetId("") 89 return nil 90 } 91 return fmt.Errorf("Error route entry: %#v", err) 92 } 93 94 d.Set("route_table_id", en.RouteTableId) 95 d.Set("destination_cidrblock", en.DestinationCidrBlock) 96 d.Set("nexthop_type", en.NextHopType) 97 d.Set("nexthop_id", en.InstanceId) 98 return nil 99 } 100 101 func resourceAliyunRouteEntryDelete(d *schema.ResourceData, meta interface{}) error { 102 con := meta.(*AliyunClient).ecsconn 103 args, err := buildAliyunRouteEntryDeleteArgs(d, meta) 104 105 if err != nil { 106 return err 107 } 108 return con.DeleteRouteEntry(args) 109 } 110 111 func buildAliyunRouteEntryArgs(d *schema.ResourceData, meta interface{}) (*ecs.CreateRouteEntryArgs, error) { 112 113 args := &ecs.CreateRouteEntryArgs{ 114 RouteTableId: d.Get("route_table_id").(string), 115 DestinationCidrBlock: d.Get("destination_cidrblock").(string), 116 } 117 118 if v := d.Get("nexthop_type").(string); v != "" { 119 args.NextHopType = ecs.NextHopType(v) 120 } 121 122 if v := d.Get("nexthop_id").(string); v != "" { 123 args.NextHopId = v 124 } 125 126 return args, nil 127 } 128 129 func buildAliyunRouteEntryDeleteArgs(d *schema.ResourceData, meta interface{}) (*ecs.DeleteRouteEntryArgs, error) { 130 131 args := &ecs.DeleteRouteEntryArgs{ 132 RouteTableId: d.Get("route_table_id").(string), 133 DestinationCidrBlock: d.Get("destination_cidrblock").(string), 134 } 135 136 if v := d.Get("destination_cidrblock").(string); v != "" { 137 args.DestinationCidrBlock = v 138 } 139 140 if v := d.Get("nexthop_id").(string); v != "" { 141 args.NextHopId = v 142 } 143 144 return args, nil 145 }