Skip to content

Commit

Permalink
Merge pull request #7 from byteplus-sdk/Feat/vpn
Browse files Browse the repository at this point in the history
Feat/vpn
  • Loading branch information
msq177 authored Sep 6, 2024
2 parents e0cec0e + cc39902 commit 7104241
Show file tree
Hide file tree
Showing 69 changed files with 7,318 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func ResourceByteplusScalingConfiguration() *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
34 changes: 14 additions & 20 deletions byteplus/ebs/volume/resource_byteplus_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

bp "github.com/byteplus-sdk/terraform-provider-byteplus/common"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

/*
Expand Down Expand Up @@ -45,24 +44,21 @@ func ResourceByteplusVolume() *schema.Resource {
Description: "The name of Volume.",
},
"volume_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The type of Volume, the value is `PTSSD` or `ESSD_PL0` or `ESSD_PL1` or `ESSD_PL2` or `ESSD_FlexPL`.",
ValidateFunc: validation.StringInSlice([]string{"ESSD_PL0", "ESSD_PL1", "ESSD_PL2", "PTSSD", "ESSD_FlexPL"}, false),
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The type of Volume, the value is `PTSSD` or `ESSD_PL0` or `ESSD_PL1` or `ESSD_PL2` or `ESSD_FlexPL`.",
},
"kind": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"data"}, false),
Description: "The kind of Volume, the value is `data`.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The kind of Volume, the value is `data`.",
},
"size": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(20), // 最小20GB
Description: "The size of Volume.",
Type: schema.TypeInt,
Required: true,
Description: "The size of Volume.",
},
"description": {
Type: schema.TypeString,
Expand All @@ -79,13 +75,11 @@ func ResourceByteplusVolume() *schema.Resource {
"When use this field to attach ecs instance, the attached volume cannot be deleted by terraform, please use `terraform state rm byteplus_volume.resource_name` command to remove it from terraform state file and management.",
},
"volume_charge_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"PostPaid", "PrePaid"}, false),
Default: "PostPaid",
Type: schema.TypeString,
Optional: true,
Default: "PostPaid",
Description: "The charge type of the Volume, the value is `PostPaid` or `PrePaid`. " +
"The `PrePaid` volume cannot be detached. " +
"Cannot convert `PrePaid` volume to `PostPaid`." +
"Please note that `PrePaid` type needs to ask the system administrator to apply for a whitelist.",
},
"status": {
Expand Down
18 changes: 7 additions & 11 deletions byteplus/ebs/volume/service_byteplus_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,17 @@ func (s *ByteplusVolumeService) ModifyResource(resourceData *schema.ResourceData
Action: "ModifyVolumeChargeType",
ConvertMode: bp.RequestConvertIgnore,
BeforeCall: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall) (bool, error) {
oldV, newV := resourceData.GetChange("volume_charge_type")
if oldV == "PrePaid" && newV == "PostPaid" {
return false, errors.New("cannot convert PrePaid volume to PostPaid")
}
if d.Get("instance_id").(string) == "" {
return false, errors.New("instance id cannot be empty")
}

chargeType := resourceData.Get("volume_charge_type")
(*call.SdkParam)["VolumeIds.1"] = d.Id()
(*call.SdkParam)["DiskChargeType"] = "PrePaid"
(*call.SdkParam)["AutoPay"] = true
(*call.SdkParam)["DiskChargeType"] = chargeType
(*call.SdkParam)["InstanceId"] = d.Get("instance_id")
if chargeType == "PrePaid" {
(*call.SdkParam)["AutoPay"] = true
}
return true, nil
},
ExecuteCall: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall) (*map[string]interface{}, error) {
Expand All @@ -269,10 +268,7 @@ func (s *ByteplusVolumeService) ModifyResource(resourceData *schema.ResourceData
return resp, err
},
CallError: func(d *schema.ResourceData, client *bp.SdkClient, call bp.SdkCall, baseErr error) error {
oldV, newV := resourceData.GetChange("volume_charge_type")
if oldV == "PrePaid" && newV == "PostPaid" {
return errors.New("cannot convert PrePaid volume to PostPaid")
}
chargeType := resourceData.Get("volume_charge_type")
if d.Get("instance_id").(string) == "" {
return errors.New("instance id cannot be empty")
}
Expand All @@ -283,7 +279,7 @@ func (s *ByteplusVolumeService) ModifyResource(resourceData *schema.ResourceData
return re.NonRetryableError(fmt.Errorf("error on reading volume %q: %w", d.Id(), callErr))
}
// 计费方式已经转变成功
if data["PayType"] == "pre" {
if (chargeType == "PrePaid" && data["PayType"] == "pre") || (chargeType == "PostPaid" && data["PayType"] == "post") {
return nil
}
// 计费方式还没有转换成功,尝试重新转换
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func ResourceByteplusEcsInstanceState() *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 *ByteplusInstanceStateService) CreateResource(resourceData *schema.Resou
targetStatus = []string{"STOPPED"}
}

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

callback := bp.Callback{
Call: bp.SdkCall{
Action: action,
Expand Down Expand Up @@ -214,6 +226,17 @@ func (s *ByteplusInstanceStateService) ModifyResource(resourceData *schema.Resou
targetStatus = []string{"STOPPED"}
}

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

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

callback := bp.Callback{
Expand Down Expand Up @@ -264,6 +287,25 @@ func (s *ByteplusInstanceStateService) ReadResourceId(id string) string {
return id
}

func (s *ByteplusInstanceStateService) 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 := bp.ObtainSdkValue("Status", data)
if err != nil {
return false, err
}
for _, v := range targetStatus {
// 目标状态和当前状态相同时,不执行操作
if v == status.(string) {
return false, nil
}
}
return true, nil
}

func getUniversalInfo(actionName string) bp.UniversalInfo {
return bp.UniversalInfo{
ServiceName: "ecs",
Expand Down
22 changes: 22 additions & 0 deletions byteplus/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ import (
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpc/security_group_rule"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpc/subnet"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpc/vpc"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpn/customer_gateway"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpn/ssl_vpn_client_cert"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpn/ssl_vpn_server"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpn/vpn_connection"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpn/vpn_gateway"
"github.com/byteplus-sdk/terraform-provider-byteplus/byteplus/vpn/vpn_gateway_route"
)

func Provider() terraform.ResourceProvider {
Expand Down Expand Up @@ -279,6 +285,14 @@ func Provider() terraform.ResourceProvider {
"byteplus_cdn_cron_jobs": cdn_cron_job.DataSourceByteplusCdnCronJobs(),
"byteplus_cdn_kv_namespaces": cdn_kv_namespace.DataSourceByteplusCdnKvNamespaces(),
"byteplus_cdn_kvs": cdn_kv.DataSourceByteplusCdnKvs(),

// ================ VPN ================
"byteplus_vpn_gateways": vpn_gateway.DataSourceByteplusVpnGateways(),
"byteplus_customer_gateways": customer_gateway.DataSourceByteplusCustomerGateways(),
"byteplus_vpn_connections": vpn_connection.DataSourceByteplusVpnConnections(),
"byteplus_vpn_gateway_routes": vpn_gateway_route.DataSourceByteplusVpnGatewayRoutes(),
"byteplus_ssl_vpn_servers": ssl_vpn_server.DataSourceByteplusSslVpnServers(),
"byteplus_ssl_vpn_client_certs": ssl_vpn_client_cert.DataSourceByteplusSslVpnClientCerts(),
},
ResourcesMap: map[string]*schema.Resource{
// ================ ECS ================
Expand Down Expand Up @@ -370,6 +384,14 @@ func Provider() terraform.ResourceProvider {
"byteplus_cdn_cron_job_state": cdn_cron_job_state.ResourceByteplusCdnCronJobState(),
"byteplus_cdn_kv_namespace": cdn_kv_namespace.ResourceByteplusCdnKvNamespace(),
"byteplus_cdn_kv": cdn_kv.ResourceByteplusCdnKv(),

// ================ VPN ================
"byteplus_vpn_gateway": vpn_gateway.ResourceByteplusVpnGateway(),
"byteplus_customer_gateway": customer_gateway.ResourceByteplusCustomerGateway(),
"byteplus_vpn_connection": vpn_connection.ResourceByteplusVpnConnection(),
"byteplus_vpn_gateway_route": vpn_gateway_route.ResourceByteplusVpnGatewayRoute(),
"byteplus_ssl_vpn_server": ssl_vpn_server.ResourceByteplusSslVpnServer(),
"byteplus_ssl_vpn_client_cert": ssl_vpn_client_cert.ResourceByteplusSslClientCertServer(),
},
ConfigureFunc: ProviderConfigure,
}
Expand Down
10 changes: 6 additions & 4 deletions byteplus/vke/cluster/resource_byteplus_vke_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ResourceByteplusVkeCluster() *schema.Resource {
}
return false
},
Description: "The version of Kubernetes specified when creating a VKE cluster (specified to patch version), with an example value of `v1.24`. If not specified, the latest Kubernetes version supported by VKE is used by default, which is a 3-segment version format starting with a lowercase v, that is, KubernetesVersion with IsLatestVersion=True in the return value of ListSupportedVersions.",
Description: "The version of Kubernetes specified when creating a VKE cluster (specified to patch version), with an example value of `1.24`. If not specified, the latest Kubernetes version supported by VKE is used by default, which is a 3-segment version format starting with a lowercase v, that is, KubernetesVersion with IsLatestVersion=True in the return value of ListSupportedVersions.",
},
"tags": bp.TagsSchema(),
"cluster_config": {
Expand All @@ -81,12 +81,14 @@ func ResourceByteplusVkeCluster() *schema.Resource {
"subnet_ids": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
MaxItems: 6,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
Description: "The subnet ID for the cluster control plane to communicate within the private network.",
Set: schema.HashString,
Description: "The subnet ID for the cluster control plane to communicate within the private network.\n" +
"Up to 3 subnets can be selected from each available zone, and a maximum of 2 subnets can be added to each available zone\n" +
"Cannot support deleting configured subnets.",
},
"api_server_public_access_enabled": {
Type: schema.TypeBool,
Expand Down
12 changes: 6 additions & 6 deletions byteplus/vke/node/resource_byteplus_vke_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,29 @@ func ResourceByteplusVkeNode() *schema.Resource {
Optional: true,
Default: false,
ForceNew: true,
Description: "The flag of additional container storage enable, the value is `true` or `false`.",
Description: "The flag of additional container storage enable, the value is `true` or `false`. This field is valid only when adding an existing instance to the default node pool.",
},
"image_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The ImageId of NodeConfig.",
Description: "The ImageId of NodeConfig. This field is valid only when adding an existing instance to the default node pool.",
},
"initialize_script": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The initializeScript of Node.",
Description: "The initializeScript of Node. This field is valid only when adding an existing instance to the default node pool.",
},
"kubernetes_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The KubernetesConfig of Node.",
Description: "The KubernetesConfig of Node. This field is valid only when adding an existing instance to the default node pool.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"labels": {
Expand Down Expand Up @@ -161,14 +161,14 @@ func ResourceByteplusVkeNode() *schema.Resource {
flag := d.Get("additional_container_storage_enabled")
return flag == nil || !flag.(bool)
},
Description: "The container storage path.",
Description: "The container storage path. This field is valid only when adding an existing instance to the default node pool.",
},
"node_pool_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The node pool id.",
Description: "The node pool id. This field is used to specify the custom node pool to which you want to add nodes. If not filled in, it means added to the default node pool.",
},
},
}
Expand Down
Loading

0 comments on commit 7104241

Please sign in to comment.