github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_forward.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 resourceAliyunForwardEntry() *schema.Resource {
    10  	return &schema.Resource{
    11  		Create: resourceAliyunForwardEntryCreate,
    12  		Read:   resourceAliyunForwardEntryRead,
    13  		Update: resourceAliyunForwardEntryUpdate,
    14  		Delete: resourceAliyunForwardEntryDelete,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"forward_table_id": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  				ForceNew: true,
    21  			},
    22  			"external_ip": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"external_port": &schema.Schema{
    28  				Type:         schema.TypeString,
    29  				Required:     true,
    30  				ValidateFunc: validateForwardPort,
    31  			},
    32  			"ip_protocol": &schema.Schema{
    33  				Type:         schema.TypeString,
    34  				Required:     true,
    35  				ValidateFunc: validateAllowedStringValue([]string{"tcp", "udp", "any"}),
    36  			},
    37  			"internal_ip": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  			},
    41  			"internal_port": &schema.Schema{
    42  				Type:         schema.TypeString,
    43  				Required:     true,
    44  				ValidateFunc: validateForwardPort,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func resourceAliyunForwardEntryCreate(d *schema.ResourceData, meta interface{}) error {
    51  	conn := meta.(*AliyunClient).vpcconn
    52  
    53  	args := &ecs.CreateForwardEntryArgs{
    54  		RegionId:       getRegion(d, meta),
    55  		ForwardTableId: d.Get("forward_table_id").(string),
    56  		ExternalIp:     d.Get("external_ip").(string),
    57  		ExternalPort:   d.Get("external_port").(string),
    58  		IpProtocol:     d.Get("ip_protocol").(string),
    59  		InternalIp:     d.Get("internal_ip").(string),
    60  		InternalPort:   d.Get("internal_port").(string),
    61  	}
    62  
    63  	resp, err := conn.CreateForwardEntry(args)
    64  	if err != nil {
    65  		return fmt.Errorf("CreateForwardEntry got error: %#v", err)
    66  	}
    67  
    68  	d.SetId(resp.ForwardEntryId)
    69  	d.Set("forward_table_id", d.Get("forward_table_id").(string))
    70  
    71  	return resourceAliyunForwardEntryRead(d, meta)
    72  }
    73  
    74  func resourceAliyunForwardEntryRead(d *schema.ResourceData, meta interface{}) error {
    75  	client := meta.(*AliyunClient)
    76  
    77  	forwardEntry, err := client.DescribeForwardEntry(d.Get("forward_table_id").(string), d.Id())
    78  
    79  	if err != nil {
    80  		if notFoundError(err) {
    81  			return nil
    82  		}
    83  		return err
    84  	}
    85  
    86  	d.Set("forward_table_id", forwardEntry.ForwardTableId)
    87  	d.Set("external_ip", forwardEntry.ExternalIp)
    88  	d.Set("external_port", forwardEntry.ExternalPort)
    89  	d.Set("ip_protocol", forwardEntry.IpProtocol)
    90  	d.Set("internal_ip", forwardEntry.InternalIp)
    91  	d.Set("internal_port", forwardEntry.InternalPort)
    92  
    93  	return nil
    94  }
    95  
    96  func resourceAliyunForwardEntryUpdate(d *schema.ResourceData, meta interface{}) error {
    97  	client := meta.(*AliyunClient)
    98  	conn := client.vpcconn
    99  
   100  	forwardEntry, err := client.DescribeForwardEntry(d.Get("forward_table_id").(string), d.Id())
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	d.Partial(true)
   106  	attributeUpdate := false
   107  	args := &ecs.ModifyForwardEntryArgs{
   108  		RegionId:       getRegion(d, meta),
   109  		ForwardTableId: forwardEntry.ForwardTableId,
   110  		ForwardEntryId: forwardEntry.ForwardEntryId,
   111  		ExternalIp:     forwardEntry.ExternalIp,
   112  		IpProtocol:     forwardEntry.IpProtocol,
   113  		ExternalPort:   forwardEntry.ExternalPort,
   114  		InternalIp:     forwardEntry.InternalIp,
   115  		InternalPort:   forwardEntry.InternalPort,
   116  	}
   117  
   118  	if d.HasChange("external_port") {
   119  		d.SetPartial("external_port")
   120  		args.ExternalPort = d.Get("external_port").(string)
   121  		attributeUpdate = true
   122  	}
   123  
   124  	if d.HasChange("ip_protocol") {
   125  		d.SetPartial("ip_protocol")
   126  		args.IpProtocol = d.Get("ip_protocol").(string)
   127  		attributeUpdate = true
   128  	}
   129  
   130  	if d.HasChange("internal_port") {
   131  		d.SetPartial("internal_port")
   132  		args.InternalPort = d.Get("internal_port").(string)
   133  		attributeUpdate = true
   134  	}
   135  
   136  	if attributeUpdate {
   137  		if err := conn.ModifyForwardEntry(args); err != nil {
   138  			return err
   139  		}
   140  	}
   141  
   142  	d.Partial(false)
   143  
   144  	return resourceAliyunForwardEntryRead(d, meta)
   145  }
   146  
   147  func resourceAliyunForwardEntryDelete(d *schema.ResourceData, meta interface{}) error {
   148  	client := meta.(*AliyunClient)
   149  	conn := client.vpcconn
   150  
   151  	forwardEntryId := d.Id()
   152  	forwardTableId := d.Get("forward_table_id").(string)
   153  
   154  	args := &ecs.DeleteForwardEntryArgs{
   155  		RegionId:       getRegion(d, meta),
   156  		ForwardTableId: forwardTableId,
   157  		ForwardEntryId: forwardEntryId,
   158  	}
   159  
   160  	if err := conn.DeleteForwardEntry(args); err != nil {
   161  		return err
   162  	}
   163  
   164  	return nil
   165  }