|
| 1 | +package alicloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceAliCloudEaisClientInstanceAttachment() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceAliCloudEaisClientInstanceAttachmentCreate, |
| 17 | + Read: resourceAliCloudEaisClientInstanceAttachmentRead, |
| 18 | + Update: resourceAliCloudEaisClientInstanceAttachmentUpdate, |
| 19 | + Delete: resourceAliCloudEaisClientInstanceAttachmentDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Timeouts: &schema.ResourceTimeout{ |
| 24 | + Create: schema.DefaultTimeout(5 * time.Minute), |
| 25 | + Update: schema.DefaultTimeout(5 * time.Minute), |
| 26 | + Delete: schema.DefaultTimeout(5 * time.Minute), |
| 27 | + }, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "client_instance_id": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + }, |
| 34 | + "create_time": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + }, |
| 38 | + "ei_instance_type": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Computed: true, |
| 42 | + ForceNew: true, |
| 43 | + }, |
| 44 | + "category": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + }, |
| 48 | + "instance_id": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + }, |
| 53 | + "region_id": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + "status": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Optional: true, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceAliCloudEaisClientInstanceAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + |
| 68 | + client := meta.(*connectivity.AliyunClient) |
| 69 | + if InArray(fmt.Sprint(d.Get("category")), []string{"eais", ""}) { |
| 70 | + action := "AttachEai" |
| 71 | + var request map[string]interface{} |
| 72 | + var response map[string]interface{} |
| 73 | + query := make(map[string]interface{}) |
| 74 | + var err error |
| 75 | + request = make(map[string]interface{}) |
| 76 | + if v, ok := d.GetOk("instance_id"); ok { |
| 77 | + request["ElasticAcceleratedInstanceId"] = v |
| 78 | + } |
| 79 | + if v, ok := d.GetOk("client_instance_id"); ok { |
| 80 | + request["ClientInstanceId"] = v |
| 81 | + } |
| 82 | + request["RegionId"] = client.RegionId |
| 83 | + |
| 84 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 85 | + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { |
| 86 | + response, err = client.RpcPost("eais", "2019-06-24", action, query, request, true) |
| 87 | + if err != nil { |
| 88 | + if NeedRetry(err) { |
| 89 | + wait() |
| 90 | + return resource.RetryableError(err) |
| 91 | + } |
| 92 | + return resource.NonRetryableError(err) |
| 93 | + } |
| 94 | + return nil |
| 95 | + }) |
| 96 | + addDebug(action, response, request) |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + return WrapErrorf(err, DefaultErrorMsg, "alicloud_eais_client_instance_attachment", action, AlibabaCloudSdkGoERROR) |
| 100 | + } |
| 101 | + |
| 102 | + d.SetId(fmt.Sprintf("%v:%v", response["ElasticAcceleratedInstanceId"], response["ClientInstanceId"])) |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + if v, ok := d.GetOk("category"); ok && InArray(fmt.Sprint(v), []string{"ei"}) { |
| 107 | + action := "AttachEaisEi" |
| 108 | + var request map[string]interface{} |
| 109 | + var response map[string]interface{} |
| 110 | + query := make(map[string]interface{}) |
| 111 | + var err error |
| 112 | + request = make(map[string]interface{}) |
| 113 | + if v, ok := d.GetOk("instance_id"); ok { |
| 114 | + request["EiInstanceId"] = v |
| 115 | + } |
| 116 | + if v, ok := d.GetOk("client_instance_id"); ok { |
| 117 | + request["ClientInstanceId"] = v |
| 118 | + } |
| 119 | + request["RegionId"] = client.RegionId |
| 120 | + |
| 121 | + if v, ok := d.GetOk("ei_instance_type"); ok { |
| 122 | + request["EiInstanceType"] = v |
| 123 | + } |
| 124 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 125 | + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { |
| 126 | + response, err = client.RpcPost("eais", "2019-06-24", action, query, request, true) |
| 127 | + if err != nil { |
| 128 | + if NeedRetry(err) { |
| 129 | + wait() |
| 130 | + return resource.RetryableError(err) |
| 131 | + } |
| 132 | + return resource.NonRetryableError(err) |
| 133 | + } |
| 134 | + return nil |
| 135 | + }) |
| 136 | + addDebug(action, response, request) |
| 137 | + |
| 138 | + if err != nil { |
| 139 | + return WrapErrorf(err, DefaultErrorMsg, "alicloud_eais_client_instance_attachment", action, AlibabaCloudSdkGoERROR) |
| 140 | + } |
| 141 | + |
| 142 | + d.SetId(fmt.Sprintf("%v:%v", response["EiInstanceId"], response["ClientInstanceId"])) |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + return resourceAliCloudEaisClientInstanceAttachmentUpdate(d, meta) |
| 147 | +} |
| 148 | + |
| 149 | +func resourceAliCloudEaisClientInstanceAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 150 | + client := meta.(*connectivity.AliyunClient) |
| 151 | + eaisServiceV2 := EaisServiceV2{client} |
| 152 | + |
| 153 | + objectRaw, err := eaisServiceV2.DescribeEaisClientInstanceAttachment(d.Id()) |
| 154 | + if err != nil { |
| 155 | + if !d.IsNewResource() && NotFoundError(err) { |
| 156 | + log.Printf("[DEBUG] Resource alicloud_eais_client_instance_attachment DescribeEaisClientInstanceAttachment Failed!!! %s", err) |
| 157 | + d.SetId("") |
| 158 | + return nil |
| 159 | + } |
| 160 | + return WrapError(err) |
| 161 | + } |
| 162 | + |
| 163 | + d.Set("create_time", objectRaw["StartTime"]) |
| 164 | + d.Set("ei_instance_type", objectRaw["InstanceType"]) |
| 165 | + d.Set("region_id", objectRaw["RegionId"]) |
| 166 | + d.Set("status", objectRaw["Status"]) |
| 167 | + d.Set("client_instance_id", objectRaw["ClientInstanceId"]) |
| 168 | + d.Set("instance_id", objectRaw["ElasticAcceleratedInstanceId"]) |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +func resourceAliCloudEaisClientInstanceAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { |
| 174 | + client := meta.(*connectivity.AliyunClient) |
| 175 | + var request map[string]interface{} |
| 176 | + var response map[string]interface{} |
| 177 | + var query map[string]interface{} |
| 178 | + |
| 179 | + enableAction := false |
| 180 | + if v, ok := d.GetOk("category"); ok && InArray(fmt.Sprint(v), []string{"ei"}) { |
| 181 | + enableAction = true |
| 182 | + } |
| 183 | + if enableAction && d.HasChange("status") { |
| 184 | + eaisServiceV2 := EaisServiceV2{client} |
| 185 | + object, err := eaisServiceV2.DescribeEaisClientInstanceAttachment(d.Id()) |
| 186 | + if err != nil { |
| 187 | + return WrapError(err) |
| 188 | + } |
| 189 | + |
| 190 | + target := d.Get("status").(string) |
| 191 | + if object["Status"].(string) != target { |
| 192 | + if target == "InUse" { |
| 193 | + parts := strings.Split(d.Id(), ":") |
| 194 | + action := "StartEaisEi" |
| 195 | + request = make(map[string]interface{}) |
| 196 | + query = make(map[string]interface{}) |
| 197 | + request["EiInstanceId"] = parts[0] |
| 198 | + request["RegionId"] = client.RegionId |
| 199 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 200 | + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { |
| 201 | + response, err = client.RpcPost("eais", "2019-06-24", action, query, request, true) |
| 202 | + if err != nil { |
| 203 | + if NeedRetry(err) { |
| 204 | + wait() |
| 205 | + return resource.RetryableError(err) |
| 206 | + } |
| 207 | + return resource.NonRetryableError(err) |
| 208 | + } |
| 209 | + return nil |
| 210 | + }) |
| 211 | + addDebug(action, response, request) |
| 212 | + if err != nil { |
| 213 | + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) |
| 214 | + } |
| 215 | + |
| 216 | + } |
| 217 | + if target == "Bound" { |
| 218 | + parts := strings.Split(d.Id(), ":") |
| 219 | + action := "StopEaisEi" |
| 220 | + request = make(map[string]interface{}) |
| 221 | + query = make(map[string]interface{}) |
| 222 | + request["EiInstanceId"] = parts[0] |
| 223 | + request["RegionId"] = client.RegionId |
| 224 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 225 | + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { |
| 226 | + response, err = client.RpcPost("eais", "2019-06-24", action, query, request, true) |
| 227 | + if err != nil { |
| 228 | + if NeedRetry(err) { |
| 229 | + wait() |
| 230 | + return resource.RetryableError(err) |
| 231 | + } |
| 232 | + return resource.NonRetryableError(err) |
| 233 | + } |
| 234 | + return nil |
| 235 | + }) |
| 236 | + addDebug(action, response, request) |
| 237 | + if err != nil { |
| 238 | + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) |
| 239 | + } |
| 240 | + |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + return resourceAliCloudEaisClientInstanceAttachmentRead(d, meta) |
| 246 | +} |
| 247 | + |
| 248 | +func resourceAliCloudEaisClientInstanceAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 249 | + |
| 250 | + enableDelete := false |
| 251 | + if InArray(fmt.Sprint(d.Get("category")), []string{"eais", ""}) { |
| 252 | + enableDelete = true |
| 253 | + } |
| 254 | + if enableDelete { |
| 255 | + client := meta.(*connectivity.AliyunClient) |
| 256 | + parts := strings.Split(d.Id(), ":") |
| 257 | + action := "DetachEai" |
| 258 | + var request map[string]interface{} |
| 259 | + var response map[string]interface{} |
| 260 | + query := make(map[string]interface{}) |
| 261 | + var err error |
| 262 | + request = make(map[string]interface{}) |
| 263 | + request["ElasticAcceleratedInstanceId"] = parts[0] |
| 264 | + request["RegionId"] = client.RegionId |
| 265 | + |
| 266 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 267 | + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { |
| 268 | + response, err = client.RpcPost("eais", "2019-06-24", action, query, request, true) |
| 269 | + |
| 270 | + if err != nil { |
| 271 | + if NeedRetry(err) { |
| 272 | + wait() |
| 273 | + return resource.RetryableError(err) |
| 274 | + } |
| 275 | + return resource.NonRetryableError(err) |
| 276 | + } |
| 277 | + return nil |
| 278 | + }) |
| 279 | + addDebug(action, response, request) |
| 280 | + |
| 281 | + if err != nil { |
| 282 | + if NotFoundError(err) { |
| 283 | + return nil |
| 284 | + } |
| 285 | + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) |
| 286 | + } |
| 287 | + |
| 288 | + } |
| 289 | + |
| 290 | + enableDelete = false |
| 291 | + if v, ok := d.GetOk("category"); ok { |
| 292 | + if InArray(fmt.Sprint(v), []string{"ei"}) { |
| 293 | + enableDelete = true |
| 294 | + } |
| 295 | + } |
| 296 | + if enableDelete { |
| 297 | + client := meta.(*connectivity.AliyunClient) |
| 298 | + parts := strings.Split(d.Id(), ":") |
| 299 | + action := "DetachEaisEi" |
| 300 | + var request map[string]interface{} |
| 301 | + var response map[string]interface{} |
| 302 | + query := make(map[string]interface{}) |
| 303 | + var err error |
| 304 | + request = make(map[string]interface{}) |
| 305 | + request["EiInstanceId"] = parts[0] |
| 306 | + request["RegionId"] = client.RegionId |
| 307 | + |
| 308 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 309 | + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { |
| 310 | + response, err = client.RpcPost("eais", "2019-06-24", action, query, request, true) |
| 311 | + |
| 312 | + if err != nil { |
| 313 | + if NeedRetry(err) { |
| 314 | + wait() |
| 315 | + return resource.RetryableError(err) |
| 316 | + } |
| 317 | + return resource.NonRetryableError(err) |
| 318 | + } |
| 319 | + return nil |
| 320 | + }) |
| 321 | + addDebug(action, response, request) |
| 322 | + |
| 323 | + if err != nil { |
| 324 | + if NotFoundError(err) { |
| 325 | + return nil |
| 326 | + } |
| 327 | + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) |
| 328 | + } |
| 329 | + |
| 330 | + } |
| 331 | + return nil |
| 332 | +} |
0 commit comments