github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_vpc.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/denverdino/aliyungo/ecs" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 "time" 11 ) 12 13 func resourceAliyunVpc() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAliyunVpcCreate, 16 Read: resourceAliyunVpcRead, 17 Update: resourceAliyunVpcUpdate, 18 Delete: resourceAliyunVpcDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "cidr_block": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 ValidateFunc: validateCIDRNetworkAddress, 26 }, 27 "name": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 31 value := v.(string) 32 if len(value) < 2 || len(value) > 128 { 33 errors = append(errors, fmt.Errorf("%s cannot be longer than 128 characters", k)) 34 } 35 36 if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") { 37 errors = append(errors, fmt.Errorf("%s cannot starts with http:// or https://", k)) 38 } 39 40 return 41 }, 42 }, 43 "description": &schema.Schema{ 44 Type: schema.TypeString, 45 Optional: true, 46 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 47 value := v.(string) 48 if len(value) < 2 || len(value) > 256 { 49 errors = append(errors, fmt.Errorf("%s cannot be longer than 256 characters", k)) 50 51 } 52 return 53 }, 54 }, 55 "router_id": &schema.Schema{ 56 Type: schema.TypeString, 57 Computed: true, 58 }, 59 "router_table_id": &schema.Schema{ 60 Type: schema.TypeString, 61 Computed: true, 62 }, 63 }, 64 } 65 } 66 67 func resourceAliyunVpcCreate(d *schema.ResourceData, meta interface{}) error { 68 69 args, err := buildAliyunVpcArgs(d, meta) 70 if err != nil { 71 return err 72 } 73 74 ecsconn := meta.(*AliyunClient).ecsconn 75 76 vpc, err := ecsconn.CreateVpc(args) 77 if err != nil { 78 return err 79 } 80 81 d.SetId(vpc.VpcId) 82 d.Set("router_table_id", vpc.RouteTableId) 83 84 err = ecsconn.WaitForVpcAvailable(args.RegionId, vpc.VpcId, 60) 85 if err != nil { 86 return fmt.Errorf("Timeout when WaitForVpcAvailable") 87 } 88 89 return resourceAliyunVpcRead(d, meta) 90 } 91 92 func resourceAliyunVpcRead(d *schema.ResourceData, meta interface{}) error { 93 94 client := meta.(*AliyunClient) 95 96 vpc, err := client.DescribeVpc(d.Id()) 97 if err != nil { 98 return err 99 } 100 101 if vpc == nil { 102 d.SetId("") 103 return nil 104 } 105 106 d.Set("cidr_block", vpc.CidrBlock) 107 d.Set("name", vpc.VpcName) 108 d.Set("description", vpc.Description) 109 d.Set("router_id", vpc.VRouterId) 110 111 return nil 112 } 113 114 func resourceAliyunVpcUpdate(d *schema.ResourceData, meta interface{}) error { 115 116 conn := meta.(*AliyunClient).ecsconn 117 118 d.Partial(true) 119 120 attributeUpdate := false 121 args := &ecs.ModifyVpcAttributeArgs{ 122 VpcId: d.Id(), 123 } 124 125 if d.HasChange("name") { 126 d.SetPartial("name") 127 args.VpcName = d.Get("name").(string) 128 129 attributeUpdate = true 130 } 131 132 if d.HasChange("description") { 133 d.SetPartial("description") 134 args.Description = d.Get("description").(string) 135 136 attributeUpdate = true 137 } 138 139 if attributeUpdate { 140 if err := conn.ModifyVpcAttribute(args); err != nil { 141 return err 142 } 143 } 144 145 d.Partial(false) 146 147 return nil 148 } 149 150 func resourceAliyunVpcDelete(d *schema.ResourceData, meta interface{}) error { 151 conn := meta.(*AliyunClient).ecsconn 152 153 return resource.Retry(5*time.Minute, func() *resource.RetryError { 154 err := conn.DeleteVpc(d.Id()) 155 156 if err != nil { 157 return resource.RetryableError(fmt.Errorf("Vpc in use - trying again while it is deleted.")) 158 } 159 160 args := &ecs.DescribeVpcsArgs{ 161 RegionId: getRegion(d, meta), 162 VpcId: d.Id(), 163 } 164 vpc, _, descErr := conn.DescribeVpcs(args) 165 if descErr != nil { 166 return resource.NonRetryableError(err) 167 } else if vpc == nil || len(vpc) < 1 { 168 return nil 169 } 170 171 return resource.RetryableError(fmt.Errorf("Vpc in use - trying again while it is deleted.")) 172 }) 173 } 174 175 func buildAliyunVpcArgs(d *schema.ResourceData, meta interface{}) (*ecs.CreateVpcArgs, error) { 176 args := &ecs.CreateVpcArgs{ 177 RegionId: getRegion(d, meta), 178 CidrBlock: d.Get("cidr_block").(string), 179 } 180 181 if v := d.Get("name").(string); v != "" { 182 args.VpcName = v 183 } 184 185 if v := d.Get("description").(string); v != "" { 186 args.Description = v 187 } 188 189 return args, nil 190 }