Skip to content

Commit

Permalink
Merge pull request #169 from volcengine/Feat/ecs-state
Browse files Browse the repository at this point in the history
Feat/ecs state
  • Loading branch information
msq177 authored Jul 2, 2024
2 parents db3014d + 5e37ce4 commit b40d332
Show file tree
Hide file tree
Showing 27 changed files with 124 additions and 23 deletions.
2 changes: 1 addition & 1 deletion common/common_volcengine_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package common

const (
TerraformProviderName = "terraform-provider-volcengine"
TerraformProviderVersion = "0.0.147"
TerraformProviderVersion = "0.0.148"
)
4 changes: 2 additions & 2 deletions example/dataSnatEntries/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ resource "volcengine_snat_entry" "foo1" {
nat_gateway_id = volcengine_nat_gateway.foo.id
eip_id = volcengine_eip_address.foo.id
source_cidr = "172.16.0.0/24"
depends_on = ["volcengine_eip_associate.foo"]
depends_on = [volcengine_eip_associate.foo]
}

resource "volcengine_snat_entry" "foo2" {
snat_entry_name = "acc-test-snat-entry"
nat_gateway_id = volcengine_nat_gateway.foo.id
eip_id = volcengine_eip_address.foo.id
source_cidr = "172.16.0.0/16"
depends_on = ["volcengine_eip_associate.foo"]
depends_on = [volcengine_eip_associate.foo]
}

data "volcengine_snat_entries" "foo"{
Expand Down
2 changes: 1 addition & 1 deletion example/snatEntry/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ resource "volcengine_snat_entry" "foo" {
nat_gateway_id = volcengine_nat_gateway.foo.id
eip_id = volcengine_eip_address.foo.id
source_cidr = "172.16.0.0/24"
depends_on = ["volcengine_eip_associate.foo"]
depends_on = [volcengine_eip_associate.foo]
}
5 changes: 3 additions & 2 deletions example/transitRouter/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
resource "volcengine_transit_router" "foo" {
transit_router_name = "acc-test-tr"
description = "acc-test"
project_name = "default"
asn = 4294967294
project_name = "default"
tags {
key = "k1"
key = "k1"
value = "v1"
}
}
13 changes: 7 additions & 6 deletions example/transitRouterVpcAttachment/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
resource "volcengine_transit_router" "foo" {
transit_router_name = "test-tf-acc"
description = "test-tf-acc"
asn = 4294967293
}

data "volcengine_zones" "foo" {
}

resource "volcengine_vpc" "foo" {
vpc_name = "acc-test-vpc-acc"
cidr_block = "172.16.0.0/16"
vpc_name = "acc-test-vpc-acc"
cidr_block = "172.16.0.0/16"
}

resource "volcengine_subnet" "foo" {
Expand All @@ -25,7 +26,6 @@ resource "volcengine_subnet" "foo2" {
subnet_name = "acc-test-subnet2"
}


resource "volcengine_transit_router_vpc_attachment" "foo" {
transit_router_id = volcengine_transit_router.foo.id
vpc_id = volcengine_vpc.foo.id
Expand All @@ -37,10 +37,11 @@ resource "volcengine_transit_router_vpc_attachment" "foo" {
subnet_id = volcengine_subnet.foo2.id
zone_id = "cn-beijing-b"
}
transit_router_attachment_name = "tf-test-acc-name1"
description = "tf-test-acc-description"
transit_router_attachment_name = "tf-test-acc-vpc-attach"
description = "tf-test-acc-description"
auto_publish_route_enabled = true
tags {
key = "k1"
key = "k1"
value = "v1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func ResourceVolcengineScalingConfiguration() *schema.Resource {
"project_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The project to which the instance created by the scaling configuration belongs.",
},
"hpc_cluster_id": {
Expand Down
1 change: 1 addition & 0 deletions volcengine/cen/cen/resource_volcengine_cen.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ResourceVolcengineCen() *schema.Resource {
"project_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The ProjectName of the cen instance.",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func ResourceVolcengineCenBandwidthPackage() *schema.Resource {
"project_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The ProjectName of the cen bandwidth package.",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func ResourceVolcengineEcsInstanceState() *schema.Resource {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"Start", "Stop", "ForceStop"}, false),
Description: "Start or Stop of Instance Action, the value can be `Start`, `Stop` or `ForceStop`.",
Description: "Start or Stop of Instance Action, the value can be `Start`, `Stop` or `ForceStop`. \n" +
"If the target status of the action is consistent with the current status of the instance, the action will not actually be executed.",
},
"instance_id": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ func (s *VolcengineInstanceStateService) CreateResource(resourceData *schema.Res
targetStatus = []string{"STOPPED"}
}

// 根据实例当前状态判断是否执行操作
update, err := s.describeCurrentStatus(resourceData, targetStatus)
if err != nil {
return []ve.Callback{{
Err: err,
}}
}
if !update {
resourceData.SetId(fmt.Sprintf("state:%v", resourceData.Get("instance_id")))
return []ve.Callback{}
}

callback := ve.Callback{
Call: ve.SdkCall{
Action: action,
Expand Down Expand Up @@ -215,6 +227,17 @@ func (s *VolcengineInstanceStateService) ModifyResource(resourceData *schema.Res
targetStatus = []string{"STOPPED"}
}

// 根据实例当前状态判断是否执行操作
update, err := s.describeCurrentStatus(resourceData, targetStatus)
if err != nil {
return []ve.Callback{{
Err: err,
}}
}
if !update {
return []ve.Callback{}
}

strs := strings.Split(resourceData.Id(), ":")

callback := ve.Callback{
Expand Down Expand Up @@ -264,3 +287,22 @@ func (s *VolcengineInstanceStateService) DatasourceResources(*schema.ResourceDat
func (s *VolcengineInstanceStateService) ReadResourceId(id string) string {
return id
}

func (s *VolcengineInstanceStateService) describeCurrentStatus(resourceData *schema.ResourceData, targetStatus []string) (bool, error) {
instanceId := resourceData.Get("instance_id").(string)
data, err := s.ReadResource(resourceData, "state:"+instanceId)
if err != nil {
return false, err
}
status, err := ve.ObtainSdkValue("Status", data)
if err != nil {
return false, err
}
for _, v := range targetStatus {
// 目标状态和当前状态相同时,不执行操作
if v == status.(string) {
return false, nil
}
}
return true, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ resource "volcengine_snat_entry" "foo1" {
nat_gateway_id = "${volcengine_nat_gateway.foo.id}"
eip_id = "${volcengine_eip_address.foo.id}"
source_cidr = "172.16.0.0/24"
depends_on = ["volcengine_eip_associate.foo"]
depends_on = [volcengine_eip_associate.foo]
}
resource "volcengine_snat_entry" "foo2" {
snat_entry_name = "acc-test-snat-entry"
nat_gateway_id = "${volcengine_nat_gateway.foo.id}"
eip_id = "${volcengine_eip_address.foo.id}"
source_cidr = "172.16.0.0/16"
depends_on = ["volcengine_eip_associate.foo"]
depends_on = [volcengine_eip_associate.foo]
}
data "volcengine_snat_entries" "foo"{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func DataSourceVolcengineTransitRouters() *schema.Resource {
Computed: true,
Description: "The name of the transit router.",
},
"asn": {
Type: schema.TypeInt,
Computed: true,
Description: "The asn of the transit router.",
},
"account_id": {
Type: schema.TypeString,
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func ResourceVolcengineTransitRouter() *schema.Resource {
Optional: true,
Description: "The description of the transit router.",
},
"asn": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The asn of the transit router. Valid value range in 64512-65534 and 4200000000-4294967294. Default is 64512.",
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
ve "github.com/volcengine/terraform-provider-volcengine/common"
Expand Down Expand Up @@ -202,6 +203,10 @@ func (s *Service) CreateResource(resourceData *schema.ResourceData, resource *sc
ConvertType: ve.ConvertListN,
},
},
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["ClientToken"] = uuid.New().String()
return true, nil
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/volcengine/terraform-provider-volcengine/volcengine/transit_router/transit_router"
"strconv"
"strings"
"time"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
ve "github.com/volcengine/terraform-provider-volcengine/common"
"github.com/volcengine/terraform-provider-volcengine/logger"
"github.com/volcengine/terraform-provider-volcengine/volcengine/transit_router/transit_router"
)

type VolcengineTransitRouterDirectConnectGatewayAttachmentService struct {
Expand Down Expand Up @@ -140,6 +141,10 @@ func (s *VolcengineTransitRouterDirectConnectGatewayAttachmentService) CreateRes
ConvertType: ve.ConvertListN,
},
},
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["ClientToken"] = uuid.New().String()
return true, nil
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
resp, err := s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func DataSourceVolcengineTransitRouterVpcAttachments() *schema.Resource {
Computed: true,
Description: "The description info.",
},
"auto_publish_route_enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether to auto publish route of the transit router to vpc instance.",
},
"tags": ve.TagsSchemaComputed(),
"attach_points": {
Description: "The collection of attach points.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func ResourceVolcengineTransitRouterVpcAttachment() *schema.Resource {
Computed: true,
Description: "The name of the transit router vpc attachment.",
},
"auto_publish_route_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to auto publish route of the transit router to vpc instance. Default is false.",
},
"tags": ve.TagsSchema(),
"attach_points": {
Type: schema.TypeSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
ve "github.com/volcengine/terraform-provider-volcengine/common"
Expand Down Expand Up @@ -159,6 +160,10 @@ func (s *Service) CreateResource(resourceData *schema.ResourceData, resource *sc
ConvertType: ve.ConvertListN,
},
},
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["ClientToken"] = uuid.New().String()
return true, nil
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
Expand Down Expand Up @@ -195,6 +200,9 @@ func (s *Service) ModifyResource(resourceData *schema.ResourceData, resource *sc
"transit_router_attachment_name": {
TargetField: "TransitRouterAttachmentName",
},
"auto_publish_route_enabled": {
TargetField: "AutoPublishRouteEnabled",
},
},
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["TransitRouterAttachmentId"] = d.Get("transit_router_attachment_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
ve "github.com/volcengine/terraform-provider-volcengine/common"
Expand Down Expand Up @@ -157,6 +158,10 @@ func (s *VolcengineTRVpnAttachmentService) CreateResource(resourceData *schema.R
ConvertType: ve.ConvertListN,
},
},
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["ClientToken"] = uuid.New().String()
return true, nil
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
Expand Down
2 changes: 1 addition & 1 deletion volcengine/vke/node/resource_volcengine_vke_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ resource "volcengine_vke_node" "foo" {
}
cordon = true
}
depends_on = ["volcengine_vke_default_node_pool.foo"]
depends_on = [volcengine_vke_default_node_pool.foo]
}
`

Expand Down
4 changes: 2 additions & 2 deletions website/docs/d/snat_entries.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ resource "volcengine_snat_entry" "foo1" {
nat_gateway_id = volcengine_nat_gateway.foo.id
eip_id = volcengine_eip_address.foo.id
source_cidr = "172.16.0.0/24"
depends_on = ["volcengine_eip_associate.foo"]
depends_on = [volcengine_eip_associate.foo]
}
resource "volcengine_snat_entry" "foo2" {
snat_entry_name = "acc-test-snat-entry"
nat_gateway_id = volcengine_nat_gateway.foo.id
eip_id = volcengine_eip_address.foo.id
source_cidr = "172.16.0.0/16"
depends_on = ["volcengine_eip_associate.foo"]
depends_on = [volcengine_eip_associate.foo]
}
data "volcengine_snat_entries" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ In addition to all arguments above, the following attributes are exported:
* `network_interface_id` - The ID of network interface.
* `subnet_id` - The ID of subnet.
* `zone_id` - The ID of zone.
* `auto_publish_route_enabled` - Whether to auto publish route of the transit router to vpc instance.
* `creation_time` - The create time.
* `description` - The description info.
* `status` - The status of the transit router.
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/transit_routers.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ In addition to all arguments above, the following attributes are exported:
* `total_count` - The total count of query.
* `transit_routers` - The collection of query.
* `account_id` - The ID of account.
* `asn` - The asn of the transit router.
* `business_status` - The business status of the transit router.
* `creation_time` - The create time.
* `description` - The description info.
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/ecs_instance_state.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ resource "volcengine_ecs_instance_state" "foo" {
```
## Argument Reference
The following arguments are supported:
* `action` - (Required) Start or Stop of Instance Action, the value can be `Start`, `Stop` or `ForceStop`.
* `action` - (Required) Start or Stop of Instance Action, the value can be `Start`, `Stop` or `ForceStop`.
If the target status of the action is consistent with the current status of the instance, the action will not actually be executed.
* `instance_id` - (Required, ForceNew) Id of Instance.
* `stopped_mode` - (Optional) Stop Mode of Instance, the value can be `KeepCharging` or `StopCharging`.

Expand Down
Loading

0 comments on commit b40d332

Please sign in to comment.