Skip to content

Commit

Permalink
add ufs volume mount point (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnmssu authored Oct 26, 2021
1 parent 75956f4 commit 5eab116
Show file tree
Hide file tree
Showing 31 changed files with 1,869 additions and 138 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
## 1.30.0 (Unreleased)

FEATURES:

* **New Resource:** `ucloud_ufs_volume_mount_point`. [GH-109]

ENHANCEMENTS:

* resource/ucloud_uk8s_cluster: add custom diff precheck of `boot_disk_type` and `instance_type` about outstanding machine. [GH-109]
* resource/ucloud_uk8s_node: add custom diff precheck of `boot_disk_type` and `instance_type` about outstanding machine. [GH-109]

BUG FIXES:

* resource/ucloud_uk8s_cluster: fix the default value to take effect of `boot_disk_type`, `data_disk_type` and `min_cpu_platform`. [GH-109]

## 1.29.0 (2021-06-22)

FEATURES:
Expand Down
65 changes: 33 additions & 32 deletions examples/k8s/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,50 @@ provider "ucloud" {
}

resource "ucloud_vpc" "foo" {
name = "tf-acc-uk8s-cluster"
tag = "tf-acc"
cidr_blocks = ["192.168.0.0/16"]
name = "tf-acc-uk8s-cluster"
tag = "tf-acc"
cidr_blocks = ["192.168.0.0/16"]
}
resource "ucloud_subnet" "foo" {
name = "tf-acc-uk8s-cluster"
tag = "tf-acc"
cidr_block = "192.168.1.0/24"
vpc_id = ucloud_vpc.foo.id
name = "tf-acc-uk8s-cluster"
tag = "tf-acc"
cidr_block = "192.168.1.0/24"
vpc_id = ucloud_vpc.foo.id
}

data "ucloud_zones" "default" {
}

resource "ucloud_uk8s_cluster" "foo" {
vpc_id = ucloud_vpc.foo.id
subnet_id = ucloud_subnet.foo.id
name = "tf-acc-uk8s-cluster-basic"
service_cidr = "172.16.0.0/16"
password = var.password
charge_type = "dynamic"
vpc_id = ucloud_vpc.foo.id
subnet_id = ucloud_subnet.foo.id
name = "tf-acc-uk8s-cluster-basic"
service_cidr = "172.16.0.0/16"
password = var.password
charge_type = "dynamic"

kube_proxy {
mode = "ipvs"
}
kube_proxy {
mode = "ipvs"
}

master {
availability_zones = [
data.ucloud_zones.default.zones[0].id,
data.ucloud_zones.default.zones[0].id,
data.ucloud_zones.default.zones[0].id,
]
instance_type = "n-basic-2"
}
master {
availability_zones = [
data.ucloud_zones.default.zones[0].id,
data.ucloud_zones.default.zones[0].id,
data.ucloud_zones.default.zones[0].id,
]
instance_type = "o-basic-2"
boot_disk_type = "cloud_rssd"
}
}

resource "ucloud_uk8s_node" "foo" {
cluster_id = ucloud_uk8s_cluster.foo.id
subnet_id = ucloud_subnet.foo.id
password = var.password
instance_type = "n-basic-2"
charge_type = "dynamic"
availability_zone = data.ucloud_zones.default.zones[0].id

count = 2
cluster_id = ucloud_uk8s_cluster.foo.id
subnet_id = ucloud_subnet.foo.id
password = var.password
instance_type = "o-basic-2"
charge_type = "dynamic"
availability_zone = data.ucloud_zones.default.zones[0].id
boot_disk_type = "cloud_rssd"
count = 2
}
4 changes: 2 additions & 2 deletions examples/redis/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ resource "ucloud_subnet" "default" {
# Create redis instance
resource "ucloud_redis_instance" "master" {
availability_zone = var.zone
// engine_version = "4.0"
instance_type = "redis-distributed-8"
engine_version = "4.0"
instance_type = "redis-master-1"
password = var.redis_password
name = "tf-example-redis"
tag = "tf-example"
Expand Down
2 changes: 1 addition & 1 deletion examples/two-tier/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ provider "ucloud" {
# Query image
data "ucloud_images" "default" {
availability_zone = var.zone
name_regex = "^CentOS 7.[1-2] 64"
name_regex = "^CentOS 8.[1-2] 64"
image_type = "base"
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ufs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# UFS Example

The UFS example launches an UFS.
The UFS example launches a UFS Volume with a UFS Volume mount point bind to it.

To run, configure your UCloud provider as described in https://www.terraform.io/docs/providers/ucloud/index.html

Expand Down
14 changes: 14 additions & 0 deletions examples/ufs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@ provider "ucloud" {
region = var.region
}

data "ucloud_vpcs" "default" {
}

data "ucloud_subnets" "default" {
vpc_id = data.ucloud_vpcs.default.vpcs[0].id
}

resource "ucloud_ufs_volume" "foo" {
name = "tf-acc-ufs-basic"
remark = "test"
tag = "tf-acc"
size = 600
storage_type = "Basic"
protocol_type = "NFSv4"
}

resource "ucloud_ufs_volume_mount_point" "foo" {
name = "tf-acc-ufs-mount-point-basic"
volume_id = ucloud_ufs_volume.foo.id
vpc_id = data.ucloud_vpcs.default.vpcs[0].id
subnet_id = data.ucloud_subnets.default.subnets[0].id
}
2 changes: 1 addition & 1 deletion examples/ufs/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "region" {
default = "cn-bj2"
default = "cn-sh2"
}

2 changes: 1 addition & 1 deletion examples/web-server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ provider "ucloud" {
# Query image
data "ucloud_images" "default" {
availability_zone = var.zone
name_regex = "^CentOS 7.[1-2] 64"
name_regex = "^CentOS 8.[1-2] 64"
image_type = "base"
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/hashicorp/terraform-plugin-sdk v1.4.0
github.com/ucloud/ucloud-sdk-go v0.21.5
github.com/ucloud/ucloud-sdk-go v0.21.19
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
gopkg.in/yaml.v2 v2.4.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ucloud/ucloud-sdk-go v0.21.5 h1:wcVdYa0sKdCBrBL03M96DLvqJgmLye6qlA4+fdUEfdY=
github.com/ucloud/ucloud-sdk-go v0.21.5/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ucloud/ucloud-sdk-go v0.21.19 h1:WqD38oLDYxWS3s92DBKR99X3bKktuveTdSigcBynDT4=
github.com/ucloud/ucloud-sdk-go v0.21.19/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
2 changes: 1 addition & 1 deletion ucloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *Config) Client() (*UCloudClient, error) {
// enable auto retry with http/connection error
cfg.MaxRetries = c.MaxRetries
cfg.LogLevel = log.PanicLevel
cfg.UserAgent = "Terraform-UCloud/1.29.0"
cfg.UserAgent = "Terraform-UCloud/1.30.0"
cfg.BaseUrl = c.BaseURL

cred := auth.NewCredential()
Expand Down
1 change: 1 addition & 0 deletions ucloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func Provider() terraform.ResourceProvider {
"ucloud_vpn_customer_gateway": resourceUCloudVPNCustomerGateway(),
"ucloud_vpn_connection": resourceUCloudVPNConnection(),
"ucloud_ufs_volume": resourceUCloudUFSVolume(),
"ucloud_ufs_volume_mount_point": resourceUCloudUFSVolumeMountPoint(),
"ucloud_us3_bucket": resourceUCloudUS3Bucket(),
"ucloud_cube_pod": resourceUCloudCubePod(),
"ucloud_uk8s_cluster": resourceUCloudUK8SCluster(),
Expand Down
144 changes: 144 additions & 0 deletions ucloud/resource_ucloud_ufs_volume_mount_point.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ucloud

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/ucloud/ucloud-sdk-go/ucloud"
"strings"
"time"
)

func resourceUCloudUFSVolumeMountPoint() *schema.Resource {
return &schema.Resource{
Create: resourceUCloudUFSVolumeMountPointCreate,
Read: resourceUCloudUFSVolumeMountPointRead,
Delete: resourceUCloudUFSVolumeMountPointDelete,
Schema: map[string]*schema.Schema{
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateUFSVolumeName,
},

"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"mount_point_ip": {
Type: schema.TypeString,
Computed: true,
},

"create_time": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceUCloudUFSVolumeMountPointCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*UCloudClient).ufsconn
volumeId := d.Get("volume_id").(string)
vpcId := d.Get("vpc_id").(string)
subnetId := d.Get("subnet_id").(string)
name := d.Get("name").(string)

req := conn.NewAddUFSVolumeMountPointRequest()
req.VolumeId = ucloud.String(volumeId)
req.VpcId = ucloud.String(vpcId)
req.SubnetId = ucloud.String(subnetId)
req.MountPointName = ucloud.String(name)
_, err := conn.AddUFSVolumeMountPoint(req)
if err != nil {
return fmt.Errorf("error on creating ufs volume mount point, %s", err)
}

d.SetId(fmt.Sprintf("%s:%s:%s", volumeId, vpcId, subnetId))

return resourceUCloudUFSVolumeMountPointRead(d, meta)
}

func resourceUCloudUFSVolumeMountPointRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*UCloudClient)
p := strings.Split(d.Id(), ":")
if len(p) != 3 {
return fmt.Errorf("illegal ufs volume mount point id, %s", d.Id())
}
resourceSet, err := client.describeUFSVolumeMountPointById(p[0], p[1], p[2])
if err != nil {
return fmt.Errorf("error on reading ufs volume mount point %q, %s", d.Id(), err)
}

d.Set("volume_id", p[0])
d.Set("name", resourceSet.MountPointName)
d.Set("vpc_id", resourceSet.VpcId)
d.Set("subnet_id", resourceSet.SubnetId)
d.Set("mount_point_ip", resourceSet.MountPointIp)
d.Set("create_time", timestampToString(resourceSet.CreateTime))

return nil
}

func resourceUCloudUFSVolumeMountPointDelete(d *schema.ResourceData, meta interface{}) error {
return resource.Retry(5*time.Minute, func() *resource.RetryError {
client := meta.(*UCloudClient)
conn := client.ufsconn

p := strings.Split(d.Id(), ":")
if len(p) != 3 {
return resource.NonRetryableError(fmt.Errorf("illegal ufs volume mount point id, %s", d.Id()))
}

req := conn.NewRemoveUFSVolumeMountPointRequest()
req.VolumeId = ucloud.String(p[0])
req.VpcId = ucloud.String(p[1])
req.SubnetId = ucloud.String(p[2])

if _, err := conn.RemoveUFSVolumeMountPoint(req); err != nil {
return resource.NonRetryableError(fmt.Errorf("error on deleting ufs volume mount point %q, %s", d.Id(), err))
}

stateConf := &resource.StateChangeConf{
Pending: []string{statusPending},
Target: []string{statusDELETED},
Refresh: func() (interface{}, string, error) {
resp, err := client.describeUFSVolumeMountPointById(p[0], p[1], p[2])
if err != nil {
if isNotFoundError(err) {
return resp, statusDELETED, nil
}
return nil, statusPending, err
}
return resp, statusPending, nil
},
Timeout: 2 * time.Minute,
Delay: 5 * time.Second,
MinTimeout: 1 * time.Second,
}

if _, err := stateConf.WaitForState(); err != nil {
if _, ok := err.(*resource.TimeoutError); ok {
return resource.RetryableError(fmt.Errorf("error on waiting for deleting ufs volume mount point %q, %s", d.Id(), err))
}
return resource.NonRetryableError(fmt.Errorf("error on waiting for deleting ufs volume mount point %q, %s", d.Id(), err))
}
return nil
})
}
Loading

0 comments on commit 5eab116

Please sign in to comment.