github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_snat.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "github.com/denverdino/aliyungo/ecs" 6 "github.com/hashicorp/terraform/helper/schema" 7 ) 8 9 func resourceAliyunSnatEntry() *schema.Resource { 10 return &schema.Resource{ 11 Create: resourceAliyunSnatEntryCreate, 12 Read: resourceAliyunSnatEntryRead, 13 Update: resourceAliyunSnatEntryUpdate, 14 Delete: resourceAliyunSnatEntryDelete, 15 16 Schema: map[string]*schema.Schema{ 17 "snat_table_id": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 ForceNew: true, 21 }, 22 "source_vswitch_id": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 "snat_ip": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 }, 31 }, 32 } 33 } 34 35 func resourceAliyunSnatEntryCreate(d *schema.ResourceData, meta interface{}) error { 36 conn := meta.(*AliyunClient).vpcconn 37 38 args := &ecs.CreateSnatEntryArgs{ 39 RegionId: getRegion(d, meta), 40 SnatTableId: d.Get("snat_table_id").(string), 41 SourceVSwitchId: d.Get("source_vswitch_id").(string), 42 SnatIp: d.Get("snat_ip").(string), 43 } 44 45 resp, err := conn.CreateSnatEntry(args) 46 if err != nil { 47 return fmt.Errorf("CreateSnatEntry got error: %#v", err) 48 } 49 50 d.SetId(resp.SnatEntryId) 51 d.Set("snat_table_id", d.Get("snat_table_id").(string)) 52 53 return resourceAliyunSnatEntryRead(d, meta) 54 } 55 56 func resourceAliyunSnatEntryRead(d *schema.ResourceData, meta interface{}) error { 57 client := meta.(*AliyunClient) 58 59 snatEntry, err := client.DescribeSnatEntry(d.Get("snat_table_id").(string), d.Id()) 60 61 if err != nil { 62 if notFoundError(err) { 63 return nil 64 } 65 return err 66 } 67 68 d.Set("snat_table_id", snatEntry.SnatTableId) 69 d.Set("source_vswitch_id", snatEntry.SourceVSwitchId) 70 d.Set("snat_ip", snatEntry.SnatIp) 71 72 return nil 73 } 74 75 func resourceAliyunSnatEntryUpdate(d *schema.ResourceData, meta interface{}) error { 76 client := meta.(*AliyunClient) 77 conn := client.vpcconn 78 79 snatEntry, err := client.DescribeSnatEntry(d.Get("snat_table_id").(string), d.Id()) 80 if err != nil { 81 return err 82 } 83 84 d.Partial(true) 85 attributeUpdate := false 86 args := &ecs.ModifySnatEntryArgs{ 87 RegionId: getRegion(d, meta), 88 SnatTableId: snatEntry.SnatTableId, 89 SnatEntryId: snatEntry.SnatEntryId, 90 } 91 92 if d.HasChange("snat_ip") { 93 d.SetPartial("snat_ip") 94 var snat_ip string 95 if v, ok := d.GetOk("snat_ip"); ok { 96 snat_ip = v.(string) 97 } else { 98 return fmt.Errorf("cann't change snap_ip to empty string") 99 } 100 args.SnatIp = snat_ip 101 102 attributeUpdate = true 103 } 104 105 if attributeUpdate { 106 if err := conn.ModifySnatEntry(args); err != nil { 107 return err 108 } 109 } 110 111 d.Partial(false) 112 113 return resourceAliyunSnatEntryRead(d, meta) 114 } 115 116 func resourceAliyunSnatEntryDelete(d *schema.ResourceData, meta interface{}) error { 117 client := meta.(*AliyunClient) 118 conn := client.vpcconn 119 120 snatEntryId := d.Id() 121 snatTableId := d.Get("snat_table_id").(string) 122 123 args := &ecs.DeleteSnatEntryArgs{ 124 RegionId: getRegion(d, meta), 125 SnatTableId: snatTableId, 126 SnatEntryId: snatEntryId, 127 } 128 129 if err := conn.DeleteSnatEntry(args); err != nil { 130 return err 131 } 132 133 return nil 134 }