-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45aec40
commit 39cce6a
Showing
12 changed files
with
670 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package alicloud | ||
|
||
import ( | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity" | ||
) | ||
|
||
func dataSourceAlicloudRenewInterfaces() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAlicloudRenewInstanceRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"instance_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"renewal_status": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "Normal", | ||
ValidateFunc: validation.StringInSlice([]string{"Normal", "AutoRenewal", "NotRenewal"}, false), | ||
}, | ||
// Computed values | ||
"instances": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"duration": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"pricing_cycle": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAlicloudRenewInstanceRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
request := vpc.CreateDescribeInstanceAutoRenewAttributeRequest() | ||
request.RegionId = string(client.Region) | ||
request.InstanceType = d.Get("instance_type").(string) | ||
request.RenewalStatus = d.Get("renewal_status").(string) | ||
if v, ok := d.GetOk("instance_id"); ok { | ||
request.InstanceId = v.(string) | ||
} | ||
invoker := NewInvoker() | ||
var err error | ||
var ids []string | ||
var raw interface{} | ||
if err := invoker.Run(func() error { | ||
raw, err = client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) { | ||
return vpcClient.DescribeInstanceAutoRenewAttribute(request) | ||
}) | ||
addDebug(request.GetActionName(), raw, request.RpcRequest, request) | ||
return err | ||
}); err != nil { | ||
return WrapErrorf(err, DataDefaultErrorMsg, "DescribeInstanceAutoRenewAttribute", request.GetActionName(), AlibabaCloudSdkGoERROR) | ||
} | ||
response := raw.(*vpc.DescribeInstanceAutoRenewAttributeResponse) | ||
var s []map[string]interface{} | ||
if len(response.InstanceRenewAttributes.InstanceRenewAttribute) > 0 { | ||
for _, val := range response.InstanceRenewAttributes.InstanceRenewAttribute { | ||
mapping := map[string]interface{}{ | ||
"duration": val.Duration, | ||
"status": val.RenewalStatus, | ||
"pricing_cycle": val.PricingCycle, | ||
"instance_id": val.InstanceId, | ||
} | ||
s = append(s, mapping) | ||
ids = append(ids, val.InstanceId) | ||
} | ||
} | ||
|
||
d.SetId(dataResourceIdHash(ids)) | ||
if err := d.Set("instances", s); err != nil { | ||
return WrapError(err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package alicloud | ||
|
||
import ( | ||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity" | ||
"time" | ||
) | ||
|
||
func resourceAlicloudRenewInterfaceNetwork() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAlicloudRenewInterfaceNetworkCreate, | ||
Read: resourceAlicloudRenewInterfaceNetworkRead, | ||
Update: resourceAlicloudRenewInterfaceNetworkUpdate, | ||
Delete: resourceAlicloudRenewInterfaceNetworkDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
"pricing_cycle": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"duration": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"instance_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"owner_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"resource_owner_account": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"resource_owner_id": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAlicloudRenewInterfaceNetworkCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
request := vpc.CreateRenewInstanceRequest() | ||
request.RegionId = client.RegionId | ||
request.Scheme = "https" | ||
request.InstanceId = d.Get("instance_id").(string) | ||
request.PricingCycle = d.Get("pricing_cycle").(string) | ||
request.Duration = requests.NewInteger(d.Get("duration").(int)) | ||
request.InstanceType = d.Get("instance_type").(string) | ||
|
||
if v, ok := d.GetOk("owner_id"); ok { | ||
request.OwnerId = requests.NewInteger(v.(int)) | ||
} | ||
if v := d.Get("resource_owner_account").(string); v != "" { | ||
request.ResourceOwnerAccount = v | ||
} | ||
if v := d.Get("resource_owner_id"); v != "" { | ||
request.ResourceOwnerId = requests.NewInteger(v.(int)) | ||
} | ||
|
||
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) { | ||
return vpcClient.RenewInstance(request) | ||
}) | ||
if err != nil { | ||
return WrapErrorf(err, DefaultErrorMsg, "RenewNetworkInstance", request.GetActionName(), AlibabaCloudSdkGoERROR) | ||
} | ||
|
||
response, _ := raw.(*vpc.RenewInstanceResponse) | ||
if response != nil { | ||
d.SetId(d.Get("instance_id").(string)) | ||
} else { | ||
return WrapError(Error("The response is nil")) | ||
} | ||
time.Sleep(10 * time.Second) | ||
return resourceAlicloudRenewInterfaceNetworkRead(d, meta) | ||
} | ||
|
||
func resourceAlicloudRenewInterfaceNetworkRead(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} | ||
|
||
func resourceAlicloudRenewInterfaceNetworkUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
d.Partial(true) | ||
wait := incrementalWait(5*time.Second, 5*time.Second) | ||
|
||
if d.HasChange("instance_id") || d.HasChange("pricing_cycle") || d.HasChange("duration") || d.HasChange("instance_type") || | ||
d.HasChange("owner_id") || d.HasChange("resource_owner_account") || d.HasChange("resource_owner_id") { | ||
request := vpc.CreateModifyInstanceAutoRenewalAttributeRequest() | ||
request.RegionId = client.RegionId | ||
request.Scheme = "https" | ||
request.InstanceId = d.Get("instance_id").(string) | ||
request.PricingCycle = d.Get("pricing_cycle").(string) | ||
request.Duration = requests.NewInteger(d.Get("duration").(int)) | ||
request.InstanceType = d.Get("instance_type").(string) | ||
|
||
err := resource.Retry(5*time.Minute, func() *resource.RetryError { | ||
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) { | ||
return vpcClient.ModifyInstanceAutoRenewalAttribute(request) | ||
}) | ||
if err != nil { | ||
if IsExpectedErrors(err, []string{RenewIncorrectStatus}) { | ||
wait() | ||
return resource.RetryableError(err) | ||
} | ||
return resource.NonRetryableError(err) | ||
} | ||
addDebug(request.GetActionName(), raw, request.RpcRequest, request) | ||
return nil | ||
}) | ||
if err != nil { | ||
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR) | ||
} | ||
d.SetPartial("instance_id") | ||
d.SetPartial("pricing_cycle") | ||
d.SetPartial("duration") | ||
d.SetPartial("instance_type") | ||
d.SetPartial("owner_id") | ||
d.SetPartial("resource_owner_account") | ||
d.SetPartial("resource_owner_id") | ||
} | ||
d.Partial(false) | ||
return resourceAlicloudRenewInterfaceNetworkRead(d, meta) | ||
} | ||
|
||
func resourceAlicloudRenewInterfaceNetworkDelete(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
resource "alicloud_renew_interface" "foo" { | ||
instance_id = var.instance_id | ||
instance_type = var.instance_type | ||
pricing_cycle = var.pricing_cycle | ||
duration = var.duration | ||
renewal_status = var.renewal_status | ||
} | ||
|
||
data "alicloud_auto_renew_instances" "foo" { | ||
instance_type = var.instance_type | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "auto_renew_instances" { | ||
value = data.alicloud_auto_renew_instances.foo.instances | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
variable "instance_id" { | ||
default = "eip-xxxxxxxxxxxx" | ||
} | ||
|
||
variable "instance_type" { | ||
default = "eip" | ||
} | ||
|
||
variable "pricing_cycle" { | ||
default = "Month" | ||
} | ||
|
||
variable "duration" { | ||
default = 1 | ||
} | ||
|
||
variable "renewal_status" { | ||
default = "Normal" | ||
} |
Oops, something went wrong.